001 package ezvcard.types;
002
003 import java.util.HashMap;
004 import java.util.List;
005 import java.util.Map;
006
007 import javax.xml.transform.OutputKeys;
008
009 import org.w3c.dom.Document;
010 import org.w3c.dom.Element;
011 import org.w3c.dom.Node;
012 import org.xml.sax.SAXException;
013
014 import ezvcard.VCardSubTypes;
015 import ezvcard.VCardVersion;
016 import ezvcard.io.CompatibilityMode;
017 import ezvcard.io.SkipMeException;
018 import ezvcard.util.XCardElement;
019 import ezvcard.util.XmlUtils;
020
021 /*
022 Copyright (c) 2012, Michael Angstadt
023 All rights reserved.
024
025 Redistribution and use in source and binary forms, with or without
026 modification, are permitted provided that the following conditions are met:
027
028 1. Redistributions of source code must retain the above copyright notice, this
029 list of conditions and the following disclaimer.
030 2. Redistributions in binary form must reproduce the above copyright notice,
031 this list of conditions and the following disclaimer in the documentation
032 and/or other materials provided with the distribution.
033
034 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
035 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
036 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
038 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
039 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
040 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
043 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044
045 The views and conclusions contained in the software and documentation are those
046 of the authors and should not be interpreted as representing official policies,
047 either expressed or implied, of the FreeBSD Project.
048 */
049
050 /**
051 * Any XML data attached to the vCard. This is used if the vCard was encoded in
052 * XML (xCard standard) and it contained some non-standard elements.
053 *
054 * <pre>
055 * VCard vcard = new VCard();
056 * XmlType xml = new XmlType("<b>Some xml</b>");
057 * vcard.addXml(xml);
058 * </pre>
059 *
060 * <p>
061 * vCard property name: XML
062 * </p>
063 * <p>
064 * vCard versions: 4.0
065 * </p>
066 * @author Michael Angstadt
067 */
068 public class XmlType extends TextType {
069 public static final String NAME = "XML";
070
071 public XmlType() {
072 super(NAME);
073 }
074
075 /**
076 * @param xml the XML element
077 */
078 public XmlType(String xml) {
079 super(NAME, xml);
080 }
081
082 /**
083 * Gets the ALTID.
084 * <p>
085 * vCard versions: 4.0
086 * </p>
087 * @return the ALTID or null if it doesn't exist
088 * @see VCardSubTypes#getAltId
089 */
090 public String getAltId() {
091 return subTypes.getAltId();
092 }
093
094 /**
095 * Sets the ALTID.
096 * <p>
097 * vCard versions: 4.0
098 * </p>
099 * @param altId the ALTID or null to remove
100 * @see VCardSubTypes#setAltId
101 */
102 public void setAltId(String altId) {
103 subTypes.setAltId(altId);
104 }
105
106 @Override
107 public VCardVersion[] getSupportedVersions() {
108 return new VCardVersion[] { VCardVersion.V4_0 };
109 }
110
111 /**
112 * Converts the text value of this property to an XML {@link Element}
113 * object.
114 * @return the element object or null if this property's value is null
115 * @throws SAXException if there's a problem parsing the XML
116 */
117 public Element asElement() throws SAXException {
118 if (value == null) {
119 return null;
120 }
121
122 Document document = XmlUtils.toDocument(value);
123 return document.getDocumentElement();
124 }
125
126 @Override
127 protected void doMarshalXml(XCardElement parent, List<String> warnings, CompatibilityMode compatibilityMode) {
128 if (value == null) {
129 throw new SkipMeException("Property does not have a value associated with it.");
130 }
131
132 //parse the XML string
133 Element root = null;
134 try {
135 Document document = XmlUtils.toDocument(value);
136 root = XmlUtils.getRootElement(document);
137 } catch (SAXException e) {
138 throw new SkipMeException("Property value is not valid XML.");
139 }
140
141 //add XML element to marshalled document
142 Node imported = parent.element().getOwnerDocument().importNode(root, true);
143 parent.element().appendChild(imported);
144 }
145
146 @Override
147 protected void doUnmarshalXml(XCardElement element, List<String> warnings, CompatibilityMode compatibilityMode) {
148 Map<String, String> outputProperties = new HashMap<String, String>();
149 outputProperties.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
150 value = XmlUtils.toString(element.element(), outputProperties);
151 }
152 }