1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/txw2/Document.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,338 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2010, 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.txw2; 1.30 + 1.31 +import com.sun.xml.internal.txw2.output.XmlSerializer; 1.32 + 1.33 +import java.util.Map; 1.34 +import java.util.HashMap; 1.35 + 1.36 +/** 1.37 + * Coordinates the entire writing process. 1.38 + * 1.39 + * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.40 + */ 1.41 +public final class Document { 1.42 + 1.43 + private final XmlSerializer out; 1.44 + 1.45 + /** 1.46 + * Set to true once we invoke {@link XmlSerializer#startDocument()}. 1.47 + * 1.48 + * <p> 1.49 + * This is so that we can defer the writing as much as possible. 1.50 + */ 1.51 + private boolean started=false; 1.52 + 1.53 + /** 1.54 + * Currently active writer. 1.55 + * 1.56 + * <p> 1.57 + * This points to the last written token. 1.58 + */ 1.59 + private Content current = null; 1.60 + 1.61 + private final Map<Class,DatatypeWriter> datatypeWriters = new HashMap<Class,DatatypeWriter>(); 1.62 + 1.63 + /** 1.64 + * Used to generate unique namespace prefix. 1.65 + */ 1.66 + private int iota = 1; 1.67 + 1.68 + /** 1.69 + * Used to keep track of in-scope namespace bindings declared in ancestors. 1.70 + */ 1.71 + private final NamespaceSupport inscopeNamespace = new NamespaceSupport(); 1.72 + 1.73 + /** 1.74 + * Remembers the namespace declarations of the last unclosed start tag, 1.75 + * so that we can fix up dummy prefixes in {@link Pcdata}. 1.76 + */ 1.77 + private NamespaceDecl activeNamespaces; 1.78 + 1.79 + 1.80 + Document(XmlSerializer out) { 1.81 + this.out = out; 1.82 + for( DatatypeWriter dw : DatatypeWriter.BUILTIN ) 1.83 + datatypeWriters.put(dw.getType(),dw); 1.84 + } 1.85 + 1.86 + void flush() { 1.87 + out.flush(); 1.88 + } 1.89 + 1.90 + void setFirstContent(Content c) { 1.91 + assert current==null; 1.92 + current = new StartDocument(); 1.93 + current.setNext(this,c); 1.94 + } 1.95 + 1.96 + /** 1.97 + * Defines additional user object -> string conversion logic. 1.98 + * 1.99 + * <p> 1.100 + * Applications can add their own {@link DatatypeWriter} so that 1.101 + * application-specific objects can be turned into {@link String} 1.102 + * for output. 1.103 + * 1.104 + * @param dw 1.105 + * The {@link DatatypeWriter} to be added. Must not be null. 1.106 + */ 1.107 + public void addDatatypeWriter( DatatypeWriter<?> dw ) { 1.108 + datatypeWriters.put(dw.getType(),dw); 1.109 + } 1.110 + 1.111 + /** 1.112 + * Performs the output as much as possible 1.113 + */ 1.114 + void run() { 1.115 + while(true) { 1.116 + Content next = current.getNext(); 1.117 + if(next==null || !next.isReadyToCommit()) 1.118 + return; 1.119 + next.accept(visitor); 1.120 + next.written(); 1.121 + current = next; 1.122 + } 1.123 + } 1.124 + 1.125 + /** 1.126 + * Appends the given object to the end of the given buffer. 1.127 + * 1.128 + * @param nsResolver 1.129 + * use 1.130 + */ 1.131 + void writeValue( Object obj, NamespaceResolver nsResolver, StringBuilder buf ) { 1.132 + if(obj==null) 1.133 + throw new IllegalArgumentException("argument contains null"); 1.134 + 1.135 + if(obj instanceof Object[]) { 1.136 + for( Object o : (Object[])obj ) 1.137 + writeValue(o,nsResolver,buf); 1.138 + return; 1.139 + } 1.140 + if(obj instanceof Iterable) { 1.141 + for( Object o : (Iterable<?>)obj ) 1.142 + writeValue(o,nsResolver,buf); 1.143 + return; 1.144 + } 1.145 + 1.146 + if(buf.length()>0) 1.147 + buf.append(' '); 1.148 + 1.149 + Class c = obj.getClass(); 1.150 + while(c!=null) { 1.151 + DatatypeWriter dw = datatypeWriters.get(c); 1.152 + if(dw!=null) { 1.153 + dw.print(obj,nsResolver,buf); 1.154 + return; 1.155 + } 1.156 + c = c.getSuperclass(); 1.157 + } 1.158 + 1.159 + // if nothing applies, just use toString 1.160 + buf.append(obj); 1.161 + } 1.162 + 1.163 + // I wanted to hide those write method from users 1.164 + private final ContentVisitor visitor = new ContentVisitor() { 1.165 + public void onStartDocument() { 1.166 + // the startDocument token is used as the sentry, so this method shall never 1.167 + // be called. 1.168 + // out.startDocument() is invoked when we write the start tag of the root element. 1.169 + throw new IllegalStateException(); 1.170 + } 1.171 + 1.172 + public void onEndDocument() { 1.173 + out.endDocument(); 1.174 + } 1.175 + 1.176 + public void onEndTag() { 1.177 + out.endTag(); 1.178 + inscopeNamespace.popContext(); 1.179 + activeNamespaces = null; 1.180 + } 1.181 + 1.182 + public void onPcdata(StringBuilder buffer) { 1.183 + if(activeNamespaces!=null) 1.184 + buffer = fixPrefix(buffer); 1.185 + out.text(buffer); 1.186 + } 1.187 + 1.188 + public void onCdata(StringBuilder buffer) { 1.189 + if(activeNamespaces!=null) 1.190 + buffer = fixPrefix(buffer); 1.191 + out.cdata(buffer); 1.192 + } 1.193 + 1.194 + public void onComment(StringBuilder buffer) { 1.195 + if(activeNamespaces!=null) 1.196 + buffer = fixPrefix(buffer); 1.197 + out.comment(buffer); 1.198 + } 1.199 + 1.200 + public void onStartTag(String nsUri, String localName, Attribute attributes, NamespaceDecl namespaces) { 1.201 + assert nsUri!=null; 1.202 + assert localName!=null; 1.203 + 1.204 + activeNamespaces = namespaces; 1.205 + 1.206 + if(!started) { 1.207 + started = true; 1.208 + out.startDocument(); 1.209 + } 1.210 + 1.211 + inscopeNamespace.pushContext(); 1.212 + 1.213 + // declare the explicitly bound namespaces 1.214 + for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) { 1.215 + ns.declared = false; // reset this flag 1.216 + 1.217 + if(ns.prefix!=null) { 1.218 + String uri = inscopeNamespace.getURI(ns.prefix); 1.219 + if(uri!=null && uri.equals(ns.uri)) 1.220 + ; // already declared 1.221 + else { 1.222 + // declare this new binding 1.223 + inscopeNamespace.declarePrefix(ns.prefix,ns.uri); 1.224 + ns.declared = true; 1.225 + } 1.226 + } 1.227 + } 1.228 + 1.229 + // then use in-scope namespace to assign prefixes to others 1.230 + for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) { 1.231 + if(ns.prefix==null) { 1.232 + if(inscopeNamespace.getURI("").equals(ns.uri)) 1.233 + ns.prefix=""; 1.234 + else { 1.235 + String p = inscopeNamespace.getPrefix(ns.uri); 1.236 + if(p==null) { 1.237 + // assign a new one 1.238 + while(inscopeNamespace.getURI(p=newPrefix())!=null) 1.239 + ; 1.240 + ns.declared = true; 1.241 + inscopeNamespace.declarePrefix(p,ns.uri); 1.242 + } 1.243 + ns.prefix = p; 1.244 + } 1.245 + } 1.246 + } 1.247 + 1.248 + // the first namespace decl must be the one for the element 1.249 + assert namespaces.uri.equals(nsUri); 1.250 + assert namespaces.prefix!=null : "a prefix must have been all allocated"; 1.251 + out.beginStartTag(nsUri,localName,namespaces.prefix); 1.252 + 1.253 + // declare namespaces 1.254 + for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) { 1.255 + if(ns.declared) 1.256 + out.writeXmlns( ns.prefix, ns.uri ); 1.257 + } 1.258 + 1.259 + // writeBody attributes 1.260 + for( Attribute a=attributes; a!=null; a=a.next) { 1.261 + String prefix; 1.262 + if(a.nsUri.length()==0) prefix=""; 1.263 + else prefix=inscopeNamespace.getPrefix(a.nsUri); 1.264 + out.writeAttribute( a.nsUri, a.localName, prefix, fixPrefix(a.value) ); 1.265 + } 1.266 + 1.267 + out.endStartTag(nsUri,localName,namespaces.prefix); 1.268 + } 1.269 + }; 1.270 + 1.271 + /** 1.272 + * Used by {@link #newPrefix()}. 1.273 + */ 1.274 + private final StringBuilder prefixSeed = new StringBuilder("ns"); 1.275 + 1.276 + private int prefixIota = 0; 1.277 + 1.278 + /** 1.279 + * Allocates a new unique prefix. 1.280 + */ 1.281 + private String newPrefix() { 1.282 + prefixSeed.setLength(2); 1.283 + prefixSeed.append(++prefixIota); 1.284 + return prefixSeed.toString(); 1.285 + } 1.286 + 1.287 + /** 1.288 + * Replaces dummy prefixes in the value to the real ones 1.289 + * by using {@link #activeNamespaces}. 1.290 + * 1.291 + * @return 1.292 + * the buffer passed as the <tt>buf</tt> parameter. 1.293 + */ 1.294 + private StringBuilder fixPrefix(StringBuilder buf) { 1.295 + assert activeNamespaces!=null; 1.296 + 1.297 + int i; 1.298 + int len=buf.length(); 1.299 + for(i=0;i<len;i++) 1.300 + if( buf.charAt(i)==MAGIC ) 1.301 + break; 1.302 + // typically it doens't contain any prefix. 1.303 + // just return the original buffer in that case 1.304 + if(i==len) 1.305 + return buf; 1.306 + 1.307 + while(i<len) { 1.308 + char uriIdx = buf.charAt(i+1); 1.309 + NamespaceDecl ns = activeNamespaces; 1.310 + while(ns!=null && ns.uniqueId!=uriIdx) 1.311 + ns=ns.next; 1.312 + if(ns==null) 1.313 + throw new IllegalStateException("Unexpected use of prefixes "+buf); 1.314 + 1.315 + int length = 2; 1.316 + String prefix = ns.prefix; 1.317 + if(prefix.length()==0) { 1.318 + if(buf.length()<=i+2 || buf.charAt(i+2)!=':') 1.319 + throw new IllegalStateException("Unexpected use of prefixes "+buf); 1.320 + length=3; 1.321 + } 1.322 + 1.323 + buf.replace(i,i+length,prefix); 1.324 + len += prefix.length()-length; 1.325 + 1.326 + while(i<len && buf.charAt(i)!=MAGIC) 1.327 + i++; 1.328 + } 1.329 + 1.330 + return buf; 1.331 + } 1.332 + 1.333 + /** 1.334 + * The first char of the dummy prefix. 1.335 + */ 1.336 + static final char MAGIC = '\u0000'; 1.337 + 1.338 + char assignNewId() { 1.339 + return (char)iota++; 1.340 + } 1.341 +}