| 1 |
package ezvcard.io.text; |
| 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.Charset; |
| 9 |
import java.nio.file.Files; |
| 10 |
import java.nio.file.Path; |
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.List; |
| 13 |
|
| 14 |
import com.github.mangstadt.vinnie.VObjectProperty; |
| 15 |
import com.github.mangstadt.vinnie.io.Context; |
| 16 |
import com.github.mangstadt.vinnie.io.SyntaxRules; |
| 17 |
import com.github.mangstadt.vinnie.io.VObjectDataListener; |
| 18 |
import com.github.mangstadt.vinnie.io.VObjectPropertyValues; |
| 19 |
import com.github.mangstadt.vinnie.io.VObjectReader; |
| 20 |
import com.github.mangstadt.vinnie.io.Warning; |
| 21 |
|
| 22 |
import ezvcard.VCard; |
| 23 |
import ezvcard.VCardDataType; |
| 24 |
import ezvcard.VCardVersion; |
| 25 |
import ezvcard.io.CannotParseException; |
| 26 |
import ezvcard.io.EmbeddedVCardException; |
| 27 |
import ezvcard.io.ParseWarning; |
| 28 |
import ezvcard.io.SkipMeException; |
| 29 |
import ezvcard.io.StreamReader; |
| 30 |
import ezvcard.io.scribe.RawPropertyScribe; |
| 31 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 32 |
import ezvcard.parameter.Encoding; |
| 33 |
import ezvcard.parameter.VCardParameters; |
| 34 |
import ezvcard.property.Address; |
| 35 |
import ezvcard.property.Label; |
| 36 |
import ezvcard.property.VCardProperty; |
| 37 |
import ezvcard.util.IOUtils; |
| 38 |
import ezvcard.util.StringUtils; |
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public class VCardReader extends StreamReader { |
| 92 |
private final VObjectReader reader; |
| 93 |
private final VCardVersion defaultVersion; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public VCardReader(String str) { |
| 100 |
this(str, VCardVersion.V2_1); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public VCardReader(String str, VCardVersion defaultVersion) { |
| 110 |
this(new StringReader(str), defaultVersion); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public VCardReader(InputStream in) { |
| 118 |
this(in, VCardVersion.V2_1); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public VCardReader(InputStream in, VCardVersion defaultVersion) { |
| 128 |
this(new InputStreamReader(in), defaultVersion); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public VCardReader(Path file) throws IOException { |
| 137 |
this(file, VCardVersion.V2_1); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public VCardReader(Path file, VCardVersion defaultVersion) throws IOException { |
| 148 |
this(Files.newBufferedReader(file), defaultVersion); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public VCardReader(Reader reader) { |
| 156 |
this(reader, VCardVersion.V2_1); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public VCardReader(Reader reader, VCardVersion defaultVersion) { |
| 166 |
SyntaxRules rules = SyntaxRules.vcard(); |
| 167 |
rules.setDefaultSyntaxStyle(defaultVersion.getSyntaxStyle()); |
| 168 |
this.reader = new VObjectReader(reader, rules); |
| 169 |
this.defaultVersion = defaultVersion; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public boolean isCaretDecodingEnabled() { |
| 180 |
return reader.isCaretDecodingEnabled(); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public void setCaretDecodingEnabled(boolean enable) { |
| 191 |
reader.setCaretDecodingEnabled(enable); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public Charset getDefaultQuotedPrintableCharset() { |
| 201 |
return reader.getDefaultQuotedPrintableCharset(); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public void setDefaultQuotedPrintableCharset(Charset charset) { |
| 211 |
reader.setDefaultQuotedPrintableCharset(charset); |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
protected VCard _readNext() throws IOException { |
| 216 |
VObjectDataListenerImpl listener = new VObjectDataListenerImpl(); |
| 217 |
reader.parse(listener); |
| 218 |
return listener.root; |
| 219 |
} |
| 220 |
|
| 221 |
private class VObjectDataListenerImpl implements VObjectDataListener { |
| 222 |
private VCard root; |
| 223 |
private final VCardStack stack = new VCardStack(); |
| 224 |
private EmbeddedVCardException embeddedVCardException; |
| 225 |
|
| 226 |
public void onComponentBegin(String name, Context context) { |
| 227 |
if (!isVCardComponent(name)) { |
| 228 |
|
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
VCard vcard = new VCard(defaultVersion); |
| 233 |
if (stack.isEmpty()) { |
| 234 |
root = vcard; |
| 235 |
} |
| 236 |
stack.push(vcard); |
| 237 |
|
| 238 |
if (embeddedVCardException != null) { |
| 239 |
embeddedVCardException.injectVCard(vcard); |
| 240 |
embeddedVCardException = null; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public void onComponentEnd(String name, Context context) { |
| 245 |
if (!isVCardComponent(name)) { |
| 246 |
|
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
VCardStack.Item item = stack.pop(); |
| 251 |
assignLabels(item.vcard, item.labels); |
| 252 |
|
| 253 |
if (stack.isEmpty()) { |
| 254 |
context.stop(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
public void onProperty(VObjectProperty vobjectProperty, Context vobjectContext) { |
| 259 |
if (!inVCardComponent(vobjectContext.getParentComponents())) { |
| 260 |
|
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if (embeddedVCardException != null) { |
| 265 |
|
| 266 |
embeddedVCardException.injectVCard(null); |
| 267 |
embeddedVCardException = null; |
| 268 |
} |
| 269 |
|
| 270 |
VCard curVCard = stack.peek().vcard; |
| 271 |
VCardVersion version = curVCard.getVersion(); |
| 272 |
|
| 273 |
VCardProperty property = parseProperty(vobjectProperty, version, vobjectContext.getLineNumber()); |
| 274 |
if (property != null) { |
| 275 |
curVCard.addProperty(property); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
private VCardProperty parseProperty(VObjectProperty vobjectProperty, VCardVersion version, int lineNumber) { |
| 280 |
String group = vobjectProperty.getGroup(); |
| 281 |
String name = vobjectProperty.getName(); |
| 282 |
VCardParameters parameters = new VCardParameters(vobjectProperty.getParameters().getMap()); |
| 283 |
String value = vobjectProperty.getValue(); |
| 284 |
|
| 285 |
context.getWarnings().clear(); |
| 286 |
context.setVersion(version); |
| 287 |
context.setLineNumber(lineNumber); |
| 288 |
context.setPropertyName(name); |
| 289 |
|
| 290 |
|
| 291 |
processNamelessParameters(parameters); |
| 292 |
processQuotedMultivaluedTypeParams(parameters, version); |
| 293 |
|
| 294 |
|
| 295 |
VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(name); |
| 296 |
if (scribe == null) { |
| 297 |
scribe = new RawPropertyScribe(name); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
VCardDataType dataType = parameters.getValue(); |
| 302 |
parameters.setValue(null); |
| 303 |
if (dataType == null) { |
| 304 |
|
| 305 |
dataType = scribe.defaultDataType(version); |
| 306 |
} |
| 307 |
|
| 308 |
VCardProperty property; |
| 309 |
try { |
| 310 |
property = scribe.parseText(value, dataType, parameters, context); |
| 311 |
warnings.addAll(context.getWarnings()); |
| 312 |
} catch (SkipMeException e) { |
| 313 |
handleSkippedProperty(name, lineNumber, e); |
| 314 |
return null; |
| 315 |
} catch (CannotParseException e) { |
| 316 |
property = handleUnparseableProperty(name, parameters, value, dataType, lineNumber, version, e); |
| 317 |
} catch (EmbeddedVCardException e) { |
| 318 |
handledEmbeddedVCard(name, value, lineNumber, e); |
| 319 |
property = e.getProperty(); |
| 320 |
} |
| 321 |
|
| 322 |
property.setGroup(group); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if (property instanceof Label) { |
| 332 |
Label label = (Label) property; |
| 333 |
stack.peek().labels.add(label); |
| 334 |
return null; |
| 335 |
} |
| 336 |
|
| 337 |
handleLabelParameter(property); |
| 338 |
|
| 339 |
return property; |
| 340 |
} |
| 341 |
|
| 342 |
private void handleSkippedProperty(String propertyName, int lineNumber, SkipMeException e) { |
| 343 |
|
| 344 |
warnings.add(new ParseWarning.Builder(context) |
| 345 |
.message(22, e.getMessage()) |
| 346 |
.build() |
| 347 |
); |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
private VCardProperty handleUnparseableProperty(String name, VCardParameters parameters, String value, VCardDataType dataType, int lineNumber, VCardVersion version, CannotParseException e) { |
| 352 |
|
| 353 |
warnings.add(new ParseWarning.Builder(context) |
| 354 |
.message(e) |
| 355 |
.build() |
| 356 |
); |
| 357 |
|
| 358 |
|
| 359 |
RawPropertyScribe scribe = new RawPropertyScribe(name); |
| 360 |
return scribe.parseText(value, dataType, parameters, null); |
| 361 |
} |
| 362 |
|
| 363 |
private void handledEmbeddedVCard(String name, String value, int lineNumber, EmbeddedVCardException exception) { |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
if (value.trim().isEmpty()) { |
| 369 |
embeddedVCardException = exception; |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
value = VObjectPropertyValues.unescape(value); |
| 378 |
|
| 379 |
VCardReader agentReader = new VCardReader(value); |
| 380 |
agentReader.setCaretDecodingEnabled(isCaretDecodingEnabled()); |
| 381 |
agentReader.setDefaultQuotedPrintableCharset(getDefaultQuotedPrintableCharset()); |
| 382 |
agentReader.setScribeIndex(index); |
| 383 |
|
| 384 |
try { |
| 385 |
VCard nestedVCard = agentReader.readNext(); |
| 386 |
if (nestedVCard != null) { |
| 387 |
exception.injectVCard(nestedVCard); |
| 388 |
} |
| 389 |
} catch (IOException ignore) { |
| 390 |
|
| 391 |
} finally { |
| 392 |
warnings.addAll(agentReader.getWarnings()); |
| 393 |
IOUtils.closeQuietly(agentReader); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
private void handleLabelParameter(VCardProperty property) { |
| 409 |
if (!(property instanceof Address)) { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
Address adr = (Address) property; |
| 414 |
String label = adr.getLabel(); |
| 415 |
if (label == null) { |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
label = label.replace("\\n", StringUtils.NEWLINE); |
| 420 |
adr.setLabel(label); |
| 421 |
} |
| 422 |
|
| 423 |
public void onVersion(String value, Context vobjectContext) { |
| 424 |
VCardVersion version = VCardVersion.valueOfByStr(value); |
| 425 |
context.setVersion(version); |
| 426 |
stack.peek().vcard.setVersion(version); |
| 427 |
} |
| 428 |
|
| 429 |
public void onWarning(Warning warning, VObjectProperty property, Exception thrown, Context vobjectContext) { |
| 430 |
if (!inVCardComponent(vobjectContext.getParentComponents())) { |
| 431 |
|
| 432 |
return; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
warnings.add(new ParseWarning.Builder(context) |
| 437 |
.lineNumber(vobjectContext.getLineNumber()) |
| 438 |
.propertyName((property == null) ? null : property.getName()) |
| 439 |
.message(27, warning.getMessage(), vobjectContext.getUnfoldedLine()) |
| 440 |
.build() |
| 441 |
); |
| 442 |
|
| 443 |
} |
| 444 |
|
| 445 |
private boolean inVCardComponent(List<String> parentComponents) { |
| 446 |
if (parentComponents.isEmpty()) { |
| 447 |
return false; |
| 448 |
} |
| 449 |
String last = parentComponents.get(parentComponents.size() - 1); |
| 450 |
return isVCardComponent(last); |
| 451 |
} |
| 452 |
|
| 453 |
private boolean isVCardComponent(String componentName) { |
| 454 |
return "VCARD".equals(componentName); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
private void processNamelessParameters(VCardParameters parameters) { |
| 463 |
List<String> namelessValues = parameters.removeAll(null); |
| 464 |
for (String value : namelessValues) { |
| 465 |
String name = guessParameterName(value); |
| 466 |
parameters.put(name, value); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
private String guessParameterName(String value) { |
| 476 |
if (VCardDataType.find(value) != null) { |
| 477 |
return VCardParameters.VALUE; |
| 478 |
} |
| 479 |
|
| 480 |
if (Encoding.find(value) != null) { |
| 481 |
return VCardParameters.ENCODING; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
return VCardParameters.TYPE; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
private void processQuotedMultivaluedTypeParams(VCardParameters parameters, VCardVersion version) { |
| 504 |
if (version == VCardVersion.V2_1) { |
| 505 |
return; |
| 506 |
} |
| 507 |
|
| 508 |
List<String> types = parameters.getTypes(); |
| 509 |
if (types.isEmpty()) { |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
String valueWithComma = null; |
| 514 |
for (String value : types) { |
| 515 |
if (value.indexOf(',') >= 0) { |
| 516 |
valueWithComma = value; |
| 517 |
break; |
| 518 |
} |
| 519 |
} |
| 520 |
if (valueWithComma == null) { |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
types.clear(); |
| 525 |
int prev = -1, cur; |
| 526 |
while ((cur = valueWithComma.indexOf(',', prev + 1)) >= 0) { |
| 527 |
types.add(valueWithComma.substring(prev + 1, cur)); |
| 528 |
prev = cur; |
| 529 |
} |
| 530 |
types.add(valueWithComma.substring(prev + 1)); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
private static class VCardStack { |
| 538 |
private final List<Item> stack = new ArrayList<>(); |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
public void push(VCard vcard) { |
| 545 |
stack.add(new Item(vcard, new ArrayList<>())); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public Item pop() { |
| 553 |
return isEmpty() ? null : stack.remove(stack.size() - 1); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public Item peek() { |
| 561 |
return isEmpty() ? null : stack.get(stack.size() - 1); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public boolean isEmpty() { |
| 569 |
return stack.isEmpty(); |
| 570 |
} |
| 571 |
|
| 572 |
private static class Item { |
| 573 |
public final VCard vcard; |
| 574 |
public final List<Label> labels; |
| 575 |
|
| 576 |
public Item(VCard vcard, List<Label> labels) { |
| 577 |
this.vcard = vcard; |
| 578 |
this.labels = labels; |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public void close() throws IOException { |
| 588 |
reader.close(); |
| 589 |
} |
| 590 |
} |