1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 import java.util.Enumeration ; 61 import java.util.NoSuchElementException ; 62 63 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 64 65 75 public class NamespaceSupport implements NamespaceContext { 76 77 81 90 protected String [] fNamespace = new String [16 * 2]; 91 92 93 protected int fNamespaceSize; 94 95 98 106 protected int[] fContext = new int[8]; 107 108 109 protected int fCurrentContext; 110 111 protected String [] fPrefixes = new String [16]; 112 113 117 118 public NamespaceSupport() { 119 } 121 125 public NamespaceSupport(NamespaceContext context) { 126 pushContext(); 127 Enumeration prefixes = context.getAllPrefixes(); 129 while (prefixes.hasMoreElements()){ 130 String prefix = (String )prefixes.nextElement(); 131 String uri = context.getURI(prefix); 132 declarePrefix(prefix, uri); 133 } 134 } 136 137 141 144 public void reset() { 145 146 fNamespaceSize = 0; 148 fCurrentContext = 0; 149 fContext[fCurrentContext] = fNamespaceSize; 150 151 fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML; 153 fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI; 154 fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS; 156 fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI; 157 ++fCurrentContext; 158 159 } 161 162 165 public void pushContext() { 166 167 if (fCurrentContext + 1 == fContext.length) { 169 int[] contextarray = new int[fContext.length * 2]; 170 System.arraycopy(fContext, 0, contextarray, 0, fContext.length); 171 fContext = contextarray; 172 } 173 174 fContext[++fCurrentContext] = fNamespaceSize; 176 177 } 179 180 183 public void popContext() { 184 fNamespaceSize = fContext[fCurrentContext--]; 185 } 187 190 public boolean declarePrefix(String prefix, String uri) { 191 if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) { 193 return false; 194 } 195 196 for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) { 198 if (fNamespace[i - 2] == prefix) { 199 fNamespace[i - 1] = uri; 206 return true; 207 } 208 } 209 210 if (fNamespaceSize == fNamespace.length) { 212 String [] namespacearray = new String [fNamespaceSize * 2]; 213 System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize); 214 fNamespace = namespacearray; 215 } 216 217 fNamespace[fNamespaceSize++] = prefix; 219 fNamespace[fNamespaceSize++] = uri; 220 221 return true; 222 223 } 225 228 public String getURI(String prefix) { 229 230 for (int i = fNamespaceSize; i > 0; i -= 2) { 232 if (fNamespace[i - 2] == prefix) { 233 return fNamespace[i - 1]; 234 } 235 } 236 237 return null; 239 240 } 242 243 246 public String getPrefix(String uri) { 247 248 for (int i = fNamespaceSize; i > 0; i -= 2) { 250 if (fNamespace[i - 1] == uri) { 251 if (getURI(fNamespace[i - 2]) == uri) 252 return fNamespace[i - 2]; 253 } 254 } 255 256 return null; 258 259 } 261 262 265 public int getDeclaredPrefixCount() { 266 return (fNamespaceSize - fContext[fCurrentContext]) / 2; 267 } 269 272 public String getDeclaredPrefixAt(int index) { 273 return fNamespace[fContext[fCurrentContext] + index * 2]; 274 } 276 279 public Enumeration getAllPrefixes() { 280 int count = 0; 281 if (fPrefixes.length < (fNamespace.length/2)) { 282 String [] prefixes = new String [fNamespaceSize]; 284 fPrefixes = prefixes; 285 } 286 String prefix = null; 287 boolean unique = true; 288 for (int i = 2; i < (fNamespaceSize-2); i += 2) { 289 prefix = fNamespace[i + 2]; 290 for (int k=0;k<count;k++){ 291 if (fPrefixes[k]==prefix){ 292 unique = false; 293 break; 294 } 295 } 296 if (unique){ 297 fPrefixes[count++] = prefix; 298 } 299 unique = true; 300 } 301 return new Prefixes(fPrefixes, count); 302 } 303 304 protected final class Prefixes implements Enumeration { 305 private String [] prefixes; 306 private int counter = 0; 307 private int size = 0; 308 309 312 public Prefixes(String [] prefixes, int size) { 313 this.prefixes = prefixes; 314 this.size = size; 315 } 316 317 320 public boolean hasMoreElements() { 321 return (counter< size); 322 } 323 324 327 public Object nextElement() { 328 if (counter< size){ 329 return fPrefixes[counter++]; 330 } 331 throw new NoSuchElementException ("Illegal access to Namespace prefixes enumeration."); 332 } 333 334 public String toString(){ 335 StringBuffer buf = new StringBuffer (); 336 for (int i=0;i<size;i++){ 337 buf.append(prefixes[i]); 338 buf.append(" "); 339 } 340 341 return buf.toString(); 342 } 343 344 } 345 346 } | Popular Tags |