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