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 031import java.io.IOException; 032 033public class XMLWriterState { 034 035 private String name; 036 private String namespace; 037 private boolean children; 038 private boolean inComment; 039 private boolean pretty; 040 041 private XMLNamespace[] namespaceDefns = null; 042 043 public void addNamespaceDefn(String namespace, String abbrev) throws IOException { 044 XMLNamespace ns; 045 ns = getDefnByAbbreviation(abbrev); 046 if (ns != null) 047 throw new IOException("duplicate namespace declaration on \""+abbrev+"\""); 048 ns = new XMLNamespace(namespace, abbrev); 049 if (namespaceDefns == null) 050 namespaceDefns = new XMLNamespace[] {ns}; 051 else { 052 XMLNamespace[] newns = new XMLNamespace[namespaceDefns.length + 1]; 053 for (int i = 0; i < namespaceDefns.length; i++) { 054 newns[i] = namespaceDefns[i]; 055 } 056 namespaceDefns = newns; 057 namespaceDefns[namespaceDefns.length-1] = ns; 058 } 059 } 060 061 public XMLNamespace getDefnByNamespace(String namespace) { 062 if (namespaceDefns == null) 063 return null; 064 for (int i = 0; i < namespaceDefns.length; i++) { 065 XMLNamespace element = namespaceDefns[i]; 066 if (element.getNamespace().equals(namespace)) 067 return element; 068 } 069 return null; 070 } 071 072 public XMLNamespace getDefnByAbbreviation(String abbreviation) { 073 if (namespaceDefns == null) 074 return null; 075 for (int i = 0; i < namespaceDefns.length; i++) { 076 XMLNamespace element = namespaceDefns[i]; 077 if (element.getAbbreviation() != null && element.getAbbreviation().equals(abbreviation)) 078 return element; 079 } 080 return null; 081 } 082 083 /** 084 * @return the name 085 */ 086 public String getName() { 087 return name; 088 } 089 090 /** 091 * @param name the name to set 092 */ 093 public void setName(String name) { 094 this.name = name; 095 } 096 097 /** 098 * @return the namespace 099 */ 100 public String getNamespace() { 101 return namespace; 102 } 103 104 /** 105 * @param namespace the namespace to set 106 */ 107 public void setNamespace(String namespace) { 108 this.namespace = namespace; 109 } 110 111 /** 112 * @return the children 113 */ 114 public boolean hasChildren() { 115 return children; 116 } 117 118 /** 119 * @param children the children to set 120 */ 121 public void seeChild() { 122 this.children = true; 123 } 124 125 public XMLNamespace getDefaultNamespace() { 126 if (namespaceDefns == null) 127 return null; 128 for (int i = 0; i < namespaceDefns.length; i++) { 129 XMLNamespace element = namespaceDefns[i]; 130 if (element.getAbbreviation() == null) 131 return element; 132 } 133 return null; 134 } 135 136 /** 137 * @return the inComment 138 */ 139 public boolean isInComment() { 140 return inComment; 141 } 142 143 /** 144 * @param inComment the inComment to set 145 */ 146 public void setInComment(boolean inComment) { 147 this.inComment = inComment; 148 } 149 150 /** 151 * @return the pretty 152 */ 153 public boolean isPretty() { 154 return pretty; 155 } 156 157 /** 158 * @param pretty the pretty to set 159 */ 160 public void setPretty(boolean pretty) { 161 this.pretty = pretty; 162 } 163 164 165}