Search in sources :

Example 1 with CClassRef

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

the class DefaultClassBinder method allow.

/**
 * Checks if a component carries a customization to map it to a class.
 * If so, make it a class.
 *
 * @param defaultBaseName
 *      The token which will be used as the basis of the class name
 *      if the class name is not specified in the customization.
 *      This is usually the name of an element declaration, and so on.
 *
 *      This parameter can be null, in that case it would be an error
 *      if a name is not given by the customization.
 */
private CElement allow(XSComponent component, String defaultBaseName) {
    BIClass decl = null;
    if (component instanceof XSComplexType) {
        XSType complexType = (XSType) component;
        BIClass lastFoundRecursiveBiClass = null;
        if (complexType.getName() != null) {
            while (!schemas.getAnyType().equals(complexType)) {
                BindInfo bindInfo = builder.getBindInfo(complexType);
                BIClass biClass = bindInfo.get(BIClass.class);
                if (biClass != null && "true".equals(biClass.getRecursive()))
                    lastFoundRecursiveBiClass = biClass;
                complexType = complexType.getBaseType();
            }
        }
        // use this as biclass for current component
        decl = lastFoundRecursiveBiClass;
    }
    BindInfo bindInfo = builder.getBindInfo(component);
    if (decl == null) {
        decl = bindInfo.get(BIClass.class);
        if (decl == null)
            return null;
    }
    decl.markAsAcknowledged();
    // first consider binding to the class reference.
    String ref = decl.getExistingClassRef();
    if (ref != null) {
        if (!JJavaName.isFullyQualifiedClassName(ref)) {
            Ring.get(ErrorReceiver.class).error(decl.getLocation(), Messages.format(Messages.ERR_INCORRECT_CLASS_NAME, ref));
        // recover by ignoring @ref
        } else {
            if (component instanceof XSComplexType) {
                // UGLY UGLY UGLY
                // since we are not going to bind this complex type, we need to figure out
                // its binding mode without actually binding it (and also expose this otherwise
                // hidden mechanism into this part of the code.)
                // 
                // this code is potentially dangerous as the base class might have been bound
                // in different ways. To be correct, we need to figure out how the content type
                // would have been bound, from the schema.
                Ring.get(ComplexTypeFieldBuilder.class).recordBindingMode((XSComplexType) component, ComplexTypeBindingMode.NORMAL);
            }
            return new CClassRef(model, component, decl, bindInfo.toCustomizationList());
        }
    }
    String clsName = decl.getClassName();
    if (clsName == null) {
        // from the current component.
        if (defaultBaseName == null) {
            Ring.get(ErrorReceiver.class).error(decl.getLocation(), Messages.format(Messages.ERR_CLASS_NAME_IS_REQUIRED));
            // recover by generating a pseudo-random name
            defaultBaseName = "undefined" + component.hashCode();
        }
        clsName = builder.deriveName(defaultBaseName, component);
    } else {
        if (!JJavaName.isJavaIdentifier(clsName)) {
            // not a valid Java class name
            Ring.get(ErrorReceiver.class).error(decl.getLocation(), Messages.format(Messages.ERR_INCORRECT_CLASS_NAME, clsName));
            // recover by a dummy name
            clsName = "Undefined" + component.hashCode();
        }
    }
    QName typeName = null;
    QName elementName = null;
    if (component instanceof XSType) {
        XSType t = (XSType) component;
        typeName = getName(t);
    }
    if (component instanceof XSElementDecl) {
        XSElementDecl e = (XSElementDecl) component;
        elementName = getName(e);
    }
    if (component instanceof XSElementDecl && !isCollapsable((XSElementDecl) component)) {
        XSElementDecl e = ((XSElementDecl) component);
        CElementInfo cei = new CElementInfo(model, elementName, selector.getClassScope(), clsName, bindInfo.toCustomizationList(), decl.getLocation());
        selector.boundElements.put(e, cei);
        // referer is element
        stb.refererStack.push(component);
        cei.initContentType(selector.bindToType(e.getType(), e), e, e.getDefaultValue());
        stb.refererStack.pop();
        return cei;
    // TODO: support javadoc and userSpecifiedImplClass
    } else {
        CClassInfo bt = new CClassInfo(model, selector.getClassScope(), clsName, decl.getLocation(), typeName, elementName, component, bindInfo.toCustomizationList());
        // set javadoc class comment.
        if (decl.getJavadoc() != null)
            bt.javadoc = decl.getJavadoc() + "\n\n";
        // add extra blank lines so that the schema fragment
        // and user-specified javadoc would be separated
        // if the implClass is given, set it to ClassItem
        String implClass = decl.getUserSpecifiedImplClass();
        if (implClass != null)
            bt.setUserSpecifiedImplClass(implClass);
        return bt;
    }
}
Also used : ComplexTypeFieldBuilder(com.sun.tools.xjc.reader.xmlschema.ct.ComplexTypeFieldBuilder) XSType(com.sun.xml.xsom.XSType) CClassInfo(com.sun.tools.xjc.model.CClassInfo) XSComplexType(com.sun.xml.xsom.XSComplexType) CClassRef(com.sun.tools.xjc.model.CClassRef) QName(javax.xml.namespace.QName) CElementInfo(com.sun.tools.xjc.model.CElementInfo) XSElementDecl(com.sun.xml.xsom.XSElementDecl) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) BIClass(com.sun.tools.xjc.reader.xmlschema.bindinfo.BIClass) BindInfo(com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo)

Example 2 with CClassRef

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

the class SimpleTypeBuilder method find.

/**
 * Checks if there's any binding available on the given type.
 *
 * @return
 *      null if not (which causes the {@link #compose(XSSimpleType)} method
 *      to do ascending.
 */
private TypeUse find(XSSimpleType type) {
    TypeUse r;
    boolean noAutoEnum = false;
    // check for user specified conversion
    BindInfo info = builder.getBindInfo(type);
    BIConversion conv = info.get(BIConversion.class);
    if (conv != null) {
        // a conversion was found
        conv.markAsAcknowledged();
        return conv.getTypeUse(type);
    }
    // look for enum customization, which is another user specified conversion
    BIEnum en = info.get(BIEnum.class);
    if (en != null) {
        en.markAsAcknowledged();
        if (!en.isMapped()) {
            noAutoEnum = true;
        } else {
            // the type is OK
            if (!canBeMappedToTypeSafeEnum(type)) {
                getErrorReporter().error(en.getLocation(), Messages.ERR_CANNOT_BE_TYPE_SAFE_ENUM);
                getErrorReporter().error(type.getLocator(), Messages.ERR_CANNOT_BE_TYPE_SAFE_ENUM_LOCATION);
                // recover by ignoring this customization
                return null;
            }
            // reference?
            if (en.ref != null) {
                if (!JJavaName.isFullyQualifiedClassName(en.ref)) {
                    Ring.get(ErrorReceiver.class).error(en.getLocation(), Messages.format(Messages.ERR_INCORRECT_CLASS_NAME, en.ref));
                    // recover by ignoring @ref
                    return null;
                }
                return new CClassRef(model, type, en, info.toCustomizationList());
            }
            // so in this stage we can safely cast it to XSRestrictionSimpleType
            return bindToTypeSafeEnum((XSRestrictionSimpleType) type, en.className, en.javadoc, en.members, getEnumMemberMode().getModeWithEnum(), en.getLocation());
        }
    }
    // if the type is built in, look for the default binding
    if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(type.getTargetNamespace())) {
        String name = type.getName();
        if (name != null) {
            r = lookupBuiltin(name);
            if (r != null)
                return r;
        }
    }
    // also check for swaRef
    if (type.getTargetNamespace().equals(WellKnownNamespace.SWA_URI)) {
        String name = type.getName();
        if (name != null && name.equals("swaRef"))
            return CBuiltinLeafInfo.STRING.makeAdapted(SwaRefAdapterMarker.class, false);
    }
    // if so, built a EnumXDucer from it and return it.
    if (type.isRestriction() && !noAutoEnum) {
        XSRestrictionSimpleType rst = type.asRestriction();
        if (shouldBeMappedToTypeSafeEnumByDefault(rst)) {
            r = bindToTypeSafeEnum(rst, null, null, Collections.emptyMap(), getEnumMemberMode(), null);
            if (r != null)
                return r;
        }
    }
    return (CNonElement) getClassSelector()._bindToClass(type, null, false);
}
Also used : TypeUse(com.sun.tools.xjc.model.TypeUse) BIConversion(com.sun.tools.xjc.reader.xmlschema.bindinfo.BIConversion) XSRestrictionSimpleType(com.sun.xml.xsom.XSRestrictionSimpleType) SwaRefAdapterMarker(org.glassfish.jaxb.core.v2.runtime.SwaRefAdapterMarker) CClassRef(com.sun.tools.xjc.model.CClassRef) CNonElement(com.sun.tools.xjc.model.CNonElement) ErrorReceiver(com.sun.tools.xjc.ErrorReceiver) BIEnum(com.sun.tools.xjc.reader.xmlschema.bindinfo.BIEnum) BindInfo(com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo)

Aggregations

ErrorReceiver (com.sun.tools.xjc.ErrorReceiver)2 CClassRef (com.sun.tools.xjc.model.CClassRef)2 BindInfo (com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo)2 CClassInfo (com.sun.tools.xjc.model.CClassInfo)1 CElementInfo (com.sun.tools.xjc.model.CElementInfo)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 BIEnum (com.sun.tools.xjc.reader.xmlschema.bindinfo.BIEnum)1 ComplexTypeFieldBuilder (com.sun.tools.xjc.reader.xmlschema.ct.ComplexTypeFieldBuilder)1 XSComplexType (com.sun.xml.xsom.XSComplexType)1 XSElementDecl (com.sun.xml.xsom.XSElementDecl)1 XSRestrictionSimpleType (com.sun.xml.xsom.XSRestrictionSimpleType)1 XSType (com.sun.xml.xsom.XSType)1 QName (javax.xml.namespace.QName)1 SwaRefAdapterMarker (org.glassfish.jaxb.core.v2.runtime.SwaRefAdapterMarker)1