1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/NamespaceContextImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,559 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime.output; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.util.Collections; 1.33 +import java.util.Iterator; 1.34 + 1.35 +import javax.xml.XMLConstants; 1.36 +import javax.xml.stream.XMLStreamException; 1.37 + 1.38 +import com.sun.istack.internal.NotNull; 1.39 +import com.sun.istack.internal.Nullable; 1.40 +import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper; 1.41 +import com.sun.xml.internal.bind.v2.WellKnownNamespace; 1.42 +import com.sun.xml.internal.bind.v2.runtime.Name; 1.43 +import com.sun.xml.internal.bind.v2.runtime.NamespaceContext2; 1.44 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 1.45 + 1.46 +import org.xml.sax.SAXException; 1.47 + 1.48 +/** 1.49 + * Keeps track of in-scope namespace bindings for the marshaller. 1.50 + * 1.51 + * <p> 1.52 + * This class is also used to keep track of tag names for each element 1.53 + * for the marshaller (for the performance reason.) 1.54 + * 1.55 + * @author Kohsuke Kawaguchi 1.56 + */ 1.57 +public final class NamespaceContextImpl implements NamespaceContext2 { 1.58 + private final XMLSerializer owner; 1.59 + 1.60 + private String[] prefixes = new String[4]; 1.61 + private String[] nsUris = new String[4]; 1.62 +// /** 1.63 +// * True if the correponding namespace declaration is an authentic one that should be printed. 1.64 +// * 1.65 +// * False if it's a re-discovered in-scope namespace binding available at the ancestor elements 1.66 +// * outside this marhsalling. The false value is used to incorporate the in-scope namespace binding 1.67 +// * information from {@link #inscopeNamespaceContext}. When false, such a declaration does not need 1.68 +// * to be printed, as it's already available in ancestors. 1.69 +// */ 1.70 +// private boolean[] visible = new boolean[4]; 1.71 +// 1.72 +// /** 1.73 +// * {@link NamespaceContext} that informs this {@link XMLSerializer} about the 1.74 +// * in-scope namespace bindings of the ancestor elements outside this marshalling. 1.75 +// * 1.76 +// * <p> 1.77 +// * This is used when the marshaller is marshalling into a subtree that has ancestor 1.78 +// * elements created outside the JAXB marshaller. 1.79 +// * 1.80 +// * Its {@link NamespaceContext#getPrefix(String)} is used to discover in-scope namespace 1.81 +// * binding, 1.82 +// */ 1.83 +// private final NamespaceContext inscopeNamespaceContext; 1.84 + 1.85 + /** 1.86 + * Number of URIs declared. Identifies the valid portion of 1.87 + * the {@link #prefixes} and {@link #nsUris} arrays. 1.88 + */ 1.89 + private int size; 1.90 + 1.91 + private Element current; 1.92 + 1.93 + /** 1.94 + * This is the {@link Element} whose prev==null. 1.95 + * This element is used to hold the contextual namespace bindings 1.96 + * that are assumed to be outside of the document we are marshalling. 1.97 + * Specifically the xml prefix and any other user-specified bindings. 1.98 + * 1.99 + * @see NamespacePrefixMapper#getPreDeclaredNamespaceUris() 1.100 + */ 1.101 + private final Element top; 1.102 + 1.103 + /** 1.104 + * Never null. 1.105 + */ 1.106 + private NamespacePrefixMapper prefixMapper = defaultNamespacePrefixMapper; 1.107 + 1.108 + /** 1.109 + * True to allow new URIs to be declared. False otherwise. 1.110 + */ 1.111 + public boolean collectionMode; 1.112 + 1.113 + 1.114 + public NamespaceContextImpl(XMLSerializer owner) { 1.115 + this.owner = owner; 1.116 + 1.117 + current = top = new Element(this,null); 1.118 + // register namespace URIs that are implicitly bound 1.119 + put(XMLConstants.XML_NS_URI,XMLConstants.XML_NS_PREFIX); 1.120 + } 1.121 + 1.122 + public void setPrefixMapper( NamespacePrefixMapper mapper ) { 1.123 + if(mapper==null) 1.124 + mapper = defaultNamespacePrefixMapper; 1.125 + this.prefixMapper = mapper; 1.126 + } 1.127 + 1.128 + public NamespacePrefixMapper getPrefixMapper() { 1.129 + return prefixMapper; 1.130 + } 1.131 + 1.132 + public void reset() { 1.133 + current = top; 1.134 + size = 1; 1.135 + collectionMode = false; 1.136 + } 1.137 + 1.138 + /** 1.139 + * Returns the prefix index to the specified URI. 1.140 + * This method allocates a new URI if necessary. 1.141 + */ 1.142 + public int declareNsUri( String uri, String preferedPrefix, boolean requirePrefix ) { 1.143 + preferedPrefix = prefixMapper.getPreferredPrefix(uri,preferedPrefix,requirePrefix); 1.144 + 1.145 + if(uri.length()==0) { 1.146 + for( int i=size-1; i>=0; i-- ) { 1.147 + if(nsUris[i].length()==0) 1.148 + return i; // already declared 1.149 + if(prefixes[i].length()==0) { 1.150 + // the default prefix is already taken. 1.151 + // move that URI to another prefix, then assign "" to the default prefix. 1.152 + assert current.defaultPrefixIndex==-1 && current.oldDefaultNamespaceUriIndex==-1; 1.153 + 1.154 + String oldUri = nsUris[i]; 1.155 + String[] knownURIs = owner.nameList.namespaceURIs; 1.156 + 1.157 + if(current.baseIndex<=i) { 1.158 + // this default prefix is declared in this context. just reassign it 1.159 + 1.160 + nsUris[i] = ""; 1.161 + 1.162 + int subst = put(oldUri,null); 1.163 + 1.164 + // update uri->prefix table if necessary 1.165 + for( int j=knownURIs.length-1; j>=0; j-- ) { 1.166 + if(knownURIs[j].equals(oldUri)) { 1.167 + owner.knownUri2prefixIndexMap[j] = subst; 1.168 + break; 1.169 + } 1.170 + } 1.171 + if (current.elementLocalName != null) { 1.172 + current.setTagName(subst, current.elementLocalName, current.getOuterPeer()); 1.173 + } 1.174 + return i; 1.175 + } else { 1.176 + // first, if the previous URI assigned to "" is 1.177 + // a "known URI", remember what we've reallocated 1.178 + // so that we can fix it when this context pops. 1.179 + for( int j=knownURIs.length-1; j>=0; j-- ) { 1.180 + if(knownURIs[j].equals(oldUri)) { 1.181 + current.defaultPrefixIndex = i; 1.182 + current.oldDefaultNamespaceUriIndex = j; 1.183 + // assert commented out; too strict/not valid any more 1.184 + // assert owner.knownUri2prefixIndexMap[j]==current.defaultPrefixIndex; 1.185 + // update the table to point to the prefix we'll declare 1.186 + owner.knownUri2prefixIndexMap[j] = size; 1.187 + break; 1.188 + } 1.189 + } 1.190 + if (current.elementLocalName!=null) { 1.191 + current.setTagName(size, current.elementLocalName, current.getOuterPeer()); 1.192 + } 1.193 + 1.194 + put(nsUris[i],null); 1.195 + return put("", ""); 1.196 + } 1.197 + } 1.198 + } 1.199 + 1.200 + // "" isn't in use 1.201 + return put("", ""); 1.202 + } else { 1.203 + // check for the existing binding 1.204 + for( int i=size-1; i>=0; i-- ) { 1.205 + String p = prefixes[i]; 1.206 + if(nsUris[i].equals(uri)) { 1.207 + if (!requirePrefix || p.length()>0) 1.208 + return i; 1.209 + // declared but this URI is bound to empty. Look further 1.210 + } 1.211 + if(p.equals(preferedPrefix)) { 1.212 + // the suggested prefix is already taken. can't use it 1.213 + preferedPrefix = null; 1.214 + } 1.215 + } 1.216 + 1.217 + if(preferedPrefix==null && requirePrefix) 1.218 + // we know we can't bind to "", but we don't have any possible name at hand. 1.219 + // generate it here to avoid this namespace to be bound to "". 1.220 + preferedPrefix = makeUniquePrefix(); 1.221 + 1.222 + // haven't been declared. allocate a new one 1.223 + // if the preferred prefix is already in use, it should have been set to null by this time 1.224 + return put(uri, preferedPrefix); 1.225 + } 1.226 + } 1.227 + 1.228 + public int force(@NotNull String uri, @NotNull String prefix) { 1.229 + // check for the existing binding 1.230 + 1.231 + for( int i=size-1; i>=0; i-- ) { 1.232 + if(prefixes[i].equals(prefix)) { 1.233 + if(nsUris[i].equals(uri)) 1.234 + return i; // found duplicate 1.235 + else 1.236 + // the prefix is used for another namespace. we need to declare it 1.237 + break; 1.238 + } 1.239 + } 1.240 + 1.241 + return put(uri, prefix); 1.242 + } 1.243 + 1.244 + /** 1.245 + * Puts this new binding into the declared prefixes list 1.246 + * without doing any duplicate check. 1.247 + * 1.248 + * This can be used to forcibly set namespace declarations. 1.249 + * 1.250 + * <p> 1.251 + * Most of the time {@link #declareNamespace(String, String, boolean)} shall be used. 1.252 + * 1.253 + * @return 1.254 + * the index of this new binding. 1.255 + */ 1.256 + public int put(@NotNull String uri, @Nullable String prefix) { 1.257 + if(size==nsUris.length) { 1.258 + // reallocate 1.259 + String[] u = new String[nsUris.length*2]; 1.260 + String[] p = new String[prefixes.length*2]; 1.261 + System.arraycopy(nsUris,0,u,0,nsUris.length); 1.262 + System.arraycopy(prefixes,0,p,0,prefixes.length); 1.263 + nsUris = u; 1.264 + prefixes = p; 1.265 + } 1.266 + if(prefix==null) { 1.267 + if(size==1) 1.268 + prefix = ""; // if this is the first user namespace URI we see, use "". 1.269 + else { 1.270 + // otherwise make up an unique name 1.271 + prefix = makeUniquePrefix(); 1.272 + } 1.273 + } 1.274 + nsUris[size] = uri; 1.275 + prefixes[size] = prefix; 1.276 + 1.277 + return size++; 1.278 + } 1.279 + 1.280 + private String makeUniquePrefix() { 1.281 + String prefix; 1.282 + prefix = new StringBuilder(5).append("ns").append(size).toString(); 1.283 + while(getNamespaceURI(prefix)!=null) { 1.284 + prefix += '_'; // under a rare circumstance there might be existing 'nsNNN', so rename them 1.285 + } 1.286 + return prefix; 1.287 + } 1.288 + 1.289 + 1.290 + public Element getCurrent() { 1.291 + return current; 1.292 + } 1.293 + 1.294 + /** 1.295 + * Returns the prefix index of the specified URI. 1.296 + * It is an error if the URI is not declared. 1.297 + */ 1.298 + public int getPrefixIndex( String uri ) { 1.299 + for( int i=size-1; i>=0; i-- ) { 1.300 + if(nsUris[i].equals(uri)) 1.301 + return i; 1.302 + } 1.303 + throw new IllegalStateException(); 1.304 + } 1.305 + 1.306 + /** 1.307 + * Gets the prefix from a prefix index. 1.308 + * 1.309 + * The behavior is undefined if the index is out of range. 1.310 + */ 1.311 + public String getPrefix(int prefixIndex) { 1.312 + return prefixes[prefixIndex]; 1.313 + } 1.314 + 1.315 + public String getNamespaceURI(int prefixIndex) { 1.316 + return nsUris[prefixIndex]; 1.317 + } 1.318 + 1.319 + /** 1.320 + * Gets the namespace URI that is bound to the specified prefix. 1.321 + * 1.322 + * @return null 1.323 + * if the prefix is unbound. 1.324 + */ 1.325 + public String getNamespaceURI(String prefix) { 1.326 + for( int i=size-1; i>=0; i-- ) 1.327 + if(prefixes[i].equals(prefix)) 1.328 + return nsUris[i]; 1.329 + return null; 1.330 + } 1.331 + 1.332 + /** 1.333 + * Returns the prefix of the specified URI, 1.334 + * or null if none exists. 1.335 + */ 1.336 + public String getPrefix( String uri ) { 1.337 + if(collectionMode) { 1.338 + return declareNamespace(uri,null,false); 1.339 + } else { 1.340 + for( int i=size-1; i>=0; i-- ) 1.341 + if(nsUris[i].equals(uri)) 1.342 + return prefixes[i]; 1.343 + return null; 1.344 + } 1.345 + } 1.346 + 1.347 + public Iterator<String> getPrefixes(String uri) { 1.348 + String prefix = getPrefix(uri); 1.349 + if(prefix==null) 1.350 + return Collections.<String>emptySet().iterator(); 1.351 + else 1.352 + return Collections.singleton(uri).iterator(); 1.353 + } 1.354 + 1.355 + public String declareNamespace(String namespaceUri, String preferedPrefix, boolean requirePrefix) { 1.356 + int idx = declareNsUri(namespaceUri,preferedPrefix,requirePrefix); 1.357 + return getPrefix(idx); 1.358 + } 1.359 + 1.360 + /** 1.361 + * Number of total bindings declared. 1.362 + */ 1.363 + public int count() { 1.364 + return size; 1.365 + } 1.366 + 1.367 + 1.368 + /** 1.369 + * This model of namespace declarations maintain the following invariants. 1.370 + * 1.371 + * <ul> 1.372 + * <li>If a non-empty prefix is declared, it will never be reassigned to different namespace URIs. 1.373 + * <li> 1.374 + */ 1.375 + public final class Element { 1.376 + 1.377 + public final NamespaceContextImpl context; 1.378 + 1.379 + /** 1.380 + * {@link Element}s form a doubly-linked list. 1.381 + */ 1.382 + private final Element prev; 1.383 + private Element next; 1.384 + 1.385 + private int oldDefaultNamespaceUriIndex; 1.386 + private int defaultPrefixIndex; 1.387 + 1.388 + 1.389 + /** 1.390 + * The numbe of prefixes declared by ancestor {@link Element}s. 1.391 + */ 1.392 + private int baseIndex; 1.393 + 1.394 + /** 1.395 + * The depth of the {@link Element}. 1.396 + * 1.397 + * This value is equivalent as the result of the following computation. 1.398 + * 1.399 + * <pre> 1.400 + * int depth() { 1.401 + * int i=-1; 1.402 + * for(Element e=this; e!=null;e=e.prev) 1.403 + * i++; 1.404 + * return i; 1.405 + * } 1.406 + * </pre> 1.407 + */ 1.408 + private final int depth; 1.409 + 1.410 + 1.411 + 1.412 + private int elementNamePrefix; 1.413 + private String elementLocalName; 1.414 + 1.415 + /** 1.416 + * Tag name of this element. 1.417 + * Either this field is used or the {@link #elementNamePrefix} and {@link #elementLocalName} pair. 1.418 + */ 1.419 + private Name elementName; 1.420 + 1.421 + /** 1.422 + * Used for the binder. The JAXB object that corresponds to this element. 1.423 + */ 1.424 + private Object outerPeer; 1.425 + private Object innerPeer; 1.426 + 1.427 + 1.428 + private Element(NamespaceContextImpl context,Element prev) { 1.429 + this.context = context; 1.430 + this.prev = prev; 1.431 + this.depth = (prev==null) ? 0 : prev.depth+1; 1.432 + } 1.433 + 1.434 + /** 1.435 + * Returns true if this {@link Element} represents the root element that 1.436 + * we are marshalling. 1.437 + */ 1.438 + public boolean isRootElement() { 1.439 + return depth==1; 1.440 + } 1.441 + 1.442 + public Element push() { 1.443 + if(next==null) 1.444 + next = new Element(context,this); 1.445 + next.onPushed(); 1.446 + return next; 1.447 + } 1.448 + 1.449 + public Element pop() { 1.450 + if(oldDefaultNamespaceUriIndex>=0) { 1.451 + // restore the old default namespace URI binding 1.452 + context.owner.knownUri2prefixIndexMap[oldDefaultNamespaceUriIndex] = defaultPrefixIndex; 1.453 + } 1.454 + context.size = baseIndex; 1.455 + context.current = prev; 1.456 + // release references to user objects 1.457 + outerPeer = innerPeer = null; 1.458 + return prev; 1.459 + } 1.460 + 1.461 + private void onPushed() { 1.462 + oldDefaultNamespaceUriIndex = defaultPrefixIndex = -1; 1.463 + baseIndex = context.size; 1.464 + context.current = this; 1.465 + } 1.466 + 1.467 + public void setTagName( int prefix, String localName, Object outerPeer ) { 1.468 + assert localName!=null; 1.469 + this.elementNamePrefix = prefix; 1.470 + this.elementLocalName = localName; 1.471 + this.elementName = null; 1.472 + this.outerPeer = outerPeer; 1.473 + } 1.474 + 1.475 + public void setTagName( Name tagName, Object outerPeer ) { 1.476 + assert tagName!=null; 1.477 + this.elementName = tagName; 1.478 + this.outerPeer = outerPeer; 1.479 + } 1.480 + 1.481 + public void startElement(XmlOutput out, Object innerPeer) throws IOException, XMLStreamException { 1.482 + this.innerPeer = innerPeer; 1.483 + if(elementName!=null) { 1.484 + out.beginStartTag(elementName); 1.485 + } else { 1.486 + out.beginStartTag(elementNamePrefix,elementLocalName); 1.487 + } 1.488 + } 1.489 + 1.490 + public void endElement(XmlOutput out) throws IOException, SAXException, XMLStreamException { 1.491 + if(elementName!=null) { 1.492 + out.endTag(elementName); 1.493 + elementName = null; 1.494 + } else { 1.495 + out.endTag(elementNamePrefix,elementLocalName); 1.496 + } 1.497 + } 1.498 + 1.499 + /** 1.500 + * Gets the number of bindings declared on this element. 1.501 + */ 1.502 + public final int count() { 1.503 + return context.size-baseIndex; 1.504 + } 1.505 + 1.506 + /** 1.507 + * Gets the prefix declared in this context. 1.508 + * 1.509 + * @param idx 1.510 + * between 0 and {@link #count()} 1.511 + */ 1.512 + public final String getPrefix(int idx) { 1.513 + return context.prefixes[baseIndex+idx]; 1.514 + } 1.515 + 1.516 + /** 1.517 + * Gets the namespace URI declared in this context. 1.518 + * 1.519 + * @param idx 1.520 + * between 0 and {@link #count()} 1.521 + */ 1.522 + public final String getNsUri(int idx) { 1.523 + return context.nsUris[baseIndex+idx]; 1.524 + } 1.525 + 1.526 + public int getBase() { 1.527 + return baseIndex; 1.528 + } 1.529 + 1.530 + public Object getOuterPeer() { 1.531 + return outerPeer; 1.532 + } 1.533 + 1.534 + public Object getInnerPeer() { 1.535 + return innerPeer; 1.536 + } 1.537 + 1.538 + /** 1.539 + * Gets the parent {@link Element}. 1.540 + */ 1.541 + public Element getParent() { 1.542 + return prev; 1.543 + } 1.544 + } 1.545 + 1.546 + 1.547 + /** 1.548 + * Default {@link NamespacePrefixMapper} implementation used when 1.549 + * it is not specified by the user. 1.550 + */ 1.551 + private static final NamespacePrefixMapper defaultNamespacePrefixMapper = new NamespacePrefixMapper() { 1.552 + public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 1.553 + if( namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE) ) 1.554 + return "xsi"; 1.555 + if( namespaceUri.equals(WellKnownNamespace.XML_SCHEMA) ) 1.556 + return "xs"; 1.557 + if( namespaceUri.equals(WellKnownNamespace.XML_MIME_URI) ) 1.558 + return "xmime"; 1.559 + return suggestion; 1.560 + } 1.561 + }; 1.562 +}