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