src/share/jaxws_classes/com/sun/xml/internal/txw2/Document.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.txw2;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.txw2.output.XmlSerializer;
aoqi@0 29
aoqi@0 30 import java.util.Map;
aoqi@0 31 import java.util.HashMap;
aoqi@0 32
aoqi@0 33 /**
aoqi@0 34 * Coordinates the entire writing process.
aoqi@0 35 *
aoqi@0 36 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 37 */
aoqi@0 38 public final class Document {
aoqi@0 39
aoqi@0 40 private final XmlSerializer out;
aoqi@0 41
aoqi@0 42 /**
aoqi@0 43 * Set to true once we invoke {@link XmlSerializer#startDocument()}.
aoqi@0 44 *
aoqi@0 45 * <p>
aoqi@0 46 * This is so that we can defer the writing as much as possible.
aoqi@0 47 */
aoqi@0 48 private boolean started=false;
aoqi@0 49
aoqi@0 50 /**
aoqi@0 51 * Currently active writer.
aoqi@0 52 *
aoqi@0 53 * <p>
aoqi@0 54 * This points to the last written token.
aoqi@0 55 */
aoqi@0 56 private Content current = null;
aoqi@0 57
aoqi@0 58 private final Map<Class,DatatypeWriter> datatypeWriters = new HashMap<Class,DatatypeWriter>();
aoqi@0 59
aoqi@0 60 /**
aoqi@0 61 * Used to generate unique namespace prefix.
aoqi@0 62 */
aoqi@0 63 private int iota = 1;
aoqi@0 64
aoqi@0 65 /**
aoqi@0 66 * Used to keep track of in-scope namespace bindings declared in ancestors.
aoqi@0 67 */
aoqi@0 68 private final NamespaceSupport inscopeNamespace = new NamespaceSupport();
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * Remembers the namespace declarations of the last unclosed start tag,
aoqi@0 72 * so that we can fix up dummy prefixes in {@link Pcdata}.
aoqi@0 73 */
aoqi@0 74 private NamespaceDecl activeNamespaces;
aoqi@0 75
aoqi@0 76
aoqi@0 77 Document(XmlSerializer out) {
aoqi@0 78 this.out = out;
aoqi@0 79 for( DatatypeWriter dw : DatatypeWriter.BUILTIN )
aoqi@0 80 datatypeWriters.put(dw.getType(),dw);
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 void flush() {
aoqi@0 84 out.flush();
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 void setFirstContent(Content c) {
aoqi@0 88 assert current==null;
aoqi@0 89 current = new StartDocument();
aoqi@0 90 current.setNext(this,c);
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * Defines additional user object -> string conversion logic.
aoqi@0 95 *
aoqi@0 96 * <p>
aoqi@0 97 * Applications can add their own {@link DatatypeWriter} so that
aoqi@0 98 * application-specific objects can be turned into {@link String}
aoqi@0 99 * for output.
aoqi@0 100 *
aoqi@0 101 * @param dw
aoqi@0 102 * The {@link DatatypeWriter} to be added. Must not be null.
aoqi@0 103 */
aoqi@0 104 public void addDatatypeWriter( DatatypeWriter<?> dw ) {
aoqi@0 105 datatypeWriters.put(dw.getType(),dw);
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * Performs the output as much as possible
aoqi@0 110 */
aoqi@0 111 void run() {
aoqi@0 112 while(true) {
aoqi@0 113 Content next = current.getNext();
aoqi@0 114 if(next==null || !next.isReadyToCommit())
aoqi@0 115 return;
aoqi@0 116 next.accept(visitor);
aoqi@0 117 next.written();
aoqi@0 118 current = next;
aoqi@0 119 }
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 /**
aoqi@0 123 * Appends the given object to the end of the given buffer.
aoqi@0 124 *
aoqi@0 125 * @param nsResolver
aoqi@0 126 * use
aoqi@0 127 */
aoqi@0 128 void writeValue( Object obj, NamespaceResolver nsResolver, StringBuilder buf ) {
aoqi@0 129 if(obj==null)
aoqi@0 130 throw new IllegalArgumentException("argument contains null");
aoqi@0 131
aoqi@0 132 if(obj instanceof Object[]) {
aoqi@0 133 for( Object o : (Object[])obj )
aoqi@0 134 writeValue(o,nsResolver,buf);
aoqi@0 135 return;
aoqi@0 136 }
aoqi@0 137 if(obj instanceof Iterable) {
aoqi@0 138 for( Object o : (Iterable<?>)obj )
aoqi@0 139 writeValue(o,nsResolver,buf);
aoqi@0 140 return;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 if(buf.length()>0)
aoqi@0 144 buf.append(' ');
aoqi@0 145
aoqi@0 146 Class c = obj.getClass();
aoqi@0 147 while(c!=null) {
aoqi@0 148 DatatypeWriter dw = datatypeWriters.get(c);
aoqi@0 149 if(dw!=null) {
aoqi@0 150 dw.print(obj,nsResolver,buf);
aoqi@0 151 return;
aoqi@0 152 }
aoqi@0 153 c = c.getSuperclass();
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 // if nothing applies, just use toString
aoqi@0 157 buf.append(obj);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 // I wanted to hide those write method from users
aoqi@0 161 private final ContentVisitor visitor = new ContentVisitor() {
aoqi@0 162 public void onStartDocument() {
aoqi@0 163 // the startDocument token is used as the sentry, so this method shall never
aoqi@0 164 // be called.
aoqi@0 165 // out.startDocument() is invoked when we write the start tag of the root element.
aoqi@0 166 throw new IllegalStateException();
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 public void onEndDocument() {
aoqi@0 170 out.endDocument();
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 public void onEndTag() {
aoqi@0 174 out.endTag();
aoqi@0 175 inscopeNamespace.popContext();
aoqi@0 176 activeNamespaces = null;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 public void onPcdata(StringBuilder buffer) {
aoqi@0 180 if(activeNamespaces!=null)
aoqi@0 181 buffer = fixPrefix(buffer);
aoqi@0 182 out.text(buffer);
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 public void onCdata(StringBuilder buffer) {
aoqi@0 186 if(activeNamespaces!=null)
aoqi@0 187 buffer = fixPrefix(buffer);
aoqi@0 188 out.cdata(buffer);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 public void onComment(StringBuilder buffer) {
aoqi@0 192 if(activeNamespaces!=null)
aoqi@0 193 buffer = fixPrefix(buffer);
aoqi@0 194 out.comment(buffer);
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 public void onStartTag(String nsUri, String localName, Attribute attributes, NamespaceDecl namespaces) {
aoqi@0 198 assert nsUri!=null;
aoqi@0 199 assert localName!=null;
aoqi@0 200
aoqi@0 201 activeNamespaces = namespaces;
aoqi@0 202
aoqi@0 203 if(!started) {
aoqi@0 204 started = true;
aoqi@0 205 out.startDocument();
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 inscopeNamespace.pushContext();
aoqi@0 209
aoqi@0 210 // declare the explicitly bound namespaces
aoqi@0 211 for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) {
aoqi@0 212 ns.declared = false; // reset this flag
aoqi@0 213
aoqi@0 214 if(ns.prefix!=null) {
aoqi@0 215 String uri = inscopeNamespace.getURI(ns.prefix);
aoqi@0 216 if(uri!=null && uri.equals(ns.uri))
aoqi@0 217 ; // already declared
aoqi@0 218 else {
aoqi@0 219 // declare this new binding
aoqi@0 220 inscopeNamespace.declarePrefix(ns.prefix,ns.uri);
aoqi@0 221 ns.declared = true;
aoqi@0 222 }
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 // then use in-scope namespace to assign prefixes to others
aoqi@0 227 for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) {
aoqi@0 228 if(ns.prefix==null) {
aoqi@0 229 if(inscopeNamespace.getURI("").equals(ns.uri))
aoqi@0 230 ns.prefix="";
aoqi@0 231 else {
aoqi@0 232 String p = inscopeNamespace.getPrefix(ns.uri);
aoqi@0 233 if(p==null) {
aoqi@0 234 // assign a new one
aoqi@0 235 while(inscopeNamespace.getURI(p=newPrefix())!=null)
aoqi@0 236 ;
aoqi@0 237 ns.declared = true;
aoqi@0 238 inscopeNamespace.declarePrefix(p,ns.uri);
aoqi@0 239 }
aoqi@0 240 ns.prefix = p;
aoqi@0 241 }
aoqi@0 242 }
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 // the first namespace decl must be the one for the element
aoqi@0 246 assert namespaces.uri.equals(nsUri);
aoqi@0 247 assert namespaces.prefix!=null : "a prefix must have been all allocated";
aoqi@0 248 out.beginStartTag(nsUri,localName,namespaces.prefix);
aoqi@0 249
aoqi@0 250 // declare namespaces
aoqi@0 251 for( NamespaceDecl ns=namespaces; ns!=null; ns=ns.next ) {
aoqi@0 252 if(ns.declared)
aoqi@0 253 out.writeXmlns( ns.prefix, ns.uri );
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 // writeBody attributes
aoqi@0 257 for( Attribute a=attributes; a!=null; a=a.next) {
aoqi@0 258 String prefix;
aoqi@0 259 if(a.nsUri.length()==0) prefix="";
aoqi@0 260 else prefix=inscopeNamespace.getPrefix(a.nsUri);
aoqi@0 261 out.writeAttribute( a.nsUri, a.localName, prefix, fixPrefix(a.value) );
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 out.endStartTag(nsUri,localName,namespaces.prefix);
aoqi@0 265 }
aoqi@0 266 };
aoqi@0 267
aoqi@0 268 /**
aoqi@0 269 * Used by {@link #newPrefix()}.
aoqi@0 270 */
aoqi@0 271 private final StringBuilder prefixSeed = new StringBuilder("ns");
aoqi@0 272
aoqi@0 273 private int prefixIota = 0;
aoqi@0 274
aoqi@0 275 /**
aoqi@0 276 * Allocates a new unique prefix.
aoqi@0 277 */
aoqi@0 278 private String newPrefix() {
aoqi@0 279 prefixSeed.setLength(2);
aoqi@0 280 prefixSeed.append(++prefixIota);
aoqi@0 281 return prefixSeed.toString();
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 /**
aoqi@0 285 * Replaces dummy prefixes in the value to the real ones
aoqi@0 286 * by using {@link #activeNamespaces}.
aoqi@0 287 *
aoqi@0 288 * @return
aoqi@0 289 * the buffer passed as the <tt>buf</tt> parameter.
aoqi@0 290 */
aoqi@0 291 private StringBuilder fixPrefix(StringBuilder buf) {
aoqi@0 292 assert activeNamespaces!=null;
aoqi@0 293
aoqi@0 294 int i;
aoqi@0 295 int len=buf.length();
aoqi@0 296 for(i=0;i<len;i++)
aoqi@0 297 if( buf.charAt(i)==MAGIC )
aoqi@0 298 break;
aoqi@0 299 // typically it doens't contain any prefix.
aoqi@0 300 // just return the original buffer in that case
aoqi@0 301 if(i==len)
aoqi@0 302 return buf;
aoqi@0 303
aoqi@0 304 while(i<len) {
aoqi@0 305 char uriIdx = buf.charAt(i+1);
aoqi@0 306 NamespaceDecl ns = activeNamespaces;
aoqi@0 307 while(ns!=null && ns.uniqueId!=uriIdx)
aoqi@0 308 ns=ns.next;
aoqi@0 309 if(ns==null)
aoqi@0 310 throw new IllegalStateException("Unexpected use of prefixes "+buf);
aoqi@0 311
aoqi@0 312 int length = 2;
aoqi@0 313 String prefix = ns.prefix;
aoqi@0 314 if(prefix.length()==0) {
aoqi@0 315 if(buf.length()<=i+2 || buf.charAt(i+2)!=':')
aoqi@0 316 throw new IllegalStateException("Unexpected use of prefixes "+buf);
aoqi@0 317 length=3;
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 buf.replace(i,i+length,prefix);
aoqi@0 321 len += prefix.length()-length;
aoqi@0 322
aoqi@0 323 while(i<len && buf.charAt(i)!=MAGIC)
aoqi@0 324 i++;
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 return buf;
aoqi@0 328 }
aoqi@0 329
aoqi@0 330 /**
aoqi@0 331 * The first char of the dummy prefix.
aoqi@0 332 */
aoqi@0 333 static final char MAGIC = '\u0000';
aoqi@0 334
aoqi@0 335 char assignNewId() {
aoqi@0 336 return (char)iota++;
aoqi@0 337 }
aoqi@0 338 }

mercurial