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