Search in sources :

Example 1 with SchemaBindings

use of org.glassfish.jaxb.core.v2.schemagen.episode.SchemaBindings in project jaxb-ri by eclipse-ee4j.

the class PluginImpl method run.

/**
 * Capture all the generated classes from global schema components
 * and generate them in an episode file.
 */
@Override
public boolean run(Outline model, Options opt, ErrorHandler errorHandler) throws SAXException {
    OutputStream episodeFileOutputStream = null;
    try {
        // reorganize qualifying components by their namespaces to
        // generate the list nicely
        Map<XSSchema, PerSchemaOutlineAdaptors> perSchema = new LinkedHashMap<>();
        boolean hasComponentInNoNamespace = false;
        // Combine classes and enums into a single list
        List<OutlineAdaptor> outlines = new ArrayList<>();
        for (ClassOutline co : model.getClasses()) {
            XSComponent sc = co.target.getSchemaComponent();
            String fullName = co.implClass.fullName();
            String packageName = co.implClass.getPackage().name();
            OutlineAdaptor adaptor = new OutlineAdaptor(sc, OutlineAdaptor.OutlineType.CLASS, fullName, packageName);
            outlines.add(adaptor);
        }
        for (EnumOutline eo : model.getEnums()) {
            XSComponent sc = eo.target.getSchemaComponent();
            String fullName = eo.clazz.fullName();
            String packageName = eo.clazz.getPackage().name();
            OutlineAdaptor adaptor = new OutlineAdaptor(sc, OutlineAdaptor.OutlineType.ENUM, fullName, packageName);
            outlines.add(adaptor);
        }
        for (OutlineAdaptor oa : outlines) {
            XSComponent sc = oa.schemaComponent;
            if (sc == null)
                continue;
            if (!(sc instanceof XSDeclaration))
                continue;
            XSDeclaration decl = (XSDeclaration) sc;
            if (decl.isLocal())
                // local components cannot be referenced from outside, so no need to list.
                continue;
            PerSchemaOutlineAdaptors list = perSchema.get(decl.getOwnerSchema());
            if (list == null) {
                list = new PerSchemaOutlineAdaptors();
                perSchema.put(decl.getOwnerSchema(), list);
            }
            list.add(oa);
            if (decl.getTargetNamespace().equals(""))
                hasComponentInNoNamespace = true;
        }
        episodeFileOutputStream = new FileOutputStream(episodeFile);
        Bindings bindings = TXW.create(Bindings.class, new StreamSerializer(episodeFileOutputStream, "UTF-8"));
        if (// otherwise jaxb binding NS should be the default namespace
        hasComponentInNoNamespace)
            bindings._namespace(Const.JAXB_NSURI, "jaxb");
        else
            bindings._namespace(Const.JAXB_NSURI, "");
        bindings.version("3.0");
        bindings._comment("\n\n" + opt.getPrologComment() + "\n  ");
        // generate listing per schema
        for (Map.Entry<XSSchema, PerSchemaOutlineAdaptors> e : perSchema.entrySet()) {
            PerSchemaOutlineAdaptors ps = e.getValue();
            Bindings group = bindings.bindings();
            String tns = e.getKey().getTargetNamespace();
            if (!tns.equals(""))
                group._namespace(tns, "tns");
            group.scd("x-schema::" + (tns.equals("") ? "" : "tns"));
            SchemaBindings schemaBindings = group.schemaBindings();
            schemaBindings.map(false);
            if (ps.packageNames.size() == 1) {
                final String packageName = ps.packageNames.iterator().next();
                if (packageName != null && packageName.length() > 0) {
                    schemaBindings._package().name(packageName);
                }
            }
            for (OutlineAdaptor oa : ps.outlineAdaptors) {
                Bindings child = group.bindings();
                oa.buildBindings(child);
            }
            group.commit(true);
        }
        bindings.commit();
        return true;
    } catch (IOException e) {
        errorHandler.error(new SAXParseException("Failed to write to " + episodeFile, null, e));
        return false;
    } finally {
        if (episodeFileOutputStream != null) {
            try {
                episodeFileOutputStream.close();
            } catch (IOException e) {
                errorHandler.error(new SAXParseException("Failed to close file handle for " + episodeFile, null, e));
            }
        }
    }
}
Also used : EnumOutline(com.sun.tools.xjc.outline.EnumOutline) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) XSComponent(com.sun.xml.xsom.XSComponent) SchemaBindings(org.glassfish.jaxb.core.v2.schemagen.episode.SchemaBindings) Bindings(org.glassfish.jaxb.core.v2.schemagen.episode.Bindings) XSSchema(com.sun.xml.xsom.XSSchema) LinkedHashMap(java.util.LinkedHashMap) ClassOutline(com.sun.tools.xjc.outline.ClassOutline) SAXParseException(org.xml.sax.SAXParseException) FileOutputStream(java.io.FileOutputStream) StreamSerializer(com.sun.xml.txw2.output.StreamSerializer) XSDeclaration(com.sun.xml.xsom.XSDeclaration) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SchemaBindings(org.glassfish.jaxb.core.v2.schemagen.episode.SchemaBindings)

Aggregations

ClassOutline (com.sun.tools.xjc.outline.ClassOutline)1 EnumOutline (com.sun.tools.xjc.outline.EnumOutline)1 StreamSerializer (com.sun.xml.txw2.output.StreamSerializer)1 XSComponent (com.sun.xml.xsom.XSComponent)1 XSDeclaration (com.sun.xml.xsom.XSDeclaration)1 XSSchema (com.sun.xml.xsom.XSSchema)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Bindings (org.glassfish.jaxb.core.v2.schemagen.episode.Bindings)1 SchemaBindings (org.glassfish.jaxb.core.v2.schemagen.episode.SchemaBindings)1 SAXParseException (org.xml.sax.SAXParseException)1