Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.xml.namespace.QName;
7
8 import ezvcard.VCardVersion;
9 import ezvcard.property.RawProperty;
10 import ezvcard.property.VCardProperty;
11 import ezvcard.property.Xml;
12
13 /*
14 Copyright (c) 2012-2023, Michael Angstadt
15 All rights reserved.
16
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions are met:
19
20 1. Redistributions of source code must retain the above copyright notice, this
21 list of conditions and the following disclaimer.
22 2. Redistributions in binary form must reproduce the above copyright notice,
23 this list of conditions and the following disclaimer in the documentation
24 and/or other materials provided with the distribution.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /**
39 * <p>
40 * Manages a collection of property scribes (aka "marshallers" or "serializers")
41 * to use when reading or writing a vCard. The same instance of this object can
42 * be re-used across multiple vCard reader/writer objects. This is useful if you
43 * have custom scribe classes defined, as it allows you to only define them once
44 * instead of each time a vCard reader/writer object is created.
45 * </p>
46 * <p>
47 * <b>Example:</b>
48 * </p>
49 *
50 * <pre class="brush:java">
51 * //init the index
52 * ScribeIndex index = new ScribeIndex();
53 * index.register(new CustomPropertyScribe());
54 * index.register(new AnotherCustomPropertyScribe());
55 *
56 * //inject the ScribeIndex into a plain-text vCard reader class and read the vCard data stream
57 * try (VCardReader vcardReader = new VCardReader(...)) {
58 * vcardReader.setScribeIndex(index);
59 * List&lt;VCard&gt; vcards = new ArrayList&lt;VCard&gt;();
60 * VCard vcard;
61 * while ((vcards = vcardReader.readNext()) != null) {
62 * vcards.add(vcard);
63 * }
64 * }
65 *
66 * //inject the same ScribeIndex instance into a jCard writer and write the vCards
67 * try (JCardWriter jcardWriter = new JCardWriter(...)) {
68 * jcardWriter.setScribeIndex(index);
69 * for (VCard vcard : vcards) {
70 * jcardWriter.write(vcard);
71 * }
72 * }
73 * </pre>
74 * @author Michael Angstadt
75 */
76 public class ScribeIndex {
77 //define standard property scribes
78 private static final Map<String, VCardPropertyScribe<? extends VCardProperty>> standardByName = new HashMap<>();
79 private static final Map<Class<? extends VCardProperty>, VCardPropertyScribe<? extends VCardProperty>> standardByClass = new HashMap<>();
80 private static final Map<QName, VCardPropertyScribe<? extends VCardProperty>> standardByQName = new HashMap<>();
81 static {
82 //2.1, RFC 2426, RFC 6350
83 registerStandard(new AddressScribe());
84 registerStandard(new AgentScribe());
85 registerStandard(new AnniversaryScribe());
86 registerStandard(new BirthdayScribe());
87 registerStandard(new CalendarRequestUriScribe());
88 registerStandard(new CalendarUriScribe());
89 registerStandard(new CategoriesScribe());
90 registerStandard(new ClassificationScribe());
91 registerStandard(new ClientPidMapScribe());
92 registerStandard(new EmailScribe());
93 registerStandard(new FreeBusyUrlScribe());
94 registerStandard(new FormattedNameScribe());
95 registerStandard(new GenderScribe());
96 registerStandard(new GeoScribe());
97 registerStandard(new ImppScribe());
98 registerStandard(new KeyScribe());
99 registerStandard(new KindScribe());
100 registerStandard(new LabelScribe());
101 registerStandard(new LanguageScribe());
102 registerStandard(new LogoScribe());
103 registerStandard(new MailerScribe());
104 registerStandard(new MemberScribe());
105 registerStandard(new NicknameScribe());
106 registerStandard(new NoteScribe());
107 registerStandard(new OrganizationScribe());
108 registerStandard(new PhotoScribe());
109 registerStandard(new ProductIdScribe());
110 registerStandard(new ProfileScribe());
111 registerStandard(new RelatedScribe());
112 registerStandard(new RevisionScribe());
113 registerStandard(new RoleScribe());
114 registerStandard(new SortStringScribe());
115 registerStandard(new SoundScribe());
116 registerStandard(new SourceDisplayTextScribe());
117 registerStandard(new SourceScribe());
118 registerStandard(new StructuredNameScribe());
119 registerStandard(new TelephoneScribe());
120 registerStandard(new TimezoneScribe());
121 registerStandard(new TitleScribe());
122 registerStandard(new UidScribe());
123 registerStandard(new UrlScribe());
124
125 //RFC 6351
126 registerStandard(new XmlScribe());
127
128 //RFC 6474
129 registerStandard(new BirthplaceScribe());
130 registerStandard(new DeathdateScribe());
131 registerStandard(new DeathplaceScribe());
132
133 //RFC 6715
134 registerStandard(new ExpertiseScribe());
135 registerStandard(new OrgDirectoryScribe());
136 registerStandard(new InterestScribe());
137 registerStandard(new HobbyScribe());
138 }
139
140 private final Map<String, VCardPropertyScribe<? extends VCardProperty>> extendedByName = new HashMap<>(0);
141 private final Map<Class<? extends VCardProperty>, VCardPropertyScribe<? extends VCardProperty>> extendedByClass = new HashMap<>(0);
142 private final Map<QName, VCardPropertyScribe<? extends VCardProperty>> extendedByQName = new HashMap<>(0);
143
144 /**
145 * Gets a property scribe by name.
146 * @param propertyName the property name (case-insensitive, e.g. "FN")
147 * @return the property scribe or null if not found
148 */
149 public VCardPropertyScribe<? extends VCardProperty> getPropertyScribe(String propertyName) {
150 propertyName = propertyName.toUpperCase();
151
152 VCardPropertyScribe<? extends VCardProperty> scribe = extendedByName.get(propertyName);
153 if (scribe != null) {
154 return scribe;
155 }
156
157 return standardByName.get(propertyName);
158 }
159
160 /**
161 * Determines if a scribe exists for a given property instance.
162 * @param property the property
163 * @return true if a scribe exists, false if not
164 */
165 public boolean hasPropertyScribe(VCardProperty property) {
166 if (property instanceof RawProperty) {
167 return true;
168 }
169
170 return getPropertyScribe(property.getClass()) != null;
171 }
172
173 /**
174 * Gets a property scribe by class.
175 * @param clazz the property class
176 * @return the property scribe or null if not found
177 */
178 public VCardPropertyScribe<? extends VCardProperty> getPropertyScribe(Class<? extends VCardProperty> clazz) {
179 VCardPropertyScribe<? extends VCardProperty> scribe = extendedByClass.get(clazz);
180 if (scribe != null) {
181 return scribe;
182 }
183
184 return standardByClass.get(clazz);
185 }
186
187 /**
188 * Gets the appropriate property scribe for a given property instance.
189 * @param property the property instance
190 * @return the property scribe or null if not found
191 */
192 public VCardPropertyScribe<? extends VCardProperty> getPropertyScribe(VCardProperty property) {
193 if (property instanceof RawProperty) {
194 RawProperty raw = (RawProperty) property;
195 return new RawPropertyScribe(raw.getPropertyName());
196 }
197
198 return getPropertyScribe(property.getClass());
199 }
200
201 /**
202 * Gets a property scribe by XML local name and namespace.
203 * @param qname the XML local name and namespace
204 * @return the property scribe or a {@link XmlScribe} if not found
205 */
206 public VCardPropertyScribe<? extends VCardProperty> getPropertyScribe(QName qname) {
207 VCardPropertyScribe<? extends VCardProperty> scribe = extendedByQName.get(qname);
208 if (scribe != null) {
209 return scribe;
210 }
211
212 scribe = standardByQName.get(qname);
213 if (scribe != null) {
214 return scribe;
215 }
216
217 if (VCardVersion.V4_0.getXmlNamespace().equals(qname.getNamespaceURI())) {
218 return new RawPropertyScribe(qname.getLocalPart().toUpperCase());
219 }
220
221 return getPropertyScribe(Xml.class);
222 }
223
224 /**
225 * Registers a property scribe.
226 * @param scribe the scribe to register
227 */
228 public void register(VCardPropertyScribe<? extends VCardProperty> scribe) {
229 extendedByName.put(scribe.getPropertyName().toUpperCase(), scribe);
230 extendedByClass.put(scribe.getPropertyClass(), scribe);
231 extendedByQName.put(scribe.getQName(), scribe);
232 }
233
234 /**
235 * Unregisters a property scribe.
236 * @param scribe the scribe to unregister
237 */
238 public void unregister(VCardPropertyScribe<? extends VCardProperty> scribe) {
239 extendedByName.remove(scribe.getPropertyName().toUpperCase());
240 extendedByClass.remove(scribe.getPropertyClass());
241 extendedByQName.remove(scribe.getQName());
242 }
243
244 private static void registerStandard(VCardPropertyScribe<? extends VCardProperty> scribe) {
245 standardByName.put(scribe.getPropertyName().toUpperCase(), scribe);
246 standardByClass.put(scribe.getPropertyClass(), scribe);
247 standardByQName.put(scribe.getQName(), scribe);
248 }
249 }