src/share/jaxws_classes/com/sun/xml/internal/txw2/ContainerElement.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.annotation.XmlAttribute;
aoqi@0 29 import com.sun.xml.internal.txw2.annotation.XmlElement;
aoqi@0 30 import com.sun.xml.internal.txw2.annotation.XmlNamespace;
aoqi@0 31 import com.sun.xml.internal.txw2.annotation.XmlValue;
aoqi@0 32 import com.sun.xml.internal.txw2.annotation.XmlCDATA;
aoqi@0 33
aoqi@0 34 import javax.xml.namespace.QName;
aoqi@0 35 import java.lang.reflect.InvocationHandler;
aoqi@0 36 import java.lang.reflect.InvocationTargetException;
aoqi@0 37 import java.lang.reflect.Method;
aoqi@0 38 import java.lang.reflect.Proxy;
aoqi@0 39
aoqi@0 40 /**
aoqi@0 41 * Dynamically implements {@link TypedXmlWriter} interfaces.
aoqi@0 42 *
aoqi@0 43 * @author Kohsuke Kawaguchi
aoqi@0 44 */
aoqi@0 45 final class ContainerElement implements InvocationHandler, TypedXmlWriter {
aoqi@0 46
aoqi@0 47 final Document document;
aoqi@0 48
aoqi@0 49 /**
aoqi@0 50 * Initially, point to the start tag token, but
aoqi@0 51 * once we know we are done with the start tag, we will reset it to null
aoqi@0 52 * so that the token sequence can be GC-ed.
aoqi@0 53 */
aoqi@0 54 StartTag startTag;
aoqi@0 55 final EndTag endTag = new EndTag();
aoqi@0 56
aoqi@0 57 /**
aoqi@0 58 * Namespace URI of this element.
aoqi@0 59 */
aoqi@0 60 private final String nsUri;
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * When this element can accept more child content, this value
aoqi@0 64 * is non-null and holds the last child {@link Content}.
aoqi@0 65 *
aoqi@0 66 * If this element is committed, this parameter is null.
aoqi@0 67 */
aoqi@0 68 private Content tail;
aoqi@0 69
aoqi@0 70 /**
aoqi@0 71 * Uncommitted {@link ContainerElement}s form a doubly-linked list,
aoqi@0 72 * so that the parent can close them recursively.
aoqi@0 73 */
aoqi@0 74 private ContainerElement prevOpen;
aoqi@0 75 private ContainerElement nextOpen;
aoqi@0 76 private final ContainerElement parent;
aoqi@0 77 private ContainerElement lastOpenChild;
aoqi@0 78
aoqi@0 79 /**
aoqi@0 80 * Set to true if the start eleent is blocked.
aoqi@0 81 */
aoqi@0 82 private boolean blocked;
aoqi@0 83
aoqi@0 84 public ContainerElement(Document document,ContainerElement parent,String nsUri, String localName) {
aoqi@0 85 this.parent = parent;
aoqi@0 86 this.document = document;
aoqi@0 87 this.nsUri = nsUri;
aoqi@0 88 this.startTag = new StartTag(this,nsUri,localName);
aoqi@0 89 tail = startTag;
aoqi@0 90
aoqi@0 91 if(isRoot())
aoqi@0 92 document.setFirstContent(startTag);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 private boolean isRoot() {
aoqi@0 96 return parent==null;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 private boolean isCommitted() {
aoqi@0 100 return tail==null;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public Document getDocument() {
aoqi@0 104 return document;
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 boolean isBlocked() {
aoqi@0 108 return blocked && !isCommitted();
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 public void block() {
aoqi@0 112 blocked = true;
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
aoqi@0 116 if(method.getDeclaringClass()==TypedXmlWriter.class || method.getDeclaringClass()==Object.class) {
aoqi@0 117 // forward to myself
aoqi@0 118 try {
aoqi@0 119 return method.invoke(this,args);
aoqi@0 120 } catch (InvocationTargetException e) {
aoqi@0 121 throw e.getTargetException();
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 XmlAttribute xa = method.getAnnotation(XmlAttribute.class);
aoqi@0 126 XmlValue xv = method.getAnnotation(XmlValue.class);
aoqi@0 127 XmlElement xe = method.getAnnotation(XmlElement.class);
aoqi@0 128
aoqi@0 129
aoqi@0 130 if(xa!=null) {
aoqi@0 131 if(xv!=null || xe!=null)
aoqi@0 132 throw new IllegalAnnotationException(method.toString());
aoqi@0 133
aoqi@0 134 addAttribute(xa,method,args);
aoqi@0 135 return proxy; // allow method chaining
aoqi@0 136 }
aoqi@0 137 if(xv!=null) {
aoqi@0 138 if(xe!=null)
aoqi@0 139 throw new IllegalAnnotationException(method.toString());
aoqi@0 140
aoqi@0 141 _pcdata(args);
aoqi@0 142 return proxy; // allow method chaining
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 return addElement(xe,method,args);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 /**
aoqi@0 149 * Writes an attribute.
aoqi@0 150 */
aoqi@0 151 private void addAttribute(XmlAttribute xa, Method method, Object[] args) {
aoqi@0 152 assert xa!=null;
aoqi@0 153
aoqi@0 154 checkStartTag();
aoqi@0 155
aoqi@0 156 String localName = xa.value();
aoqi@0 157 if(xa.value().length()==0)
aoqi@0 158 localName = method.getName();
aoqi@0 159
aoqi@0 160 _attribute(xa.ns(),localName,args);
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 private void checkStartTag() {
aoqi@0 164 if(startTag==null)
aoqi@0 165 throw new IllegalStateException("start tag has already been written");
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 /**
aoqi@0 169 * Writes a new element.
aoqi@0 170 */
aoqi@0 171 private Object addElement(XmlElement e, Method method, Object[] args) {
aoqi@0 172 Class<?> rt = method.getReturnType();
aoqi@0 173
aoqi@0 174 // the last precedence: default name
aoqi@0 175 String nsUri = "##default";
aoqi@0 176 String localName = method.getName();
aoqi@0 177
aoqi@0 178 if(e!=null) {
aoqi@0 179 // then the annotation on this method
aoqi@0 180 if(e.value().length()!=0)
aoqi@0 181 localName = e.value();
aoqi@0 182 nsUri = e.ns();
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 if(nsUri.equals("##default")) {
aoqi@0 186 // look for the annotation on the declaring class
aoqi@0 187 Class<?> c = method.getDeclaringClass();
aoqi@0 188 XmlElement ce = c.getAnnotation(XmlElement.class);
aoqi@0 189 if(ce!=null) {
aoqi@0 190 nsUri = ce.ns();
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 if(nsUri.equals("##default"))
aoqi@0 194 // then default to the XmlNamespace
aoqi@0 195 nsUri = getNamespace(c.getPackage());
aoqi@0 196 }
aoqi@0 197
aoqi@0 198
aoqi@0 199
aoqi@0 200 if(rt==Void.TYPE) {
aoqi@0 201 // leaf element with just a value
aoqi@0 202
aoqi@0 203 boolean isCDATA = method.getAnnotation(XmlCDATA.class)!=null;
aoqi@0 204
aoqi@0 205 StartTag st = new StartTag(document,nsUri,localName);
aoqi@0 206 addChild(st);
aoqi@0 207 for( Object arg : args ) {
aoqi@0 208 Text text;
aoqi@0 209 if(isCDATA) text = new Cdata(document,st,arg);
aoqi@0 210 else text = new Pcdata(document,st,arg);
aoqi@0 211 addChild(text);
aoqi@0 212 }
aoqi@0 213 addChild(new EndTag());
aoqi@0 214 return null;
aoqi@0 215 }
aoqi@0 216 if(TypedXmlWriter.class.isAssignableFrom(rt)) {
aoqi@0 217 // sub writer
aoqi@0 218 return _element(nsUri,localName,(Class)rt);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 throw new IllegalSignatureException("Illegal return type: "+rt);
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 /**
aoqi@0 225 * Decides the namespace URI of the given package.
aoqi@0 226 */
aoqi@0 227 private String getNamespace(Package pkg) {
aoqi@0 228 if(pkg==null) return "";
aoqi@0 229
aoqi@0 230 String nsUri;
aoqi@0 231 XmlNamespace ns = pkg.getAnnotation(XmlNamespace.class);
aoqi@0 232 if(ns!=null)
aoqi@0 233 nsUri = ns.value();
aoqi@0 234 else
aoqi@0 235 nsUri = "";
aoqi@0 236 return nsUri;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 /**
aoqi@0 240 * Appends this child object to the tail.
aoqi@0 241 */
aoqi@0 242 private void addChild(Content child) {
aoqi@0 243 tail.setNext(document,child);
aoqi@0 244 tail = child;
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 public void commit() {
aoqi@0 248 commit(true);
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 public void commit(boolean includingAllPredecessors) {
aoqi@0 252 _commit(includingAllPredecessors);
aoqi@0 253 document.flush();
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 private void _commit(boolean includingAllPredecessors) {
aoqi@0 257 if(isCommitted()) return;
aoqi@0 258
aoqi@0 259 addChild(endTag);
aoqi@0 260 if(isRoot())
aoqi@0 261 addChild(new EndDocument());
aoqi@0 262 tail = null;
aoqi@0 263
aoqi@0 264 // _commit predecessors if so told
aoqi@0 265 if(includingAllPredecessors) {
aoqi@0 266 for( ContainerElement e=this; e!=null; e=e.parent ) {
aoqi@0 267 while(e.prevOpen!=null) {
aoqi@0 268 e.prevOpen._commit(false);
aoqi@0 269 // e.prevOpen should change as a result of committing it.
aoqi@0 270 }
aoqi@0 271 }
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 // _commit all children recursively
aoqi@0 275 while(lastOpenChild!=null)
aoqi@0 276 lastOpenChild._commit(false);
aoqi@0 277
aoqi@0 278 // remove this node from the link
aoqi@0 279 if(parent!=null) {
aoqi@0 280 if(parent.lastOpenChild==this) {
aoqi@0 281 assert nextOpen==null : "this must be the last one";
aoqi@0 282 parent.lastOpenChild = prevOpen;
aoqi@0 283 } else {
aoqi@0 284 assert nextOpen.prevOpen==this;
aoqi@0 285 nextOpen.prevOpen = this.prevOpen;
aoqi@0 286 }
aoqi@0 287 if(prevOpen!=null) {
aoqi@0 288 assert prevOpen.nextOpen==this;
aoqi@0 289 prevOpen.nextOpen = this.nextOpen;
aoqi@0 290 }
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 this.nextOpen = null;
aoqi@0 294 this.prevOpen = null;
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 public void _attribute(String localName, Object value) {
aoqi@0 298 _attribute("",localName,value);
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 public void _attribute(String nsUri, String localName, Object value) {
aoqi@0 302 checkStartTag();
aoqi@0 303 startTag.addAttribute(nsUri,localName,value);
aoqi@0 304 }
aoqi@0 305
aoqi@0 306 public void _attribute(QName attributeName, Object value) {
aoqi@0 307 _attribute(attributeName.getNamespaceURI(),attributeName.getLocalPart(),value);
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 public void _namespace(String uri) {
aoqi@0 311 _namespace(uri,false);
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 public void _namespace(String uri, String prefix) {
aoqi@0 315 if(prefix==null)
aoqi@0 316 throw new IllegalArgumentException();
aoqi@0 317 checkStartTag();
aoqi@0 318 startTag.addNamespaceDecl(uri,prefix,false);
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 public void _namespace(String uri, boolean requirePrefix) {
aoqi@0 322 checkStartTag();
aoqi@0 323 startTag.addNamespaceDecl(uri,null,requirePrefix);
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 public void _pcdata(Object value) {
aoqi@0 327 // we need to allow this method even when startTag has already been completed.
aoqi@0 328 // checkStartTag();
aoqi@0 329 addChild(new Pcdata(document,startTag,value));
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 public void _cdata(Object value) {
aoqi@0 333 addChild(new Cdata(document,startTag,value));
aoqi@0 334 }
aoqi@0 335
aoqi@0 336 public void _comment(Object value) throws UnsupportedOperationException {
aoqi@0 337 addChild(new Comment(document,startTag,value));
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 public <T extends TypedXmlWriter> T _element(String localName, Class<T> contentModel) {
aoqi@0 341 return _element(nsUri,localName,contentModel);
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 public <T extends TypedXmlWriter> T _element(QName tagName, Class<T> contentModel) {
aoqi@0 345 return _element(tagName.getNamespaceURI(),tagName.getLocalPart(),contentModel);
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 public <T extends TypedXmlWriter> T _element(Class<T> contentModel) {
aoqi@0 349 return _element(TXW.getTagName(contentModel),contentModel);
aoqi@0 350 }
aoqi@0 351
aoqi@0 352 public <T extends TypedXmlWriter> T _cast(Class<T> facadeType) {
aoqi@0 353 return facadeType.cast(Proxy.newProxyInstance(facadeType.getClassLoader(),new Class[]{facadeType},this));
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 public <T extends TypedXmlWriter> T _element(String nsUri, String localName, Class<T> contentModel) {
aoqi@0 357 ContainerElement child = new ContainerElement(document,this,nsUri,localName);
aoqi@0 358 addChild(child.startTag);
aoqi@0 359 tail = child.endTag;
aoqi@0 360
aoqi@0 361 // update uncommitted link list
aoqi@0 362 if(lastOpenChild!=null) {
aoqi@0 363 assert lastOpenChild.parent==this;
aoqi@0 364
aoqi@0 365 assert child.prevOpen==null;
aoqi@0 366 assert child.nextOpen==null;
aoqi@0 367 child.prevOpen = lastOpenChild;
aoqi@0 368 assert lastOpenChild.nextOpen==null;
aoqi@0 369 lastOpenChild.nextOpen = child;
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 this.lastOpenChild = child;
aoqi@0 373
aoqi@0 374 return child._cast(contentModel);
aoqi@0 375 }
aoqi@0 376 }

mercurial