src/share/jaxws_classes/com/sun/xml/internal/txw2/StartTag.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 javax.xml.namespace.QName;
aoqi@0 29
aoqi@0 30
aoqi@0 31 /**
aoqi@0 32 * Start tag.
aoqi@0 33 *
aoqi@0 34 * <p>
aoqi@0 35 * This object implements {@link NamespaceResolver} for attribute values.
aoqi@0 36 *
aoqi@0 37 * @author Kohsuke Kawaguchi
aoqi@0 38 */
aoqi@0 39 class StartTag extends Content implements NamespaceResolver {
aoqi@0 40 /**
aoqi@0 41 * Tag name of the element.
aoqi@0 42 *
aoqi@0 43 * <p>
aoqi@0 44 * This field is also used as a flag to indicate
aoqi@0 45 * whether the start tag has been written.
aoqi@0 46 * This field is initially set to non-null, and
aoqi@0 47 * then reset to null when it's written.
aoqi@0 48 */
aoqi@0 49 private String uri;
aoqi@0 50 // but we keep the local name non-null so that
aoqi@0 51 // we can diagnose an error
aoqi@0 52 private final String localName;
aoqi@0 53
aoqi@0 54 private Attribute firstAtt;
aoqi@0 55 private Attribute lastAtt;
aoqi@0 56
aoqi@0 57 /**
aoqi@0 58 * If this {@link StartTag} has the parent {@link ContainerElement},
aoqi@0 59 * that value. Otherwise null.
aoqi@0 60 */
aoqi@0 61 private ContainerElement owner;
aoqi@0 62
aoqi@0 63 /**
aoqi@0 64 * Explicitly given namespace declarations on this element.
aoqi@0 65 *
aoqi@0 66 * <p>
aoqi@0 67 * Additional namespace declarations might be necessary to
aoqi@0 68 * generate child {@link QName}s and attributes.
aoqi@0 69 */
aoqi@0 70 private NamespaceDecl firstNs;
aoqi@0 71 private NamespaceDecl lastNs;
aoqi@0 72
aoqi@0 73 final Document document;
aoqi@0 74
aoqi@0 75 public StartTag(ContainerElement owner, String uri, String localName) {
aoqi@0 76 this(owner.document,uri,localName);
aoqi@0 77 this.owner = owner;
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 public StartTag(Document document, String uri, String localName) {
aoqi@0 81 assert uri!=null;
aoqi@0 82 assert localName!=null;
aoqi@0 83
aoqi@0 84 this.uri = uri;
aoqi@0 85 this.localName = localName;
aoqi@0 86 this.document = document;
aoqi@0 87
aoqi@0 88 // TODO: think about a better way to maintain namespace decls.
aoqi@0 89 // this requires at least one NamespaceDecl per start tag,
aoqi@0 90 // which is rather expensive.
aoqi@0 91 addNamespaceDecl(uri,null,false);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 public void addAttribute(String nsUri, String localName, Object arg) {
aoqi@0 95 checkWritable();
aoqi@0 96
aoqi@0 97 // look for the existing ones
aoqi@0 98 Attribute a;
aoqi@0 99 for(a=firstAtt; a!=null; a=a.next) {
aoqi@0 100 if(a.hasName(nsUri,localName)) {
aoqi@0 101 break;
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 // if not found, declare a new one
aoqi@0 106 if(a==null) {
aoqi@0 107 a = new Attribute(nsUri,localName);
aoqi@0 108 if(lastAtt==null) {
aoqi@0 109 assert firstAtt==null;
aoqi@0 110 firstAtt = lastAtt = a;
aoqi@0 111 } else {
aoqi@0 112 assert firstAtt!=null;
aoqi@0 113 lastAtt.next = a;
aoqi@0 114 lastAtt = a;
aoqi@0 115 }
aoqi@0 116 if(nsUri.length()>0)
aoqi@0 117 addNamespaceDecl(nsUri,null,true);
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 document.writeValue(arg,this,a.value);
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 /**
aoqi@0 124 * Declares a new namespace URI on this tag.
aoqi@0 125 *
aoqi@0 126 * @param uri
aoqi@0 127 * namespace URI to be bound. Can be empty, but must not be null.
aoqi@0 128 * @param prefix
aoqi@0 129 * If non-null and non-empty, this prefix is bound to the URI
aoqi@0 130 * on this element. If empty, then the runtime will still try to
aoqi@0 131 * use the URI as the default namespace, but it may fail to do so
aoqi@0 132 * because of the constraints in the XML.
aoqi@0 133 * <p>
aoqi@0 134 * If this parameter is null, the runtime will allocate an unique prefix.
aoqi@0 135 * @param requirePrefix
aoqi@0 136 * Used only when the prefix parameter is null. If true, this indicates
aoqi@0 137 * that the non-empty prefix must be assigned to this URI. If false,
aoqi@0 138 * then this URI might be used as the default namespace.
aoqi@0 139 * <p>
aoqi@0 140 * Normally you just need to set it to false.
aoqi@0 141 */
aoqi@0 142 public NamespaceDecl addNamespaceDecl(String uri, String prefix,boolean requirePrefix) {
aoqi@0 143 checkWritable();
aoqi@0 144
aoqi@0 145 if(uri==null)
aoqi@0 146 throw new IllegalArgumentException();
aoqi@0 147 if(uri.length()==0) {
aoqi@0 148 if(requirePrefix)
aoqi@0 149 throw new IllegalArgumentException("The empty namespace cannot have a non-empty prefix");
aoqi@0 150 if(prefix!=null && prefix.length()>0)
aoqi@0 151 throw new IllegalArgumentException("The empty namespace can be only bound to the empty prefix");
aoqi@0 152 prefix = "";
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 // check for the duplicate
aoqi@0 156 for(NamespaceDecl n=firstNs; n!=null; n=n.next) {
aoqi@0 157 if(uri.equals(n.uri)) {
aoqi@0 158 if(prefix==null) {
aoqi@0 159 // reuse this binding
aoqi@0 160 n.requirePrefix |= requirePrefix;
aoqi@0 161 return n;
aoqi@0 162 }
aoqi@0 163 if(n.prefix==null) {
aoqi@0 164 // reuse this binding
aoqi@0 165 n.prefix = prefix;
aoqi@0 166 n.requirePrefix |= requirePrefix;
aoqi@0 167 return n;
aoqi@0 168 }
aoqi@0 169 if(prefix.equals(n.prefix)) {
aoqi@0 170 // reuse this binding
aoqi@0 171 n.requirePrefix |= requirePrefix;
aoqi@0 172 return n;
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175 if(prefix!=null && n.prefix!=null && n.prefix.equals(prefix))
aoqi@0 176 throw new IllegalArgumentException(
aoqi@0 177 "Prefix '"+prefix+"' is already bound to '"+n.uri+'\'');
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 NamespaceDecl ns = new NamespaceDecl(document.assignNewId(),uri,prefix,requirePrefix);
aoqi@0 181 if(lastNs==null) {
aoqi@0 182 assert firstNs==null;
aoqi@0 183 firstNs = lastNs = ns;
aoqi@0 184 } else {
aoqi@0 185 assert firstNs!=null;
aoqi@0 186 lastNs.next = ns;
aoqi@0 187 lastNs = ns;
aoqi@0 188 }
aoqi@0 189 return ns;
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 /**
aoqi@0 193 * Throws an error if the start tag has already been committed.
aoqi@0 194 */
aoqi@0 195 private void checkWritable() {
aoqi@0 196 if(isWritten())
aoqi@0 197 throw new IllegalStateException(
aoqi@0 198 "The start tag of "+this.localName+" has already been written. " +
aoqi@0 199 "If you need out of order writing, see the TypedXmlWriter.block method");
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 /**
aoqi@0 203 * Returns true if this start tag has already been written.
aoqi@0 204 */
aoqi@0 205 boolean isWritten() {
aoqi@0 206 return uri==null;
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 /**
aoqi@0 210 * A {@link StartTag} can be only written after
aoqi@0 211 * we are sure that all the necessary namespace declarations are given.
aoqi@0 212 */
aoqi@0 213 boolean isReadyToCommit() {
aoqi@0 214 if(owner!=null && owner.isBlocked())
aoqi@0 215 return false;
aoqi@0 216
aoqi@0 217 for( Content c=getNext(); c!=null; c=c.getNext() )
aoqi@0 218 if(c.concludesPendingStartTag())
aoqi@0 219 return true;
aoqi@0 220
aoqi@0 221 return false;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 public void written() {
aoqi@0 225 firstAtt = lastAtt = null;
aoqi@0 226 uri = null;
aoqi@0 227 if(owner!=null) {
aoqi@0 228 assert owner.startTag==this;
aoqi@0 229 owner.startTag = null;
aoqi@0 230 }
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 boolean concludesPendingStartTag() {
aoqi@0 234 return true;
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 void accept(ContentVisitor visitor) {
aoqi@0 238 visitor.onStartTag(uri,localName,firstAtt,firstNs);
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 public String getPrefix(String nsUri) {
aoqi@0 242 NamespaceDecl ns = addNamespaceDecl(nsUri,null,false);
aoqi@0 243 if(ns.prefix!=null)
aoqi@0 244 // if the prefix has already been declared, use it.
aoqi@0 245 return ns.prefix;
aoqi@0 246 return ns.dummyPrefix;
aoqi@0 247 }
aoqi@0 248 }

mercurial