Search in sources :

Example 1 with XSDeclaration

use of com.sun.xml.xsom.XSDeclaration 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)

Example 2 with XSDeclaration

use of com.sun.xml.xsom.XSDeclaration in project jaxb-ri by eclipse-ee4j.

the class ClassSelector method _bindToClass.

/**
 * The real meat of the "bindToType" code.
 *
 * @param cannotBeDelayed
 *      if the binding of the body of the class cannot be defered
 *      and needs to be done immediately. If the flag is false,
 *      the binding of the body will be done later, to avoid
 *      cyclic binding problem.
 * @param referer
 *      The component that refers to {@code sc}. This can be null,
 *      if figuring out the referer is too hard, in which case
 *      the error message might be less user friendly.
 */
// TODO: consider getting rid of "cannotBeDelayed"
CTypeInfo _bindToClass(@NotNull XSComponent sc, XSComponent referer, boolean cannotBeDelayed) {
    // check if this class is already built.
    if (!bindMap.containsKey(sc)) {
        // craete a bind task
        // if this is a global declaration, make sure they will be generated
        // under a package.
        boolean isGlobal = false;
        if (sc instanceof XSDeclaration) {
            isGlobal = ((XSDeclaration) sc).isGlobal();
            if (isGlobal)
                pushClassScope(new CClassInfoParent.Package(getPackage(((XSDeclaration) sc).getTargetNamespace())));
        }
        // otherwise check if this component should become a class.
        CElement bean = sc.apply(classBinder);
        if (isGlobal)
            popClassScope();
        if (bean == null)
            return null;
        // can this namespace generate a class?
        if (bean instanceof CClassInfo) {
            XSSchema os = sc.getOwnerSchema();
            BISchemaBinding sb = builder.getBindInfo(os).get(BISchemaBinding.class);
            if (sb != null && !sb.map) {
                // nope
                getErrorReporter().error(sc.getLocator(), Messages.ERR_REFERENCE_TO_NONEXPORTED_CLASS, sc.apply(new ComponentNameFunction()));
                getErrorReporter().error(sb.getLocation(), Messages.ERR_REFERENCE_TO_NONEXPORTED_CLASS_MAP_FALSE, os.getTargetNamespace());
                if (referer != null)
                    getErrorReporter().error(referer.getLocator(), Messages.ERR_REFERENCE_TO_NONEXPORTED_CLASS_REFERER, referer.apply(new ComponentNameFunction()));
            }
        }
        queueBuild(sc, bean);
    }
    Binding bind = bindMap.get(sc);
    if (cannotBeDelayed)
        bind.build();
    return bind.bean;
}
Also used : BISchemaBinding(com.sun.tools.xjc.reader.xmlschema.bindinfo.BISchemaBinding) CClassInfo(com.sun.tools.xjc.model.CClassInfo) CElement(com.sun.tools.xjc.model.CElement) BISchemaBinding(com.sun.tools.xjc.reader.xmlschema.bindinfo.BISchemaBinding) ComponentNameFunction(com.sun.xml.xsom.util.ComponentNameFunction) JPackage(com.sun.codemodel.JPackage) XSDeclaration(com.sun.xml.xsom.XSDeclaration) XSSchema(com.sun.xml.xsom.XSSchema)

Aggregations

XSDeclaration (com.sun.xml.xsom.XSDeclaration)2 XSSchema (com.sun.xml.xsom.XSSchema)2 JPackage (com.sun.codemodel.JPackage)1 CClassInfo (com.sun.tools.xjc.model.CClassInfo)1 CElement (com.sun.tools.xjc.model.CElement)1 ClassOutline (com.sun.tools.xjc.outline.ClassOutline)1 EnumOutline (com.sun.tools.xjc.outline.EnumOutline)1 BISchemaBinding (com.sun.tools.xjc.reader.xmlschema.bindinfo.BISchemaBinding)1 StreamSerializer (com.sun.xml.txw2.output.StreamSerializer)1 XSComponent (com.sun.xml.xsom.XSComponent)1 ComponentNameFunction (com.sun.xml.xsom.util.ComponentNameFunction)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