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