Debian Codegraph @

src/main/java/ezvcard/io/chain/ChainingXmlWriter.java

raw symbols SCIP
1 package ezvcard.io.chain;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.Writer;
6 import java.nio.file.Path;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerException;
13
14 import org.w3c.dom.Document;
15
16 import ezvcard.Ezvcard;
17 import ezvcard.VCard;
18 import ezvcard.VCardDataType;
19 import ezvcard.io.scribe.VCardPropertyScribe;
20 import ezvcard.io.xml.XCardDocument;
21 import ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter;
22 import ezvcard.io.xml.XCardOutputProperties;
23 import ezvcard.property.VCardProperty;
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 * Chainer class for writing xCards (XML-encoded vCards).
52 * @see Ezvcard#writeXml(Collection)
53 * @see Ezvcard#writeXml(VCard...)
54 * @author Michael Angstadt
55 */
56 public class ChainingXmlWriter extends ChainingWriter<ChainingXmlWriter> {
57 private final XCardOutputProperties outputProperties = new XCardOutputProperties();
58 private final Map<String, VCardDataType> parameterDataTypes = new HashMap<>(0);
59
60 /**
61 * @param vcards the vCards to write
62 */
63 public ChainingXmlWriter(Collection<VCard> vcards) {
64 super(vcards);
65 }
66
67 /**
68 * Sets the number of indent spaces to use for pretty-printing. If not set,
69 * then the XML will not be pretty-printed.
70 * @param indent the number of spaces in the indent string or null not to
71 * pretty-print (disabled by default)
72 * @return this
73 */
74 public ChainingXmlWriter indent(Integer indent) {
75 outputProperties.setIndent(indent);
76 return this;
77 }
78
79 /**
80 * Sets the XML version to use. Note that many JDKs only support 1.0
81 * natively. For XML 1.1 support, add a JAXP library like <a href=
82 * "http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22xalan%22%20AND%20a%3A%22xalan%22"
83 * >xalan</a> to your project.
84 * @param xmlVersion the XML version (defaults to "1.0")
85 * @return this
86 */
87 public ChainingXmlWriter xmlVersion(String xmlVersion) {
88 outputProperties.setXmlVersion(xmlVersion);
89 return this;
90 }
91
92 /**
93 * Assigns an output property to the JAXP transformer (see
94 * {@link Transformer#setOutputProperty}).
95 * @param name the property name
96 * @param value the property value
97 * @return this
98 */
99 public ChainingXmlWriter outputProperty(String name, String value) {
100 outputProperties.put(name, value);
101 return this;
102 }
103
104 /**
105 * Assigns all of the given output properties to the JAXP transformer (see
106 * {@link Transformer#setOutputProperty}).
107 * @param outputProperties the properties
108 * @return this
109 */
110 public ChainingXmlWriter outputProperties(Map<String, String> outputProperties) {
111 this.outputProperties.putAll(outputProperties);
112 return this;
113 }
114
115 @Override
116 public ChainingXmlWriter prodId(boolean include) {
117 return super.prodId(include);
118 }
119
120 @Override
121 public ChainingXmlWriter versionStrict(boolean versionStrict) {
122 return super.versionStrict(versionStrict);
123 }
124
125 @Override
126 public ChainingXmlWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) {
127 return super.register(scribe);
128 }
129
130 /**
131 * Registers the data type of a non-standard parameter. Non-standard
132 * parameters use the "unknown" data type by default.
133 * @param parameterName the parameter name (e.g. "x-foo")
134 * @param dataType the data type
135 * @return this
136 */
137 public ChainingXmlWriter register(String parameterName, VCardDataType dataType) {
138 parameterDataTypes.put(parameterName, dataType);
139 return this;
140 }
141
142 /**
143 * Writes the xCards to a string.
144 * @return the XML document
145 */
146 public String go() {
147 return createXCardDocument().write(outputProperties);
148 }
149
150 /**
151 * Writes the xCards to an output stream.
152 * @param out the output stream to write to
153 * @throws TransformerException if there's a problem writing to the output
154 * stream
155 */
156 public void go(OutputStream out) throws TransformerException {
157 createXCardDocument().write(out, outputProperties);
158 }
159
160 /**
161 * Writes the xCards to a file.
162 * @param file the file to write to
163 * @throws IOException if the file can't be opened
164 * @throws TransformerException if there's a problem writing to the file
165 */
166 public void go(Path file) throws IOException, TransformerException {
167 createXCardDocument().write(file, outputProperties);
168 }
169
170 /**
171 * Writes the xCards to a writer.
172 * @param writer the writer to write to
173 * @throws TransformerException if there's a problem writing to the writer
174 */
175 public void go(Writer writer) throws TransformerException {
176 createXCardDocument().write(writer, outputProperties);
177 }
178
179 /**
180 * Generates an XML document object model (DOM) containing the xCards.
181 * @return the DOM
182 */
183 public Document dom() {
184 return createXCardDocument().getDocument();
185 }
186
187 private XCardDocument createXCardDocument() {
188 XCardDocument document = new XCardDocument();
189
190 XCardDocumentStreamWriter writer = document.writer();
191 writer.setAddProdId(prodId);
192 writer.setVersionStrict(versionStrict);
193 for (Map.Entry<String, VCardDataType> entry : parameterDataTypes.entrySet()) {
194 String parameterName = entry.getKey();
195 VCardDataType dataType = entry.getValue();
196 writer.registerParameterDataType(parameterName, dataType);
197 }
198 if (index != null) {
199 writer.setScribeIndex(index);
200 }
201
202 for (VCard vcard : vcards) {
203 writer.write(vcard);
204 }
205
206 return document;
207 }
208 }