Debian Codegraph @

src/main/java/ezvcard/io/scribe/XmlScribe.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.xml.transform.OutputKeys;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.xml.sax.SAXException;
11
12 import com.github.mangstadt.vinnie.io.VObjectPropertyValues;
13
14 import ezvcard.VCardDataType;
15 import ezvcard.VCardVersion;
16 import ezvcard.io.CannotParseException;
17 import ezvcard.io.ParseContext;
18 import ezvcard.io.json.JCardValue;
19 import ezvcard.io.text.WriteContext;
20 import ezvcard.io.xml.XCardElement;
21 import ezvcard.parameter.VCardParameters;
22 import ezvcard.property.Xml;
23 import ezvcard.util.XmlUtils;
24
25 /*
26 Copyright (c) 2012-2023, Michael Angstadt
27 All rights reserved.
28
29 Redistribution and use in source and binary forms, with or without
30 modification, are permitted provided that the following conditions are met:
31
32 1. Redistributions of source code must retain the above copyright notice, this
33 list of conditions and the following disclaimer.
34 2. Redistributions in binary form must reproduce the above copyright notice,
35 this list of conditions and the following disclaimer in the documentation
36 and/or other materials provided with the distribution.
37
38 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
39 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
42 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 */
49
50 /**
51 * Marshals {@link Xml} properties.
52 * @author Michael Angstadt
53 */
54 public class XmlScribe extends VCardPropertyScribe<Xml> {
55 public XmlScribe() {
56 super(Xml.class, "XML");
57 }
58
59 @Override
60 protected VCardDataType _defaultDataType(VCardVersion version) {
61 return VCardDataType.TEXT;
62 }
63
64 @Override
65 protected String _writeText(Xml property, WriteContext context) {
66 Document value = property.getValue();
67 if (value == null) {
68 return "";
69 }
70
71 String xml = valueToString(value);
72 return escape(xml, context);
73 }
74
75 @Override
76 protected Xml _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
77 value = VObjectPropertyValues.unescape(value);
78 try {
79 return new Xml(value);
80 } catch (SAXException e) {
81 throw new CannotParseException(21);
82 }
83 }
84
85 @Override
86 protected void _writeXml(Xml property, XCardElement element) {
87 //Xml properties are handled as a special case when writing xCard documents, so this method should never get called (see: "XCardDocument" class)
88 super._writeXml(property, element);
89 }
90
91 @Override
92 protected Xml _parseXml(XCardElement element, VCardParameters parameters, ParseContext context) {
93 Xml xml = new Xml(element.element());
94
95 //remove the <parameters> element
96 Element root = xml.getValue().getDocumentElement();
97 for (Element child : XmlUtils.toElementList(root.getChildNodes())) {
98 if ("parameters".equals(child.getLocalName()) && VCardVersion.V4_0.getXmlNamespace().equals(child.getNamespaceURI())) {
99 root.removeChild(child);
100 }
101 }
102
103 return xml;
104 }
105
106 @Override
107 protected JCardValue _writeJson(Xml property) {
108 String xml = null;
109 Document value = property.getValue();
110 if (value != null) {
111 xml = valueToString(value);
112 }
113
114 return JCardValue.single(xml);
115 }
116
117 @Override
118 protected Xml _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
119 try {
120 String xml = value.asSingle();
121 return xml.isEmpty() ? new Xml((Document) null) : new Xml(xml);
122 } catch (SAXException e) {
123 throw new CannotParseException(22);
124 }
125 }
126
127 private String valueToString(Document document) {
128 Map<String, String> props = new HashMap<>();
129 props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
130 return XmlUtils.toString(document, props);
131 }
132 }