1 57 58 package org.apache.soap.util.xml; 59 60 import java.io.*; 61 import java.util.*; 62 import org.apache.soap.util.xml.* ; 63 64 83 public class NSStack { 84 Vector nss = new Vector (); int nssCount = 0; Vector tos; private final static String nsPrefixPrefix = "ns"; 88 private int nsPrefixCount = 1; 89 90 94 public void pushScope () { 95 nss.addElement (tos = new Vector ()); 96 nssCount++; 97 } 98 99 104 public void popScope () { 105 nss.removeElementAt (--nssCount); 106 tos = (nssCount != 0) ? (Vector) nss.elementAt (nssCount-1) : null; 107 } 108 109 116 synchronized public void addNSDeclaration (String prefix, String URI) { 117 tos.addElement (new NSDecl (prefix, URI)); 118 } 119 120 131 synchronized public String addNSDeclaration (String URI) { 132 String uniquePrefix = getPrefixFromURI (URI); 133 if (uniquePrefix == null) { 134 do { 135 uniquePrefix = nsPrefixPrefix + nsPrefixCount++; 136 } while (getURIFromPrefix (uniquePrefix) != null); 137 addNSDeclaration (uniquePrefix, URI); 138 } 139 return uniquePrefix; 140 } 141 142 149 public String getPrefixFromURI (String URI) { 150 for (int i = nssCount-1; i >= 0; i--) { 151 Vector scope = (Vector) nss.elementAt (i); 152 for (Enumeration e = scope.elements (); e.hasMoreElements (); ) { 153 NSDecl nsd = (NSDecl) e.nextElement (); 154 if (nsd.URI.equals (URI)) { 155 return nsd.prefix; 156 } 157 } 158 } 159 return null; 160 } 161 162 171 synchronized public String getPrefixFromURI (String namespaceURI, 172 Writer sink) 173 throws IOException { 174 String prefix = getPrefixFromURI (namespaceURI); 175 176 if (prefix == null) { 177 prefix = addNSDeclaration (namespaceURI); 178 179 sink.write (" xmlns:" + prefix + "=\"" + namespaceURI + '\"'); 180 } 181 182 return prefix; 183 } 184 185 192 public String getURIFromPrefix (String prefix) { 193 for (int i = nssCount-1; i >= 0; i--) { 194 Vector scope = (Vector) nss.elementAt (i); 195 for (Enumeration e = scope.elements (); e.hasMoreElements (); ) { 196 NSDecl nsd = (NSDecl) e.nextElement (); 197 if (nsd.prefix.equals (prefix)) { 198 return nsd.URI; 199 } 200 } 201 } 202 return null; 203 } 204 205 public String toString() 207 { 208 return nss.toString(); 209 } 210 } 212 213 | Popular Tags |