325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.reader.xmlschema.bindinfo;
325N/A
325N/Aimport java.io.FilterWriter;
325N/Aimport java.io.IOException;
325N/Aimport java.io.StringWriter;
325N/Aimport java.io.Writer;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Iterator;
325N/Aimport java.util.List;
325N/A
325N/Aimport javax.xml.bind.JAXBContext;
325N/Aimport javax.xml.bind.JAXBException;
325N/Aimport javax.xml.bind.annotation.XmlAnyElement;
325N/Aimport javax.xml.bind.annotation.XmlElement;
325N/Aimport javax.xml.bind.annotation.XmlMixed;
325N/Aimport javax.xml.bind.annotation.XmlRootElement;
325N/Aimport javax.xml.bind.annotation.XmlType;
325N/Aimport javax.xml.transform.Transformer;
325N/Aimport javax.xml.transform.TransformerException;
325N/Aimport javax.xml.transform.dom.DOMSource;
325N/Aimport javax.xml.transform.stream.StreamResult;
325N/A
325N/Aimport com.sun.codemodel.internal.JDocComment;
325N/Aimport com.sun.tools.internal.xjc.SchemaCache;
325N/Aimport com.sun.tools.internal.xjc.model.CCustomizations;
325N/Aimport com.sun.tools.internal.xjc.model.CPluginCustomization;
325N/Aimport com.sun.tools.internal.xjc.model.Model;
325N/Aimport com.sun.tools.internal.xjc.reader.Ring;
325N/Aimport com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder;
325N/Aimport com.sun.xml.internal.bind.annotation.XmlLocation;
325N/Aimport com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler;
325N/Aimport com.sun.xml.internal.bind.v2.WellKnownNamespace;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
325N/Aimport com.sun.xml.internal.xsom.XSComponent;
325N/A
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.xml.sax.Locator;
325N/A
325N/A/**
325N/A * Container for customization declarations.
325N/A *
325N/A * We use JAXB ourselves and parse this object from "xs:annotation".
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke,kawaguchi@sun.com)
325N/A */
325N/A@XmlRootElement(namespace=WellKnownNamespace.XML_SCHEMA,name="annotation")
325N/A@XmlType(namespace=WellKnownNamespace.XML_SCHEMA,name="foobar")
325N/Apublic final class BindInfo implements Iterable<BIDeclaration> {
325N/A
325N/A private BGMBuilder builder;
325N/A
325N/A @XmlLocation
325N/A private Locator location;
325N/A
325N/A /**
325N/A * Documentation taken from &lt;xs:documentation>s.
325N/A */
325N/A @XmlElement(namespace=WellKnownNamespace.XML_SCHEMA)
325N/A private Documentation documentation;
325N/A
325N/A /**
325N/A * Returns true if this {@link BindInfo} doesn't contain any useful
325N/A * information.
325N/A *
325N/A * This flag is used to discard unused {@link BindInfo}s early to save memory footprint.
325N/A */
325N/A public boolean isPointless() {
325N/A if(size()>0) return false;
325N/A if(documentation!=null && !documentation.contents.isEmpty())
325N/A return false;
325N/A
325N/A return true;
325N/A }
325N/A
325N/A private static final class Documentation {
325N/A @XmlAnyElement
325N/A @XmlMixed
325N/A List<Object> contents = new ArrayList<Object>();
325N/A
325N/A void addAll(Documentation rhs) {
325N/A if(rhs==null) return;
325N/A
325N/A if(contents==null)
325N/A contents = new ArrayList<Object>();
325N/A if(!contents.isEmpty())
325N/A contents.add("\n\n");
325N/A contents.addAll(rhs.contents);
325N/A }
325N/A }
325N/A
325N/A /** list of individual declarations. */
325N/A private final List<BIDeclaration> decls = new ArrayList<BIDeclaration>();
325N/A
325N/A private static final class AppInfo {
325N/A /**
325N/A * Receives {@link BIDeclaration}s and other DOMs.
325N/A */
325N/A @XmlAnyElement(lax=true,value=DomHandlerEx.class)
325N/A List<Object> contents = new ArrayList<Object>();
325N/A
325N/A public void addTo(BindInfo bi) {
325N/A if(contents==null) return;
325N/A
325N/A for (Object o : contents) {
325N/A if(o instanceof BIDeclaration)
325N/A bi.addDecl((BIDeclaration)o);
325N/A // this is really PITA! I can't get the source location
325N/A if(o instanceof DomHandlerEx.DomAndLocation) {
325N/A DomHandlerEx.DomAndLocation e = (DomHandlerEx.DomAndLocation)o;
325N/A String nsUri = e.element.getNamespaceURI();
325N/A if(nsUri==null || nsUri.equals("")
325N/A || nsUri.equals(WellKnownNamespace.XML_SCHEMA))
325N/A continue; // this is definitely not a customization
325N/A bi.addDecl(new BIXPluginCustomization(e.element,e.loc));
325N/A }
325N/A }
325N/A }
325N/A }
325N/A
325N/A
325N/A // only used by JAXB
325N/A @XmlElement(namespace=WellKnownNamespace.XML_SCHEMA)
325N/A void setAppinfo(AppInfo aib) {
325N/A aib.addTo(this);
325N/A }
325N/A
325N/A
325N/A
325N/A /**
325N/A * Gets the location of this annotation in the source file.
325N/A *
325N/A * @return
325N/A * If the declarations are in fact specified in the source
325N/A * code, a non-null valid object will be returned.
325N/A * If this BindInfo is generated internally by XJC, then
325N/A * null will be returned.
325N/A */
325N/A public Locator getSourceLocation() { return location; }
325N/A
325N/A
325N/A private XSComponent owner;
325N/A /**
325N/A * Sets the owner schema component and a reference to BGMBuilder.
325N/A * This method is called from the BGMBuilder before
325N/A * any BIDeclaration inside it is used.
325N/A */
325N/A public void setOwner( BGMBuilder _builder, XSComponent _owner ) {
325N/A this.owner = _owner;
325N/A this.builder = _builder;
325N/A for (BIDeclaration d : decls)
325N/A d.onSetOwner();
325N/A }
325N/A public XSComponent getOwner() { return owner; }
325N/A
325N/A /**
325N/A * Back pointer to the BGMBuilder which is building
325N/A * a BGM from schema components including this customization.
325N/A */
325N/A public BGMBuilder getBuilder() { return builder; }
325N/A
325N/A /** Adds a new declaration. */
325N/A public void addDecl( BIDeclaration decl ) {
325N/A if(decl==null) throw new IllegalArgumentException();
325N/A decl.setParent(this);
325N/A decls.add(decl);
325N/A }
325N/A
325N/A /**
325N/A * Gets the first declaration with a given name, or null
325N/A * if none is found.
325N/A */
325N/A public <T extends BIDeclaration>
325N/A T get( Class<T> kind ) {
325N/A for( BIDeclaration decl : decls ) {
325N/A if( kind.isInstance(decl) )
325N/A return kind.cast(decl);
325N/A }
325N/A return null; // not found
325N/A }
325N/A
325N/A /**
325N/A * Gets all the declarations
325N/A */
325N/A public BIDeclaration[] getDecls() {
325N/A return decls.toArray(new BIDeclaration[decls.size()]);
325N/A }
325N/A
325N/A /**
325N/A * Gets the documentation parsed from &lt;xs:documentation>s.
325N/A * The returned collection is to be added to {@link JDocComment#append(Object)}.
325N/A * @return maybe null.
325N/A */
325N/A public String getDocumentation() {
325N/A // TODO: FIXME: correctly turn individual items to String including DOM
325N/A if(documentation==null || documentation.contents==null) return null;
325N/A
325N/A StringBuilder buf = new StringBuilder();
325N/A for (Object c : documentation.contents) {
325N/A if(c instanceof String) {
325N/A buf.append(c.toString());
325N/A }
325N/A if(c instanceof Element) {
325N/A Transformer t = builder.getIdentityTransformer();
325N/A StringWriter w = new StringWriter();
325N/A try {
325N/A Writer fw = new FilterWriter(w) {
325N/A char[] buf = new char[1];
325N/A
325N/A public void write(int c) throws IOException {
325N/A buf[0] = (char)c;
325N/A write(buf,0,1);
325N/A }
325N/A
325N/A public void write(char[] cbuf, int off, int len) throws IOException {
325N/A MinimumEscapeHandler.theInstance.escape(cbuf,off,len,false,out);
325N/A }
325N/A
325N/A public void write(String str, int off, int len) throws IOException {
325N/A write(str.toCharArray(),off,len);
325N/A }
325N/A };
325N/A t.transform(new DOMSource((Element)c),new StreamResult(fw));
325N/A } catch (TransformerException e) {
325N/A throw new Error(e); // impossible
325N/A }
325N/A buf.append("\n<pre>\n");
325N/A buf.append(w);
325N/A buf.append("\n</pre>\n");
325N/A }
325N/A }
325N/A return buf.toString();
325N/A }
325N/A
325N/A /**
325N/A * Merges all the declarations inside the given BindInfo
325N/A * to this BindInfo.
325N/A */
325N/A public void absorb( BindInfo bi ) {
325N/A for( BIDeclaration d : bi )
325N/A d.setParent(this);
325N/A this.decls.addAll( bi.decls );
325N/A
325N/A if(this.documentation==null)
325N/A this.documentation = bi.documentation;
325N/A else
325N/A this.documentation.addAll(bi.documentation);
325N/A }
325N/A
325N/A /** Gets the number of declarations. */
325N/A public int size() { return decls.size(); }
325N/A
325N/A public BIDeclaration get( int idx ) { return decls.get(idx); }
325N/A
325N/A public Iterator<BIDeclaration> iterator() {
325N/A return decls.iterator();
325N/A }
325N/A
325N/A /**
325N/A * Gets the list of {@link CPluginCustomization}s from this.
325N/A *
325N/A * <p>
325N/A * Note that calling this method marks all those plug-in customizations
325N/A * as 'used'. So call it only when it's really necessary.
325N/A */
325N/A public CCustomizations toCustomizationList() {
325N/A CCustomizations r=null;
325N/A for( BIDeclaration d : this ) {
325N/A if(d instanceof BIXPluginCustomization) {
325N/A BIXPluginCustomization pc = (BIXPluginCustomization) d;
325N/A pc.markAsAcknowledged();
325N/A if(!Ring.get(Model.class).options.pluginURIs.contains(pc.getName().getNamespaceURI()))
325N/A continue; // this isn't a plugin customization
325N/A if(r==null)
325N/A r = new CCustomizations();
325N/A r.add(new CPluginCustomization(pc.element,pc.getLocation()));
325N/A }
325N/A }
325N/A
325N/A if(r==null) r = CCustomizations.EMPTY;
325N/A return new CCustomizations(r);
325N/A }
325N/A /** An instance with the empty contents. */
325N/A public final static BindInfo empty = new BindInfo();
325N/A
325N/A /**
325N/A * Lazily prepared {@link JAXBContext}.
325N/A */
325N/A private static JAXBContextImpl customizationContext;
325N/A
325N/A public static JAXBContextImpl getJAXBContext() {
325N/A synchronized(AnnotationParserFactoryImpl.class) {
325N/A try {
325N/A if(customizationContext==null)
325N/A customizationContext = new JAXBContextImpl.JAXBContextBuilder().setClasses(
325N/A new Class[] {
325N/A BindInfo.class, // for xs:annotation
325N/A BIClass.class,
325N/A BIConversion.User.class,
325N/A BIConversion.UserAdapter.class,
325N/A BIDom.class,
325N/A BIFactoryMethod.class,
325N/A BIInlineBinaryData.class,
325N/A BIXDom.class,
325N/A BIXSubstitutable.class,
325N/A BIEnum.class,
325N/A BIEnumMember.class,
325N/A BIGlobalBinding.class,
325N/A BIProperty.class,
325N/A BISchemaBinding.class
325N/A }).build();
325N/A return customizationContext;
325N/A } catch (JAXBException e) {
325N/A throw new AssertionError(e);
325N/A }
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Lazily parsed schema for the binding file.
325N/A */
325N/A public static final SchemaCache bindingFileSchema = new SchemaCache(BindInfo.class.getResource("binding.xsd"));
325N/A}