001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029package org.hl7.fhir.utilities.xml;
030
031/*
032 * #%L
033 * HAPI FHIR - Core Library
034 * %%
035 * Copyright (C) 2014 - 2017 University Health Network
036 * %%
037 * Licensed under the Apache License, Version 2.0 (the "License");
038 * you may not use this file except in compliance with the License.
039 * You may obtain a copy of the License at
040 * 
041 *      http://www.apache.org/licenses/LICENSE-2.0
042 * 
043 * Unless required by applicable law or agreed to in writing, software
044 * distributed under the License is distributed on an "AS IS" BASIS,
045 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
046 * See the License for the specific language governing permissions and
047 * limitations under the License.
048 * #L%
049 */
050
051
052import java.io.IOException;
053
054public class XMLWriterState {
055
056        private String name;
057        private String namespace;
058        private boolean children;
059        private boolean inComment;
060        private boolean pretty;
061        
062        private XMLNamespace[] namespaceDefns = null;
063        
064        public void addNamespaceDefn(String namespace, String abbrev) throws IOException {
065                XMLNamespace ns;
066                ns = getDefnByAbbreviation(abbrev);
067                if (ns != null)
068                        throw new IOException("duplicate namespace declaration on \""+abbrev+"\"");
069                ns = new XMLNamespace(namespace, abbrev);
070                if (namespaceDefns == null)
071                        namespaceDefns = new XMLNamespace[] {ns};
072                else {
073                        XMLNamespace[] newns = new XMLNamespace[namespaceDefns.length + 1];
074                        for (int i = 0; i < namespaceDefns.length; i++) {
075                                newns[i] = namespaceDefns[i];
076                        }
077                        namespaceDefns = newns;
078                        namespaceDefns[namespaceDefns.length-1] = ns;                   
079                }
080        }
081
082        public XMLNamespace getDefnByNamespace(String namespace) {
083                if (namespaceDefns == null)
084                        return null;
085                for (int  i = 0; i < namespaceDefns.length; i++) {
086                        XMLNamespace element = namespaceDefns[i];
087                        if (element.getNamespace().equals(namespace))
088                                return element;
089                }
090                return null;
091        }
092
093        public XMLNamespace getDefnByAbbreviation(String abbreviation) {
094                if (namespaceDefns == null)
095                        return null;
096                for (int  i = 0; i < namespaceDefns.length; i++) {
097                        XMLNamespace element = namespaceDefns[i];
098                        if (element.getAbbreviation() != null && element.getAbbreviation().equals(abbreviation))
099                                return element;
100                }
101                return null;
102        }
103
104        /**
105         * @return the name
106         */
107        public String getName() {
108                return name;
109        }
110
111        /**
112         * @param name the name to set
113         */
114        public void setName(String name) {
115                this.name = name;
116        }
117
118        /**
119         * @return the namespace
120         */
121        public String getNamespace() {
122                return namespace;
123        }
124
125        /**
126         * @param namespace the namespace to set
127         */
128        public void setNamespace(String namespace) {
129                this.namespace = namespace;
130        }
131
132        /**
133         * @return the children
134         */
135        public boolean hasChildren() {
136                return children;
137        }
138
139        public void seeChild() {
140                this.children = true;
141        }
142
143        public XMLNamespace getDefaultNamespace() {
144                if (namespaceDefns == null)
145                        return null;
146                for (int  i = 0; i < namespaceDefns.length; i++) {
147                        XMLNamespace element = namespaceDefns[i];
148                        if (element.getAbbreviation() == null)
149                                return element;
150                }
151                return null;
152        }
153
154        /**
155         * @return the inComment
156         */
157        public boolean isInComment() {
158                return inComment;
159        }
160
161        /**
162         * @param inComment the inComment to set
163         */
164        public void setInComment(boolean inComment) {
165                this.inComment = inComment;
166        }
167
168        /**
169         * @return the pretty
170         */
171        public boolean isPretty() {
172                return pretty;
173        }
174
175        /**
176         * @param pretty the pretty to set
177         */
178        public void setPretty(boolean pretty) {
179                this.pretty = pretty;
180        }
181
182        
183}