| 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.OutputStream; |
| 10 |
import java.io.OutputStreamWriter; |
| 11 |
import java.io.Writer; |
| 12 |
import java.nio.charset.StandardCharsets; |
| 13 |
import java.nio.file.Files; |
| 14 |
import java.nio.file.Path; |
| 15 |
import java.util.Collections; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Map; |
| 18 |
|
| 19 |
import javax.xml.namespace.QName; |
| 20 |
import javax.xml.transform.Result; |
| 21 |
import javax.xml.transform.Transformer; |
| 22 |
import javax.xml.transform.TransformerConfigurationException; |
| 23 |
import javax.xml.transform.TransformerFactory; |
| 24 |
import javax.xml.transform.dom.DOMResult; |
| 25 |
import javax.xml.transform.sax.SAXTransformerFactory; |
| 26 |
import javax.xml.transform.sax.TransformerHandler; |
| 27 |
import javax.xml.transform.stream.StreamResult; |
| 28 |
|
| 29 |
import org.w3c.dom.Document; |
| 30 |
import org.w3c.dom.Element; |
| 31 |
import org.w3c.dom.NamedNodeMap; |
| 32 |
import org.w3c.dom.Node; |
| 33 |
import org.w3c.dom.NodeList; |
| 34 |
import org.w3c.dom.Text; |
| 35 |
import org.xml.sax.Attributes; |
| 36 |
import org.xml.sax.SAXException; |
| 37 |
import org.xml.sax.helpers.AttributesImpl; |
| 38 |
|
| 39 |
import ezvcard.VCard; |
| 40 |
import ezvcard.VCardDataType; |
| 41 |
import ezvcard.io.EmbeddedVCardException; |
| 42 |
import ezvcard.io.SkipMeException; |
| 43 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 44 |
import ezvcard.parameter.VCardParameters; |
| 45 |
import ezvcard.property.VCardProperty; |
| 46 |
import ezvcard.property.Xml; |
| 47 |
import ezvcard.util.ListMultimap; |
| 48 |
import ezvcard.util.XmlUtils; |
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public class XCardWriter extends XCardWriterBase { |
| 100 |
|
| 101 |
|
| 102 |
private final Document DOC = XmlUtils.createDocument(); |
| 103 |
|
| 104 |
private final Writer writer; |
| 105 |
private final TransformerHandler handler; |
| 106 |
private final boolean vcardsElementExists; |
| 107 |
private boolean started = false; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public XCardWriter(OutputStream out) { |
| 113 |
this(out, (Integer) null); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public XCardWriter(OutputStream out, Integer indent) { |
| 122 |
this(out, indent, null); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public XCardWriter(OutputStream out, Integer indent, String xmlVersion) { |
| 136 |
this(out, new XCardOutputProperties(indent, xmlVersion)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public XCardWriter(OutputStream out, Map<String, String> outputProperties) { |
| 145 |
this(new OutputStreamWriter(out, StandardCharsets.UTF_8), outputProperties); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public XCardWriter(Path file) throws IOException { |
| 153 |
this(file, (Integer) null); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public XCardWriter(Path file, Integer indent) throws IOException { |
| 163 |
this(file, indent, null); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public XCardWriter(Path file, Integer indent, String xmlVersion) throws IOException { |
| 178 |
this(file, new XCardOutputProperties(indent, xmlVersion)); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public XCardWriter(Path file, Map<String, String> outputProperties) throws IOException { |
| 188 |
this(Files.newBufferedWriter(file, StandardCharsets.UTF_8), outputProperties); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public XCardWriter(Writer writer) { |
| 195 |
this(writer, (Integer) null); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public XCardWriter(Writer writer, Integer indent) { |
| 204 |
this(writer, indent, null); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public XCardWriter(Writer writer, Integer indent, String xmlVersion) { |
| 218 |
this(writer, new XCardOutputProperties(indent, xmlVersion)); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public XCardWriter(Writer writer, Map<String, String> outputProperties) { |
| 227 |
this(writer, null, outputProperties); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public XCardWriter(Node parent) { |
| 234 |
this(null, parent, Collections.<String, String> emptyMap()); |
| 235 |
} |
| 236 |
|
| 237 |
private XCardWriter(Writer writer, Node parent, Map<String, String> outputProperties) { |
| 238 |
this.writer = writer; |
| 239 |
|
| 240 |
if (parent instanceof Document) { |
| 241 |
Node root = ((Document) parent).getDocumentElement(); |
| 242 |
if (root != null) { |
| 243 |
parent = root; |
| 244 |
} |
| 245 |
} |
| 246 |
this.vcardsElementExists = isVCardsElement(parent); |
| 247 |
|
| 248 |
try { |
| 249 |
SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance(); |
| 250 |
handler = factory.newTransformerHandler(); |
| 251 |
} catch (TransformerConfigurationException e) { |
| 252 |
throw new RuntimeException(e); |
| 253 |
} |
| 254 |
|
| 255 |
Transformer transformer = handler.getTransformer(); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
for (Map.Entry<String, String> entry : outputProperties.entrySet()) { |
| 262 |
String key = entry.getKey(); |
| 263 |
String value = entry.getValue(); |
| 264 |
transformer.setOutputProperty(key, value); |
| 265 |
} |
| 266 |
|
| 267 |
Result result = (writer == null) ? new DOMResult(parent) : new StreamResult(writer); |
| 268 |
handler.setResult(result); |
| 269 |
} |
| 270 |
|
| 271 |
private boolean isVCardsElement(Node node) { |
| 272 |
if (node == null) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
if (!(node instanceof Element)) { |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
return XmlUtils.hasQName(node, VCARDS); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException { |
| 285 |
try { |
| 286 |
if (!started) { |
| 287 |
handler.startDocument(); |
| 288 |
|
| 289 |
if (!vcardsElementExists) { |
| 290 |
|
| 291 |
start(VCARDS); |
| 292 |
} |
| 293 |
|
| 294 |
started = true; |
| 295 |
} |
| 296 |
|
| 297 |
ListMultimap<String, VCardProperty> propertiesByGroup = new ListMultimap<>(); |
| 298 |
for (VCardProperty property : properties) { |
| 299 |
propertiesByGroup.put(property.getGroup(), property); |
| 300 |
} |
| 301 |
|
| 302 |
start(VCARD); |
| 303 |
|
| 304 |
for (Map.Entry<String, List<VCardProperty>> entry : propertiesByGroup) { |
| 305 |
String groupName = entry.getKey(); |
| 306 |
if (groupName != null) { |
| 307 |
AttributesImpl attr = new AttributesImpl(); |
| 308 |
attr.addAttribute(XCardQNames.NAMESPACE, "", "name", "", groupName); |
| 309 |
|
| 310 |
start(GROUP, attr); |
| 311 |
} |
| 312 |
|
| 313 |
for (VCardProperty property : entry.getValue()) { |
| 314 |
write(property, vcard); |
| 315 |
} |
| 316 |
|
| 317 |
if (groupName != null) { |
| 318 |
end(GROUP); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
end(VCARD); |
| 323 |
} catch (SAXException e) { |
| 324 |
throw new IOException(e); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public void close() throws IOException { |
| 332 |
try { |
| 333 |
if (!started) { |
| 334 |
handler.startDocument(); |
| 335 |
|
| 336 |
if (!vcardsElementExists) { |
| 337 |
|
| 338 |
start(VCARDS); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if (!vcardsElementExists) { |
| 343 |
end(VCARDS); |
| 344 |
} |
| 345 |
handler.endDocument(); |
| 346 |
} catch (SAXException e) { |
| 347 |
throw new IOException(e); |
| 348 |
} |
| 349 |
|
| 350 |
if (writer != null) { |
| 351 |
writer.close(); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 356 |
private void write(VCardProperty property, VCard vcard) throws SAXException { |
| 357 |
VCardPropertyScribe scribe = index.getPropertyScribe(property); |
| 358 |
VCardParameters parameters = scribe.prepareParameters(property, targetVersion, vcard); |
| 359 |
|
| 360 |
removeUnsupportedParameters(parameters); |
| 361 |
|
| 362 |
|
| 363 |
Element propertyElement; |
| 364 |
if (property instanceof Xml) { |
| 365 |
Xml xml = (Xml) property; |
| 366 |
Document value = xml.getValue(); |
| 367 |
if (value == null) { |
| 368 |
return; |
| 369 |
} |
| 370 |
propertyElement = value.getDocumentElement(); |
| 371 |
} else { |
| 372 |
QName qname = scribe.getQName(); |
| 373 |
propertyElement = DOC.createElementNS(qname.getNamespaceURI(), qname.getLocalPart()); |
| 374 |
try { |
| 375 |
scribe.writeXml(property, propertyElement); |
| 376 |
} catch (SkipMeException e) { |
| 377 |
return; |
| 378 |
} catch (EmbeddedVCardException e) { |
| 379 |
return; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
start(propertyElement); |
| 384 |
|
| 385 |
write(parameters); |
| 386 |
write(propertyElement); |
| 387 |
|
| 388 |
end(propertyElement); |
| 389 |
} |
| 390 |
|
| 391 |
private void write(Element propertyElement) throws SAXException { |
| 392 |
NodeList children = propertyElement.getChildNodes(); |
| 393 |
for (int i = 0; i < children.getLength(); i++) { |
| 394 |
Node child = children.item(i); |
| 395 |
|
| 396 |
if (child instanceof Element) { |
| 397 |
Element element = (Element) child; |
| 398 |
|
| 399 |
if (element.hasChildNodes()) { |
| 400 |
start(element); |
| 401 |
write(element); |
| 402 |
end(element); |
| 403 |
} else { |
| 404 |
childless(element); |
| 405 |
} |
| 406 |
|
| 407 |
continue; |
| 408 |
} |
| 409 |
|
| 410 |
if (child instanceof Text) { |
| 411 |
Text text = (Text) child; |
| 412 |
text(text.getTextContent()); |
| 413 |
continue; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
private void write(VCardParameters parameters) throws SAXException { |
| 419 |
if (parameters.isEmpty()) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
start(PARAMETERS); |
| 424 |
|
| 425 |
for (Map.Entry<String, List<String>> parameter : parameters) { |
| 426 |
String parameterName = parameter.getKey().toLowerCase(); |
| 427 |
start(parameterName); |
| 428 |
|
| 429 |
for (String parameterValue : parameter.getValue()) { |
| 430 |
VCardDataType dataType = parameterDataTypes.get(parameterName); |
| 431 |
String dataTypeElementName = (dataType == null) ? "unknown" : dataType.getName().toLowerCase(); |
| 432 |
|
| 433 |
start(dataTypeElementName); |
| 434 |
text(parameterValue); |
| 435 |
end(dataTypeElementName); |
| 436 |
} |
| 437 |
|
| 438 |
end(parameterName); |
| 439 |
} |
| 440 |
|
| 441 |
end(PARAMETERS); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
private void childless(Element element) throws SAXException { |
| 451 |
Attributes attributes = getElementAttributes(element); |
| 452 |
handler.startElement(element.getNamespaceURI(), "", element.getLocalName(), attributes); |
| 453 |
handler.endElement(element.getNamespaceURI(), "", element.getLocalName()); |
| 454 |
} |
| 455 |
|
| 456 |
private void start(Element element) throws SAXException { |
| 457 |
Attributes attributes = getElementAttributes(element); |
| 458 |
start(element.getNamespaceURI(), element.getLocalName(), attributes); |
| 459 |
} |
| 460 |
|
| 461 |
private void start(String element) throws SAXException { |
| 462 |
start(element, new AttributesImpl()); |
| 463 |
} |
| 464 |
|
| 465 |
private void start(QName qname) throws SAXException { |
| 466 |
start(qname, new AttributesImpl()); |
| 467 |
} |
| 468 |
|
| 469 |
private void start(QName qname, Attributes attributes) throws SAXException { |
| 470 |
start(qname.getNamespaceURI(), qname.getLocalPart(), attributes); |
| 471 |
} |
| 472 |
|
| 473 |
private void start(String element, Attributes attributes) throws SAXException { |
| 474 |
start(targetVersion.getXmlNamespace(), element, attributes); |
| 475 |
} |
| 476 |
|
| 477 |
private void start(String namespace, String element, Attributes attributes) throws SAXException { |
| 478 |
handler.startElement(namespace, "", element, attributes); |
| 479 |
} |
| 480 |
|
| 481 |
private void end(Element element) throws SAXException { |
| 482 |
end(element.getNamespaceURI(), element.getLocalName()); |
| 483 |
} |
| 484 |
|
| 485 |
private void end(String element) throws SAXException { |
| 486 |
end(targetVersion.getXmlNamespace(), element); |
| 487 |
} |
| 488 |
|
| 489 |
private void end(QName qname) throws SAXException { |
| 490 |
end(qname.getNamespaceURI(), qname.getLocalPart()); |
| 491 |
} |
| 492 |
|
| 493 |
private void end(String namespace, String element) throws SAXException { |
| 494 |
handler.endElement(namespace, "", element); |
| 495 |
} |
| 496 |
|
| 497 |
private void text(String text) throws SAXException { |
| 498 |
handler.characters(text.toCharArray(), 0, text.length()); |
| 499 |
} |
| 500 |
|
| 501 |
private Attributes getElementAttributes(Element element) { |
| 502 |
AttributesImpl attributes = new AttributesImpl(); |
| 503 |
NamedNodeMap attributeNodes = element.getAttributes(); |
| 504 |
for (int i = 0; i < attributeNodes.getLength(); i++) { |
| 505 |
Node node = attributeNodes.item(i); |
| 506 |
|
| 507 |
String localName = node.getLocalName(); |
| 508 |
if ("xmlns".equals(localName)) { |
| 509 |
continue; |
| 510 |
} |
| 511 |
|
| 512 |
attributes.addAttribute(node.getNamespaceURI(), "", localName, "", node.getNodeValue()); |
| 513 |
} |
| 514 |
return attributes; |
| 515 |
} |
| 516 |
} |