Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.chain;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.StringWriter;
6 import java.io.UncheckedIOException;
7 import java.io.Writer;
8 import java.nio.file.Path;
9 import java.util.Collection;
10
11 import ezvcard.Ezvcard;
12 import ezvcard.VCard;
13 import ezvcard.io.json.JCardWriter;
14 import ezvcard.io.scribe.VCardPropertyScribe;
15 import ezvcard.property.VCardProperty;
16
17 /*
18 Copyright (c) 2012-2023, Michael Angstadt
19 All rights reserved.
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23
24 1. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /**
43 * Chainer class for writing jCards (JSON-encoded vCards).
44 * @see Ezvcard#writeJson(Collection)
45 * @see Ezvcard#writeJson(VCard...)
46 * @author Michael Angstadt
47 */
48 public class ChainingJsonWriter extends ChainingWriter<ChainingJsonWriter> {
49 private boolean prettyPrint = false;
50
51 /**
52 * @param vcards the vCards to write
53 */
54 public ChainingJsonWriter(Collection<VCard> vcards) {
55 super(vcards);
56 }
57
58 /**
59 * Sets whether or not to pretty-print the JSON.
60 * @param prettyPrint true to pretty-print it, false not to (defaults to
61 * false)
62 * @return this
63 */
64 public ChainingJsonWriter prettyPrint(boolean prettyPrint) {
65 this.prettyPrint = prettyPrint;
66 return this;
67 }
68
69 @Override
70 public ChainingJsonWriter prodId(boolean include) {
71 return super.prodId(include);
72 }
73
74 @Override
75 public ChainingJsonWriter versionStrict(boolean versionStrict) {
76 return super.versionStrict(versionStrict);
77 }
78
79 @Override
80 public ChainingJsonWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) {
81 return super.register(scribe);
82 }
83
84 /**
85 * Writes the jCards to a string.
86 * @return the JSON string
87 */
88 public String go() {
89 StringWriter sw = new StringWriter();
90 try {
91 go(sw);
92 } catch (IOException e) {
93 //should never be thrown because we're writing to a string
94 throw new UncheckedIOException(e);
95 }
96 return sw.toString();
97 }
98
99 /**
100 * Writes the jCards to an output stream.
101 * @param out the output stream to write to
102 * @throws IOException if there's a problem writing to the output stream
103 */
104 public void go(OutputStream out) throws IOException {
105 go(new JCardWriter(out, wrapInArray()));
106 }
107
108 /**
109 * Writes the jCards to a file.
110 * @param file the file to write to
111 * @throws IOException if there's a problem writing to the file
112 */
113 public void go(Path file) throws IOException {
114 try (JCardWriter writer = new JCardWriter(file, wrapInArray())) {
115 go(writer);
116 }
117 }
118
119 /**
120 * Writes the jCards to a writer.
121 * @param writer the writer to write to
122 * @throws IOException if there's a problem writing to the writer
123 */
124 public void go(Writer writer) throws IOException {
125 go(new JCardWriter(writer, wrapInArray()));
126 }
127
128 private void go(JCardWriter writer) throws IOException {
129 writer.setAddProdId(prodId);
130 writer.setPrettyPrint(prettyPrint);
131 writer.setVersionStrict(versionStrict);
132 if (index != null) {
133 writer.setScribeIndex(index);
134 }
135 try {
136 for (VCard vcard : vcards) {
137 writer.write(vcard);
138 writer.flush();
139 }
140 } finally {
141 writer.closeJsonStream();
142 }
143 }
144
145 private boolean wrapInArray() {
146 return vcards.size() > 1;
147 }
148 }