001package ezvcard.io.chain;
002
003import java.io.IOException;
004import java.io.OutputStream;
005import java.io.Writer;
006import java.nio.file.Path;
007import java.util.Collection;
008
009import ezvcard.Ezvcard;
010import ezvcard.VCard;
011import ezvcard.io.html.HCardPage;
012import freemarker.template.Template;
013
014/*
015 Copyright (c) 2012-2023, Michael Angstadt
016 All rights reserved.
017
018 Redistribution and use in source and binary forms, with or without
019 modification, are permitted provided that the following conditions are met: 
020
021 1. Redistributions of source code must retain the above copyright notice, this
022 list of conditions and the following disclaimer. 
023 2. Redistributions in binary form must reproduce the above copyright notice,
024 this list of conditions and the following disclaimer in the documentation
025 and/or other materials provided with the distribution. 
026
027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
028 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
029 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
030 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
032 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
033 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
034 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
035 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037 */
038
039/**
040 * Chainer class for writing hCards (HTML-encoded vCards).
041 * @see Ezvcard#writeHtml(Collection)
042 * @see Ezvcard#writeHtml(VCard...)
043 * @author Michael Angstadt
044 */
045public class ChainingHtmlWriter extends ChainingWriter<ChainingHtmlWriter> {
046        private Template template;
047
048        /**
049         * @param vcards the vCards to write
050         */
051        public ChainingHtmlWriter(Collection<VCard> vcards) {
052                super(vcards);
053        }
054
055        /**
056         * Sets the freemarker template to use.
057         * @param template the template (defaults to an embedded template)
058         * @return this
059         */
060        public ChainingHtmlWriter template(Template template) {
061                this.template = template;
062                return this;
063        }
064
065        /**
066         * Writes the hCards to a string.
067         * @return the HTML page
068         */
069        public String go() {
070                return buildPage().write();
071        }
072
073        /**
074         * Writes the hCards to an output stream.
075         * @param out the output stream to write to
076         * @throws IOException if there's a problem writing to the output stream
077         */
078        public void go(OutputStream out) throws IOException {
079                buildPage().write(out);
080        }
081
082        /**
083         * Writes the hCards to a file.
084         * @param file the file to write to
085         * @throws IOException if there's a problem writing to the file
086         */
087        public void go(Path file) throws IOException {
088                buildPage().write(file);
089        }
090
091        /**
092         * Writes the hCards to a writer.
093         * @param writer the writer to write to
094         * @throws IOException if there's a problem writing to the writer
095         */
096        public void go(Writer writer) throws IOException {
097                buildPage().write(writer);
098        }
099
100        private HCardPage buildPage() {
101                HCardPage page = (template == null) ? new HCardPage() : new HCardPage(template);
102                for (VCard vcard : vcards) {
103                        page.add(vcard);
104                }
105                return page;
106        }
107}