001package ezvcard.io.chain;
002
003import java.io.File;
004import java.io.IOException;
005import java.io.OutputStream;
006import java.io.StringWriter;
007import java.io.Writer;
008import java.util.Collection;
009
010import ezvcard.Ezvcard;
011import ezvcard.VCard;
012import ezvcard.io.json.JCardWriter;
013import ezvcard.io.scribe.VCardPropertyScribe;
014import ezvcard.property.VCardProperty;
015
016/*
017 Copyright (c) 2012-2018, Michael Angstadt
018 All rights reserved.
019
020 Redistribution and use in source and binary forms, with or without
021 modification, are permitted provided that the following conditions are met: 
022
023 1. Redistributions of source code must retain the above copyright notice, this
024 list of conditions and the following disclaimer. 
025 2. Redistributions in binary form must reproduce the above copyright notice,
026 this list of conditions and the following disclaimer in the documentation
027 and/or other materials provided with the distribution. 
028
029 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
030 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
031 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
032 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
033 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
035 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
036 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
037 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
038 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
039 */
040
041/**
042 * Chainer class for writing jCards (JSON-encoded vCards).
043 * @see Ezvcard#writeJson(Collection)
044 * @see Ezvcard#writeJson(VCard...)
045 * @author Michael Angstadt
046 */
047public class ChainingJsonWriter extends ChainingWriter<ChainingJsonWriter> {
048        private boolean prettyPrint = false;
049
050        /**
051         * @param vcards the vCards to write
052         */
053        public ChainingJsonWriter(Collection<VCard> vcards) {
054                super(vcards);
055        }
056
057        /**
058         * Sets whether or not to pretty-print the JSON.
059         * @param prettyPrint true to pretty-print it, false not to (defaults to
060         * false)
061         * @return this
062         */
063        public ChainingJsonWriter prettyPrint(boolean prettyPrint) {
064                this.prettyPrint = prettyPrint;
065                return this;
066        }
067
068        @Override
069        public ChainingJsonWriter prodId(boolean include) {
070                return super.prodId(include);
071        }
072
073        @Override
074        public ChainingJsonWriter versionStrict(boolean versionStrict) {
075                return super.versionStrict(versionStrict);
076        }
077
078        @Override
079        public ChainingJsonWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) {
080                return super.register(scribe);
081        }
082
083        /**
084         * Writes the jCards to a string.
085         * @return the JSON string
086         */
087        public String go() {
088                StringWriter sw = new StringWriter();
089                try {
090                        go(sw);
091                } catch (IOException e) {
092                        //should never be thrown because we're writing to a string
093                        throw new RuntimeException(e);
094                }
095                return sw.toString();
096        }
097
098        /**
099         * Writes the jCards to an output stream.
100         * @param out the output stream to write to
101         * @throws IOException if there's a problem writing to the output stream
102         */
103        public void go(OutputStream out) throws IOException {
104                go(new JCardWriter(out, wrapInArray()));
105        }
106
107        /**
108         * Writes the jCards to a file.
109         * @param file the file to write to
110         * @throws IOException if there's a problem writing to the file
111         */
112        public void go(File file) throws IOException {
113                JCardWriter writer = new JCardWriter(file, wrapInArray());
114                try {
115                        go(writer);
116                } finally {
117                        writer.close();
118                }
119        }
120
121        /**
122         * Writes the jCards to a writer.
123         * @param writer the writer to write to
124         * @throws IOException if there's a problem writing to the writer
125         */
126        public void go(Writer writer) throws IOException {
127                go(new JCardWriter(writer, wrapInArray()));
128        }
129
130        private void go(JCardWriter writer) throws IOException {
131                writer.setAddProdId(prodId);
132                writer.setPrettyPrint(prettyPrint);
133                writer.setVersionStrict(versionStrict);
134                if (index != null) {
135                        writer.setScribeIndex(index);
136                }
137                try {
138                        for (VCard vcard : vcards) {
139                                writer.write(vcard);
140                                writer.flush();
141                        }
142                } finally {
143                        writer.closeJsonStream();
144                }
145        }
146
147        private boolean wrapInArray() {
148                return vcards.size() > 1;
149        }
150}