| 1 | package ezvcard.io.xml; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | |
| 5 | import javax.xml.transform.OutputKeys; |
| 6 | |
| 7 | import ezvcard.Messages; |
| 8 | |
| 9 | /* |
| 10 | Copyright (c) 2012-2023, Michael Angstadt |
| 11 | All rights reserved. |
| 12 | |
| 13 | Redistribution and use in source and binary forms, with or without |
| 14 | modification, are permitted provided that the following conditions are met: |
| 15 | |
| 16 | 1. Redistributions of source code must retain the above copyright notice, this |
| 17 | list of conditions and the following disclaimer. |
| 18 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 | this list of conditions and the following disclaimer in the documentation |
| 20 | and/or other materials provided with the distribution. |
| 21 | |
| 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 26 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 29 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | |
| 33 | The views and conclusions contained in the software and documentation are those |
| 34 | of the authors and should not be interpreted as representing official policies, |
| 35 | either expressed or implied, of the FreeBSD Project. |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * Helper class for setting commonly-used JAXP output properties. |
| 40 | * @author Michael Angstadt |
| 41 | */ |
| 42 | public class XCardOutputProperties extends HashMap<String, String> { |
| 43 | private static final long serialVersionUID = -1038397031136827278L; |
| 44 | private static final String INDENT_AMT = "{http://xml.apache.org/xslt}indent-amount"; |
| 45 | |
| 46 | public XCardOutputProperties() { |
| 47 | put(OutputKeys.METHOD, "xml"); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param indent the number of indent spaces to use for pretty-printing or |
| 52 | * null to disable pretty-printing (disabled by default) |
| 53 | * @param xmlVersion the XML version to use (defaults to "1.0") (Note: Many |
| 54 | * JDKs only support 1.0 natively. For XML 1.1 support, add a JAXP library |
| 55 | * like <a href= |
| 56 | * "http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22xalan%22%20AND%20a%3A%22xalan%22" |
| 57 | * >xalan</a> to your project) |
| 58 | * @throws IllegalArgumentException if the indent amount is less than zero |
| 59 | */ |
| 60 | public XCardOutputProperties(Integer indent, String xmlVersion) { |
| 61 | this(); |
| 62 | setIndent(indent); |
| 63 | setXmlVersion(xmlVersion); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Gets the number of indent spaces to use for pretty-printing. |
| 68 | * @return the number of indent spaces or null if pretty-printing is |
| 69 | * disabled |
| 70 | */ |
| 71 | public Integer getIndent() { |
| 72 | if (!"yes".equals(get(OutputKeys.INDENT))) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | String value = get(INDENT_AMT); |
| 77 | return (value == null) ? null : Integer.valueOf(value); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Sets the number of indent spaces to use for pretty-printing (disabled by |
| 82 | * default). |
| 83 | * @param indent the number of indent spaces to use or null to disable |
| 84 | * pretty-printing |
| 85 | * @throws IllegalArgumentException if the indent amount is less than zero |
| 86 | */ |
| 87 | public void setIndent(Integer indent) { |
| 88 | if (indent == null) { |
| 89 | remove(OutputKeys.INDENT); |
| 90 | remove(INDENT_AMT); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (indent < 0) { |
| 95 | throw Messages.INSTANCE.getIllegalArgumentException(30); |
| 96 | } |
| 97 | |
| 98 | put(OutputKeys.INDENT, "yes"); |
| 99 | put(INDENT_AMT, indent.toString()); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Gets the XML version to use. |
| 104 | * @return the XML version or null if not set |
| 105 | */ |
| 106 | public String getXmlVersion() { |
| 107 | return get(OutputKeys.VERSION); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * <p> |
| 112 | * Sets the XML version to use (defaults to "1.0"). |
| 113 | * </p> |
| 114 | * <p> |
| 115 | * Note: Many JDKs only support 1.0 natively. For XML 1.1 support, add a |
| 116 | * JAXP library like <a href= |
| 117 | * "http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22xalan%22%20AND%20a%3A%22xalan%22" |
| 118 | * >xalan</a> to your project. |
| 119 | * </p> |
| 120 | * @param version the XML version or null to remove |
| 121 | */ |
| 122 | public void setXmlVersion(String version) { |
| 123 | if (version == null) { |
| 124 | remove(OutputKeys.VERSION); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | put(OutputKeys.VERSION, version); |
| 129 | } |
| 130 | } |