001package ezvcard.io.chain;
002
003import java.io.IOException;
004import java.io.OutputStream;
005import java.io.StringWriter;
006import java.io.UncheckedIOException;
007import java.io.Writer;
008import java.nio.file.Path;
009import java.util.Collection;
010
011import ezvcard.Ezvcard;
012import ezvcard.VCard;
013import ezvcard.io.json.JCardWriter;
014import ezvcard.io.scribe.VCardPropertyScribe;
015import ezvcard.property.VCardProperty;
016
017/*
018 Copyright (c) 2012-2023, Michael Angstadt
019 All rights reserved.
020
021 Redistribution and use in source and binary forms, with or without
022 modification, are permitted provided that the following conditions are met: 
023
024 1. Redistributions of source code must retain the above copyright notice, this
025 list of conditions and the following disclaimer. 
026 2. Redistributions in binary form must reproduce the above copyright notice,
027 this list of conditions and the following disclaimer in the documentation
028 and/or other materials provided with the distribution. 
029
030 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
031 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
032 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
033 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
034 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
036 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
037 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
038 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
039 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
040 */
041
042/**
043 * Chainer class for writing jCards (JSON-encoded vCards).
044 * @see Ezvcard#writeJson(Collection)
045 * @see Ezvcard#writeJson(VCard...)
046 * @author Michael Angstadt
047 */
048public class ChainingJsonWriter extends ChainingWriter<ChainingJsonWriter> {
049        private boolean prettyPrint = false;
050
051        /**
052         * @param vcards the vCards to write
053         */
054        public ChainingJsonWriter(Collection<VCard> vcards) {
055                super(vcards);
056        }
057
058        /**
059         * Sets whether or not to pretty-print the JSON.
060         * @param prettyPrint true to pretty-print it, false not to (defaults to
061         * false)
062         * @return this
063         */
064        public ChainingJsonWriter prettyPrint(boolean prettyPrint) {
065                this.prettyPrint = prettyPrint;
066                return this;
067        }
068
069        @Override
070        public ChainingJsonWriter prodId(boolean include) {
071                return super.prodId(include);
072        }
073
074        @Override
075        public ChainingJsonWriter versionStrict(boolean versionStrict) {
076                return super.versionStrict(versionStrict);
077        }
078
079        @Override
080        public ChainingJsonWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) {
081                return super.register(scribe);
082        }
083
084        /**
085         * Writes the jCards to a string.
086         * @return the JSON string
087         */
088        public String go() {
089                StringWriter sw = new StringWriter();
090                try {
091                        go(sw);
092                } catch (IOException e) {
093                        //should never be thrown because we're writing to a string
094                        throw new UncheckedIOException(e);
095                }
096                return sw.toString();
097        }
098
099        /**
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}