1 16 package org.apache.axis.utils; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.axis.AxisProperties; 20 import org.apache.axis.Constants; 21 import org.apache.commons.logging.Log; 22 23 import java.util.ArrayList ; 24 25 46 public class NSStack { 47 protected static Log log = 48 LogFactory.getLog(NSStack.class.getName()); 49 50 private Mapping[] stack; 51 private int top = 0; 52 private int iterator = 0; 53 private int currentDefaultNS = -1; 54 private boolean optimizePrefixes = true; 55 56 private final boolean traceEnabled = log.isTraceEnabled(); 60 61 public NSStack(boolean optimizePrefixes) { 62 this.optimizePrefixes = optimizePrefixes; 63 stack = new Mapping[32]; 64 stack[0] = null; 65 } 66 67 public NSStack() { 68 stack = new Mapping[32]; 69 stack[0] = null; 70 } 71 72 75 public void push() { 76 top ++; 77 78 if (top >= stack.length) { 79 Mapping newstack[] = new Mapping[stack.length*2]; 80 System.arraycopy (stack, 0, newstack, 0, stack.length); 81 stack = newstack; 82 } 83 84 if (traceEnabled) 85 log.trace("NSPush (" + stack.length + ")"); 86 87 stack[top] = null; 88 } 89 90 93 public void pop() { 94 clearFrame(); 95 96 top--; 97 98 if (top < currentDefaultNS) { 101 currentDefaultNS = top; 103 while (currentDefaultNS > 0) { 104 if (stack[currentDefaultNS] != null && 105 stack[currentDefaultNS].getPrefix().length() == 0) 106 break; 107 currentDefaultNS--; 108 } 109 } 110 111 if (top == 0) { 112 if (traceEnabled) 113 log.trace("NSPop (" + Messages.getMessage("empty00") + ")"); 114 115 return; 116 } 117 118 if (traceEnabled){ 119 log.trace("NSPop (" + stack.length + ")"); 120 } 121 } 122 123 126 public ArrayList cloneFrame() { 127 if (stack[top] == null) return null; 128 129 ArrayList clone = new ArrayList (); 130 131 for (Mapping map=topOfFrame(); map!=null; map=next()) { 132 clone.add(map); 133 } 134 135 return clone; 136 } 137 138 141 private void clearFrame() { 142 while (stack[top] != null) top--; 143 } 144 145 151 public Mapping topOfFrame() { 152 iterator = top; 153 while (stack[iterator] != null) iterator--; 154 iterator++; 155 return next(); 156 } 157 158 161 public Mapping next() { 162 if (iterator > top) { 163 return null; 164 } else { 165 return stack[iterator++]; 166 } 167 } 168 169 174 public void add(String namespaceURI, String prefix) { 175 int idx = top; 176 prefix = prefix.intern(); 177 try { 178 for (int cursor=top; stack[cursor]!=null; cursor--) { 180 if (stack[cursor].getPrefix() == prefix) { 181 stack[cursor].setNamespaceURI(namespaceURI); 182 idx = cursor; 183 return; 184 } 185 } 186 187 push(); 188 stack[top] = new Mapping(namespaceURI, prefix); 189 idx = top; 190 } finally { 191 if (prefix.length() == 0) { 194 currentDefaultNS = idx; 195 } 196 } 197 } 198 199 214 public String getPrefix(String namespaceURI, boolean noDefault) { 215 if ((namespaceURI == null) || (namespaceURI.length()==0)) 216 return null; 217 218 if(optimizePrefixes) { 219 if (!noDefault && currentDefaultNS > 0 && stack[currentDefaultNS] != null && 222 namespaceURI == stack[currentDefaultNS].getNamespaceURI()) 223 return ""; 224 } 225 namespaceURI = namespaceURI.intern(); 226 227 for (int cursor=top; cursor>0; cursor--) { 228 Mapping map = stack[cursor]; 229 if (map == null) 230 continue; 231 232 if (map.getNamespaceURI() == namespaceURI) { 233 String possiblePrefix = map.getPrefix(); 234 if (noDefault && possiblePrefix.length() == 0) 235 continue; 236 237 for (int cursor2 = top; true; cursor2--) { 240 if (cursor2 == cursor) 241 return possiblePrefix; 242 map = stack[cursor2]; 243 if (map == null) 244 continue; 245 if (possiblePrefix == map.getPrefix()) 246 break; 247 } 248 } 249 } 250 251 return null; 252 } 253 254 258 public String getPrefix(String namespaceURI) { 259 return getPrefix(namespaceURI, false); 260 } 261 262 265 public String getNamespaceURI(String prefix) { 266 if (prefix == null) 267 prefix = ""; 268 269 prefix = prefix.intern(); 270 271 for (int cursor=top; cursor>0; cursor--) { 272 Mapping map = stack[cursor]; 273 if (map == null) continue; 274 275 if (map.getPrefix() == prefix) 276 return map.getNamespaceURI(); 277 } 278 279 return null; 280 } 281 282 286 public void dump(String dumpPrefix) 287 { 288 for (int cursor=top; cursor>0; cursor--) { 289 Mapping map = stack[cursor]; 290 291 if (map == null) { 292 log.trace(dumpPrefix + Messages.getMessage("stackFrame00")); 293 } else { 294 log.trace(dumpPrefix + map.getNamespaceURI() + " -> " + map.getPrefix()); 295 } 296 } 297 } 298 } 299 | Popular Tags |