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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/BindInfo.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,357 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.xjc.reader.xmlschema.bindinfo;
    1.30 +
    1.31 +import java.io.FilterWriter;
    1.32 +import java.io.IOException;
    1.33 +import java.io.StringWriter;
    1.34 +import java.io.Writer;
    1.35 +import java.util.ArrayList;
    1.36 +import java.util.Iterator;
    1.37 +import java.util.List;
    1.38 +
    1.39 +import javax.xml.bind.JAXBContext;
    1.40 +import javax.xml.bind.JAXBException;
    1.41 +import javax.xml.bind.Unmarshaller;
    1.42 +import javax.xml.bind.annotation.XmlAnyElement;
    1.43 +import javax.xml.bind.annotation.XmlElement;
    1.44 +import javax.xml.bind.annotation.XmlMixed;
    1.45 +import javax.xml.bind.annotation.XmlRootElement;
    1.46 +import javax.xml.bind.annotation.XmlType;
    1.47 +import javax.xml.transform.Transformer;
    1.48 +import javax.xml.transform.TransformerException;
    1.49 +import javax.xml.transform.dom.DOMSource;
    1.50 +import javax.xml.transform.stream.StreamResult;
    1.51 +
    1.52 +import com.sun.codemodel.internal.JDocComment;
    1.53 +import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.54 +import com.sun.tools.internal.xjc.SchemaCache;
    1.55 +import com.sun.tools.internal.xjc.model.CCustomizations;
    1.56 +import com.sun.tools.internal.xjc.model.CPluginCustomization;
    1.57 +import com.sun.tools.internal.xjc.model.Model;
    1.58 +import com.sun.tools.internal.xjc.reader.Ring;
    1.59 +import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder;
    1.60 +import com.sun.xml.internal.bind.annotation.XmlLocation;
    1.61 +import com.sun.xml.internal.bind.marshaller.MinimumEscapeHandler;
    1.62 +import com.sun.xml.internal.xsom.XSComponent;
    1.63 +
    1.64 +import org.w3c.dom.Element;
    1.65 +import org.xml.sax.Locator;
    1.66 +
    1.67 +/**
    1.68 + * Container for customization declarations.
    1.69 + *
    1.70 + * We use JAXB ourselves and parse this object from "xs:annotation".
    1.71 + *
    1.72 + * @author
    1.73 + *     Kohsuke Kawaguchi (kohsuke,kawaguchi@sun.com)
    1.74 + */
    1.75 +@XmlRootElement(namespace= WellKnownNamespace.XML_SCHEMA,name="annotation")
    1.76 +@XmlType(namespace=WellKnownNamespace.XML_SCHEMA,name="foobar")
    1.77 +public final class BindInfo implements Iterable<BIDeclaration> {
    1.78 +
    1.79 +    private BGMBuilder builder;
    1.80 +
    1.81 +    @XmlLocation
    1.82 +    private Locator location;
    1.83 +
    1.84 +    /**
    1.85 +     * Documentation taken from &lt;xs:documentation>s.
    1.86 +     */
    1.87 +    @XmlElement(namespace=WellKnownNamespace.XML_SCHEMA)
    1.88 +    private Documentation documentation;
    1.89 +
    1.90 +    /**
    1.91 +     * Returns true if this {@link BindInfo} doesn't contain any useful
    1.92 +     * information.
    1.93 +     *
    1.94 +     * This flag is used to discard unused {@link BindInfo}s early to save memory footprint.
    1.95 +     */
    1.96 +    public boolean isPointless() {
    1.97 +        if(size()>0)     return false;
    1.98 +        if(documentation!=null && !documentation.contents.isEmpty())
    1.99 +            return false;
   1.100 +
   1.101 +        return true;
   1.102 +    }
   1.103 +
   1.104 +    private static final class Documentation {
   1.105 +        @XmlAnyElement
   1.106 +        @XmlMixed
   1.107 +        List<Object> contents = new ArrayList<Object>();
   1.108 +
   1.109 +        void addAll(Documentation rhs) {
   1.110 +            if(rhs==null)   return;
   1.111 +
   1.112 +            if(contents==null)
   1.113 +                contents = new ArrayList<Object>();
   1.114 +            if(!contents.isEmpty())
   1.115 +                contents.add("\n\n");
   1.116 +            contents.addAll(rhs.contents);
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    /** list of individual declarations. */
   1.121 +    private final List<BIDeclaration> decls = new ArrayList<BIDeclaration>();
   1.122 +
   1.123 +    private static final class AppInfo {
   1.124 +        /**
   1.125 +         * Receives {@link BIDeclaration}s and other DOMs.
   1.126 +         */
   1.127 +        @XmlAnyElement(lax=true,value=DomHandlerEx.class)
   1.128 +        List<Object> contents = new ArrayList<Object>();
   1.129 +
   1.130 +        public void addTo(BindInfo bi) {
   1.131 +            if(contents==null)  return;
   1.132 +
   1.133 +            for (Object o : contents) {
   1.134 +                if(o instanceof BIDeclaration)
   1.135 +                    bi.addDecl((BIDeclaration)o);
   1.136 +                // this is really PITA! I can't get the source location
   1.137 +                if(o instanceof DomHandlerEx.DomAndLocation) {
   1.138 +                    DomHandlerEx.DomAndLocation e = (DomHandlerEx.DomAndLocation)o;
   1.139 +                    String nsUri = e.element.getNamespaceURI();
   1.140 +                    if(nsUri==null || nsUri.equals("")
   1.141 +                    || nsUri.equals(WellKnownNamespace.XML_SCHEMA))
   1.142 +                        continue;   // this is definitely not a customization
   1.143 +                    bi.addDecl(new BIXPluginCustomization(e.element,e.loc));
   1.144 +                }
   1.145 +            }
   1.146 +        }
   1.147 +    }
   1.148 +
   1.149 +
   1.150 +    // only used by JAXB
   1.151 +    @XmlElement(namespace=WellKnownNamespace.XML_SCHEMA)
   1.152 +    void setAppinfo(AppInfo aib) {
   1.153 +        aib.addTo(this);
   1.154 +    }
   1.155 +
   1.156 +
   1.157 +
   1.158 +    /**
   1.159 +     * Gets the location of this annotation in the source file.
   1.160 +     *
   1.161 +     * @return
   1.162 +     *      If the declarations are in fact specified in the source
   1.163 +     *      code, a non-null valid object will be returned.
   1.164 +     *      If this BindInfo is generated internally by XJC, then
   1.165 +     *      null will be returned.
   1.166 +     */
   1.167 +    public Locator getSourceLocation() { return location; }
   1.168 +
   1.169 +
   1.170 +    private XSComponent owner;
   1.171 +    /**
   1.172 +     * Sets the owner schema component and a reference to BGMBuilder.
   1.173 +     * This method is called from the BGMBuilder before
   1.174 +     * any BIDeclaration inside it is used.
   1.175 +     */
   1.176 +    public void setOwner( BGMBuilder _builder, XSComponent _owner ) {
   1.177 +        this.owner = _owner;
   1.178 +        this.builder = _builder;
   1.179 +        for (BIDeclaration d : decls)
   1.180 +            d.onSetOwner();
   1.181 +    }
   1.182 +    public XSComponent getOwner() { return owner; }
   1.183 +
   1.184 +    /**
   1.185 +     * Back pointer to the BGMBuilder which is building
   1.186 +     * a BGM from schema components including this customization.
   1.187 +     */
   1.188 +    public BGMBuilder getBuilder() { return builder; }
   1.189 +
   1.190 +    /** Adds a new declaration. */
   1.191 +    public void addDecl( BIDeclaration decl ) {
   1.192 +        if(decl==null)  throw new IllegalArgumentException();
   1.193 +        decl.setParent(this);
   1.194 +        decls.add(decl);
   1.195 +    }
   1.196 +
   1.197 +    /**
   1.198 +     * Gets the first declaration with a given name, or null
   1.199 +     * if none is found.
   1.200 +     */
   1.201 +    public <T extends BIDeclaration>
   1.202 +    T get( Class<T> kind ) {
   1.203 +        for( BIDeclaration decl : decls ) {
   1.204 +            if( kind.isInstance(decl) )
   1.205 +                return kind.cast(decl);
   1.206 +        }
   1.207 +        return null; // not found
   1.208 +    }
   1.209 +
   1.210 +    /**
   1.211 +     * Gets all the declarations
   1.212 +     */
   1.213 +    public BIDeclaration[] getDecls() {
   1.214 +        return decls.toArray(new BIDeclaration[decls.size()]);
   1.215 +    }
   1.216 +
   1.217 +    /**
   1.218 +     * Gets the documentation parsed from &lt;xs:documentation>s.
   1.219 +     * The returned collection is to be added to {@link JDocComment#append(Object)}.
   1.220 +     * @return  maybe null.
   1.221 +     */
   1.222 +    public String getDocumentation() {
   1.223 +        // TODO: FIXME: correctly turn individual items to String including DOM
   1.224 +        if(documentation==null || documentation.contents==null) return null;
   1.225 +
   1.226 +        StringBuilder buf = new StringBuilder();
   1.227 +        for (Object c : documentation.contents) {
   1.228 +            if(c instanceof String) {
   1.229 +                buf.append(c.toString());
   1.230 +            }
   1.231 +            if(c instanceof Element) {
   1.232 +                Transformer t = builder.getIdentityTransformer();
   1.233 +                StringWriter w = new StringWriter();
   1.234 +                try {
   1.235 +                    Writer fw = new FilterWriter(w) {
   1.236 +                        char[] buf = new char[1];
   1.237 +
   1.238 +                        public void write(int c) throws IOException {
   1.239 +                            buf[0] = (char)c;
   1.240 +                            write(buf,0,1);
   1.241 +                        }
   1.242 +
   1.243 +                        public void write(char[] cbuf, int off, int len) throws IOException {
   1.244 +                            MinimumEscapeHandler.theInstance.escape(cbuf,off,len,false,out);
   1.245 +                        }
   1.246 +
   1.247 +                        public void write(String str, int off, int len) throws IOException {
   1.248 +                            write(str.toCharArray(),off,len);
   1.249 +                        }
   1.250 +                    };
   1.251 +                    t.transform(new DOMSource((Element)c),new StreamResult(fw));
   1.252 +                } catch (TransformerException e) {
   1.253 +                    throw new Error(e); // impossible
   1.254 +                }
   1.255 +                buf.append("\n<pre>\n");
   1.256 +                buf.append(w);
   1.257 +                buf.append("\n</pre>\n");
   1.258 +            }
   1.259 +        }
   1.260 +        return buf.toString();
   1.261 +    }
   1.262 +
   1.263 +    /**
   1.264 +     * Merges all the declarations inside the given BindInfo
   1.265 +     * to this BindInfo.
   1.266 +     */
   1.267 +    public void absorb( BindInfo bi ) {
   1.268 +        for( BIDeclaration d : bi )
   1.269 +            d.setParent(this);
   1.270 +        this.decls.addAll( bi.decls );
   1.271 +
   1.272 +        if(this.documentation==null)
   1.273 +            this.documentation = bi.documentation;
   1.274 +        else
   1.275 +            this.documentation.addAll(bi.documentation);
   1.276 +    }
   1.277 +
   1.278 +    /** Gets the number of declarations. */
   1.279 +    public int size() { return decls.size(); }
   1.280 +
   1.281 +    public BIDeclaration get( int idx ) { return decls.get(idx); }
   1.282 +
   1.283 +    public Iterator<BIDeclaration> iterator() {
   1.284 +        return decls.iterator();
   1.285 +    }
   1.286 +
   1.287 +    /**
   1.288 +     * Gets the list of {@link CPluginCustomization}s from this.
   1.289 +     *
   1.290 +     * <p>
   1.291 +     * Note that calling this method marks all those plug-in customizations
   1.292 +     * as 'used'. So call it only when it's really necessary.
   1.293 +     */
   1.294 +    public CCustomizations toCustomizationList() {
   1.295 +        CCustomizations r=null;
   1.296 +        for( BIDeclaration d : this ) {
   1.297 +            if(d instanceof BIXPluginCustomization) {
   1.298 +                BIXPluginCustomization pc = (BIXPluginCustomization) d;
   1.299 +                pc.markAsAcknowledged();
   1.300 +                if(!Ring.get(Model.class).options.pluginURIs.contains(pc.getName().getNamespaceURI()))
   1.301 +                    continue;   // this isn't a plugin customization
   1.302 +                if(r==null)
   1.303 +                    r = new CCustomizations();
   1.304 +                r.add(new CPluginCustomization(pc.element,pc.getLocation()));
   1.305 +            }
   1.306 +        }
   1.307 +
   1.308 +        if(r==null)     r = CCustomizations.EMPTY;
   1.309 +        return new CCustomizations(r);
   1.310 +    }
   1.311 +    /** An instance with the empty contents. */
   1.312 +    public final static BindInfo empty = new BindInfo();
   1.313 +
   1.314 +    /**
   1.315 +     * Lazily prepared {@link JAXBContext}.
   1.316 +     */
   1.317 +    private static volatile JAXBContext customizationContext;
   1.318 +
   1.319 +    public static JAXBContext getCustomizationContext() {
   1.320 +        try {
   1.321 +            if (customizationContext == null) {
   1.322 +                synchronized (BindInfo.class) {
   1.323 +                    if (customizationContext == null) {
   1.324 +                        customizationContext = JAXBContext.newInstance(
   1.325 +                                BindInfo.class, // for xs:annotation
   1.326 +                                BIClass.class,
   1.327 +                                BIConversion.User.class,
   1.328 +                                BIConversion.UserAdapter.class,
   1.329 +                                BIDom.class,
   1.330 +                                BIFactoryMethod.class,
   1.331 +                                BIInlineBinaryData.class,
   1.332 +                                BIXDom.class,
   1.333 +                                BIXSubstitutable.class,
   1.334 +                                BIEnum.class,
   1.335 +                                BIEnumMember.class,
   1.336 +                                BIGlobalBinding.class,
   1.337 +                                BIProperty.class,
   1.338 +                                BISchemaBinding.class);
   1.339 +                    }
   1.340 +                }
   1.341 +            }
   1.342 +            return customizationContext;
   1.343 +        } catch (JAXBException e) {
   1.344 +            throw new AssertionError(e);
   1.345 +        }
   1.346 +    }
   1.347 +
   1.348 +    public static Unmarshaller getCustomizationUnmarshaller() {
   1.349 +        try {
   1.350 +            return getCustomizationContext().createUnmarshaller();
   1.351 +        } catch (JAXBException e) {
   1.352 +            throw new AssertionError(e);
   1.353 +        }
   1.354 +    }
   1.355 +
   1.356 +    /**
   1.357 +     * Lazily parsed schema for the binding file.
   1.358 +     */
   1.359 +    public static final SchemaCache bindingFileSchema = new SchemaCache(BindInfo.class.getResource("binding.xsd"));
   1.360 +}

mercurial