Search in sources :

Example 1 with CElement

use of com.sun.tools.xjc.model.CElement in project jaxb-ri by eclipse-ee4j.

the class PackageOutlineImpl method calcDefaultValues.

/**
 * Compute the most common namespace URI in this package
 * (to put into {@link XmlSchema#namespace()} and what value
 * we should put into {@link XmlSchema#elementFormDefault()}.
 *
 * This method is called after {@link #classes} field is filled up.
 */
public void calcDefaultValues() {
    // package-info.java
    if (!_model.isPackageLevelAnnotations()) {
        mostUsedNamespaceURI = "";
        elementFormDefault = XmlNsForm.UNQUALIFIED;
        return;
    }
    // used to visit properties
    CPropertyVisitor<Void> propVisitor = new CPropertyVisitor<>() {

        @Override
        public Void onElement(CElementPropertyInfo p) {
            for (CTypeRef tr : p.getTypes()) {
                countURI(propUriCountMap, tr.getTagName());
            }
            return null;
        }

        @Override
        public Void onReference(CReferencePropertyInfo p) {
            for (CElement e : p.getElements()) {
                countURI(propUriCountMap, e.getElementName());
            }
            return null;
        }

        @Override
        public Void onAttribute(CAttributePropertyInfo p) {
            return null;
        }

        @Override
        public Void onValue(CValuePropertyInfo p) {
            return null;
        }
    };
    for (ClassOutlineImpl co : classes) {
        CClassInfo ci = co.target;
        countURI(uriCountMap, ci.getTypeName());
        countURI(uriCountMap, ci.getElementName());
        for (CPropertyInfo p : ci.getProperties()) p.accept(propVisitor);
    }
    mostUsedNamespaceURI = getMostUsedURI(uriCountMap);
    elementFormDefault = getFormDefault();
    attributeFormDefault = XmlNsForm.UNQUALIFIED;
    try {
        XmlNsForm modelValue = _model.getAttributeFormDefault(mostUsedNamespaceURI);
        attributeFormDefault = modelValue;
    } catch (Exception e) {
    // ignore and accept default
    }
    // we won't get this far if the user specified -npa
    if (!mostUsedNamespaceURI.equals("") || elementFormDefault == XmlNsForm.QUALIFIED || (attributeFormDefault == XmlNsForm.QUALIFIED)) {
        XmlSchemaWriter w = _model.strategy.getPackage(_package, Aspect.IMPLEMENTATION).annotate2(XmlSchemaWriter.class);
        if (!mostUsedNamespaceURI.equals(""))
            w.namespace(mostUsedNamespaceURI);
        if (elementFormDefault == XmlNsForm.QUALIFIED)
            w.elementFormDefault(elementFormDefault);
        if (attributeFormDefault == XmlNsForm.QUALIFIED)
            w.attributeFormDefault(attributeFormDefault);
    }
}
Also used : XmlSchemaWriter(com.sun.tools.xjc.generator.annotation.spec.XmlSchemaWriter) CPropertyVisitor(com.sun.tools.xjc.model.CPropertyVisitor) CAttributePropertyInfo(com.sun.tools.xjc.model.CAttributePropertyInfo) CPropertyInfo(com.sun.tools.xjc.model.CPropertyInfo) CElementPropertyInfo(com.sun.tools.xjc.model.CElementPropertyInfo) CClassInfo(com.sun.tools.xjc.model.CClassInfo) CElement(com.sun.tools.xjc.model.CElement) CValuePropertyInfo(com.sun.tools.xjc.model.CValuePropertyInfo) XmlNsForm(jakarta.xml.bind.annotation.XmlNsForm) CTypeRef(com.sun.tools.xjc.model.CTypeRef) CReferencePropertyInfo(com.sun.tools.xjc.model.CReferencePropertyInfo)

Example 2 with CElement

use of com.sun.tools.xjc.model.CElement in project jaxb-ri by eclipse-ee4j.

the class DefaultClassBinder method complexType.

@Override
public CElement complexType(XSComplexType type) {
    CElement ci = allow(type, type.getName());
    if (ci != null)
        return ci;
    // no customization is given -- do as the default binding.
    BindInfo bi = builder.getBindInfo(type);
    if (type.isGlobal()) {
        QName tagName = null;
        String className = deriveName(type);
        Locator loc = type.getLocator();
        if (getGlobalBinding().isSimpleMode()) {
            // in the simple mode, we may optimize it away
            XSElementDecl referer = getSoleElementReferer(type);
            if (referer != null && isCollapsable(referer)) {
                // if a global element contains
                // a collpsable complex type, we bind this element to a named one
                // and collapses element and complex type.
                tagName = getName(referer);
                className = deriveName(referer);
                loc = referer.getLocator();
            }
        }
        // by default, global ones get their own classes.
        JPackage pkg = selector.getPackage(type.getTargetNamespace());
        return new CClassInfo(model, pkg, className, loc, getTypeName(type), tagName, type, bi.toCustomizationList());
    } else {
        XSElementDecl element = type.getScope();
        if (element.isGlobal() && isCollapsable(element)) {
            if (builder.getBindInfo(element).get(BIClass.class) != null)
                // cause unnecessary wrapping
                return null;
            // which creates unnecessary classes
            return new CClassInfo(model, selector.getClassScope(), deriveName(element), element.getLocator(), null, getName(element), element, bi.toCustomizationList());
        }
        CElement parentType = selector.isBound(element, type);
        String className;
        CClassInfoParent scope;
        if (parentType != null && parentType instanceof CElementInfo && ((CElementInfo) parentType).hasClass()) {
            // special case where we put a nested 'Type' element
            scope = (CElementInfo) parentType;
            className = "Type";
        } else {
            // since the parent element isn't bound to a type, merge the customizations associated to it, too.
            // custs = CCustomizations.merge( custs, builder.getBindInfo(type.getScope()).toCustomizationList());
            className = builder.getNameConverter().toClassName(element.getName());
            BISchemaBinding sb = builder.getBindInfo(type.getOwnerSchema()).get(BISchemaBinding.class);
            if (sb != null)
                className = sb.mangleAnonymousTypeClassName(className);
            scope = selector.getClassScope();
        }
        return new CClassInfo(model, scope, className, type.getLocator(), null, null, type, bi.toCustomizationList());
    }
}
Also used : Locator(org.xml.sax.Locator) CClassInfo(com.sun.tools.xjc.model.CClassInfo) CElement(com.sun.tools.xjc.model.CElement) CClassInfoParent(com.sun.tools.xjc.model.CClassInfoParent) QName(javax.xml.namespace.QName) BISchemaBinding(com.sun.tools.xjc.reader.xmlschema.bindinfo.BISchemaBinding) JPackage(com.sun.codemodel.JPackage) CElementInfo(com.sun.tools.xjc.model.CElementInfo) XSElementDecl(com.sun.xml.xsom.XSElementDecl) BIClass(com.sun.tools.xjc.reader.xmlschema.bindinfo.BIClass) BindInfo(com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo)

Example 3 with CElement

use of com.sun.tools.xjc.model.CElement 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)

Example 4 with CElement

use of com.sun.tools.xjc.model.CElement in project jaxb-ri by eclipse-ee4j.

the class DefaultClassBinder method elementDecl.

@Override
public CElement elementDecl(XSElementDecl decl) {
    CElement r = allow(decl, decl.getName());
    if (r == null) {
        QName tagName = getName(decl);
        CCustomizations custs = builder.getBindInfo(decl).toCustomizationList();
        if (decl.isGlobal()) {
            if (isCollapsable(decl)) {
                // so the binding cannot be delayed.
                return selector.bindToType(decl.getType().asComplexType(), decl, true);
            } else {
                String className = null;
                if (getGlobalBinding().isGenerateElementClass())
                    className = deriveName(decl);
                // otherwise map global elements to JAXBElement
                CElementInfo cei = new CElementInfo(model, tagName, selector.getClassScope(), className, custs, decl.getLocator());
                selector.boundElements.put(decl, cei);
                // referer is element
                stb.refererStack.push(decl);
                cei.initContentType(selector.bindToType(decl.getType(), decl), decl, decl.getDefaultValue());
                stb.refererStack.pop();
                r = cei;
            }
        }
    }
    // have the substitution member derive from the substitution head
    XSElementDecl top = decl.getSubstAffiliation();
    if (top != null) {
        CElement topci = selector.bindToType(top, decl);
        if (r instanceof CClassInfo && topci instanceof CClassInfo)
            ((CClassInfo) r).setBaseClass((CClassInfo) topci);
        if (r instanceof CElementInfo && topci instanceof CElementInfo)
            ((CElementInfo) r).setSubstitutionHead((CElementInfo) topci);
    }
    return r;
}
Also used : CCustomizations(com.sun.tools.xjc.model.CCustomizations) CClassInfo(com.sun.tools.xjc.model.CClassInfo) CElement(com.sun.tools.xjc.model.CElement) QName(javax.xml.namespace.QName) CElementInfo(com.sun.tools.xjc.model.CElementInfo) XSElementDecl(com.sun.xml.xsom.XSElementDecl)

Example 5 with CElement

use of com.sun.tools.xjc.model.CElement in project jaxb-ri by eclipse-ee4j.

the class AbstractField method annotateReference.

private void annotateReference(JAnnotatable field) {
    CReferencePropertyInfo rp = (CReferencePropertyInfo) prop;
    TODO.prototype();
    // this is just a quick hack to get the basic test working
    Collection<CElement> elements = rp.getElements();
    XmlElementRefWriter refw;
    if (elements.size() == 1) {
        refw = field.annotate2(XmlElementRefWriter.class);
        CElement e = elements.iterator().next();
        refw.name(e.getElementName().getLocalPart()).namespace(e.getElementName().getNamespaceURI()).type(e.getType().toType(outline.parent(), IMPLEMENTATION));
        refw.required(rp.isRequired());
    } else if (elements.size() > 1) {
        XmlElementRefsWriter refsw = field.annotate2(XmlElementRefsWriter.class);
        for (CElement e : elements) {
            refw = refsw.value();
            refw.name(e.getElementName().getLocalPart()).namespace(e.getElementName().getNamespaceURI()).type(e.getType().toType(outline.parent(), IMPLEMENTATION));
            refw.required(rp.isRequired());
        }
    }
    if (rp.isMixed())
        field.annotate(XmlMixed.class);
    NClass dh = rp.getDOMHandler();
    if (dh != null) {
        XmlAnyElementWriter xaew = field.annotate2(XmlAnyElementWriter.class);
        xaew.lax(rp.getWildcard().allowTypedObject);
        final JClass value = dh.toType(outline.parent(), IMPLEMENTATION);
        if (!value.equals(codeModel.ref(W3CDomHandler.class))) {
            xaew.value(value);
        }
    }
}
Also used : XmlElementRefsWriter(com.sun.tools.xjc.generator.annotation.spec.XmlElementRefsWriter) CElement(com.sun.tools.xjc.model.CElement) XmlElementRefWriter(com.sun.tools.xjc.generator.annotation.spec.XmlElementRefWriter) NClass(com.sun.tools.xjc.model.nav.NClass) JClass(com.sun.codemodel.JClass) XmlAnyElementWriter(com.sun.tools.xjc.generator.annotation.spec.XmlAnyElementWriter) CReferencePropertyInfo(com.sun.tools.xjc.model.CReferencePropertyInfo) XmlMixed(jakarta.xml.bind.annotation.XmlMixed)

Aggregations

CElement (com.sun.tools.xjc.model.CElement)6 CClassInfo (com.sun.tools.xjc.model.CClassInfo)5 CElementInfo (com.sun.tools.xjc.model.CElementInfo)3 CReferencePropertyInfo (com.sun.tools.xjc.model.CReferencePropertyInfo)3 JPackage (com.sun.codemodel.JPackage)2 CElementPropertyInfo (com.sun.tools.xjc.model.CElementPropertyInfo)2 CPropertyInfo (com.sun.tools.xjc.model.CPropertyInfo)2 BISchemaBinding (com.sun.tools.xjc.reader.xmlschema.bindinfo.BISchemaBinding)2 XSElementDecl (com.sun.xml.xsom.XSElementDecl)2 QName (javax.xml.namespace.QName)2 JClass (com.sun.codemodel.JClass)1 Property (com.sun.tools.xjc.api.Property)1 XmlAnyElementWriter (com.sun.tools.xjc.generator.annotation.spec.XmlAnyElementWriter)1 XmlElementRefWriter (com.sun.tools.xjc.generator.annotation.spec.XmlElementRefWriter)1 XmlElementRefsWriter (com.sun.tools.xjc.generator.annotation.spec.XmlElementRefsWriter)1 XmlSchemaWriter (com.sun.tools.xjc.generator.annotation.spec.XmlSchemaWriter)1 CAttributePropertyInfo (com.sun.tools.xjc.model.CAttributePropertyInfo)1 CClassInfoParent (com.sun.tools.xjc.model.CClassInfoParent)1 CCustomizations (com.sun.tools.xjc.model.CCustomizations)1 CPropertyVisitor (com.sun.tools.xjc.model.CPropertyVisitor)1