1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.util.Enumeration ; 22 import java.util.Hashtable ; 23 import java.util.Stack ; 24 25 import org.xml.sax.ContentHandler ; 26 import org.xml.sax.SAXException ; 27 28 59 public class NamespaceMappings 60 { 61 65 private int count = 0; 66 67 75 private java.util.Stack m_prefixStack = new Stack (); 76 77 80 private Hashtable m_namespaces = new Hashtable (); 81 82 94 private Stack m_nodeStack = new Stack (); 95 96 private static final String EMPTYSTRING = ""; 97 private static final String XML_PREFIX = "xml"; 99 103 public NamespaceMappings() 104 { 105 initNamespaces(); 106 } 107 108 112 private void initNamespaces() 113 { 114 115 116 Stack stack; 118 m_namespaces.put(EMPTYSTRING, stack = new Stack ()); 119 stack.push(EMPTYSTRING); 120 m_prefixStack.push(EMPTYSTRING); 121 122 m_namespaces.put(XML_PREFIX, stack = new Stack ()); 123 stack.push("http://www.w3.org/XML/1998/namespace"); 124 m_prefixStack.push(XML_PREFIX); 125 126 m_nodeStack.push(new Integer (-1)); 127 128 } 129 130 136 public String lookupNamespace(String prefix) 137 { 138 final Stack stack = (Stack ) m_namespaces.get(prefix); 139 return stack != null && !stack.isEmpty() ? (String ) stack.peek() : null; 140 } 141 142 150 public String lookupPrefix(String uri) 151 { 152 String foundPrefix = null; 153 Enumeration prefixes = m_namespaces.keys(); 154 while (prefixes.hasMoreElements()) 155 { 156 String prefix = (String ) prefixes.nextElement(); 157 String uri2 = lookupNamespace(prefix); 158 if (uri2 != null && uri2.equals(uri)) 159 { 160 foundPrefix = prefix; 161 break; 162 } 163 } 164 return foundPrefix; 165 } 166 167 170 public boolean popNamespace(String prefix) 171 { 172 if (prefix.startsWith(XML_PREFIX)) 174 { 175 return false; 176 } 177 178 Stack stack; 179 if ((stack = (Stack ) m_namespaces.get(prefix)) != null) 180 { 181 stack.pop(); 182 return true; 183 } 184 return false; 185 } 186 187 193 public boolean pushNamespace(String prefix, String uri, int elemDepth) 194 { 195 if (prefix.startsWith(XML_PREFIX)) 197 { 198 return false; 199 } 200 201 Stack stack; 202 if ((stack = (Stack ) m_namespaces.get(prefix)) == null) 204 { 205 m_namespaces.put(prefix, stack = new Stack ()); 206 } 207 208 if (!stack.empty() && uri.equals(stack.peek())) 209 { 210 return false; 211 } 212 213 stack.push(uri); 214 m_prefixStack.push(prefix); 215 m_nodeStack.push(new Integer (elemDepth)); 216 return true; 217 } 218 219 227 public void popNamespaces(int elemDepth, ContentHandler saxHandler) 228 { 229 while (true) 230 { 231 if (m_nodeStack.isEmpty()) 232 return; 233 Integer i = (Integer ) (m_nodeStack.peek()); 234 if (i.intValue() < elemDepth) 235 return; 236 239 240 m_nodeStack.pop(); 241 final String prefix = (String ) m_prefixStack.pop(); 242 popNamespace(prefix); 243 if (saxHandler != null) 244 { 245 try 246 { 247 saxHandler.endPrefixMapping(prefix); 248 } 249 catch (SAXException e) 250 { 251 } 253 } 254 255 } 256 } 257 258 262 public String generateNextPrefix() 263 { 264 return "ns" + (count++); 265 } 266 267 268 272 public Object clone() throws CloneNotSupportedException { 273 NamespaceMappings clone = new NamespaceMappings(); 274 clone.m_prefixStack = (Stack )m_prefixStack.clone(); 275 clone.m_nodeStack = (Stack ) m_nodeStack.clone(); 276 clone.m_namespaces = (Hashtable ) m_namespaces.clone(); 277 278 clone.count = count; 279 return clone; 280 281 } 282 283 public final void reset() 284 { 285 this.count = 0; 286 this.m_namespaces.clear(); 287 this.m_nodeStack.clear(); 288 this.m_prefixStack.clear(); 289 290 initNamespaces(); 291 } 292 293 } 294 | Popular Tags |