1 17 package com.sun.org.apache.xml.internal.security.c14n.implementations; 18 19 import java.lang.reflect.Array ; 20 import java.util.AbstractList ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 30 31 import org.w3c.dom.Attr ; 32 import org.w3c.dom.Node ; 33 34 35 36 42 public class NameSpaceSymbTable { 43 44 45 SymbMap symb = new SymbMap(); 46 47 int nameSpaces=0; 48 49 List level = new ArrayList (); 50 boolean cloned=true; 51 static final String XMLNS="xmlns"; 52 55 public NameSpaceSymbTable() { 56 NameSpaceSymbEntry ne=new NameSpaceSymbEntry("",null,true); 58 ne.lastrendered=""; 59 symb.put(XMLNS,ne); 60 } 61 62 67 public void getUnrenderedNodes(Collection result) { 68 Iterator it=symb.entrySet().iterator(); 70 while (it.hasNext()) { 71 NameSpaceSymbEntry n=(NameSpaceSymbEntry)(it.next()); 72 if ((!n.rendered) && (n.n!=null)) { 74 result.add(n.n); 75 n.rendered=true; 76 } 77 } 78 } 79 80 84 public void outputNodePush() { 85 nameSpaces++; 86 push(); 87 } 88 89 92 public void outputNodePop() { 93 nameSpaces--; 94 pop(); 95 } 96 97 101 public void push() { 102 107 level.add(null); 108 cloned=false; 109 } 110 111 115 public void pop() { 116 int size=level.size()-1; 117 Object ob= level.remove(size); 118 if (ob!=null) { 119 symb=(SymbMap)ob; 120 if (size==0) { 121 cloned=false; 122 } else 123 cloned=(level.get(size-1)!=symb); 124 } else { 125 cloned=false; 126 } 127 128 129 } 130 131 final void needsClone() { 132 if (!cloned) { 133 level.remove(level.size()-1); 134 level.add(symb); 135 symb=(SymbMap) symb.clone(); 136 cloned=true; 137 } 138 } 139 140 141 147 public Attr getMapping(String prefix) { 148 NameSpaceSymbEntry entry=symb.get(prefix); 149 if (entry==null) { 150 return null; 152 } 153 if (entry.rendered) { 154 return null; 156 } 157 entry=(NameSpaceSymbEntry) entry.clone(); 159 needsClone(); 160 symb.put(prefix,entry); 161 entry.rendered=true; 162 entry.level=nameSpaces; 163 entry.lastrendered=entry.uri; 164 return entry.n; 166 } 167 168 174 public Attr getMappingWithoutRendered(String prefix) { 175 NameSpaceSymbEntry entry= symb.get(prefix); 176 if (entry==null) { 177 return null; 178 } 179 if (entry.rendered) { 180 return null; 181 } 182 return entry.n; 183 } 184 185 192 public boolean addMapping(String prefix, String uri,Attr n) { 193 NameSpaceSymbEntry ob = symb.get(prefix); 194 if ((ob!=null) && uri.equals(ob.uri)) { 195 return false; 197 } 198 NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,false); 200 needsClone(); 201 symb.put(prefix, ne); 202 if (ob != null) { 203 ne.lastrendered=ob.lastrendered; 206 if ((ob.lastrendered!=null)&& (ob.lastrendered.equals(uri))) { 207 ne.rendered=true; 209 } 210 } 211 return true; 212 } 213 214 222 public Node addMappingAndRender(String prefix, String uri,Attr n) { 223 NameSpaceSymbEntry ob = symb.get(prefix); 224 225 if ((ob!=null) && uri.equals(ob.uri)) { 226 if (!ob.rendered) { 227 ob=(NameSpaceSymbEntry) ob.clone(); 228 needsClone(); 229 symb.put(prefix,ob); 230 ob.lastrendered=uri; 231 ob.rendered=true; 232 return ob.n; 233 } 234 return null; 235 } 236 237 NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,true); 238 ne.lastrendered=uri; 239 needsClone(); 240 symb.put(prefix, ne); 241 if (ob != null) { 242 243 if ((ob.lastrendered!=null)&& (ob.lastrendered.equals(uri))) { 244 ne.rendered=true; 245 return null; 246 } 247 } 248 return ne.n; 249 } 250 261 public Node addMappingAndRenderXNodeSet(String prefix, String uri,Attr n,boolean outputNode) { 262 NameSpaceSymbEntry ob = symb.get(prefix); 263 int visibleNameSpaces=nameSpaces; 264 if ((ob!=null) && uri.equals(ob.uri)) { 265 if (!ob.rendered) { 266 ob=(NameSpaceSymbEntry)ob.clone(); 267 needsClone(); 268 symb.put(prefix,ob); 269 ob.rendered=true; 270 ob.level=visibleNameSpaces; 271 return ob.n; 272 } 273 ob=(NameSpaceSymbEntry)ob.clone(); 274 needsClone(); 275 symb.put(prefix,ob); 276 if (outputNode && (((visibleNameSpaces-ob.level)<2) || XMLNS.equals(prefix)) ) { 277 ob.level=visibleNameSpaces; 278 return null; } 280 ob.level=visibleNameSpaces; 281 return ob.n; 282 } 283 284 NameSpaceSymbEntry ne=new NameSpaceSymbEntry(uri,n,true); 285 ne.level=nameSpaces; 286 ne.rendered=true; 287 needsClone(); 288 symb.put(prefix, ne); 289 if (ob != null) { 290 ne.lastrendered=ob.lastrendered; 291 292 if ((ob.lastrendered!=null)&& (ob.lastrendered.equals(uri))) { 293 ne.rendered=true; 294 } 295 } 296 return ne.n; 297 } 298 } 299 300 303 class NameSpaceSymbEntry implements Cloneable { 304 NameSpaceSymbEntry(String name,Attr n,boolean rendered) { 305 this.uri=name; 306 this.rendered=rendered; 307 this.n=n; 308 } 309 310 public Object clone() { 311 try { 312 return super.clone(); 313 } catch (CloneNotSupportedException e) { 314 return null; 315 } 316 } 317 318 int level=0; 319 320 String uri; 321 322 String lastrendered=null; 323 324 boolean rendered=false; 325 326 Attr n; 327 }; 328 329 class SymbMap implements Cloneable { 330 int free=23; 331 NameSpaceSymbEntry[] entries=new NameSpaceSymbEntry[free]; 332 String [] keys=new String [free]; 333 334 void put(String key, NameSpaceSymbEntry value) { 335 int index = index(key); 336 Object oldKey = keys[index]; 337 keys[index] = key; 338 entries[index] = value; 339 if (oldKey==null || !oldKey.equals(key)) { 340 if (--free == 0) { 341 free=entries.length; 342 int newCapacity = free<<2; 343 rehash(newCapacity); 344 } 345 } 346 } 347 348 List entrySet() { 349 List a=new ArrayList (); 350 for (int i=0;i<entries.length;i++) { 351 if ((entries[i]!=null) && !("".equals(entries[i]))) { 352 a.add(entries[i]); 353 } 354 } 355 return a; 356 } 357 358 359 protected int index(Object obj) { 360 Object [] set = keys; 361 int length = set.length; 362 int index = (obj.hashCode() & 0x7fffffff) % length; 364 Object cur = set[index]; 365 366 if (cur == null || (cur.equals( obj))) { 367 return index; 368 } 369 do { 370 index=index==length? 0:++index; 371 cur = set[index]; 372 } while (cur != null && (!cur.equals(obj))); 373 return index; 374 } 375 380 protected void rehash(int newCapacity) { 381 int oldCapacity = keys.length; 382 String oldKeys[] = keys; 383 NameSpaceSymbEntry oldVals[] = entries; 384 385 keys = new String [newCapacity]; 386 entries = new NameSpaceSymbEntry[newCapacity]; 387 388 for (int i = oldCapacity; i-- > 0;) { 389 if(oldKeys[i] != null) { 390 String o = oldKeys[i]; 391 int index = index(o); 392 keys[index] = o; 393 entries[index] = oldVals[i]; 394 } 395 } 396 } 397 NameSpaceSymbEntry get(String key) { 398 return entries[index(key)]; 399 } 400 protected Object clone() { 401 try { 403 SymbMap copy=(SymbMap) super.clone(); 404 copy.entries=new NameSpaceSymbEntry[entries.length]; 405 System.arraycopy(entries,0,copy.entries,0,entries.length); 406 copy.keys=new String [keys.length]; 407 System.arraycopy(keys,0,copy.keys,0,keys.length); 408 409 return copy; 410 } catch (CloneNotSupportedException e) { 411 e.printStackTrace(); 413 } 414 return null; 415 } 416 } 417 | Popular Tags |