| 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.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.OutputStream; |
| 11 |
import java.io.OutputStreamWriter; |
| 12 |
import java.io.Reader; |
| 13 |
import java.io.StringWriter; |
| 14 |
import java.io.UncheckedIOException; |
| 15 |
import java.io.Writer; |
| 16 |
import java.nio.charset.StandardCharsets; |
| 17 |
import java.nio.file.Files; |
| 18 |
import java.nio.file.Path; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Collections; |
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.List; |
| 23 |
import java.util.Map; |
| 24 |
|
| 25 |
import javax.xml.namespace.QName; |
| 26 |
import javax.xml.transform.Transformer; |
| 27 |
import javax.xml.transform.TransformerConfigurationException; |
| 28 |
import javax.xml.transform.TransformerException; |
| 29 |
import javax.xml.transform.TransformerFactory; |
| 30 |
import javax.xml.transform.TransformerFactoryConfigurationError; |
| 31 |
import javax.xml.transform.dom.DOMSource; |
| 32 |
import javax.xml.transform.stream.StreamResult; |
| 33 |
import javax.xml.xpath.XPath; |
| 34 |
import javax.xml.xpath.XPathConstants; |
| 35 |
import javax.xml.xpath.XPathExpressionException; |
| 36 |
import javax.xml.xpath.XPathFactory; |
| 37 |
|
| 38 |
import org.w3c.dom.Document; |
| 39 |
import org.w3c.dom.Element; |
| 40 |
import org.w3c.dom.Node; |
| 41 |
import org.xml.sax.SAXException; |
| 42 |
|
| 43 |
import ezvcard.VCard; |
| 44 |
import ezvcard.VCardDataType; |
| 45 |
import ezvcard.VCardVersion; |
| 46 |
import ezvcard.io.CannotParseException; |
| 47 |
import ezvcard.io.EmbeddedVCardException; |
| 48 |
import ezvcard.io.ParseWarning; |
| 49 |
import ezvcard.io.SkipMeException; |
| 50 |
import ezvcard.io.StreamReader; |
| 51 |
import ezvcard.io.StreamWriter; |
| 52 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 53 |
import ezvcard.parameter.VCardParameters; |
| 54 |
import ezvcard.property.VCardProperty; |
| 55 |
import ezvcard.property.Xml; |
| 56 |
import ezvcard.util.ListMultimap; |
| 57 |
import ezvcard.util.XmlUtils; |
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public class XCardDocument { |
| 139 |
private final VCardVersion version4 = VCardVersion.V4_0; |
| 140 |
private final Document document; |
| 141 |
private Element vcardsRootElement; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public XCardDocument() { |
| 147 |
document = XmlUtils.createDocument(); |
| 148 |
vcardsRootElement = document.createElementNS(VCARDS.getNamespaceURI(), VCARDS.getLocalPart()); |
| 149 |
document.appendChild(vcardsRootElement); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public XCardDocument(String xml) throws SAXException { |
| 158 |
this(XmlUtils.toDocument(xml)); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public XCardDocument(InputStream in) throws SAXException, IOException { |
| 168 |
this(XmlUtils.toDocument(in)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public XCardDocument(Path file) throws SAXException, IOException { |
| 178 |
this(XmlUtils.toDocument(file)); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public XCardDocument(Reader reader) throws SAXException, IOException { |
| 198 |
this(XmlUtils.toDocument(reader)); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public XCardDocument(Document document) { |
| 206 |
this.document = document; |
| 207 |
|
| 208 |
XCardNamespaceContext nsContext = new XCardNamespaceContext(version4, "v"); |
| 209 |
XPath xpath = XPathFactory.newInstance().newXPath(); |
| 210 |
xpath.setNamespaceContext(nsContext); |
| 211 |
|
| 212 |
try { |
| 213 |
|
| 214 |
vcardsRootElement = (Element) xpath.evaluate("//" + nsContext.getPrefix() + ":" + VCARDS.getLocalPart(), document, XPathConstants.NODE); |
| 215 |
} catch (XPathExpressionException e) { |
| 216 |
|
| 217 |
throw new RuntimeException(e); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public StreamReader reader() { |
| 227 |
return new XCardDocumentStreamReader(); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public XCardDocumentStreamWriter writer() { |
| 236 |
return new XCardDocumentStreamWriter(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public Document getDocument() { |
| 244 |
return document; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public List<VCard> getVCards() { |
| 253 |
try { |
| 254 |
return reader().readAll(); |
| 255 |
} catch (IOException e) { |
| 256 |
|
| 257 |
throw new UncheckedIOException(e); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public void addVCard(VCard vcard) { |
| 268 |
writer().write(vcard); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public String write() { |
| 276 |
return write((Integer) null); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public String write(Integer indent) { |
| 286 |
return write(indent, null); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public String write(Integer indent, String xmlVersion) { |
| 301 |
return write(new XCardOutputProperties(indent, xmlVersion)); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public String write(Map<String, String> outputProperties) { |
| 311 |
StringWriter sw = new StringWriter(); |
| 312 |
try { |
| 313 |
write(sw, outputProperties); |
| 314 |
} catch (TransformerException e) { |
| 315 |
|
| 316 |
throw new RuntimeException(e); |
| 317 |
} |
| 318 |
return sw.toString(); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public void write(OutputStream out) throws TransformerException { |
| 328 |
write(out, (Integer) null); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public void write(OutputStream out, Integer indent) throws TransformerException { |
| 340 |
write(out, indent, null); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public void write(OutputStream out, Integer indent, String xmlVersion) throws TransformerException { |
| 357 |
write(out, new XCardOutputProperties(indent, xmlVersion)); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public void write(OutputStream out, Map<String, String> outputProperties) throws TransformerException { |
| 369 |
write(new OutputStreamWriter(out, StandardCharsets.UTF_8), outputProperties); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
public void write(Path file) throws TransformerException, IOException { |
| 379 |
write(file, (Integer) null); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public void write(Path file, Integer indent) throws TransformerException, IOException { |
| 391 |
write(file, indent, null); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public void write(Path file, Integer indent, String xmlVersion) throws TransformerException, IOException { |
| 408 |
write(file, new XCardOutputProperties(indent, xmlVersion)); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public void write(Path file, Map<String, String> outputProperties) throws TransformerException, IOException { |
| 420 |
try (Writer writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { |
| 421 |
write(writer, outputProperties); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public void write(Writer writer) throws TransformerException { |
| 431 |
write(writer, (Integer) null); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
public void write(Writer writer, Integer indent) throws TransformerException { |
| 442 |
write(writer, indent, null); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public void write(Writer writer, Integer indent, String xmlVersion) throws TransformerException { |
| 458 |
write(writer, new XCardOutputProperties(indent, xmlVersion)); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
public void write(Writer writer, Map<String, String> outputProperties) throws TransformerException { |
| 469 |
Transformer transformer; |
| 470 |
try { |
| 471 |
transformer = TransformerFactory.newInstance().newTransformer(); |
| 472 |
} catch (TransformerConfigurationException e) { |
| 473 |
|
| 474 |
throw new RuntimeException(e); |
| 475 |
} catch (TransformerFactoryConfigurationError e) { |
| 476 |
|
| 477 |
throw new RuntimeException(e); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
for (Map.Entry<String, String> entry : outputProperties.entrySet()) { |
| 485 |
String key = entry.getKey(); |
| 486 |
String value = entry.getValue(); |
| 487 |
transformer.setOutputProperty(key, value); |
| 488 |
} |
| 489 |
|
| 490 |
DOMSource source = new DOMSource(document); |
| 491 |
StreamResult result = new StreamResult(writer); |
| 492 |
transformer.transform(source, result); |
| 493 |
} |
| 494 |
|
| 495 |
private class XCardDocumentStreamReader extends StreamReader { |
| 496 |
private final Iterator<Element> vcardElements; |
| 497 |
{ |
| 498 |
List<Element> list = (vcardsRootElement == null) ? Collections.<Element> emptyList() : getChildElements(vcardsRootElement, VCARD); |
| 499 |
vcardElements = list.iterator(); |
| 500 |
} |
| 501 |
|
| 502 |
private VCard vcard; |
| 503 |
|
| 504 |
@Override |
| 505 |
public VCard readNext() { |
| 506 |
try { |
| 507 |
return super.readNext(); |
| 508 |
} catch (IOException e) { |
| 509 |
|
| 510 |
throw new UncheckedIOException(e); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
@Override |
| 515 |
protected VCard _readNext() throws IOException { |
| 516 |
if (!vcardElements.hasNext()) { |
| 517 |
return null; |
| 518 |
} |
| 519 |
|
| 520 |
vcard = new VCard(); |
| 521 |
vcard.setVersion(version4); |
| 522 |
context.setVersion(version4); |
| 523 |
parseVCardElement(vcardElements.next()); |
| 524 |
return vcard; |
| 525 |
} |
| 526 |
|
| 527 |
public void close() { |
| 528 |
|
| 529 |
} |
| 530 |
|
| 531 |
private void parseVCardElement(Element vcardElement) { |
| 532 |
List<Element> children = XmlUtils.toElementList(vcardElement.getChildNodes()); |
| 533 |
for (Element child : children) { |
| 534 |
if (XmlUtils.hasQName(child, GROUP)) { |
| 535 |
String group = child.getAttribute("name"); |
| 536 |
if (group.isEmpty()) { |
| 537 |
group = null; |
| 538 |
} |
| 539 |
List<Element> grandChildren = XmlUtils.toElementList(child.getChildNodes()); |
| 540 |
for (Element grandChild : grandChildren) { |
| 541 |
parseAndAddElement(grandChild, group); |
| 542 |
} |
| 543 |
continue; |
| 544 |
} |
| 545 |
|
| 546 |
parseAndAddElement(child, null); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
private void parseAndAddElement(Element element, String group) { |
| 558 |
VCardParameters parameters = parseParameters(element); |
| 559 |
|
| 560 |
VCardProperty property; |
| 561 |
String propertyName = element.getLocalName(); |
| 562 |
String ns = element.getNamespaceURI(); |
| 563 |
QName qname = new QName(ns, propertyName); |
| 564 |
VCardPropertyScribe<? extends VCardProperty> scribe = index.getPropertyScribe(qname); |
| 565 |
|
| 566 |
context.getWarnings().clear(); |
| 567 |
context.setPropertyName(propertyName); |
| 568 |
try { |
| 569 |
property = scribe.parseXml(element, parameters, context); |
| 570 |
property.setGroup(group); |
| 571 |
warnings.addAll(context.getWarnings()); |
| 572 |
} catch (SkipMeException e) { |
| 573 |
|
| 574 |
warnings.add(new ParseWarning.Builder(context) |
| 575 |
.message(22, e.getMessage()) |
| 576 |
.build() |
| 577 |
); |
| 578 |
|
| 579 |
return; |
| 580 |
} catch (CannotParseException e) { |
| 581 |
|
| 582 |
warnings.add(new ParseWarning.Builder(context) |
| 583 |
.message(e) |
| 584 |
.build() |
| 585 |
); |
| 586 |
|
| 587 |
|
| 588 |
scribe = index.getPropertyScribe(Xml.class); |
| 589 |
property = scribe.parseXml(element, parameters, context); |
| 590 |
property.setGroup(group); |
| 591 |
} catch (EmbeddedVCardException e) { |
| 592 |
|
| 593 |
warnings.add(new ParseWarning.Builder(context) |
| 594 |
.message(34) |
| 595 |
.build() |
| 596 |
); |
| 597 |
|
| 598 |
return; |
| 599 |
} |
| 600 |
|
| 601 |
vcard.addProperty(property); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
private VCardParameters parseParameters(Element element) { |
| 610 |
VCardParameters parameters = new VCardParameters(); |
| 611 |
|
| 612 |
List<Element> roots = XmlUtils.toElementList(element.getElementsByTagNameNS(PARAMETERS.getNamespaceURI(), PARAMETERS.getLocalPart())); |
| 613 |
for (Element root : roots) { |
| 614 |
List<Element> parameterElements = XmlUtils.toElementList(root.getChildNodes()); |
| 615 |
for (Element parameterElement : parameterElements) { |
| 616 |
String name = parameterElement.getLocalName().toUpperCase(); |
| 617 |
List<Element> valueElements = XmlUtils.toElementList(parameterElement.getChildNodes()); |
| 618 |
for (Element valueElement : valueElements) { |
| 619 |
String value = valueElement.getTextContent(); |
| 620 |
parameters.put(name, value); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
return parameters; |
| 626 |
} |
| 627 |
|
| 628 |
private List<Element> getChildElements(Element parent, QName qname) { |
| 629 |
List<Element> elements = new ArrayList<>(); |
| 630 |
for (Element child : XmlUtils.toElementList(parent.getChildNodes())) { |
| 631 |
if (XmlUtils.hasQName(child, qname)) { |
| 632 |
elements.add(child); |
| 633 |
} |
| 634 |
} |
| 635 |
return elements; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
public class XCardDocumentStreamWriter extends XCardWriterBase { |
| 640 |
@Override |
| 641 |
public void write(VCard vcard) { |
| 642 |
try { |
| 643 |
super.write(vcard); |
| 644 |
} catch (IOException ignore) { |
| 645 |
|
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
@Override |
| 650 |
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException { |
| 651 |
|
| 652 |
ListMultimap<String, VCardProperty> propertiesByGroup = new ListMultimap<>(); |
| 653 |
for (VCardProperty property : properties) { |
| 654 |
propertiesByGroup.put(property.getGroup(), property); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
Element vcardElement = createElement(VCARD); |
| 659 |
for (Map.Entry<String, List<VCardProperty>> entry : propertiesByGroup) { |
| 660 |
String groupName = entry.getKey(); |
| 661 |
Element parent; |
| 662 |
if (groupName != null) { |
| 663 |
Element groupElement = createElement(GROUP); |
| 664 |
groupElement.setAttribute("name", groupName); |
| 665 |
vcardElement.appendChild(groupElement); |
| 666 |
parent = groupElement; |
| 667 |
} else { |
| 668 |
parent = vcardElement; |
| 669 |
} |
| 670 |
|
| 671 |
for (VCardProperty property : entry.getValue()) { |
| 672 |
try { |
| 673 |
Element propertyElement = marshalProperty(property, vcard); |
| 674 |
parent.appendChild(propertyElement); |
| 675 |
} catch (SkipMeException e) { |
| 676 |
|
| 677 |
} catch (EmbeddedVCardException e) { |
| 678 |
|
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
if (vcardsRootElement == null) { |
| 684 |
vcardsRootElement = createElement(VCARDS); |
| 685 |
Element documentRoot = document.getDocumentElement(); |
| 686 |
if (documentRoot == null) { |
| 687 |
document.appendChild(vcardsRootElement); |
| 688 |
} else { |
| 689 |
documentRoot.appendChild(vcardsRootElement); |
| 690 |
} |
| 691 |
} |
| 692 |
vcardsRootElement.appendChild(vcardElement); |
| 693 |
} |
| 694 |
|
| 695 |
public void close() { |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 706 |
private Element marshalProperty(VCardProperty property, VCard vcard) { |
| 707 |
VCardPropertyScribe scribe = index.getPropertyScribe(property); |
| 708 |
|
| 709 |
Element propertyElement; |
| 710 |
if (property instanceof Xml) { |
| 711 |
Xml xml = (Xml) property; |
| 712 |
Document propertyDocument = xml.getValue(); |
| 713 |
if (propertyDocument == null) { |
| 714 |
throw new SkipMeException(); |
| 715 |
} |
| 716 |
propertyElement = propertyDocument.getDocumentElement(); |
| 717 |
propertyElement = (Element) document.importNode(propertyElement, true); |
| 718 |
} else { |
| 719 |
QName qname = scribe.getQName(); |
| 720 |
propertyElement = createElement(qname); |
| 721 |
scribe.writeXml(property, propertyElement); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard); |
| 726 |
removeUnsupportedParameters(parameters); |
| 727 |
if (!parameters.isEmpty()) { |
| 728 |
Element parametersElement = marshalParameters(parameters); |
| 729 |
Node firstChild = propertyElement.getFirstChild(); |
| 730 |
propertyElement.insertBefore(parametersElement, firstChild); |
| 731 |
} |
| 732 |
|
| 733 |
return propertyElement; |
| 734 |
} |
| 735 |
|
| 736 |
private Element marshalParameters(VCardParameters parameters) { |
| 737 |
Element parametersElement = createElement(PARAMETERS); |
| 738 |
|
| 739 |
for (Map.Entry<String, List<String>> parameter : parameters) { |
| 740 |
String parameterName = parameter.getKey().toLowerCase(); |
| 741 |
Element parameterElement = createElement(parameterName); |
| 742 |
|
| 743 |
for (String parameterValue : parameter.getValue()) { |
| 744 |
VCardDataType dataType = parameterDataTypes.get(parameterName); |
| 745 |
String dataTypeElementName = (dataType == null) ? "unknown" : dataType.getName().toLowerCase(); |
| 746 |
Element dataTypeElement = createElement(dataTypeElementName); |
| 747 |
dataTypeElement.setTextContent(parameterValue); |
| 748 |
parameterElement.appendChild(dataTypeElement); |
| 749 |
} |
| 750 |
|
| 751 |
parametersElement.appendChild(parameterElement); |
| 752 |
} |
| 753 |
|
| 754 |
return parametersElement; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
private Element createElement(String name) { |
| 763 |
return createElement(name, targetVersion.getXmlNamespace()); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
private Element createElement(String name, String ns) { |
| 773 |
return document.createElementNS(ns, name); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
private Element createElement(QName qname) { |
| 782 |
return createElement(qname.getLocalPart(), qname.getNamespaceURI()); |
| 783 |
} |
| 784 |
} |
| 785 |
} |