Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.Reader;
7 import java.io.StringReader;
8 import java.nio.charset.StandardCharsets;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11
12 import com.fasterxml.jackson.core.JsonParser;
13
14 import ezvcard.VCard;
15 import ezvcard.VCardDataType;
16 import ezvcard.VCardVersion;
17 import ezvcard.io.CannotParseException;
18 import ezvcard.io.EmbeddedVCardException;
19 import ezvcard.io.ParseWarning;
20 import ezvcard.io.SkipMeException;
21 import ezvcard.io.StreamReader;
22 import ezvcard.io.json.JCardRawReader.JCardDataStreamListener;
23 import ezvcard.io.scribe.RawPropertyScribe;
24 import ezvcard.io.scribe.VCardPropertyScribe;
25 import ezvcard.parameter.VCardParameters;
26 import ezvcard.property.VCardProperty;
27
28 /*
29 Copyright (c) 2012-2023, Michael Angstadt
30 All rights reserved.
31
32 Redistribution and use in source and binary forms, with or without
33 modification, are permitted provided that the following conditions are met:
34
35 1. Redistributions of source code must retain the above copyright notice, this
36 list of conditions and the following disclaimer.
37 2. Redistributions in binary form must reproduce the above copyright notice,
38 this list of conditions and the following disclaimer in the documentation
39 and/or other materials provided with the distribution.
40
41 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
42 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
45 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
52 The views and conclusions contained in the software and documentation are those
53 of the authors and should not be interpreted as representing official policies,
54 either expressed or implied, of the FreeBSD Project.
55 */
56
57 /**
58 * <p>
59 * Parses {@link VCard} objects from a JSON data stream (jCard format).
60 * </p>
61 * <p>
62 * <b>Example:</b>
63 * </p>
64 *
65 * <pre class="brush:java">
66 * Path file = Paths.get("vcards.json");
67 * try (JCardReader reader = new JCardReader(file)) {
68 * VCard vcard;
69 * while ((vcard = reader.readNext()) != null) {
70 * //...
71 * }
72 * }
73 * </pre>
74 * @author Michael Angstadt
75 * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
76 */
77 public class JCardReader extends StreamReader {
78 private final JCardRawReader reader;
79
80 /**
81 * @param json the JSON string to read from
82 */
83 public JCardReader(String json) {
84 this(new StringReader(json));
85 }
86
87 /**
88 * @param in the input stream to read from
89 */
90 public JCardReader(InputStream in) {
91 this(new InputStreamReader(in, StandardCharsets.UTF_8));
92 }
93
94 /**
95 * @param file the file to read from
96 * @throws IOException if there is a problem reading the file
97 */
98 public JCardReader(Path file) throws IOException {
99 this(Files.newBufferedReader(file, StandardCharsets.UTF_8));
100 }
101
102 /**
103 * @param reader the reader to read from
104 */
105 public JCardReader(Reader reader) {
106 this.reader = new JCardRawReader(reader);
107 }
108
109 /**
110 * @param parser the parser to read from
111 */
112 public JCardReader(JsonParser parser) {
113 this.reader = new JCardRawReader(parser, true);
114 }
115
116 @Override
117 protected VCard _readNext() throws IOException {
118 if (reader.eof()) {
119 return null;
120 }
121
122 context.setVersion(VCardVersion.V4_0);
123
124 JCardDataStreamListenerImpl listener = new JCardDataStreamListenerImpl();
125 reader.readNext(listener);
126 VCard vcard = listener.vcard;
127 if (vcard != null && !listener.versionFound) {
128 //@formatter:off
129 warnings.add(new ParseWarning.Builder()
130 .lineNumber(reader.getLineNum())
131 .message(29)
132 .build()
133 );
134 //@formatter:on
135 }
136 return vcard;
137 }
138
139 public void close() throws IOException {
140 reader.close();
141 }
142
143 private class JCardDataStreamListenerImpl implements JCardDataStreamListener {
144 private VCard vcard = null;
145 private boolean versionFound = false;
146
147 public void beginVCard() {
148 vcard = new VCard();
149 vcard.setVersion(VCardVersion.V4_0);
150 }
151
152 public void readProperty(String group, String propertyName, VCardParameters parameters, VCardDataType dataType, JCardValue value) {
153 context.getWarnings().clear();
154 context.setLineNumber(reader.getLineNum());
155 context.setPropertyName(propertyName);
156
157 if ("version".equalsIgnoreCase(propertyName)) {
158 //don't unmarshal "version" because we don't treat it as a property
159 versionFound = true;
160
161 VCardVersion version = VCardVersion.valueOfByStr(value.asSingle());
162 if (version != VCardVersion.V4_0) {
163 //@formatter:off
164 warnings.add(new ParseWarning.Builder(context)
165 .message(30)
166 .build()
167 );
168 //@formatter:on
169 }
170 return;
171 }
172
173 VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(propertyName);
174 if (scribe == null) {
175 scribe = new RawPropertyScribe(propertyName);
176 }
177
178 VCardProperty property;
179 try {
180 property = scribe.parseJson(value, dataType, parameters, context);
181 warnings.addAll(context.getWarnings());
182 } catch (SkipMeException e) {
183 //@formatter:off
184 warnings.add(new ParseWarning.Builder(context)
185 .message(22, e.getMessage())
186 .build()
187 );
188 //@formatter:on
189 return;
190 } catch (CannotParseException e) {
191 scribe = new RawPropertyScribe(propertyName);
192 property = scribe.parseJson(value, dataType, parameters, context);
193
194 //@formatter:off
195 warnings.add(new ParseWarning.Builder(context)
196 .message(e)
197 .build()
198 );
199 //@formatter:on
200 } catch (EmbeddedVCardException e) {
201 //@formatter:off
202 warnings.add(new ParseWarning.Builder(context)
203 .message(31)
204 .build()
205 );
206 //@formatter:on
207 return;
208 }
209
210 property.setGroup(group);
211 vcard.addProperty(property);
212 }
213 }
214 }