1 16 17 package org.apache.xerces.util; 18 19 import java.util.Enumeration ; 20 import java.util.NoSuchElementException ; 21 22 import org.apache.xerces.xni.NamespaceContext; 23 24 34 public class NamespaceSupport implements NamespaceContext { 35 36 40 49 protected String [] fNamespace = new String [16 * 2]; 50 51 52 protected int fNamespaceSize; 53 54 57 65 protected int[] fContext = new int[8]; 66 67 68 protected int fCurrentContext; 69 70 protected String [] fPrefixes = new String [16]; 71 72 76 77 public NamespaceSupport() { 78 } 80 84 public NamespaceSupport(NamespaceContext context) { 85 pushContext(); 86 Enumeration prefixes = context.getAllPrefixes(); 88 while (prefixes.hasMoreElements()){ 89 String prefix = (String )prefixes.nextElement(); 90 String uri = context.getURI(prefix); 91 declarePrefix(prefix, uri); 92 } 93 } 95 96 100 103 public void reset() { 104 105 fNamespaceSize = 0; 107 fCurrentContext = 0; 108 fContext[fCurrentContext] = fNamespaceSize; 109 110 fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML; 112 fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI; 113 fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS; 115 fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI; 116 ++fCurrentContext; 117 118 } 120 121 124 public void pushContext() { 125 126 if (fCurrentContext + 1 == fContext.length) { 128 int[] contextarray = new int[fContext.length * 2]; 129 System.arraycopy(fContext, 0, contextarray, 0, fContext.length); 130 fContext = contextarray; 131 } 132 133 fContext[++fCurrentContext] = fNamespaceSize; 135 136 } 138 139 142 public void popContext() { 143 fNamespaceSize = fContext[fCurrentContext--]; 144 } 146 149 public boolean declarePrefix(String prefix, String uri) { 150 if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) { 152 return false; 153 } 154 155 for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) { 157 if (fNamespace[i - 2] == prefix) { 158 fNamespace[i - 1] = uri; 165 return true; 166 } 167 } 168 169 if (fNamespaceSize == fNamespace.length) { 171 String [] namespacearray = new String [fNamespaceSize * 2]; 172 System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize); 173 fNamespace = namespacearray; 174 } 175 176 fNamespace[fNamespaceSize++] = prefix; 178 fNamespace[fNamespaceSize++] = uri; 179 180 return true; 181 182 } 184 187 public String getURI(String prefix) { 188 189 for (int i = fNamespaceSize; i > 0; i -= 2) { 191 if (fNamespace[i - 2] == prefix) { 192 return fNamespace[i - 1]; 193 } 194 } 195 196 return null; 198 199 } 201 202 205 public String getPrefix(String uri) { 206 207 for (int i = fNamespaceSize; i > 0; i -= 2) { 209 if (fNamespace[i - 1] == uri) { 210 if (getURI(fNamespace[i - 2]) == uri) 211 return fNamespace[i - 2]; 212 } 213 } 214 215 return null; 217 218 } 220 221 224 public int getDeclaredPrefixCount() { 225 return (fNamespaceSize - fContext[fCurrentContext]) / 2; 226 } 228 231 public String getDeclaredPrefixAt(int index) { 232 return fNamespace[fContext[fCurrentContext] + index * 2]; 233 } 235 238 public Enumeration getAllPrefixes() { 239 int count = 0; 240 if (fPrefixes.length < (fNamespace.length/2)) { 241 String [] prefixes = new String [fNamespaceSize]; 243 fPrefixes = prefixes; 244 } 245 String prefix = null; 246 boolean unique = true; 247 for (int i = 2; i < (fNamespaceSize-2); i += 2) { 248 prefix = fNamespace[i + 2]; 249 for (int k=0;k<count;k++){ 250 if (fPrefixes[k]==prefix){ 251 unique = false; 252 break; 253 } 254 } 255 if (unique){ 256 fPrefixes[count++] = prefix; 257 } 258 unique = true; 259 } 260 return new Prefixes(fPrefixes, count); 261 } 262 263 266 267 275 public boolean containsPrefix(String prefix) { 276 277 for (int i = fNamespaceSize; i > 0; i -= 2) { 279 if (fNamespace[i - 2] == prefix) { 280 return true; 281 } 282 } 283 284 return false; 286 } 287 288 protected final class Prefixes implements Enumeration { 289 private String [] prefixes; 290 private int counter = 0; 291 private int size = 0; 292 293 296 public Prefixes(String [] prefixes, int size) { 297 this.prefixes = prefixes; 298 this.size = size; 299 } 300 301 304 public boolean hasMoreElements() { 305 return (counter< size); 306 } 307 308 311 public Object nextElement() { 312 if (counter< size){ 313 return fPrefixes[counter++]; 314 } 315 throw new NoSuchElementException ("Illegal access to Namespace prefixes enumeration."); 316 } 317 318 public String toString(){ 319 StringBuffer buf = new StringBuffer (); 320 for (int i=0;i<size;i++){ 321 buf.append(prefixes[i]); 322 buf.append(" "); 323 } 324 325 return buf.toString(); 326 } 327 328 } 329 330 } | Popular Tags |