001    package biweekly.property.marshaller;
002    
003    import java.util.List;
004    
005    import org.w3c.dom.Element;
006    
007    import biweekly.ICalDataType;
008    import biweekly.io.xml.XCalElement;
009    import biweekly.io.xml.XCalNamespaceContext;
010    import biweekly.parameter.ICalParameters;
011    import biweekly.property.RawProperty;
012    import biweekly.util.XmlUtils;
013    
014    /*
015     Copyright (c) 2013, Michael Angstadt
016     All rights reserved.
017    
018     Redistribution and use in source and binary forms, with or without
019     modification, are permitted provided that the following conditions are met: 
020    
021     1. Redistributions of source code must retain the above copyright notice, this
022     list of conditions and the following disclaimer. 
023     2. Redistributions in binary form must reproduce the above copyright notice,
024     this list of conditions and the following disclaimer in the documentation
025     and/or other materials provided with the distribution. 
026    
027     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
028     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
029     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
030     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
031     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
032     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
033     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
034     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
035     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037     */
038    
039    /**
040     * Marshals properties that do not have a marshaller associated with them.
041     * @author Michael Angstadt
042     */
043    public class RawPropertyMarshaller extends ICalPropertyMarshaller<RawProperty> {
044            public RawPropertyMarshaller(String propertyName) {
045                    super(RawProperty.class, propertyName, null);
046            }
047    
048            @Override
049            protected ICalDataType _dataType(RawProperty property) {
050                    return property.getDataType();
051            }
052    
053            @Override
054            protected String _writeText(RawProperty property) {
055                    String value = property.getValue();
056                    if (value != null) {
057                            return value;
058                    }
059    
060                    return "";
061            }
062    
063            @Override
064            protected RawProperty _parseText(String value, ICalDataType dataType, ICalParameters parameters, List<String> warnings) {
065                    return new RawProperty(propertyName, dataType, value);
066            }
067    
068            @Override
069            protected RawProperty _parseXml(XCalElement element, ICalParameters parameters, List<String> warnings) {
070                    Element rawElement = element.getElement();
071                    String name = rawElement.getLocalName();
072    
073                    //get the text content of the first child element with the xCard namespace
074                    List<Element> children = XmlUtils.toElementList(rawElement.getChildNodes());
075                    for (Element child : children) {
076                            if (!XCalNamespaceContext.XCAL_NS.equals(child.getNamespaceURI())) {
077                                    continue;
078                            }
079    
080                            String value = child.getTextContent();
081                            ICalDataType dataType = ICalDataType.get(child.getLocalName());
082                            return new RawProperty(name, dataType, value);
083                    }
084    
085                    //get the text content of the property element
086                    String value = rawElement.getTextContent();
087                    return new RawProperty(name, null, value);
088            }
089    }