001package ezvcard.io.xml;
002
003import java.util.HashMap;
004
005import javax.xml.transform.OutputKeys;
006
007import ezvcard.Messages;
008
009/*
010 Copyright (c) 2012-2023, Michael Angstadt
011 All rights reserved.
012
013 Redistribution and use in source and binary forms, with or without
014 modification, are permitted provided that the following conditions are met: 
015
016 1. Redistributions of source code must retain the above copyright notice, this
017 list of conditions and the following disclaimer. 
018 2. Redistributions in binary form must reproduce the above copyright notice,
019 this list of conditions and the following disclaimer in the documentation
020 and/or other materials provided with the distribution. 
021
022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
023 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
024 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
025 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
026 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
027 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
029 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032
033 The views and conclusions contained in the software and documentation are those
034 of the authors and should not be interpreted as representing official policies, 
035 either expressed or implied, of the FreeBSD Project.
036 */
037
038/**
039 * Helper class for setting commonly-used JAXP output properties.
040 * @author Michael Angstadt
041 */
042public class XCardOutputProperties extends HashMap<String, String> {
043        private static final long serialVersionUID = -1038397031136827278L;
044        private static final String INDENT_AMT = "{http://xml.apache.org/xslt}indent-amount";
045
046        public XCardOutputProperties() {
047                put(OutputKeys.METHOD, "xml");
048        }
049
050        /**
051         * @param indent the number of indent spaces to use for pretty-printing or
052         * null to disable pretty-printing (disabled by default)
053         * @param xmlVersion the XML version to use (defaults to "1.0") (Note: Many
054         * JDKs only support 1.0 natively. For XML 1.1 support, add a JAXP library
055         * like <a href=
056         * "http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22xalan%22%20AND%20a%3A%22xalan%22"
057         * >xalan</a> to your project)
058         * @throws IllegalArgumentException if the indent amount is less than zero
059         */
060        public XCardOutputProperties(Integer indent, String xmlVersion) {
061                this();
062                setIndent(indent);
063                setXmlVersion(xmlVersion);
064        }
065
066        /**
067         * Gets the number of indent spaces to use for pretty-printing.
068         * @return the number of indent spaces or null if pretty-printing is
069         * disabled
070         */
071        public Integer getIndent() {
072                if (!"yes".equals(get(OutputKeys.INDENT))) {
073                        return null;
074                }
075
076                String value = get(INDENT_AMT);
077                return (value == null) ? null : Integer.valueOf(value);
078        }
079
080        /**
081         * Sets the number of indent spaces to use for pretty-printing (disabled by
082         * default).
083         * @param indent the number of indent spaces to use or null to disable
084         * pretty-printing
085         * @throws IllegalArgumentException if the indent amount is less than zero
086         */
087        public void setIndent(Integer indent) {
088                if (indent == null) {
089                        remove(OutputKeys.INDENT);
090                        remove(INDENT_AMT);
091                        return;
092                }
093
094                if (indent < 0) {
095                        throw Messages.INSTANCE.getIllegalArgumentException(30);
096                }
097
098                put(OutputKeys.INDENT, "yes");
099                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}