| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public class JCardReader extends StreamReader { |
| 78 |
private final JCardRawReader reader; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public JCardReader(String json) { |
| 84 |
this(new StringReader(json)); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public JCardReader(InputStream in) { |
| 91 |
this(new InputStreamReader(in, StandardCharsets.UTF_8)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public JCardReader(Path file) throws IOException { |
| 99 |
this(Files.newBufferedReader(file, StandardCharsets.UTF_8)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public JCardReader(Reader reader) { |
| 106 |
this.reader = new JCardRawReader(reader); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 129 |
warnings.add(new ParseWarning.Builder() |
| 130 |
.lineNumber(reader.getLineNum()) |
| 131 |
.message(29) |
| 132 |
.build() |
| 133 |
); |
| 134 |
|
| 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 |
|
| 159 |
versionFound = true; |
| 160 |
|
| 161 |
VCardVersion version = VCardVersion.valueOfByStr(value.asSingle()); |
| 162 |
if (version != VCardVersion.V4_0) { |
| 163 |
|
| 164 |
warnings.add(new ParseWarning.Builder(context) |
| 165 |
.message(30) |
| 166 |
.build() |
| 167 |
); |
| 168 |
|
| 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 |
|
| 184 |
warnings.add(new ParseWarning.Builder(context) |
| 185 |
.message(22, e.getMessage()) |
| 186 |
.build() |
| 187 |
); |
| 188 |
|
| 189 |
return; |
| 190 |
} catch (CannotParseException e) { |
| 191 |
scribe = new RawPropertyScribe(propertyName); |
| 192 |
property = scribe.parseJson(value, dataType, parameters, context); |
| 193 |
|
| 194 |
|
| 195 |
warnings.add(new ParseWarning.Builder(context) |
| 196 |
.message(e) |
| 197 |
.build() |
| 198 |
); |
| 199 |
|
| 200 |
} catch (EmbeddedVCardException e) { |
| 201 |
|
| 202 |
warnings.add(new ParseWarning.Builder(context) |
| 203 |
.message(31) |
| 204 |
.build() |
| 205 |
); |
| 206 |
|
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
property.setGroup(group); |
| 211 |
vcard.addProperty(property); |
| 212 |
} |
| 213 |
} |
| 214 |
} |