src/share/jaxws_classes/com/sun/tools/internal/jxc/api/impl/j2s/JAXBModelImpl.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/api/impl/j2s/JAXBModelImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,194 @@
     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.jxc.api.impl.j2s;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.util.ArrayList;
    1.33 +import java.util.Collection;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.Iterator;
    1.36 +import java.util.List;
    1.37 +import java.util.Map;
    1.38 +
    1.39 +import javax.xml.bind.SchemaOutputResolver;
    1.40 +import javax.xml.bind.annotation.XmlList;
    1.41 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    1.42 +import javax.xml.namespace.QName;
    1.43 +import javax.xml.transform.Result;
    1.44 +
    1.45 +import javax.lang.model.element.ExecutableElement;
    1.46 +import javax.lang.model.element.TypeElement;
    1.47 +import javax.lang.model.element.VariableElement;
    1.48 +import javax.lang.model.type.TypeMirror;
    1.49 +import com.sun.tools.internal.xjc.api.ErrorListener;
    1.50 +import com.sun.tools.internal.xjc.api.J2SJAXBModel;
    1.51 +import com.sun.tools.internal.xjc.api.Reference;
    1.52 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
    1.53 +import com.sun.xml.internal.bind.v2.model.core.ArrayInfo;
    1.54 +import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
    1.55 +import com.sun.xml.internal.bind.v2.model.core.Element;
    1.56 +import com.sun.xml.internal.bind.v2.model.core.ElementInfo;
    1.57 +import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;
    1.58 +import com.sun.xml.internal.bind.v2.model.core.NonElement;
    1.59 +import com.sun.xml.internal.bind.v2.model.core.Ref;
    1.60 +import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet;
    1.61 +import com.sun.xml.internal.bind.v2.model.nav.Navigator;
    1.62 +import com.sun.xml.internal.bind.v2.schemagen.XmlSchemaGenerator;
    1.63 +import com.sun.xml.internal.txw2.output.ResultFactory;
    1.64 +
    1.65 +/**
    1.66 + * @author Kohsuke Kawaguchi (kk@kohsuke.org)
    1.67 + */
    1.68 +final class JAXBModelImpl implements J2SJAXBModel {
    1.69 +
    1.70 +    private final Map<QName,Reference> additionalElementDecls;
    1.71 +
    1.72 +    private final List<String> classList = new ArrayList<String>();
    1.73 +
    1.74 +    private final TypeInfoSet<TypeMirror, TypeElement, VariableElement, ExecutableElement> types;
    1.75 +
    1.76 +    private final AnnotationReader<TypeMirror, TypeElement, VariableElement, ExecutableElement> reader;
    1.77 +
    1.78 +    /**
    1.79 +     * Lazily created schema generator.
    1.80 +     */
    1.81 +    private XmlSchemaGenerator<TypeMirror, TypeElement, VariableElement, ExecutableElement> xsdgen;
    1.82 +
    1.83 +    /**
    1.84 +     * Look up table from an externally visible {@link Reference} object
    1.85 +     * to our internal format.
    1.86 +     */
    1.87 +    private final Map<Reference, NonElement<TypeMirror, TypeElement>> refMap = new HashMap<Reference, NonElement<TypeMirror, TypeElement>>();
    1.88 +
    1.89 +    public JAXBModelImpl(TypeInfoSet<TypeMirror, TypeElement, VariableElement, ExecutableElement> types,
    1.90 +                         AnnotationReader<TypeMirror, TypeElement, VariableElement, ExecutableElement> reader,
    1.91 +                         Collection<Reference> rootClasses,
    1.92 +                         Map<QName, Reference> additionalElementDecls) {
    1.93 +        this.types = types;
    1.94 +        this.reader = reader;
    1.95 +        this.additionalElementDecls = additionalElementDecls;
    1.96 +
    1.97 +        Navigator<TypeMirror, TypeElement, VariableElement, ExecutableElement> navigator = types.getNavigator();
    1.98 +
    1.99 +        for (ClassInfo<TypeMirror, TypeElement> i : types.beans().values()) {
   1.100 +            classList.add(i.getName());
   1.101 +        }
   1.102 +
   1.103 +        for (ArrayInfo<TypeMirror, TypeElement> a : types.arrays().values()) {
   1.104 +            String javaName = navigator.getTypeName(a.getType());
   1.105 +            classList.add(javaName);
   1.106 +        }
   1.107 +
   1.108 +        for (EnumLeafInfo<TypeMirror, TypeElement> l : types.enums().values()) {
   1.109 +            QName tn = l.getTypeName();
   1.110 +            if(tn!=null) {
   1.111 +                String javaName = navigator.getTypeName(l.getType());
   1.112 +                classList.add(javaName);
   1.113 +            }
   1.114 +        }
   1.115 +
   1.116 +        for (Reference ref : rootClasses)
   1.117 +            refMap.put(ref,getXmlType(ref));
   1.118 +
   1.119 +        // check for collision between "additional" ones and the ones given to JAXB
   1.120 +        // and eliminate duplication
   1.121 +        Iterator<Map.Entry<QName, Reference>> itr = additionalElementDecls.entrySet().iterator();
   1.122 +        while(itr.hasNext()) {
   1.123 +            Map.Entry<QName, Reference> entry = itr.next();
   1.124 +            if(entry.getValue()==null)      continue;
   1.125 +
   1.126 +            NonElement<TypeMirror, TypeElement> xt = getXmlType(entry.getValue());
   1.127 +
   1.128 +            assert xt!=null;
   1.129 +            refMap.put(entry.getValue(),xt);
   1.130 +            if(xt instanceof ClassInfo) {
   1.131 +                ClassInfo<TypeMirror, TypeElement> xct = (ClassInfo<TypeMirror, TypeElement>) xt;
   1.132 +                Element<TypeMirror, TypeElement> elem = xct.asElement();
   1.133 +                if(elem!=null && elem.getElementName().equals(entry.getKey())) {
   1.134 +                    itr.remove();
   1.135 +                    continue;
   1.136 +                }
   1.137 +            }
   1.138 +            ElementInfo<TypeMirror, TypeElement> ei = types.getElementInfo(null, entry.getKey());
   1.139 +            if(ei!=null && ei.getContentType()==xt)
   1.140 +                itr.remove();
   1.141 +        }
   1.142 +    }
   1.143 +
   1.144 +    public List<String> getClassList() {
   1.145 +        return classList;
   1.146 +    }
   1.147 +
   1.148 +    public QName getXmlTypeName(Reference javaType) {
   1.149 +        NonElement<TypeMirror, TypeElement> ti = refMap.get(javaType);
   1.150 +
   1.151 +        if(ti!=null)
   1.152 +            return ti.getTypeName();
   1.153 +
   1.154 +        return null;
   1.155 +    }
   1.156 +
   1.157 +    private NonElement<TypeMirror, TypeElement> getXmlType(Reference r) {
   1.158 +        if(r==null)
   1.159 +            throw new IllegalArgumentException();
   1.160 +
   1.161 +        XmlJavaTypeAdapter xjta = r.annotations.getAnnotation(XmlJavaTypeAdapter.class);
   1.162 +        XmlList xl = r.annotations.getAnnotation(XmlList.class);
   1.163 +
   1.164 +        Ref<TypeMirror, TypeElement> ref = new Ref<TypeMirror, TypeElement>(
   1.165 +            reader,types.getNavigator(),r.type,xjta,xl);
   1.166 +
   1.167 +        return types.getTypeInfo(ref);
   1.168 +    }
   1.169 +
   1.170 +    public void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException {
   1.171 +        getSchemaGenerator().write(outputResolver,errorListener);
   1.172 +    }
   1.173 +
   1.174 +    public void generateEpisodeFile(Result output) {
   1.175 +        getSchemaGenerator().writeEpisodeFile(ResultFactory.createSerializer(output));
   1.176 +    }
   1.177 +
   1.178 +    private synchronized XmlSchemaGenerator<TypeMirror, TypeElement, VariableElement, ExecutableElement> getSchemaGenerator() {
   1.179 +        if(xsdgen==null) {
   1.180 +            xsdgen = new XmlSchemaGenerator<TypeMirror, TypeElement, VariableElement, ExecutableElement>(types.getNavigator(), types);
   1.181 +
   1.182 +            for (Map.Entry<QName, Reference> e : additionalElementDecls.entrySet()) {
   1.183 +                Reference value = e.getValue();
   1.184 +                if(value!=null) {
   1.185 +                    NonElement<TypeMirror, TypeElement> typeInfo = refMap.get(value);
   1.186 +                    if(typeInfo==null)
   1.187 +                        throw new IllegalArgumentException(e.getValue()+" was not specified to JavaCompiler.bind");
   1.188 +                    TypeMirror type = value.type;
   1.189 +                    xsdgen.add(e.getKey(), !(type != null && type.getKind().isPrimitive()), typeInfo);
   1.190 +                } else {
   1.191 +                    xsdgen.add(e.getKey(),false,null);
   1.192 +                }
   1.193 +            }
   1.194 +        }
   1.195 +        return xsdgen;
   1.196 +    }
   1.197 +}

mercurial