001    package ezvcard.types;
002    
003    import java.util.List;
004    
005    import ezvcard.VCard;
006    import ezvcard.VCardSubTypes;
007    import ezvcard.VCardVersion;
008    import ezvcard.io.CompatibilityMode;
009    import ezvcard.io.SkipMeException;
010    import ezvcard.parameters.ValueParameter;
011    import ezvcard.util.VCardStringUtils;
012    import ezvcard.util.XCardElement;
013    
014    /*
015     Copyright (c) 2012, 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     The views and conclusions contained in the software and documentation are those
039     of the authors and should not be interpreted as representing official policies, 
040     either expressed or implied, of the FreeBSD Project.
041     */
042    
043    /**
044     * Defines the location of the person's death.
045     * 
046     * <pre>
047     * VCard vcard = new VCard();
048     * 
049     * //URI (geo)
050     * DeathplaceType deathplace = new DeathplaceType();
051     * deathplace.setUri(&quot;geo:46.176502,-122.191658&quot;);
052     * vcard.setDeathplace(deathplace);
053     * 
054     * //text
055     * deathplace = new DeathplaceType();
056     * deathplace.setText(&quot;Mount St. Helens&quot;);
057     * vcard.setDeathplace(deathplace);
058     * </pre>
059     * 
060     * <p>
061     * vCard property name: DEATHPLACE
062     * </p>
063     * <p>
064     * vCard versions: 4.0
065     * </p>
066     * @author Michael Angstadt
067     * @see <a href="http://tools.ietf.org/html/rfc6474">RFC 6474</a>
068     */
069    public class DeathplaceType extends VCardType {
070            public static final String NAME = "DEATHPLACE";
071    
072            private String uri;
073            private String text;
074    
075            public DeathplaceType() {
076                    super(NAME);
077            }
078    
079            /**
080             * Gets the URI value.
081             * @return the URI value or null if no URI value is set
082             */
083            public String getUri() {
084                    return uri;
085            }
086    
087            /**
088             * Sets the value to a URI.
089             * @param uri the URI
090             */
091            public void setUri(String uri) {
092                    this.uri = uri;
093                    text = null;
094            }
095    
096            /**
097             * Gets the text value.
098             * @return the text value or null if no text value is set
099             */
100            public String getText() {
101                    return text;
102            }
103    
104            /**
105             * Sets the value to free-form text.
106             * @param text the text
107             */
108            public void setText(String text) {
109                    this.text = text;
110                    uri = null;
111            }
112    
113            /**
114             * Gets the ALTID.
115             * <p>
116             * vCard versions: 4.0
117             * </p>
118             * @return the ALTID or null if it doesn't exist
119             * @see VCardSubTypes#getAltId
120             */
121            public String getAltId() {
122                    return subTypes.getAltId();
123            }
124    
125            /**
126             * Sets the ALTID.
127             * <p>
128             * vCard versions: 4.0
129             * </p>
130             * @param altId the ALTID or null to remove
131             * @see VCardSubTypes#setAltId
132             */
133            public void setAltId(String altId) {
134                    subTypes.setAltId(altId);
135            }
136    
137            /**
138             * Gets the LANGUAGE parameter.
139             * @return the language or null if not set
140             * @see VCardSubTypes#getLanguage
141             */
142            public String getLanguage() {
143                    return subTypes.getLanguage();
144            }
145    
146            /**
147             * Sets the LANGUAGE parameter.
148             * @param language the language or null to remove
149             * @see VCardSubTypes#setLanguage
150             */
151            public void setLanguage(String language) {
152                    subTypes.setLanguage(language);
153            }
154    
155            @Override
156            public VCardVersion[] getSupportedVersions() {
157                    return new VCardVersion[] { VCardVersion.V4_0 };
158            }
159    
160            @Override
161            protected void doMarshalSubTypes(VCardSubTypes copy, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode, VCard vcard) {
162                    if (uri != null) {
163                            copy.setValue(ValueParameter.URI);
164                    } else if (text != null) {
165                            copy.setValue(ValueParameter.TEXT);
166                    }
167            }
168    
169            @Override
170            protected void doMarshalText(StringBuilder sb, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode) {
171                    if (uri != null) {
172                            sb.append(VCardStringUtils.escape(uri));
173                    } else if (text != null) {
174                            sb.append(VCardStringUtils.escape(text));
175                    } else {
176                            throw new SkipMeException("Property has neither a URI nor a text value associated with it.");
177                    }
178            }
179    
180            @Override
181            protected void doUnmarshalText(String value, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode) {
182                    value = VCardStringUtils.unescape(value);
183                    if (subTypes.getValue() == ValueParameter.URI) {
184                            setUri(value);
185                    } else if (subTypes.getValue() == ValueParameter.TEXT) {
186                            setText(value);
187                    } else {
188                            warnings.add("No valid VALUE parameter specified for " + NAME + " type.  Assuming it's text.");
189                            setText(value);
190                    }
191            }
192    
193            @Override
194            protected void doMarshalXml(XCardElement parent, List<String> warnings, CompatibilityMode compatibilityMode) {
195                    if (uri != null) {
196                            parent.uri(uri);
197                    } else if (text != null) {
198                            parent.text(text);
199                    } else {
200                            throw new SkipMeException("Property has neither a URI nor a text value associated with it.");
201                    }
202            }
203    
204            @Override
205            protected void doUnmarshalXml(XCardElement element, List<String> warnings, CompatibilityMode compatibilityMode) {
206                    String value = element.uri();
207                    if (value != null) {
208                            setUri(value);
209                    } else {
210                            setText(element.text());
211                    }
212            }
213    }