001    package ezvcard.types;
002    
003    import java.util.List;
004    import java.util.regex.Matcher;
005    import java.util.regex.Pattern;
006    
007    import ezvcard.VCard;
008    import ezvcard.VCardSubTypes;
009    import ezvcard.VCardVersion;
010    import ezvcard.io.CompatibilityMode;
011    import ezvcard.parameters.EmailTypeParameter;
012    import ezvcard.util.HCardElement;
013    import ezvcard.util.VCardStringUtils;
014    import ezvcard.util.XCardElement;
015    
016    /*
017     Copyright (c) 2012, Michael Angstadt
018     All rights reserved.
019    
020     Redistribution and use in source and binary forms, with or without
021     modification, are permitted provided that the following conditions are met: 
022    
023     1. Redistributions of source code must retain the above copyright notice, this
024     list of conditions and the following disclaimer. 
025     2. Redistributions in binary form must reproduce the above copyright notice,
026     this list of conditions and the following disclaimer in the documentation
027     and/or other materials provided with the distribution. 
028    
029     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
030     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
031     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
032     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
033     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
035     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
036     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
037     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
038     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
039    
040     The views and conclusions contained in the software and documentation are those
041     of the authors and should not be interpreted as representing official policies, 
042     either expressed or implied, of the FreeBSD Project.
043     */
044    
045    /**
046     * An email address associated with a person.
047     * 
048     * <pre>
049     * VCard vcard = new VCard();
050     * 
051     * EmailType email = new EmailType(&quot;superdude55@hotmail.com&quot;);
052     * email.addType(EmailTypeParameter.HOME);
053     * vcard.addEmail(email);
054     * 
055     * email = new EmailType(&quot;doe.john@company.com&quot;);
056     * email.addType(EmailTypeParameter.WORK);
057     * email.setPref(1); //the most preferred email
058     * vcard.addEmail(email);
059     * </pre>
060     * 
061     * <p>
062     * vCard property name: EMAIL
063     * </p>
064     * <p>
065     * vCard versions: 2.1, 3.0, 4.0
066     * </p>
067     * @author Michael Angstadt
068     */
069    public class EmailType extends MultiValuedTypeParameterType<EmailTypeParameter> {
070            public static final String NAME = "EMAIL";
071    
072            private String value;
073    
074            public EmailType() {
075                    this(null);
076            }
077    
078            /**
079             * @param email the email
080             */
081            public EmailType(String email) {
082                    super(NAME);
083                    setValue(email);
084            }
085    
086            @Override
087            protected EmailTypeParameter buildTypeObj(String type) {
088                    EmailTypeParameter param = EmailTypeParameter.valueOf(type);
089                    if (param == null) {
090                            param = new EmailTypeParameter(type);
091                    }
092                    return param;
093            }
094    
095            /**
096             * Gets the email address.
097             * @return the email address
098             */
099            public String getValue() {
100                    return value;
101            }
102    
103            /**
104             * Sets the email address
105             * @param email the email address
106             */
107            public void setValue(String email) {
108                    this.value = email;
109            }
110    
111            /**
112             * Gets all PID parameter values.
113             * <p>
114             * vCard versions: 4.0
115             * </p>
116             * @return the PID values or empty set if there are none
117             * @see VCardSubTypes#getPids
118             */
119            public List<Integer[]> getPids() {
120                    return subTypes.getPids();
121            }
122    
123            /**
124             * Adds a PID value.
125             * <p>
126             * vCard versions: 4.0
127             * </p>
128             * @param localId the local ID
129             * @param clientPidMapRef the ID used to reference the property's globally
130             * unique identifier in the CLIENTPIDMAP property.
131             * @see VCardSubTypes#addPid(int, int)
132             */
133            public void addPid(int localId, int clientPidMapRef) {
134                    subTypes.addPid(localId, clientPidMapRef);
135            }
136    
137            /**
138             * Removes all PID values.
139             * <p>
140             * vCard versions: 4.0
141             * </p>
142             * @see VCardSubTypes#removePids
143             */
144            public void removePids() {
145                    subTypes.removePids();
146            }
147    
148            /**
149             * Gets the preference value.
150             * <p>
151             * vCard versions: 4.0
152             * </p>
153             * @return the preference value or null if it doesn't exist
154             * @see VCardSubTypes#getPref
155             */
156            public Integer getPref() {
157                    return subTypes.getPref();
158            }
159    
160            /**
161             * Sets the preference value.
162             * <p>
163             * vCard versions: 4.0
164             * </p>
165             * @param pref the preference value or null to remove
166             * @see VCardSubTypes#setPref
167             */
168            public void setPref(Integer pref) {
169                    subTypes.setPref(pref);
170            }
171    
172            /**
173             * Gets the ALTID.
174             * <p>
175             * vCard versions: 4.0
176             * </p>
177             * @return the ALTID or null if it doesn't exist
178             * @see VCardSubTypes#getAltId
179             */
180            public String getAltId() {
181                    return subTypes.getAltId();
182            }
183    
184            /**
185             * Sets the ALTID.
186             * <p>
187             * vCard versions: 4.0
188             * </p>
189             * @param altId the ALTID or null to remove
190             * @see VCardSubTypes#setAltId
191             */
192            public void setAltId(String altId) {
193                    subTypes.setAltId(altId);
194            }
195    
196            @Override
197            protected void doMarshalSubTypes(VCardSubTypes copy, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode, VCard vcard) {
198                    //replace "TYPE=pref" with "PREF=1"
199                    if (version == VCardVersion.V4_0) {
200                            if (getTypes().contains(EmailTypeParameter.PREF)) {
201                                    copy.removeType(EmailTypeParameter.PREF.getValue());
202                                    copy.setPref(1);
203                            }
204                    } else {
205                            copy.setPref(null);
206    
207                            //find the EMAIL with the lowest PREF value in the vCard
208                            EmailType mostPreferred = null;
209                            for (EmailType email : vcard.getEmails()) {
210                                    Integer pref = email.getPref();
211                                    if (pref != null) {
212                                            if (mostPreferred == null || pref < mostPreferred.getPref()) {
213                                                    mostPreferred = email;
214                                            }
215                                    }
216                            }
217                            if (this == mostPreferred) {
218                                    copy.addType(EmailTypeParameter.PREF.getValue());
219                            }
220                    }
221            }
222    
223            @Override
224            protected void doMarshalText(StringBuilder sb, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode) {
225                    sb.append(VCardStringUtils.escape(value));
226            }
227    
228            @Override
229            protected void doUnmarshalText(String value, VCardVersion version, List<String> warnings, CompatibilityMode compatibilityMode) {
230                    this.value = VCardStringUtils.unescape(value);
231            }
232    
233            @Override
234            protected void doMarshalXml(XCardElement parent, List<String> warnings, CompatibilityMode compatibilityMode) {
235                    parent.text(getValue());
236            }
237    
238            @Override
239            protected void doUnmarshalXml(XCardElement element, List<String> warnings, CompatibilityMode compatibilityMode) {
240                    setValue(element.text());
241            }
242    
243            @Override
244            protected void doUnmarshalHtml(HCardElement element, List<String> warnings) {
245                    List<String> types = element.types();
246                    for (String type : types) {
247                            subTypes.addType(type);
248                    }
249    
250                    //check to see if the email address is within in "mailto:" link
251                    String email = null;
252                    String href = element.attr("href");
253                    if (href.length() > 0) {
254                            Pattern p = Pattern.compile("^mailto:(.*)$", Pattern.CASE_INSENSITIVE);
255                            Matcher m = p.matcher(href);
256                            if (m.find()) {
257                                    email = m.group(1);
258                            }
259                    }
260                    if (email == null) {
261                            email = element.value();
262                    }
263                    setValue(email);
264            }
265    }