1 55 package org.jboss.axis.utils; 56 57 import org.jboss.logging.Logger; 58 59 import java.util.ArrayList ; 60 61 82 public class NSStack 83 { 84 private static Logger log = Logger.getLogger(NSStack.class.getName()); 85 86 private Mapping[] stack; 87 private int top = 0; 88 private int iterator = 0; 89 private int currentDefaultNS = -1; 90 91 public NSStack() 92 { 93 stack = new Mapping[32]; 94 stack[0] = null; 95 } 96 97 100 public void push() 101 { 102 top++; 103 104 if (top >= stack.length) 105 { 106 Mapping newstack[] = new Mapping[stack.length * 2]; 107 System.arraycopy(stack, 0, newstack, 0, stack.length); 108 stack = newstack; 109 } 110 111 114 stack[top] = null; 115 } 116 117 120 public void pop() 121 { 122 clearFrame(); 123 124 top--; 125 126 if (top < currentDefaultNS) 129 { 130 while (currentDefaultNS > 0) 131 { 132 if (stack[currentDefaultNS] != null && 133 stack[currentDefaultNS].getPrefix().length() == 0) 134 break; 135 currentDefaultNS--; 136 } 137 } 138 139 if (top == 0) 140 { 141 return; 144 } 145 146 } 149 150 153 public ArrayList cloneFrame() 154 { 155 if (stack[top] == null) return null; 156 157 ArrayList clone = new ArrayList (); 158 159 for (Mapping map = topOfFrame(); map != null; map = next()) 160 { 161 clone.add(map); 162 } 163 164 return clone; 165 } 166 167 170 private void clearFrame() 171 { 172 while (stack[top] != null) top--; 173 } 174 175 181 public Mapping topOfFrame() 182 { 183 iterator = top; 184 while (stack[iterator] != null) iterator--; 185 iterator++; 186 return next(); 187 } 188 189 192 public Mapping next() 193 { 194 if (iterator > top) 195 { 196 return null; 197 } 198 else 199 { 200 return stack[iterator++]; 201 } 202 } 203 204 209 public void add(String namespaceURI, String prefix) 210 { 211 int idx = top; 212 try 213 { 214 for (int cursor = top; stack[cursor] != null; cursor--) 216 { 217 if (stack[cursor].getPrefix().equals(prefix)) 218 { 219 stack[cursor].setNamespaceURI(namespaceURI); 220 idx = cursor; 221 return; 222 } 223 } 224 225 push(); 226 stack[top] = new Mapping(namespaceURI, prefix); 227 idx = top; 228 } 229 finally 230 { 231 if (prefix.length() == 0) 234 { 235 currentDefaultNS = idx; 236 } 237 } 238 } 239 240 255 public String getPrefix(String namespaceURI, boolean noDefault) 256 { 257 if ((namespaceURI == null) || (namespaceURI.equals(""))) 258 return null; 259 260 int hash = namespaceURI.hashCode(); 261 262 if (!noDefault && currentDefaultNS > 0 && stack[currentDefaultNS] != null && 265 namespaceURI.equals(stack[currentDefaultNS].getNamespaceURI())) 266 return ""; 267 268 for (int cursor = top; cursor > 0; cursor--) 269 { 270 Mapping map = stack[cursor]; 271 if (map == null) continue; 272 273 if (map.getNamespaceHash() == hash && 274 map.getNamespaceURI().equals(namespaceURI)) 275 { 276 String possiblePrefix = map.getPrefix(); 277 if (noDefault && possiblePrefix.length() == 0) continue; 278 279 int ppHash = possiblePrefix.hashCode(); 282 for (int cursor2 = top; true; cursor2--) 283 { 284 if (cursor2 == cursor) return possiblePrefix; 285 map = stack[cursor2]; 286 if (map == null) continue; 287 if (ppHash == map.getPrefixHash() && 288 possiblePrefix.equals(map.getPrefix())) 289 break; 290 } 291 } 292 } 293 294 return null; 295 } 296 297 301 public String getPrefix(String namespaceURI) 302 { 303 return getPrefix(namespaceURI, false); 304 } 305 306 309 public String getNamespaceURI(String prefix) 310 { 311 if (prefix == null) 312 prefix = ""; 313 314 int hash = prefix.hashCode(); 315 316 for (int cursor = top; cursor > 0; cursor--) 317 { 318 Mapping map = stack[cursor]; 319 if (map == null) continue; 320 321 if (map.getPrefixHash() == hash && map.getPrefix().equals(prefix)) 322 return map.getNamespaceURI(); 323 } 324 325 return null; 326 } 327 328 332 public void dump(String dumpPrefix) 333 { 334 for (int cursor = top; cursor > 0; cursor--) 335 { 336 Mapping map = stack[cursor]; 337 338 if (map == null) 339 { 340 log.trace(dumpPrefix + Messages.getMessage("stackFrame00")); 341 } 342 else 343 { 344 log.trace(dumpPrefix + map.getNamespaceURI() + " -> " + map.getPrefix()); 345 } 346 } 347 } 348 } 349 | Popular Tags |