| 1 | package ezvcard.util; |
| 2 | |
| 3 | import ezvcard.VCardVersion; |
| 4 | |
| 5 | /* |
| 6 | Copyright (c) 2012-2023, Michael Angstadt |
| 7 | All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without |
| 10 | modification, are permitted provided that the following conditions are met: |
| 11 | |
| 12 | 1. Redistributions of source code must retain the above copyright notice, this |
| 13 | list of conditions and the following disclaimer. |
| 14 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | this list of conditions and the following disclaimer in the documentation |
| 16 | and/or other materials provided with the distribution. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | The views and conclusions contained in the software and documentation are those |
| 30 | of the authors and should not be interpreted as representing official policies, |
| 31 | either expressed or implied, of the FreeBSD Project. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Basic class for building an xCard string. |
| 36 | * @author Michael Angstadt |
| 37 | */ |
| 38 | public class XCardBuilder { |
| 39 | private final StringBuilder sb = new StringBuilder(); |
| 40 | private int count = 0; |
| 41 | |
| 42 | public XCardBuilder() { |
| 43 | sb.append("<vcards xmlns=\"").append(VCardVersion.V4_0.getXmlNamespace()).append("\">"); |
| 44 | begin(); |
| 45 | } |
| 46 | |
| 47 | public XCardBuilder begin() { |
| 48 | if (count > 0) { |
| 49 | sb.append("</vcard>"); |
| 50 | } |
| 51 | sb.append("<vcard>"); |
| 52 | count++; |
| 53 | return this; |
| 54 | } |
| 55 | |
| 56 | public XCardBuilder prop(String name, String xml) { |
| 57 | return prop(null, name, xml); |
| 58 | } |
| 59 | |
| 60 | public XCardBuilder prop(String ns, String name, String xml) { |
| 61 | sb.append('<').append(name); |
| 62 | if (ns != null) { |
| 63 | sb.append(" xmlns=\"").append(ns).append("\""); |
| 64 | } |
| 65 | sb.append('>'); |
| 66 | sb.append(xml); |
| 67 | sb.append("</").append(name).append('>'); |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | public int size() { |
| 72 | return count; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | return sb.toString() + "</vcard></vcards>"; |
| 78 | } |
| 79 | } |