Debian Codegraph @

src/main/java/ezvcard/io/scribe/AgentScribe.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import java.util.Set;
4
5 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
6
7 import ezvcard.Messages;
8 import ezvcard.VCard;
9 import ezvcard.VCardDataType;
10 import ezvcard.VCardVersion;
11 import ezvcard.io.EmbeddedVCardException;
12 import ezvcard.io.ParseContext;
13 import ezvcard.io.SkipMeException;
14 import ezvcard.io.html.HCardElement;
15 import ezvcard.io.text.WriteContext;
16 import ezvcard.parameter.VCardParameters;
17 import ezvcard.property.Agent;
18 import ezvcard.property.VCardProperty;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 /**
46 * Marshals {@link Agent} properties.
47 * @author Michael Angstadt
48 */
49 public class AgentScribe extends VCardPropertyScribe<Agent> {
50 public AgentScribe() {
51 super(Agent.class, "AGENT");
52 }
53
54 @Override
55 protected VCardDataType _defaultDataType(VCardVersion version) {
56 return null;
57 }
58
59 @Override
60 protected VCardDataType _dataType(Agent property, VCardVersion version) {
61 if (property.getUrl() != null) {
62 return (version == VCardVersion.V2_1) ? VCardDataType.URL : VCardDataType.URI;
63 }
64 return null;
65 }
66
67 @Override
68 protected String _writeText(Agent property, WriteContext context) {
69 String url = property.getUrl();
70 if (url != null) {
71 return url;
72 }
73
74 VCard vcard = property.getVCard();
75 if (vcard != null) {
76 throw new EmbeddedVCardException(vcard);
77 }
78
79 //don't write an empty value because parsers could interpret that as there being an embedded vCard on the next line
80 throw new SkipMeException(Messages.INSTANCE.getValidationWarning(8));
81 }
82
83 @Override
84 protected Agent _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
85 Agent property = new Agent();
86
87 if (dataType == null) {
88 throw new EmbeddedVCardException(new Injector(property));
89 }
90
91 property.setUrl(VObjectPropertyValues.unescape(value));
92 return property;
93 }
94
95 @Override
96 protected Agent _parseHtml(HCardElement element, ParseContext context) {
97 Agent property = new Agent();
98
99 Set<String> classes = element.classNames();
100 if (classes.contains("vcard")) {
101 throw new EmbeddedVCardException(new Injector(property));
102 }
103
104 String url = element.absUrl("href");
105 if (url.isEmpty()) {
106 url = element.value();
107 }
108 property.setUrl(url);
109
110 return property;
111 }
112
113 private static class Injector implements EmbeddedVCardException.InjectionCallback {
114 private final Agent property;
115
116 public Injector(Agent property) {
117 this.property = property;
118 }
119
120 public void injectVCard(VCard vcard) {
121 property.setVCard(vcard);
122 }
123
124 public VCardProperty getProperty() {
125 return property;
126 }
127 }
128 }