001    package ezvcard.io.scribe;
002    
003    import java.util.List;
004    
005    import ezvcard.VCardDataType;
006    import ezvcard.VCardVersion;
007    import ezvcard.io.html.HCardElement;
008    import ezvcard.io.json.JCardValue;
009    import ezvcard.io.xml.XCardElement;
010    import ezvcard.parameter.VCardParameters;
011    import ezvcard.property.VCardProperty;
012    
013    /*
014     Copyright (c) 2013, Michael Angstadt
015     All rights reserved.
016    
017     Redistribution and use in source and binary forms, with or without
018     modification, are permitted provided that the following conditions are met: 
019    
020     1. Redistributions of source code must retain the above copyright notice, this
021     list of conditions and the following disclaimer. 
022     2. Redistributions in binary form must reproduce the above copyright notice,
023     this list of conditions and the following disclaimer in the documentation
024     and/or other materials provided with the distribution. 
025    
026     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
027     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
028     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
030     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
031     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
032     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
034     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036     */
037    
038    /**
039     * Marshals properties that have just a single value thats need no parsing or
040     * writing logic and that always has the same data type.
041     * @param <T> the property class
042     * @author Michael Angstadt
043     */
044    public abstract class SimplePropertyScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {
045            protected final VCardDataType dataType;
046    
047            public SimplePropertyScribe(Class<T> clazz, String propertyName, VCardDataType dataType) {
048                    super(clazz, propertyName);
049                    this.dataType = dataType;
050            }
051    
052            @Override
053            protected VCardDataType _defaultDataType(VCardVersion version) {
054                    return dataType;
055            }
056    
057            @Override
058            protected String _writeText(T property, VCardVersion version) {
059                    String value = _writeValue(property);
060                    return (value == null) ? "" : escape(value);
061            }
062    
063            @Override
064            protected T _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) {
065                    value = unescape(value);
066                    return _parseValue(value);
067            }
068    
069            @Override
070            protected void _writeXml(T property, XCardElement parent) {
071                    parent.append(dataType, _writeValue(property));
072            }
073    
074            @Override
075            protected T _parseXml(XCardElement element, VCardParameters parameters, List<String> warnings) {
076                    String value = element.first(dataType);
077                    if (value != null) {
078                            return _parseValue(value);
079                    }
080    
081                    throw super.missingXmlElements(dataType);
082            }
083    
084            @Override
085            protected T _parseHtml(HCardElement element, List<String> warnings) {
086                    String value = element.value();
087                    return _parseValue(value);
088            }
089    
090            @Override
091            protected JCardValue _writeJson(T property) {
092                    String value = _writeValue(property);
093                    if (value == null) {
094                            value = "";
095                    }
096    
097                    return JCardValue.single(value);
098            }
099    
100            @Override
101            protected T _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, List<String> warnings) {
102                    String valueStr = value.asSingle();
103                    return _parseValue(valueStr);
104            }
105    
106            /**
107             * Writes the property value to a string.
108             * @param property the property to write
109             * @return the property value
110             */
111            protected abstract String _writeValue(T property);
112    
113            /**
114             * Parses the property from a string.
115             * @param value the property value
116             * @return the parsed property object
117             */
118            protected abstract T _parseValue(String value);
119    }