1 22 23 28 29 package org.xquark.util; 30 31 import java.util.*; 32 33 36 public class NamespaceContextStack implements NamespaceContext 37 { 38 private static final String RCSRevision = "$Revision: 1.1 $"; 39 private static final String RCSName = "$Name: $"; 40 41 44 private ArrayList contexts = new ArrayList(); 45 48 private NSContextInfo current = null; 49 52 private int level = 0; 53 58 private int count = 0; 59 60 public NamespaceContextStack() 61 { 62 reset(); 63 } 64 65 public void reset() 66 { 67 level = 0; 68 count = 0; 69 current = push(); 70 current.declarePrefix("xml", "http://www.w3.org/XML/1998/namespace"); 71 } 72 73 public void declarePrefix(String prefix, String uri) 74 { 75 if (current.getLevel() < level) 76 current = push(); 77 current.declarePrefix(prefix, uri); 78 } 79 80 public void pushContext() 81 { 82 level++; 83 } 84 85 public void popContext() 86 { 87 level--; 88 if (current.getLevel() > level) 89 current = pop(); 90 } 91 92 private NSContextInfo push() 96 { 97 NSContextInfo context = null; 98 if (count == contexts.size()) 99 { 100 context = new NSContextInfo(); 102 contexts.add(context); 103 } 104 else 105 { 106 context = (NSContextInfo) contexts.get(count); 108 } 109 count++; 110 context.initialize(level, current); 111 return context; 112 } 113 114 private NSContextInfo pop() 115 { 116 if (count > 1) 118 count--; 119 else 120 reset(); 121 return (NSContextInfo) contexts.get(count - 1); 122 } 123 124 public NamespaceContext getNamespaceContext() 125 { 126 return current.getContext(true); 127 } 128 129 public String getNamespaceURI(String prefix) 130 { 131 return current.getContext().getNamespaceURI(prefix); 132 } 133 134 public Collection getNamespaceURIs() { 135 return current.getContext().getNamespaceURIs(); 136 } 137 138 public String getPrefix(String uri) 139 { 140 return current.getContext().getPrefix(uri); 141 } 142 143 public List getDeclaredPrefixes() 144 { 145 return current.getDeclaredPrefixes(level); 146 } 147 148 public Collection getPrefixes() 149 { 150 return current.getContext().getPrefixes(); 151 } 152 153 public Collection getPrefixes(String uri) 154 { 155 return current.getContext().getPrefixes(uri); 156 } 157 158 static class NSContextInfo 162 { 163 private static final String RCSRevision = "$Revision: 1.1 $"; 164 private static final String RCSName = "$Name: $"; 165 static private final List EMPTY_LIST = new ArrayList(1); 166 167 private NamespaceContextImpl context = new NamespaceContextImpl(); 168 private int level = -1; 169 private boolean locked = false; 170 171 void initialize(int level, NSContextInfo parent) 172 { 173 this.level = level; 174 if (parent != null) 175 { 176 if (locked) 177 context = new NamespaceContextImpl(); 178 context.initialize(parent.getContext()); 179 } 180 locked = false; 181 } 182 183 int getLevel() 184 { 185 return level; 186 } 187 188 NamespaceContextImpl getContext() 189 { 190 return getContext(false); 191 } 192 193 NamespaceContextImpl getContext(boolean lock) 194 { 195 if (!locked) 196 locked = lock; 197 return context; 198 } 199 200 void declarePrefix(String prefix, String uri) 201 { 202 context.declarePrefix(prefix, uri); 203 } 204 205 public List getDeclaredPrefixes(int currentLevel) 206 { 207 if (currentLevel == level) 208 return context.getDeclaredPrefixes(); 209 else 210 return EMPTY_LIST; 211 } 212 } 213 214 static class NamespaceContextImpl implements NamespaceContext 215 { 216 private static final String RCSRevision = "$Revision: 1.1 $"; 217 private static final String RCSName = "$Name: $"; 218 private HashMap prefixMap = new HashMap(); 219 private HashMap namespaceMap = new HashMap(); 220 private ArrayList declarations = new ArrayList(); 221 222 void initialize(NamespaceContextImpl parent) 223 { 224 prefixMap.clear(); 225 namespaceMap.clear(); 226 declarations.clear(); 227 prefixMap.putAll(parent.getPrefixMap()); 228 namespaceMap.putAll(parent.getNamespaceMap()); 229 } 230 231 HashMap getPrefixMap() 232 { 233 return prefixMap; 234 } 235 236 HashMap getNamespaceMap() 237 { 238 return namespaceMap; 239 } 240 241 void declarePrefix(String prefix, String uri) 242 { 243 prefixMap.put(prefix, uri); 244 namespaceMap.put(uri, prefix); 245 declarations.add(prefix); 246 } 247 248 public String getNamespaceURI(String prefix) 249 { 250 return (String ) prefixMap.get(prefix); 251 } 252 253 public String getPrefix(String uri) 254 { 255 return (String ) namespaceMap.get(uri); 256 } 257 258 public Collection getPrefixes() 259 { 260 return Collections.unmodifiableCollection(prefixMap.keySet()); 261 } 262 263 public Collection getNamespaceURIs() { 264 return Collections.unmodifiableCollection(namespaceMap.keySet()); 265 } 266 267 public List getDeclaredPrefixes() { 268 return Collections.unmodifiableList(declarations); 269 } 270 271 public Collection getPrefixes(String uri) 272 { 273 ArrayList result = new ArrayList(); 274 Iterator it = prefixMap.entrySet().iterator(); 275 while (it.hasNext()) 276 { 277 Map.Entry entry = (Map.Entry) it.next(); 278 if (uri.equals(entry.getValue())) 279 result.add(entry.getKey()); 280 } 281 return result; 282 } 283 } 284 285 } 286 | Popular Tags |