001    package ezvcard.io.scribe;
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.VCardVersion;
009    import ezvcard.io.html.HCardElement;
010    import ezvcard.parameter.VCardParameters;
011    import ezvcard.property.Email;
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 {@link Email} properties.
040     * @author Michael Angstadt
041     */
042    public class EmailScribe extends StringPropertyScribe<Email> {
043            public EmailScribe() {
044                    super(Email.class, "EMAIL");
045            }
046    
047            @Override
048            protected void _prepareParameters(Email property, VCardParameters copy, VCardVersion version, VCard vcard) {
049                    handlePrefParam(property, copy, version, vcard);
050            }
051    
052            @Override
053            protected Email _parseValue(String value) {
054                    return new Email(value);
055            }
056    
057            @Override
058            protected Email _parseHtml(HCardElement element, List<String> warnings) {
059                    //check to see if the email address is within in "mailto:" link
060                    String email;
061                    String href = element.attr("href");
062                    if (href.length() > 0) {
063                            Pattern p = Pattern.compile("^mailto:(.*)$", Pattern.CASE_INSENSITIVE);
064                            Matcher m = p.matcher(href);
065                            email = m.find() ? m.group(1) : element.value();
066                    } else {
067                            email = element.value();
068                    }
069    
070                    Email property = new Email(email);
071    
072                    //add TYPE parameters
073                    List<String> types = element.types();
074                    for (String type : types) {
075                            property.getParameters().addType(type);
076                    }
077    
078                    return property;
079            }
080    }