src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/BindInfo.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, 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.tools.internal.xjc.reader.xmlschema.bindinfo;
aoqi@0 27
aoqi@0 28 import java.io.FilterWriter;
aoqi@0 29 import java.io.IOException;
aoqi@0 30 import java.io.StringWriter;
aoqi@0 31 import java.io.Writer;
aoqi@0 32 import java.util.ArrayList;
aoqi@0 33 import java.util.Iterator;
aoqi@0 34 import java.util.List;
aoqi@0 35
aoqi@0 36 import javax.xml.bind.JAXBContext;
aoqi@0 37 import javax.xml.bind.JAXBException;
aoqi@0 38 import javax.xml.bind.Unmarshaller;
aoqi@0 39 import javax.xml.bind.annotation.XmlAnyElement;
aoqi@0 40 import javax.xml.bind.annotation.XmlElement;
aoqi@0 41 import javax.xml.bind.annotation.XmlMixed;
aoqi@0 42 import javax.xml.bind.annotation.XmlRootElement;
aoqi@0 43 import javax.xml.bind.annotation.XmlType;
aoqi@0 44 import javax.xml.transform.Transformer;
aoqi@0 45 import javax.xml.transform.TransformerException;
aoqi@0 46 import javax.xml.transform.dom.DOMSource;
aoqi@0 47 import javax.xml.transform.stream.StreamResult;
aoqi@0 48
aoqi@0 49 import com.sun.codemodel.internal.JDocComment;
aoqi@0 50 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
aoqi@0 51 import com.sun.tools.internal.xjc.SchemaCache;
aoqi@0 52 import com.sun.tools.internal.xjc.model.CCustomizations;
aoqi@0 53 import com.sun.tools.internal.xjc.model.CPluginCustomization;
aoqi@0 54 import com.sun.tools.internal.xjc.model.Model;
aoqi@0 55 import com.sun.tools.internal.xjc.reader.Ring;
aoqi@0 56 import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder;
aoqi@0 57 import com.sun.xml.internal.bind.annotation.XmlLocation;
aoqi@0 58 import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler;
aoqi@0 59 import com.sun.xml.internal.xsom.XSComponent;
aoqi@0 60
aoqi@0 61 import org.w3c.dom.Element;
aoqi@0 62 import org.xml.sax.Locator;
aoqi@0 63
aoqi@0 64 /**
aoqi@0 65 * Container for customization declarations.
aoqi@0 66 *
aoqi@0 67 * We use JAXB ourselves and parse this object from "xs:annotation".
aoqi@0 68 *
aoqi@0 69 * @author
aoqi@0 70 * Kohsuke Kawaguchi (kohsuke,kawaguchi@sun.com)
aoqi@0 71 */
aoqi@0 72 @XmlRootElement(namespace= WellKnownNamespace.XML_SCHEMA,name="annotation")
aoqi@0 73 @XmlType(namespace=WellKnownNamespace.XML_SCHEMA,name="foobar")
aoqi@0 74 public final class BindInfo implements Iterable<BIDeclaration> {
aoqi@0 75
aoqi@0 76 private BGMBuilder builder;
aoqi@0 77
aoqi@0 78 @XmlLocation
aoqi@0 79 private Locator location;
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * Documentation taken from &lt;xs:documentation>s.
aoqi@0 83 */
aoqi@0 84 @XmlElement(namespace=WellKnownNamespace.XML_SCHEMA)
aoqi@0 85 private Documentation documentation;
aoqi@0 86
aoqi@0 87 /**
aoqi@0 88 * Returns true if this {@link BindInfo} doesn't contain any useful
aoqi@0 89 * information.
aoqi@0 90 *
aoqi@0 91 * This flag is used to discard unused {@link BindInfo}s early to save memory footprint.
aoqi@0 92 */
aoqi@0 93 public boolean isPointless() {
aoqi@0 94 if(size()>0) return false;
aoqi@0 95 if(documentation!=null && !documentation.contents.isEmpty())
aoqi@0 96 return false;
aoqi@0 97
aoqi@0 98 return true;
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 private static final class Documentation {
aoqi@0 102 @XmlAnyElement
aoqi@0 103 @XmlMixed
aoqi@0 104 List<Object> contents = new ArrayList<Object>();
aoqi@0 105
aoqi@0 106 void addAll(Documentation rhs) {
aoqi@0 107 if(rhs==null) return;
aoqi@0 108
aoqi@0 109 if(contents==null)
aoqi@0 110 contents = new ArrayList<Object>();
aoqi@0 111 if(!contents.isEmpty())
aoqi@0 112 contents.add("\n\n");
aoqi@0 113 contents.addAll(rhs.contents);
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 /** list of individual declarations. */
aoqi@0 118 private final List<BIDeclaration> decls = new ArrayList<BIDeclaration>();
aoqi@0 119
aoqi@0 120 private static final class AppInfo {
aoqi@0 121 /**
aoqi@0 122 * Receives {@link BIDeclaration}s and other DOMs.
aoqi@0 123 */
aoqi@0 124 @XmlAnyElement(lax=true,value=DomHandlerEx.class)
aoqi@0 125 List<Object> contents = new ArrayList<Object>();
aoqi@0 126
aoqi@0 127 public void addTo(BindInfo bi) {
aoqi@0 128 if(contents==null) return;
aoqi@0 129
aoqi@0 130 for (Object o : contents) {
aoqi@0 131 if(o instanceof BIDeclaration)
aoqi@0 132 bi.addDecl((BIDeclaration)o);
aoqi@0 133 // this is really PITA! I can't get the source location
aoqi@0 134 if(o instanceof DomHandlerEx.DomAndLocation) {
aoqi@0 135 DomHandlerEx.DomAndLocation e = (DomHandlerEx.DomAndLocation)o;
aoqi@0 136 String nsUri = e.element.getNamespaceURI();
aoqi@0 137 if(nsUri==null || nsUri.equals("")
aoqi@0 138 || nsUri.equals(WellKnownNamespace.XML_SCHEMA))
aoqi@0 139 continue; // this is definitely not a customization
aoqi@0 140 bi.addDecl(new BIXPluginCustomization(e.element,e.loc));
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146
aoqi@0 147 // only used by JAXB
aoqi@0 148 @XmlElement(namespace=WellKnownNamespace.XML_SCHEMA)
aoqi@0 149 void setAppinfo(AppInfo aib) {
aoqi@0 150 aib.addTo(this);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153
aoqi@0 154
aoqi@0 155 /**
aoqi@0 156 * Gets the location of this annotation in the source file.
aoqi@0 157 *
aoqi@0 158 * @return
aoqi@0 159 * If the declarations are in fact specified in the source
aoqi@0 160 * code, a non-null valid object will be returned.
aoqi@0 161 * If this BindInfo is generated internally by XJC, then
aoqi@0 162 * null will be returned.
aoqi@0 163 */
aoqi@0 164 public Locator getSourceLocation() { return location; }
aoqi@0 165
aoqi@0 166
aoqi@0 167 private XSComponent owner;
aoqi@0 168 /**
aoqi@0 169 * Sets the owner schema component and a reference to BGMBuilder.
aoqi@0 170 * This method is called from the BGMBuilder before
aoqi@0 171 * any BIDeclaration inside it is used.
aoqi@0 172 */
aoqi@0 173 public void setOwner( BGMBuilder _builder, XSComponent _owner ) {
aoqi@0 174 this.owner = _owner;
aoqi@0 175 this.builder = _builder;
aoqi@0 176 for (BIDeclaration d : decls)
aoqi@0 177 d.onSetOwner();
aoqi@0 178 }
aoqi@0 179 public XSComponent getOwner() { return owner; }
aoqi@0 180
aoqi@0 181 /**
aoqi@0 182 * Back pointer to the BGMBuilder which is building
aoqi@0 183 * a BGM from schema components including this customization.
aoqi@0 184 */
aoqi@0 185 public BGMBuilder getBuilder() { return builder; }
aoqi@0 186
aoqi@0 187 /** Adds a new declaration. */
aoqi@0 188 public void addDecl( BIDeclaration decl ) {
aoqi@0 189 if(decl==null) throw new IllegalArgumentException();
aoqi@0 190 decl.setParent(this);
aoqi@0 191 decls.add(decl);
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 /**
aoqi@0 195 * Gets the first declaration with a given name, or null
aoqi@0 196 * if none is found.
aoqi@0 197 */
aoqi@0 198 public <T extends BIDeclaration>
aoqi@0 199 T get( Class<T> kind ) {
aoqi@0 200 for( BIDeclaration decl : decls ) {
aoqi@0 201 if( kind.isInstance(decl) )
aoqi@0 202 return kind.cast(decl);
aoqi@0 203 }
aoqi@0 204 return null; // not found
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 /**
aoqi@0 208 * Gets all the declarations
aoqi@0 209 */
aoqi@0 210 public BIDeclaration[] getDecls() {
aoqi@0 211 return decls.toArray(new BIDeclaration[decls.size()]);
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 /**
aoqi@0 215 * Gets the documentation parsed from &lt;xs:documentation>s.
aoqi@0 216 * The returned collection is to be added to {@link JDocComment#append(Object)}.
aoqi@0 217 * @return maybe null.
aoqi@0 218 */
aoqi@0 219 public String getDocumentation() {
aoqi@0 220 // TODO: FIXME: correctly turn individual items to String including DOM
aoqi@0 221 if(documentation==null || documentation.contents==null) return null;
aoqi@0 222
aoqi@0 223 StringBuilder buf = new StringBuilder();
aoqi@0 224 for (Object c : documentation.contents) {
aoqi@0 225 if(c instanceof String) {
aoqi@0 226 buf.append(c.toString());
aoqi@0 227 }
aoqi@0 228 if(c instanceof Element) {
aoqi@0 229 Transformer t = builder.getIdentityTransformer();
aoqi@0 230 StringWriter w = new StringWriter();
aoqi@0 231 try {
aoqi@0 232 Writer fw = new FilterWriter(w) {
aoqi@0 233 char[] buf = new char[1];
aoqi@0 234
aoqi@0 235 public void write(int c) throws IOException {
aoqi@0 236 buf[0] = (char)c;
aoqi@0 237 write(buf,0,1);
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 public void write(char[] cbuf, int off, int len) throws IOException {
aoqi@0 241 MinimumEscapeHandler.theInstance.escape(cbuf,off,len,false,out);
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 public void write(String str, int off, int len) throws IOException {
aoqi@0 245 write(str.toCharArray(),off,len);
aoqi@0 246 }
aoqi@0 247 };
aoqi@0 248 t.transform(new DOMSource((Element)c),new StreamResult(fw));
aoqi@0 249 } catch (TransformerException e) {
aoqi@0 250 throw new Error(e); // impossible
aoqi@0 251 }
aoqi@0 252 buf.append("\n<pre>\n");
aoqi@0 253 buf.append(w);
aoqi@0 254 buf.append("\n</pre>\n");
aoqi@0 255 }
aoqi@0 256 }
aoqi@0 257 return buf.toString();
aoqi@0 258 }
aoqi@0 259
aoqi@0 260 /**
aoqi@0 261 * Merges all the declarations inside the given BindInfo
aoqi@0 262 * to this BindInfo.
aoqi@0 263 */
aoqi@0 264 public void absorb( BindInfo bi ) {
aoqi@0 265 for( BIDeclaration d : bi )
aoqi@0 266 d.setParent(this);
aoqi@0 267 this.decls.addAll( bi.decls );
aoqi@0 268
aoqi@0 269 if(this.documentation==null)
aoqi@0 270 this.documentation = bi.documentation;
aoqi@0 271 else
aoqi@0 272 this.documentation.addAll(bi.documentation);
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 /** Gets the number of declarations. */
aoqi@0 276 public int size() { return decls.size(); }
aoqi@0 277
aoqi@0 278 public BIDeclaration get( int idx ) { return decls.get(idx); }
aoqi@0 279
aoqi@0 280 public Iterator<BIDeclaration> iterator() {
aoqi@0 281 return decls.iterator();
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 /**
aoqi@0 285 * Gets the list of {@link CPluginCustomization}s from this.
aoqi@0 286 *
aoqi@0 287 * <p>
aoqi@0 288 * Note that calling this method marks all those plug-in customizations
aoqi@0 289 * as 'used'. So call it only when it's really necessary.
aoqi@0 290 */
aoqi@0 291 public CCustomizations toCustomizationList() {
aoqi@0 292 CCustomizations r=null;
aoqi@0 293 for( BIDeclaration d : this ) {
aoqi@0 294 if(d instanceof BIXPluginCustomization) {
aoqi@0 295 BIXPluginCustomization pc = (BIXPluginCustomization) d;
aoqi@0 296 pc.markAsAcknowledged();
aoqi@0 297 if(!Ring.get(Model.class).options.pluginURIs.contains(pc.getName().getNamespaceURI()))
aoqi@0 298 continue; // this isn't a plugin customization
aoqi@0 299 if(r==null)
aoqi@0 300 r = new CCustomizations();
aoqi@0 301 r.add(new CPluginCustomization(pc.element,pc.getLocation()));
aoqi@0 302 }
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 if(r==null) r = CCustomizations.EMPTY;
aoqi@0 306 return new CCustomizations(r);
aoqi@0 307 }
aoqi@0 308 /** An instance with the empty contents. */
aoqi@0 309 public final static BindInfo empty = new BindInfo();
aoqi@0 310
aoqi@0 311 /**
aoqi@0 312 * Lazily prepared {@link JAXBContext}.
aoqi@0 313 */
aoqi@0 314 private static volatile JAXBContext customizationContext;
aoqi@0 315
aoqi@0 316 public static JAXBContext getCustomizationContext() {
aoqi@0 317 try {
aoqi@0 318 if (customizationContext == null) {
aoqi@0 319 synchronized (BindInfo.class) {
aoqi@0 320 if (customizationContext == null) {
aoqi@0 321 customizationContext = JAXBContext.newInstance(
aoqi@0 322 BindInfo.class, // for xs:annotation
aoqi@0 323 BIClass.class,
aoqi@0 324 BIConversion.User.class,
aoqi@0 325 BIConversion.UserAdapter.class,
aoqi@0 326 BIDom.class,
aoqi@0 327 BIFactoryMethod.class,
aoqi@0 328 BIInlineBinaryData.class,
aoqi@0 329 BIXDom.class,
aoqi@0 330 BIXSubstitutable.class,
aoqi@0 331 BIEnum.class,
aoqi@0 332 BIEnumMember.class,
aoqi@0 333 BIGlobalBinding.class,
aoqi@0 334 BIProperty.class,
aoqi@0 335 BISchemaBinding.class);
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338 }
aoqi@0 339 return customizationContext;
aoqi@0 340 } catch (JAXBException e) {
aoqi@0 341 throw new AssertionError(e);
aoqi@0 342 }
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 public static Unmarshaller getCustomizationUnmarshaller() {
aoqi@0 346 try {
aoqi@0 347 return getCustomizationContext().createUnmarshaller();
aoqi@0 348 } catch (JAXBException e) {
aoqi@0 349 throw new AssertionError(e);
aoqi@0 350 }
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 /**
aoqi@0 354 * Lazily parsed schema for the binding file.
aoqi@0 355 */
aoqi@0 356 public static final SchemaCache bindingFileSchema = new SchemaCache(BindInfo.class.getResource("binding.xsd"));
aoqi@0 357 }

mercurial