Debian Codegraph @

src/main/java/ezvcard/property/Xml.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.xml.sax.SAXException;
11
12 import ezvcard.SupportedVersions;
13 import ezvcard.VCard;
14 import ezvcard.VCardVersion;
15 import ezvcard.ValidationWarning;
16 import ezvcard.util.XmlUtils;
17
18 /*
19 Copyright (c) 2012-2023, Michael Angstadt
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24
25 1. Redistributions of source code must retain the above copyright notice, this
26 list of conditions and the following disclaimer.
27 2. Redistributions in binary form must reproduce the above copyright notice,
28 this list of conditions and the following disclaimer in the documentation
29 and/or other materials provided with the distribution.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
35 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 The views and conclusions contained in the software and documentation are those
43 of the authors and should not be interpreted as representing official policies,
44 either expressed or implied, of the FreeBSD Project.
45 */
46
47 /**
48 * <p>
49 * Contains an XML element that was not recognized when parsing an xCard
50 * (XML-formatted vCard).
51 * </p>
52 *
53 * <p>
54 * <b>Code sample</b>
55 * </p>
56 *
57 * <pre class="brush:java">
58 * VCard vcard = new VCard();
59 *
60 * Xml xml = new Xml("&lt;b&gt;Some xml&lt;/b&gt;");
61 * vcard.addXml(xml);
62 * </pre>
63 *
64 * <p>
65 * <b>Property name:</b> {@code XML}
66 * </p>
67 * <p>
68 * <b>Supported versions:</b> {@code 4.0}
69 * </p>
70 * @author Michael Angstadt
71 * @see <a href="http://tools.ietf.org/html/rfc6350#page-27">RFC 6350 p.27</a>
72 */
73 /*
74 * Note: This class does not extend SimpleProperty because of issues
75 * implementing "equals". SimpleProperty's "equals" method calls the "equals"
76 * method on the "value" field. However, equals method for the "Document" class
77 * does not check for true equality.
78 */
79 @SupportedVersions(VCardVersion.V4_0)
80 public class Xml extends VCardProperty implements HasAltId {
81 private Document value;
82
83 /**
84 * Creates an XML property.
85 * @param xml the XML to use as the property's value
86 * @throws SAXException if the XML cannot be parsed
87 */
88 public Xml(String xml) throws SAXException {
89 this((xml == null) ? null : XmlUtils.toDocument(xml));
90 }
91
92 /**
93 * Creates an XML property.
94 * @param element the XML element to use as the property's value (the
95 * element is imported into an empty {@link Document} object)
96 */
97 public Xml(Element element) {
98 this((element == null) ? null : detachElement(element));
99 }
100
101 private static Document detachElement(Element element) {
102 Document document = XmlUtils.createDocument();
103 Node imported = document.importNode(element, true);
104 document.appendChild(imported);
105 return document;
106 }
107
108 /**
109 * Creates an XML property.
110 * @param document the XML document to use as the property's value
111 */
112 public Xml(Document document) {
113 this.value = document;
114 }
115
116 /**
117 * Copy constructor.
118 * @param original the property to make a copy of
119 */
120 public Xml(Xml original) {
121 super(original);
122 if (original.value != null) {
123 Element root = original.value.getDocumentElement();
124 value = (root == null) ? XmlUtils.createDocument() : detachElement(root);
125 }
126 }
127
128 /**
129 * Gets the value of this property.
130 * @return the value or null if not set
131 */
132 public Document getValue() {
133 return value;
134 }
135
136 /**
137 * Sets the value of this property.
138 * @param value the value
139 */
140 public void setValue(Document value) {
141 this.value = value;
142 }
143
144 //@Override
145 public String getAltId() {
146 return parameters.getAltId();
147 }
148
149 //@Override
150 public void setAltId(String altId) {
151 parameters.setAltId(altId);
152 }
153
154 @Override
155 protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
156 if (value == null) {
157 warnings.add(new ValidationWarning(8));
158 }
159 }
160
161 @Override
162 protected Map<String, Object> toStringValues() {
163 Map<String, Object> values = new LinkedHashMap<>();
164 values.put("value", (value == null) ? "null" : XmlUtils.toString(value));
165 return values;
166 }
167
168 @Override
169 public Xml copy() {
170 return new Xml(this);
171 }
172
173 @Override
174 public int hashCode() {
175 final int prime = 31;
176 int result = super.hashCode();
177 result = prime * result + ((value == null) ? 0 : XmlUtils.toString(value).hashCode());
178 return result;
179 }
180
181 @Override
182 public boolean equals(Object obj) {
183 if (this == obj) return true;
184 if (!super.equals(obj)) return false;
185 Xml other = (Xml) obj;
186 if (value == null) {
187 if (other.value != null) return false;
188 } else {
189 if (other.value == null) return false;
190 if (!XmlUtils.toString(value).equals(XmlUtils.toString(other.value))) return false;
191 }
192 return true;
193 }
194 }