001 package ezvcard.property;
002
003 import java.util.EnumSet;
004 import java.util.Set;
005
006 import org.w3c.dom.Document;
007 import org.w3c.dom.Element;
008 import org.w3c.dom.Node;
009 import org.xml.sax.SAXException;
010
011 import ezvcard.VCardVersion;
012 import ezvcard.util.XmlUtils;
013
014 /*
015 Copyright (c) 2013, Michael Angstadt
016 All rights reserved.
017
018 Redistribution and use in source and binary forms, with or without
019 modification, are permitted provided that the following conditions are met:
020
021 1. Redistributions of source code must retain the above copyright notice, this
022 list of conditions and the following disclaimer.
023 2. Redistributions in binary form must reproduce the above copyright notice,
024 this list of conditions and the following disclaimer in the documentation
025 and/or other materials provided with the distribution.
026
027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
028 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
029 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
030 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
032 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
033 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
034 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
035 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037
038 The views and conclusions contained in the software and documentation are those
039 of the authors and should not be interpreted as representing official policies,
040 either expressed or implied, of the FreeBSD Project.
041 */
042
043 /**
044 * Any XML data attached to the vCard. This is used if the vCard was encoded in
045 * XML (xCard standard) and it contained some non-standard elements.
046 *
047 * <p>
048 * <b>Code sample</b>
049 * </p>
050 *
051 * <pre class="brush:java">
052 * VCard vcard = new VCard();
053 * Xml xml = new Xml("<b>Some xml</b>");
054 * vcard.addXml(xml);
055 * </pre>
056 *
057 * <p>
058 * <b>Property name:</b> {@code XML}
059 * </p>
060 * <p>
061 * <b>Supported versions:</b> {@code 4.0}
062 * </p>
063 * @author Michael Angstadt
064 */
065 public class Xml extends SimpleProperty<Document> implements HasAltId {
066 /**
067 * Creates an XML property.
068 * @param xml the XML to use as the property's value
069 * @throws SAXException if the XML cannot be parsed
070 */
071 public Xml(String xml) throws SAXException {
072 this(XmlUtils.toDocument(xml));
073 }
074
075 /**
076 * Creates an XML property.
077 * @param element the XML element to use as the property's value (the
078 * element is imported into an empty {@link Document} object)
079 */
080 public Xml(Element element) {
081 this(detachElement(element));
082 }
083
084 /**
085 * Creates an XML property.
086 * @param document the XML document to use as the property's value
087 */
088 public Xml(Document document) {
089 super(document);
090 }
091
092 @Override
093 public Set<VCardVersion> _supportedVersions() {
094 return EnumSet.of(VCardVersion.V4_0);
095 }
096
097 //@Override
098 public String getAltId() {
099 return parameters.getAltId();
100 }
101
102 //@Override
103 public void setAltId(String altId) {
104 parameters.setAltId(altId);
105 }
106
107 private static Document detachElement(Element element) {
108 Document document = XmlUtils.createDocument();
109 Node imported = document.importNode(element, true);
110 document.appendChild(imported);
111 return document;
112 }
113 }