| 1 |
package ezvcard.io.xml; |
| 2 |
|
| 3 |
import static ezvcard.io.xml.XCardQNames.GROUP; |
| 4 |
import static ezvcard.io.xml.XCardQNames.PARAMETERS; |
| 5 |
import static ezvcard.io.xml.XCardQNames.VCARD; |
| 6 |
import static ezvcard.io.xml.XCardQNames.VCARDS; |
| 7 |
|
| 8 |
import java.io.BufferedInputStream; |
| 9 |
import java.io.Closeable; |
| 10 |
import java.io.IOException; |
| 11 |
import java.io.InputStream; |
| 12 |
import java.io.Reader; |
| 13 |
import java.io.StringReader; |
| 14 |
import java.nio.file.Files; |
| 15 |
import java.nio.file.Path; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.List; |
| 18 |
import java.util.concurrent.ArrayBlockingQueue; |
| 19 |
import java.util.concurrent.BlockingQueue; |
| 20 |
|
| 21 |
import javax.xml.namespace.QName; |
| 22 |
import javax.xml.transform.ErrorListener; |
| 23 |
import javax.xml.transform.Source; |
| 24 |
import javax.xml.transform.Transformer; |
| 25 |
import javax.xml.transform.TransformerConfigurationException; |
| 26 |
import javax.xml.transform.TransformerException; |
| 27 |
import javax.xml.transform.TransformerFactory; |
| 28 |
import javax.xml.transform.dom.DOMSource; |
| 29 |
import javax.xml.transform.sax.SAXResult; |
| 30 |
import javax.xml.transform.stream.StreamSource; |
| 31 |
|
| 32 |
import org.w3c.dom.Document; |
| 33 |
import org.w3c.dom.Element; |
| 34 |
import org.w3c.dom.Node; |
| 35 |
import org.xml.sax.Attributes; |
| 36 |
import org.xml.sax.SAXException; |
| 37 |
import org.xml.sax.helpers.DefaultHandler; |
| 38 |
|
| 39 |
import ezvcard.VCard; |
| 40 |
import ezvcard.VCardVersion; |
| 41 |
import ezvcard.io.CannotParseException; |
| 42 |
import ezvcard.io.EmbeddedVCardException; |
| 43 |
import ezvcard.io.ParseWarning; |
| 44 |
import ezvcard.io.SkipMeException; |
| 45 |
import ezvcard.io.StreamReader; |
| 46 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 47 |
import ezvcard.parameter.VCardParameters; |
| 48 |
import ezvcard.property.VCardProperty; |
| 49 |
import ezvcard.property.Xml; |
| 50 |
import ezvcard.util.ClearableStringBuilder; |
| 51 |
import ezvcard.util.XmlUtils; |
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public class XCardReader extends StreamReader { |
| 104 |
private final VCardVersion version = VCardVersion.V4_0; |
| 105 |
private final String NS = version.getXmlNamespace(); |
| 106 |
|
| 107 |
private final Source source; |
| 108 |
private final Closeable stream; |
| 109 |
|
| 110 |
private volatile VCard readVCard; |
| 111 |
private volatile TransformerException thrown; |
| 112 |
|
| 113 |
private final ReadThread thread = new ReadThread(); |
| 114 |
private final Object lock = new Object(); |
| 115 |
private final BlockingQueue<Object> readerBlock = new ArrayBlockingQueue<>(1); |
| 116 |
private final BlockingQueue<Object> threadBlock = new ArrayBlockingQueue<>(1); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public XCardReader(String xml) { |
| 122 |
this(new StringReader(xml)); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public XCardReader(InputStream in) { |
| 129 |
source = new StreamSource(in); |
| 130 |
stream = in; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public XCardReader(Path file) throws IOException { |
| 138 |
this(new BufferedInputStream(Files.newInputStream(file))); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public XCardReader(Reader reader) { |
| 145 |
source = new StreamSource(reader); |
| 146 |
stream = reader; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public XCardReader(Node node) { |
| 153 |
source = new DOMSource(node); |
| 154 |
stream = null; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
protected VCard _readNext() throws IOException { |
| 159 |
readVCard = null; |
| 160 |
thrown = null; |
| 161 |
context.setVersion(version); |
| 162 |
|
| 163 |
if (!thread.started) { |
| 164 |
thread.start(); |
| 165 |
} else { |
| 166 |
if (thread.finished || thread.closed) { |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
try { |
| 171 |
threadBlock.put(lock); |
| 172 |
} catch (InterruptedException e) { |
| 173 |
return null; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
try { |
| 179 |
readerBlock.take(); |
| 180 |
} catch (InterruptedException e) { |
| 181 |
return null; |
| 182 |
} |
| 183 |
|
| 184 |
if (thrown != null) { |
| 185 |
throw new IOException(thrown); |
| 186 |
} |
| 187 |
|
| 188 |
return readVCard; |
| 189 |
} |
| 190 |
|
| 191 |
private class ReadThread extends Thread { |
| 192 |
private final SAXResult result; |
| 193 |
private final Transformer transformer; |
| 194 |
private volatile boolean finished = false, started = false, closed = false; |
| 195 |
|
| 196 |
public ReadThread() { |
| 197 |
setName(getClass().getSimpleName()); |
| 198 |
|
| 199 |
|
| 200 |
try { |
| 201 |
TransformerFactory factory = TransformerFactory.newInstance(); |
| 202 |
XmlUtils.applyXXEProtection(factory); |
| 203 |
|
| 204 |
transformer = factory.newTransformer(); |
| 205 |
} catch (TransformerConfigurationException e) { |
| 206 |
|
| 207 |
throw new RuntimeException(e); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
transformer.setErrorListener(new NoOpErrorListener()); |
| 212 |
|
| 213 |
result = new SAXResult(new ContentHandlerImpl()); |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public void run() { |
| 218 |
started = true; |
| 219 |
|
| 220 |
try { |
| 221 |
transformer.transform(source, result); |
| 222 |
} catch (TransformerException e) { |
| 223 |
if (!thread.closed) { |
| 224 |
thrown = e; |
| 225 |
} |
| 226 |
} finally { |
| 227 |
finished = true; |
| 228 |
try { |
| 229 |
readerBlock.put(lock); |
| 230 |
} catch (InterruptedException e) { |
| 231 |
|
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
private class ContentHandlerImpl extends DefaultHandler { |
| 238 |
private final Document DOC = XmlUtils.createDocument(); |
| 239 |
private final XCardStructure structure = new XCardStructure(); |
| 240 |
private final ClearableStringBuilder characterBuffer = new ClearableStringBuilder(); |
| 241 |
|
| 242 |
private String group; |
| 243 |
private Element propertyElement, parent; |
| 244 |
private QName paramName; |
| 245 |
private VCardParameters parameters; |
| 246 |
|
| 247 |
@Override |
| 248 |
public void characters(char[] buffer, int start, int length) throws SAXException { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
if (propertyElement == null) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
characterBuffer.append(buffer, start, length); |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public void startElement(String namespace, String localName, String qName, Attributes attributes) throws SAXException { |
| 263 |
QName qname = new QName(namespace, localName); |
| 264 |
String textContent = characterBuffer.getAndClear(); |
| 265 |
|
| 266 |
if (structure.isEmpty()) { |
| 267 |
|
| 268 |
if (VCARDS.equals(qname)) { |
| 269 |
structure.push(ElementType.vcards); |
| 270 |
} |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
ElementType parentType = structure.peek(); |
| 275 |
ElementType typeToPush = null; |
| 276 |
|
| 277 |
if (parentType != null) { |
| 278 |
switch (parentType) { |
| 279 |
case vcards: |
| 280 |
|
| 281 |
if (VCARD.equals(qname)) { |
| 282 |
readVCard = new VCard(); |
| 283 |
readVCard.setVersion(version); |
| 284 |
typeToPush = ElementType.vcard; |
| 285 |
} |
| 286 |
break; |
| 287 |
|
| 288 |
case vcard: |
| 289 |
|
| 290 |
if (GROUP.equals(qname)) { |
| 291 |
group = attributes.getValue("name"); |
| 292 |
typeToPush = ElementType.group; |
| 293 |
} else { |
| 294 |
propertyElement = createElement(namespace, localName, attributes); |
| 295 |
parameters = new VCardParameters(); |
| 296 |
parent = propertyElement; |
| 297 |
typeToPush = ElementType.property; |
| 298 |
} |
| 299 |
break; |
| 300 |
|
| 301 |
case group: |
| 302 |
propertyElement = createElement(namespace, localName, attributes); |
| 303 |
parameters = new VCardParameters(); |
| 304 |
parent = propertyElement; |
| 305 |
typeToPush = ElementType.property; |
| 306 |
break; |
| 307 |
|
| 308 |
case property: |
| 309 |
|
| 310 |
if (PARAMETERS.equals(qname)) { |
| 311 |
typeToPush = ElementType.parameters; |
| 312 |
} |
| 313 |
break; |
| 314 |
|
| 315 |
case parameters: |
| 316 |
|
| 317 |
if (NS.equals(namespace)) { |
| 318 |
paramName = qname; |
| 319 |
typeToPush = ElementType.parameter; |
| 320 |
} |
| 321 |
break; |
| 322 |
|
| 323 |
case parameter: |
| 324 |
if (NS.equals(namespace)) { |
| 325 |
typeToPush = ElementType.parameterValue; |
| 326 |
} |
| 327 |
break; |
| 328 |
|
| 329 |
case parameterValue: |
| 330 |
|
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
if (propertyElement != null && typeToPush != ElementType.property && typeToPush != ElementType.parameters && !structure.isUnderParameters()) { |
| 337 |
if (textContent.length() > 0) { |
| 338 |
parent.appendChild(DOC.createTextNode(textContent)); |
| 339 |
} |
| 340 |
Element element = createElement(namespace, localName, attributes); |
| 341 |
parent.appendChild(element); |
| 342 |
parent = element; |
| 343 |
} |
| 344 |
|
| 345 |
structure.push(typeToPush); |
| 346 |
} |
| 347 |
|
| 348 |
@Override |
| 349 |
public void endElement(String namespace, String localName, String qName) throws SAXException { |
| 350 |
String textContent = characterBuffer.getAndClear(); |
| 351 |
|
| 352 |
if (structure.isEmpty()) { |
| 353 |
|
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
ElementType type = structure.pop(); |
| 358 |
if (type == null && (propertyElement == null || structure.isUnderParameters())) { |
| 359 |
|
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
if (type != null) { |
| 364 |
switch (type) { |
| 365 |
case parameterValue: |
| 366 |
parameters.put(paramName.getLocalPart(), textContent); |
| 367 |
break; |
| 368 |
|
| 369 |
case parameter: |
| 370 |
|
| 371 |
break; |
| 372 |
|
| 373 |
case parameters: |
| 374 |
|
| 375 |
break; |
| 376 |
|
| 377 |
case property: |
| 378 |
propertyElement.appendChild(DOC.createTextNode(textContent)); |
| 379 |
|
| 380 |
String propertyName = localName; |
| 381 |
VCardProperty property; |
| 382 |
QName propertyQName = new QName(propertyElement.getNamespaceURI(), propertyElement.getLocalName()); |
| 383 |
VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(propertyQName); |
| 384 |
|
| 385 |
context.getWarnings().clear(); |
| 386 |
context.setPropertyName(propertyName); |
| 387 |
try { |
| 388 |
property = scribe.parseXml(propertyElement, parameters, context); |
| 389 |
property.setGroup(group); |
| 390 |
readVCard.addProperty(property); |
| 391 |
warnings.addAll(context.getWarnings()); |
| 392 |
} catch (SkipMeException e) { |
| 393 |
|
| 394 |
warnings.add(new ParseWarning.Builder(context) |
| 395 |
.message(22, e.getMessage()) |
| 396 |
.build() |
| 397 |
); |
| 398 |
|
| 399 |
} catch (CannotParseException e) { |
| 400 |
|
| 401 |
warnings.add(new ParseWarning.Builder(context) |
| 402 |
.message(e) |
| 403 |
.build() |
| 404 |
); |
| 405 |
|
| 406 |
|
| 407 |
scribe = index.getPropertyScribe(Xml.class); |
| 408 |
property = scribe.parseXml(propertyElement, parameters, context); |
| 409 |
property.setGroup(group); |
| 410 |
readVCard.addProperty(property); |
| 411 |
} catch (EmbeddedVCardException e) { |
| 412 |
|
| 413 |
warnings.add(new ParseWarning.Builder(context) |
| 414 |
.message(34) |
| 415 |
.build() |
| 416 |
); |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
propertyElement = null; |
| 421 |
break; |
| 422 |
|
| 423 |
case group: |
| 424 |
group = null; |
| 425 |
break; |
| 426 |
|
| 427 |
case vcard: |
| 428 |
|
| 429 |
try { |
| 430 |
readerBlock.put(lock); |
| 431 |
threadBlock.take(); |
| 432 |
} catch (InterruptedException e) { |
| 433 |
throw new SAXException(e); |
| 434 |
} |
| 435 |
break; |
| 436 |
|
| 437 |
case vcards: |
| 438 |
|
| 439 |
break; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
if (propertyElement != null && type != ElementType.property && type != ElementType.parameters && !structure.isUnderParameters()) { |
| 445 |
if (textContent.length() > 0) { |
| 446 |
parent.appendChild(DOC.createTextNode(textContent)); |
| 447 |
} |
| 448 |
parent = (Element) parent.getParentNode(); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
private Element createElement(String namespace, String localName, Attributes attributes) { |
| 453 |
Element element = DOC.createElementNS(namespace, localName); |
| 454 |
applyAttributesTo(element, attributes); |
| 455 |
return element; |
| 456 |
} |
| 457 |
|
| 458 |
private void applyAttributesTo(Element element, Attributes attributes) { |
| 459 |
for (int i = 0; i < attributes.getLength(); i++) { |
| 460 |
String qname = attributes.getQName(i); |
| 461 |
if (qname.startsWith("xmlns:")) { |
| 462 |
continue; |
| 463 |
} |
| 464 |
|
| 465 |
String name = attributes.getLocalName(i); |
| 466 |
String value = attributes.getValue(i); |
| 467 |
element.setAttribute(name, value); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
private enum ElementType { |
| 473 |
|
| 474 |
vcards, vcard, group, property, parameters, parameter, parameterValue |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
private static class XCardStructure { |
| 491 |
private final List<ElementType> stack = new ArrayList<>(); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public ElementType pop() { |
| 498 |
return isEmpty() ? null : stack.remove(stack.size() - 1); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public ElementType peek() { |
| 506 |
return isEmpty() ? null : stack.get(stack.size() - 1); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public void push(ElementType type) { |
| 515 |
stack.add(type); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public boolean isUnderParameters() { |
| 523 |
|
| 524 |
ElementType nonNull = null; |
| 525 |
for (int i = stack.size() - 1; i >= 0; i--) { |
| 526 |
ElementType type = stack.get(i); |
| 527 |
if (type != null) { |
| 528 |
nonNull = type; |
| 529 |
break; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
return |
| 535 |
nonNull == ElementType.parameters || |
| 536 |
nonNull == ElementType.parameter || |
| 537 |
nonNull == ElementType.parameterValue; |
| 538 |
|
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public boolean isEmpty() { |
| 546 |
return stack.isEmpty(); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
private static class NoOpErrorListener implements ErrorListener { |
| 554 |
public void error(TransformerException e) { |
| 555 |
|
| 556 |
} |
| 557 |
|
| 558 |
public void fatalError(TransformerException e) { |
| 559 |
|
| 560 |
} |
| 561 |
|
| 562 |
public void warning(TransformerException e) { |
| 563 |
|
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
public void close() throws IOException { |
| 571 |
if (thread.isAlive()) { |
| 572 |
thread.closed = true; |
| 573 |
thread.interrupt(); |
| 574 |
} |
| 575 |
|
| 576 |
if (stream != null) { |
| 577 |
stream.close(); |
| 578 |
} |
| 579 |
} |
| 580 |
} |