Debian Codegraph @

src/main/java/ezvcard/io/json/JCardModule.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import com.fasterxml.jackson.core.Version;
4 import com.fasterxml.jackson.databind.module.SimpleModule;
5
6 import ezvcard.Ezvcard;
7 import ezvcard.VCard;
8 import ezvcard.io.scribe.ScribeIndex;
9 import ezvcard.io.scribe.VCardPropertyScribe;
10 import ezvcard.property.ProductId;
11 import ezvcard.property.VCardProperty;
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 The views and conclusions contained in the software and documentation are those
38 of the authors and should not be interpreted as representing official policies,
39 either expressed or implied, of the FreeBSD Project.
40 */
41
42 /**
43 * <p>
44 * Module for jackson-databind that serializes and deserializes jCards.
45 * </p>
46 * <p>
47 * <b>Example:</b>
48 * </p>
49 *
50 * <pre class="brush:java">
51 * ObjectMapper mapper = new ObjectMapper();
52 * mapper.registerModule(new JCardModule());
53 * VCard result = mapper.readValue(..., VCard.class);
54 * </pre>
55 * @author Buddy Gorven
56 */
57 public class JCardModule extends SimpleModule {
58 private static final long serialVersionUID = 6545279961222677077L;
59 private static final String MODULE_NAME = "ez-vcard-jcard";
60 private static final Version MODULE_VERSION = moduleVersion();
61
62 private final JCardDeserializer deserializer = new JCardDeserializer();
63 private final JCardSerializer serializer = new JCardSerializer();
64
65 private ScribeIndex index;
66
67 /**
68 * Creates the module.
69 */
70 public JCardModule() {
71 super(MODULE_NAME, MODULE_VERSION);
72
73 setScribeIndex(new ScribeIndex());
74 addSerializer(serializer);
75 addDeserializer(VCard.class, deserializer);
76 }
77
78 private static Version moduleVersion() {
79 String[] split = Ezvcard.VERSION.split("[.-]");
80 if (split.length < 3) {
81 /*
82 * This can happen during development if the "ez-vcard.properties"
83 * file has not been filtered by Maven.
84 */
85 return new Version(0, 0, 0, "", Ezvcard.GROUP_ID, Ezvcard.ARTIFACT_ID);
86 }
87
88 int major = Integer.parseInt(split[0]);
89 int minor = Integer.parseInt(split[1]);
90 int patch = Integer.parseInt(split[2]);
91 String snapshot = (split.length > 3) ? split[3] : "RELEASE";
92
93 return new Version(major, minor, patch, snapshot, Ezvcard.GROUP_ID, Ezvcard.ARTIFACT_ID);
94 }
95
96 /**
97 * Gets whether a {@link ProductId} property will be added to each
98 * serialized vCard that marks it as having been generated by this library.
99 * @return true if the property will be added, false if not (defaults to
100 * true)
101 */
102 public boolean isAddProdId() {
103 return serializer.isAddProdId();
104 }
105
106 /**
107 * Sets whether to add a {@link ProductId} property to each serialized vCard
108 * that marks it as having been generated by this library.
109 * @param addProdId true to add the property, false not to (defaults to
110 * true)
111 */
112 public void setAddProdId(boolean addProdId) {
113 serializer.setAddProdId(addProdId);
114 }
115
116 /**
117 * Gets whether properties that do not support jCard will be excluded from
118 * each serialized vCard. jCard only supports properties defined in the
119 * vCard version 4.0 specification.
120 * @return true if the properties will be excluded, false if not (defaults
121 * to true)
122 */
123 public boolean isVersionStrict() {
124 return serializer.isVersionStrict();
125 }
126
127 /**
128 * Sets whether properties that do not support jCard will be excluded from
129 * each serialized vCard. jCard only supports properties defined in the
130 * vCard version 4.0 specification.
131 * @param versionStrict true to exclude such properties, false not to
132 * (defaults to true)
133 */
134 public void setVersionStrict(boolean versionStrict) {
135 serializer.setVersionStrict(versionStrict);
136 }
137
138 /**
139 * <p>
140 * Registers a property scribe. This is the same as calling:
141 * </p>
142 * <p>
143 * {@code getScribeIndex().register(scribe)}
144 * </p>
145 * @param scribe the scribe to register
146 */
147 public void registerScribe(VCardPropertyScribe<? extends VCardProperty> scribe) {
148 index.register(scribe);
149 }
150
151 /**
152 * Gets the scribe index used by the serializer and deserializer.
153 * @return the scribe index
154 */
155 public ScribeIndex getScribeIndex() {
156 return index;
157 }
158
159 /**
160 * Sets the scribe index for the serializer and deserializer to use.
161 * @param index the scribe index
162 */
163 public void setScribeIndex(ScribeIndex index) {
164 this.index = index;
165 serializer.setScribeIndex(index);
166 deserializer.setScribeIndex(index);
167 }
168 }