1 8 package com.sun.japex.testsuite.impl.runtime; 9 10 import java.util.Enumeration ; 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.xml.XMLConstants ; 18 19 import org.xml.sax.SAXException ; 20 21 import com.sun.xml.bind.marshaller.NamespacePrefixMapper; 22 import com.sun.xml.bind.marshaller.NamespaceSupport; 23 24 33 public class NamespaceContextImpl implements NamespaceContext2 34 { 35 39 private int iota = 1; 40 41 44 private final NamespaceSupport nss = new NamespaceSupport(); 45 46 49 private boolean inCollectingMode; 50 51 52 private final NamespacePrefixMapper prefixMapper; 53 54 60 private final Map decls = new HashMap (); 61 62 private final Map reverseDecls = new HashMap (); 63 64 65 public NamespaceContextImpl(NamespacePrefixMapper _prefixMapper) { 66 this.prefixMapper = _prefixMapper; 67 nss.declarePrefix("",""); 70 nss.declarePrefix( "xmlns", XMLConstants.XMLNS_ATTRIBUTE_NS_URI ); 71 } 74 75 public final NamespacePrefixMapper getNamespacePrefixMapper() { 76 return prefixMapper; 77 } 78 79 88 public String declareNamespace( String namespaceUri, String preferedPrefix, boolean requirePrefix ) { 89 if( !inCollectingMode ) { 90 if( !requirePrefix && nss.getURI("").equals(namespaceUri) ) 91 return ""; 93 if (requirePrefix) 98 return nss.getPrefix2(namespaceUri); 99 else 100 return nss.getPrefix(namespaceUri); 101 } else { 102 if( requirePrefix && namespaceUri.length()==0 ) 103 return ""; 104 105 String prefix = (String )reverseDecls.get(namespaceUri); 107 if( prefix!=null ) { 108 if( !requirePrefix || prefix.length()!=0 ) { 109 return prefix; 112 } else { 113 116 decls.remove(prefix); 118 reverseDecls.remove(namespaceUri); 119 } 120 } 121 122 123 if( namespaceUri.length()==0 ) { 124 prefix = ""; 126 } else { 127 prefix = nss.getPrefix(namespaceUri); 129 if( prefix==null ) 130 prefix = (String )reverseDecls.get(namespaceUri); 131 132 if( prefix==null ) { 133 135 if( prefixMapper!=null ) 139 prefix = prefixMapper.getPreferredPrefix( 140 namespaceUri,preferedPrefix,requirePrefix); 141 else 142 prefix = preferedPrefix; 143 144 if( prefix==null ) 145 prefix = "ns"+(iota++); 147 } 148 } 149 150 152 if( requirePrefix && prefix.length()==0 ) 153 prefix = "ns"+(iota++); 155 156 157 while(true) { 158 String existingUri = (String )decls.get(prefix); 159 160 if( existingUri==null ) { 161 decls.put( prefix, namespaceUri ); 163 reverseDecls.put( namespaceUri, prefix ); 164 return prefix; 165 } 166 167 if( existingUri.length()==0 ) { 168 ; 172 } else { 173 177 decls.put( prefix, namespaceUri ); 179 reverseDecls.put( namespaceUri, prefix ); 180 181 namespaceUri = existingUri; 182 } 183 184 prefix = "ns"+(iota++); 187 188 } 190 } 191 } 192 193 194 public String getPrefix( String namespaceUri ) { 195 198 return declareNamespace(namespaceUri,null,false); 203 } 204 205 209 public String getNamespaceURI( String prefix ) { 210 String uri = (String )decls.get(prefix); 211 if(uri!=null) return uri; 212 213 return nss.getURI(prefix); 214 } 215 216 public Iterator getPrefixes( String namespaceUri ) { 217 Set s = new HashSet (); 219 220 String prefix = (String )reverseDecls.get(namespaceUri); 221 if(prefix!=null) s.add(prefix); 222 223 if( nss.getURI("").equals(namespaceUri) ) 224 s.add(""); 225 226 for( Enumeration e=nss.getPrefixes(namespaceUri); e.hasMoreElements(); ) 227 s.add(e.nextElement()); 228 229 return s.iterator(); 230 } 231 232 238 public void startElement() { 239 nss.pushContext(); 240 inCollectingMode = true; 241 } 242 243 251 public void endNamespaceDecls() { 252 if(!decls.isEmpty()) { 253 for( Iterator itr=decls.entrySet().iterator(); itr.hasNext(); ) { 255 Map.Entry e = (Map.Entry )itr.next(); 256 String prefix = (String )e.getKey(); 257 String uri = (String )e.getValue(); 258 if(!uri.equals(nss.getURI(prefix))) nss.declarePrefix( prefix, uri ); 260 } 261 decls.clear(); 262 reverseDecls.clear(); 263 } 264 inCollectingMode = false; 265 } 266 267 273 public void endElement() { 274 nss.popContext(); 275 } 276 277 278 279 280 public void iterateDeclaredPrefixes( PrefixCallback callback ) throws SAXException { 281 for( Enumeration e=nss.getDeclaredPrefixes(); e.hasMoreElements(); ) { 282 String p = (String )e.nextElement(); 283 String uri = nss.getURI(p); 284 285 callback.onPrefixMapping( p, uri ); 286 } 287 } 288 289 290 } 291 | Popular Tags |