Search in sources :

Example 1 with CClassInfoParent

use of com.sun.tools.xjc.model.CClassInfoParent 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 2 with CClassInfoParent

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

the class SimpleTypeBuilder method bindToTypeSafeEnum.

/**
 * Builds a type-safe enum conversion from a simple type
 * with enumeration facets.
 *
 * @param className
 *      The class name of the type-safe enum. Or null to
 *      create a default name.
 * @param javadoc
 *      Additional javadoc that will be added at the beginning of the
 *      class, or null if none is necessary.
 * @param members
 *      A map from enumeration values (as String) to BIEnumMember objects.
 *      if some of the value names need to be overrided.
 *      Cannot be null, but the map may not contain entries
 *      for all enumeration values.
 * @param loc
 *      The source location where the above customizations are
 *      specified, or null if none is available.
 */
private TypeUse bindToTypeSafeEnum(XSRestrictionSimpleType type, String className, String javadoc, Map<String, BIEnumMember> members, EnumMemberMode mode, Locator loc) {
    if (// use the location of the simple type as the default
    loc == null)
        loc = type.getLocator();
    if (className == null) {
        // the simple type must be a global one.
        if (!type.isGlobal()) {
            getErrorReporter().error(loc, Messages.ERR_NO_ENUM_NAME_AVAILABLE);
            // recover by returning a meaningless conversion
            return CBuiltinLeafInfo.STRING;
        }
        className = type.getName();
    }
    // we apply name conversion in any case
    className = builder.deriveName(className, type);
    {
        // compute Javadoc
        StringWriter out = new StringWriter();
        SchemaWriter sw = new SchemaWriter(new JavadocEscapeWriter(out));
        type.visit((XSVisitor) sw);
        if (javadoc != null)
            javadoc += "\n\n";
        else
            javadoc = "";
        javadoc += Messages.format(Messages.JAVADOC_HEADING, type.getName()) + "\n<pre>\n" + out.getBuffer() + "</pre>";
    }
    // build base type
    refererStack.push(type.getSimpleBaseType());
    TypeUse use = build(type.getSimpleBaseType());
    refererStack.pop();
    if (use.isCollection())
        // can't bind a list to enum constant
        return null;
    // for now just ignore that case
    CNonElement baseDt = use.getInfo();
    if (baseDt instanceof CClassInfo)
        // can't bind to an enum if the base is a class, since we don't have the value constrctor
        return null;
    // if the member names collide, re-generate numbered constant names.
    XSFacet[] errorRef = new XSFacet[1];
    List<CEnumConstant> memberList = buildCEnumConstants(type, false, members, errorRef);
    if (memberList == null || checkMemberNameCollision(memberList) != null) {
        switch(mode) {
            case SKIP:
                // abort
                return null;
            case ERROR:
                // error
                if (memberList == null) {
                    getErrorReporter().error(errorRef[0].getLocator(), Messages.ERR_CANNOT_GENERATE_ENUM_NAME, errorRef[0].getValue());
                } else {
                    CEnumConstant[] collision = checkMemberNameCollision(memberList);
                    getErrorReporter().error(collision[0].getLocator(), Messages.ERR_ENUM_MEMBER_NAME_COLLISION, collision[0].getName());
                    getErrorReporter().error(collision[1].getLocator(), Messages.ERR_ENUM_MEMBER_NAME_COLLISION_RELATED);
                }
                // recover from error
                return null;
            case GENERATE:
                // generate
                memberList = buildCEnumConstants(type, true, members, null);
                break;
        }
    }
    if (memberList.isEmpty()) {
        getErrorReporter().error(loc, Messages.ERR_NO_ENUM_FACET);
        return null;
    }
    // use the name of the simple type as the name of the class.
    CClassInfoParent scope;
    if (type.isGlobal())
        scope = new CClassInfoParent.Package(getClassSelector().getPackage(type.getTargetNamespace()));
    else
        scope = getClassSelector().getClassScope();
    CEnumLeafInfo xducer = new CEnumLeafInfo(model, BGMBuilder.getName(type), scope, className, baseDt, memberList, type, builder.getBindInfo(type).toCustomizationList(), loc);
    xducer.javadoc = javadoc;
    BIConversion conv = new BIConversion.Static(type.getLocator(), xducer);
    conv.markAsAcknowledged();
    // attach this new conversion object to this simple type
    // so that successive look up will use the same object.
    builder.getOrCreateBindInfo(type).addDecl(conv);
    return conv.getTypeUse(type);
}
Also used : XSVisitor(com.sun.xml.xsom.visitor.XSVisitor) CClassInfoParent(com.sun.tools.xjc.model.CClassInfoParent) BIConversion(com.sun.tools.xjc.reader.xmlschema.bindinfo.BIConversion) CEnumLeafInfo(com.sun.tools.xjc.model.CEnumLeafInfo) CEnumConstant(com.sun.tools.xjc.model.CEnumConstant) TypeUse(com.sun.tools.xjc.model.TypeUse) CClassInfo(com.sun.tools.xjc.model.CClassInfo) XSFacet(com.sun.xml.xsom.XSFacet) StringWriter(java.io.StringWriter) CNonElement(com.sun.tools.xjc.model.CNonElement) JavadocEscapeWriter(com.sun.codemodel.util.JavadocEscapeWriter) SchemaWriter(com.sun.xml.xsom.impl.util.SchemaWriter)

Aggregations

CClassInfo (com.sun.tools.xjc.model.CClassInfo)2 CClassInfoParent (com.sun.tools.xjc.model.CClassInfoParent)2 JPackage (com.sun.codemodel.JPackage)1 JavadocEscapeWriter (com.sun.codemodel.util.JavadocEscapeWriter)1 CElement (com.sun.tools.xjc.model.CElement)1 CElementInfo (com.sun.tools.xjc.model.CElementInfo)1 CEnumConstant (com.sun.tools.xjc.model.CEnumConstant)1 CEnumLeafInfo (com.sun.tools.xjc.model.CEnumLeafInfo)1 CNonElement (com.sun.tools.xjc.model.CNonElement)1 TypeUse (com.sun.tools.xjc.model.TypeUse)1 BIClass (com.sun.tools.xjc.reader.xmlschema.bindinfo.BIClass)1 BIConversion (com.sun.tools.xjc.reader.xmlschema.bindinfo.BIConversion)1 BISchemaBinding (com.sun.tools.xjc.reader.xmlschema.bindinfo.BISchemaBinding)1 BindInfo (com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo)1 XSElementDecl (com.sun.xml.xsom.XSElementDecl)1 XSFacet (com.sun.xml.xsom.XSFacet)1 SchemaWriter (com.sun.xml.xsom.impl.util.SchemaWriter)1 XSVisitor (com.sun.xml.xsom.visitor.XSVisitor)1 StringWriter (java.io.StringWriter)1 QName (javax.xml.namespace.QName)1