| 1 | package ezvcard.io.chain; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.OutputStream; |
| 5 | import java.io.Writer; |
| 6 | import java.nio.file.Path; |
| 7 | import java.util.Collection; |
| 8 | |
| 9 | import ezvcard.Ezvcard; |
| 10 | import ezvcard.VCard; |
| 11 | import ezvcard.io.html.HCardPage; |
| 12 | import freemarker.template.Template; |
| 13 | |
| 14 | /* |
| 15 | Copyright (c) 2012-2023, Michael Angstadt |
| 16 | All rights reserved. |
| 17 | |
| 18 | Redistribution and use in source and binary forms, with or without |
| 19 | modification, are permitted provided that the following conditions are met: |
| 20 | |
| 21 | 1. Redistributions of source code must retain the above copyright notice, this |
| 22 | list of conditions and the following disclaimer. |
| 23 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 24 | this list of conditions and the following disclaimer in the documentation |
| 25 | and/or other materials provided with the distribution. |
| 26 | |
| 27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 28 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 29 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 30 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 31 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 32 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 33 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 34 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | /** |
| 40 | * Chainer class for writing hCards (HTML-encoded vCards). |
| 41 | * @see Ezvcard#writeHtml(Collection) |
| 42 | * @see Ezvcard#writeHtml(VCard...) |
| 43 | * @author Michael Angstadt |
| 44 | */ |
| 45 | public class ChainingHtmlWriter extends ChainingWriter<ChainingHtmlWriter> { |
| 46 | private Template template; |
| 47 | |
| 48 | /** |
| 49 | * @param vcards the vCards to write |
| 50 | */ |
| 51 | public ChainingHtmlWriter(Collection<VCard> vcards) { |
| 52 | super(vcards); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the freemarker template to use. |
| 57 | * @param template the template (defaults to an embedded template) |
| 58 | * @return this |
| 59 | */ |
| 60 | public ChainingHtmlWriter template(Template template) { |
| 61 | this.template = template; |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Writes the hCards to a string. |
| 67 | * @return the HTML page |
| 68 | */ |
| 69 | public String go() { |
| 70 | return buildPage().write(); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Writes the hCards to an output stream. |
| 75 | * @param out the output stream to write to |
| 76 | * @throws IOException if there's a problem writing to the output stream |
| 77 | */ |
| 78 | public void go(OutputStream out) throws IOException { |
| 79 | buildPage().write(out); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Writes the hCards to a file. |
| 84 | * @param file the file to write to |
| 85 | * @throws IOException if there's a problem writing to the file |
| 86 | */ |
| 87 | public void go(Path file) throws IOException { |
| 88 | buildPage().write(file); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Writes the hCards to a writer. |
| 93 | * @param writer the writer to write to |
| 94 | * @throws IOException if there's a problem writing to the writer |
| 95 | */ |
| 96 | public void go(Writer writer) throws IOException { |
| 97 | buildPage().write(writer); |
| 98 | } |
| 99 | |
| 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 | } |