001    package ezvcard.io.scribe;
002    
003    import java.util.List;
004    import java.util.Set;
005    
006    import ezvcard.Messages;
007    import ezvcard.VCard;
008    import ezvcard.VCardDataType;
009    import ezvcard.VCardVersion;
010    import ezvcard.io.EmbeddedVCardException;
011    import ezvcard.io.SkipMeException;
012    import ezvcard.io.html.HCardElement;
013    import ezvcard.parameter.VCardParameters;
014    import ezvcard.property.Agent;
015    import ezvcard.property.VCardProperty;
016    
017    /*
018     Copyright (c) 2013, 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 {@link Agent} properties.
044     * @author Michael Angstadt
045     */
046    public class AgentScribe extends VCardPropertyScribe<Agent> {
047            public AgentScribe() {
048                    super(Agent.class, "AGENT");
049            }
050    
051            @Override
052            protected VCardDataType _defaultDataType(VCardVersion version) {
053                    return null;
054            }
055    
056            @Override
057            protected VCardDataType _dataType(Agent property, VCardVersion version) {
058                    if (property.getUrl() != null) {
059                            return (version == VCardVersion.V2_1) ? VCardDataType.URL : VCardDataType.URI;
060                    }
061                    return null;
062            }
063    
064            @Override
065            protected String _writeText(Agent property, VCardVersion version) {
066                    String url = property.getUrl();
067                    if (url != null) {
068                            return url;
069                    }
070    
071                    VCard vcard = property.getVCard();
072                    if (vcard != null) {
073                            throw new EmbeddedVCardException(vcard);
074                    }
075    
076                    //don't write an empty value because parsers could interpret that as there being an embedded vCard on the next line
077                    throw new SkipMeException(Messages.INSTANCE.getValidationWarning(8));
078            }
079    
080            @Override
081            protected Agent _parseText(String value, VCardDataType dataType, VCardVersion version, VCardParameters parameters, List<String> warnings) {
082                    Agent property = new Agent();
083    
084                    if (dataType == null) {
085                            throw new EmbeddedVCardException(new Injector(property));
086                    }
087    
088                    property.setUrl(unescape(value));
089                    return property;
090            }
091    
092            @Override
093            protected Agent _parseHtml(HCardElement element, List<String> warnings) {
094                    Agent property = new Agent();
095    
096                    Set<String> classes = element.classNames();
097                    if (classes.contains("vcard")) {
098                            throw new EmbeddedVCardException(new Injector(property));
099                    }
100    
101                    String url = element.absUrl("href");
102                    if (url.length() == 0) {
103                            url = element.value();
104                    }
105                    property.setUrl(url);
106    
107                    return property;
108            }
109    
110            private static class Injector implements EmbeddedVCardException.InjectionCallback {
111                    private final Agent property;
112    
113                    public Injector(Agent property) {
114                            this.property = property;
115                    }
116    
117                    public void injectVCard(VCard vcard) {
118                            property.setVCard(vcard);
119                    }
120    
121                    public VCardProperty getProperty() {
122                            return property;
123                    }
124            }
125    }