Search in sources :

Example 1 with VariableMapping

use of org.apache.openejb.jee.VariableMapping in project tomee by apache.

the class JavaXmlTypeMapping$JAXB method _read.

public static final JavaXmlTypeMapping _read(final XoXMLStreamReader reader, RuntimeContext context) throws Exception {
    // Check for xsi:nil
    if (reader.isXsiNil()) {
        return null;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final JavaXmlTypeMapping javaXmlTypeMapping = new JavaXmlTypeMapping();
    context.beforeUnmarshal(javaXmlTypeMapping, LifecycleCallback.NONE);
    List<VariableMapping> variableMapping = null;
    // Check xsi:type
    final QName xsiType = reader.getXsiType();
    if (xsiType != null) {
        if (("java-xml-type-mappingType" != xsiType.getLocalPart()) || ("http://java.sun.com/xml/ns/javaee" != xsiType.getNamespaceURI())) {
            return context.unexpectedXsiType(reader, JavaXmlTypeMapping.class);
        }
    }
    // Read attributes
    for (final Attribute attribute : reader.getAttributes()) {
        if (("id" == attribute.getLocalName()) && (("" == attribute.getNamespace()) || (attribute.getNamespace() == null))) {
            // ATTRIBUTE: id
            final String id = Adapters.collapsedStringAdapterAdapter.unmarshal(attribute.getValue());
            context.addXmlId(reader, id, javaXmlTypeMapping);
            javaXmlTypeMapping.id = id;
        } else if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI != attribute.getNamespace()) {
            context.unexpectedAttribute(attribute, new QName("", "id"));
        }
    }
    // Read elements
    for (final XoXMLStreamReader elementReader : reader.getChildElements()) {
        if (("java-type" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: javaType
            final String javaTypeRaw = elementReader.getElementAsString();
            final String javaType;
            try {
                javaType = Adapters.collapsedStringAdapterAdapter.unmarshal(javaTypeRaw);
            } catch (final Exception e) {
                context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
                continue;
            }
            javaXmlTypeMapping.javaType = javaType;
        } else if (("root-type-qname" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: rootTypeQname
            final QName rootTypeQname = elementReader.getElementAsQName();
            javaXmlTypeMapping.rootTypeQname = rootTypeQname;
        } else if (("anonymous-type-qname" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: anonymousTypeQname
            final String anonymousTypeQnameRaw = elementReader.getElementAsString();
            final String anonymousTypeQname;
            try {
                anonymousTypeQname = Adapters.collapsedStringAdapterAdapter.unmarshal(anonymousTypeQnameRaw);
            } catch (final Exception e) {
                context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
                continue;
            }
            javaXmlTypeMapping.anonymousTypeQname = anonymousTypeQname;
        } else if (("qname-scope" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: qnameScope
            final String qnameScopeRaw = elementReader.getElementAsString();
            final String qnameScope;
            try {
                qnameScope = Adapters.collapsedStringAdapterAdapter.unmarshal(qnameScopeRaw);
            } catch (final Exception e) {
                context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
                continue;
            }
            javaXmlTypeMapping.qnameScope = qnameScope;
        } else if (("variable-mapping" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: variableMapping
            final VariableMapping variableMappingItem = readVariableMapping(elementReader, context);
            if (variableMapping == null) {
                variableMapping = javaXmlTypeMapping.variableMapping;
                if (variableMapping != null) {
                    variableMapping.clear();
                } else {
                    variableMapping = new ArrayList<VariableMapping>();
                }
            }
            variableMapping.add(variableMappingItem);
        } else {
            context.unexpectedElement(elementReader, new QName("http://java.sun.com/xml/ns/javaee", "java-type"), new QName("http://java.sun.com/xml/ns/javaee", "root-type-qname"), new QName("http://java.sun.com/xml/ns/javaee", "anonymous-type-qname"), new QName("http://java.sun.com/xml/ns/javaee", "qname-scope"), new QName("http://java.sun.com/xml/ns/javaee", "variable-mapping"));
        }
    }
    if (variableMapping != null) {
        javaXmlTypeMapping.variableMapping = variableMapping;
    }
    context.afterUnmarshal(javaXmlTypeMapping, LifecycleCallback.NONE);
    return javaXmlTypeMapping;
}
Also used : CollapsedStringAdapter(javax.xml.bind.annotation.adapters.CollapsedStringAdapter) Attribute(org.metatype.sxc.util.Attribute) VariableMapping$JAXB.readVariableMapping(org.apache.openejb.jee.VariableMapping$JAXB.readVariableMapping) VariableMapping$JAXB.writeVariableMapping(org.apache.openejb.jee.VariableMapping$JAXB.writeVariableMapping) QName(javax.xml.namespace.QName) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext) XoXMLStreamReader(org.metatype.sxc.util.XoXMLStreamReader)

Example 2 with VariableMapping

use of org.apache.openejb.jee.VariableMapping in project tomee by apache.

the class JavaXmlTypeMapping$JAXB method _write.

public static final void _write(final XoXMLStreamWriter writer, final JavaXmlTypeMapping javaXmlTypeMapping, RuntimeContext context) throws Exception {
    if (javaXmlTypeMapping == null) {
        writer.writeXsiNil();
        return;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final String prefix = writer.getUniquePrefix("http://java.sun.com/xml/ns/javaee");
    if (JavaXmlTypeMapping.class != javaXmlTypeMapping.getClass()) {
        context.unexpectedSubclass(writer, javaXmlTypeMapping, JavaXmlTypeMapping.class);
        return;
    }
    context.beforeMarshal(javaXmlTypeMapping, LifecycleCallback.NONE);
    // ATTRIBUTE: id
    final String idRaw = javaXmlTypeMapping.id;
    if (idRaw != null) {
        String id = null;
        try {
            id = Adapters.collapsedStringAdapterAdapter.marshal(idRaw);
        } catch (final Exception e) {
            context.xmlAdapterError(javaXmlTypeMapping, "id", CollapsedStringAdapter.class, String.class, String.class, e);
        }
        writer.writeAttribute("", "", "id", id);
    }
    // ELEMENT: javaType
    final String javaTypeRaw = javaXmlTypeMapping.javaType;
    String javaType = null;
    try {
        javaType = Adapters.collapsedStringAdapterAdapter.marshal(javaTypeRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(javaXmlTypeMapping, "javaType", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (javaType != null) {
        writer.writeStartElement(prefix, "java-type", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(javaType);
        writer.writeEndElement();
    } else {
        context.unexpectedNullValue(javaXmlTypeMapping, "javaType");
    }
    // ELEMENT: rootTypeQname
    final QName rootTypeQname = javaXmlTypeMapping.rootTypeQname;
    if (rootTypeQname != null) {
        writer.writeStartElement(prefix, "root-type-qname", "http://java.sun.com/xml/ns/javaee");
        writer.writeQName(rootTypeQname);
        writer.writeEndElement();
    }
    // ELEMENT: anonymousTypeQname
    final String anonymousTypeQnameRaw = javaXmlTypeMapping.anonymousTypeQname;
    String anonymousTypeQname = null;
    try {
        anonymousTypeQname = Adapters.collapsedStringAdapterAdapter.marshal(anonymousTypeQnameRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(javaXmlTypeMapping, "anonymousTypeQname", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (anonymousTypeQname != null) {
        writer.writeStartElement(prefix, "anonymous-type-qname", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(anonymousTypeQname);
        writer.writeEndElement();
    }
    // ELEMENT: qnameScope
    final String qnameScopeRaw = javaXmlTypeMapping.qnameScope;
    String qnameScope = null;
    try {
        qnameScope = Adapters.collapsedStringAdapterAdapter.marshal(qnameScopeRaw);
    } catch (final Exception e) {
        context.xmlAdapterError(javaXmlTypeMapping, "qnameScope", CollapsedStringAdapter.class, String.class, String.class, e);
    }
    if (qnameScope != null) {
        writer.writeStartElement(prefix, "qname-scope", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters(qnameScope);
        writer.writeEndElement();
    } else {
        context.unexpectedNullValue(javaXmlTypeMapping, "qnameScope");
    }
    // ELEMENT: variableMapping
    final List<VariableMapping> variableMapping = javaXmlTypeMapping.variableMapping;
    if (variableMapping != null) {
        for (final VariableMapping variableMappingItem : variableMapping) {
            if (variableMappingItem != null) {
                writer.writeStartElement(prefix, "variable-mapping", "http://java.sun.com/xml/ns/javaee");
                writeVariableMapping(writer, variableMappingItem, context);
                writer.writeEndElement();
            }
        }
    }
    context.afterMarshal(javaXmlTypeMapping, LifecycleCallback.NONE);
}
Also used : CollapsedStringAdapter(javax.xml.bind.annotation.adapters.CollapsedStringAdapter) QName(javax.xml.namespace.QName) VariableMapping$JAXB.readVariableMapping(org.apache.openejb.jee.VariableMapping$JAXB.readVariableMapping) VariableMapping$JAXB.writeVariableMapping(org.apache.openejb.jee.VariableMapping$JAXB.writeVariableMapping) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext)

Example 3 with VariableMapping

use of org.apache.openejb.jee.VariableMapping in project tomee by apache.

the class HeavyweightTypeInfoBuilder method mapFields.

/**
     * Map the (nested) fields of a XML Schema Type to Java Beans properties or public fields of the specified Java Class.
     *
     * @param javaClass          the java class to map
     * @param xmlTypeInfo        the xml schema for the type
     * @param javaXmlTypeMapping the java to xml type mapping metadata
     * @param typeInfo           the JaxRpcTypeInfo for this type
     * @throws OpenEJBException if the XML Schema Type can not be mapped to the Java Class
     */
private void mapFields(Class javaClass, XmlTypeInfo xmlTypeInfo, JavaXmlTypeMapping javaXmlTypeMapping, JaxRpcTypeInfo typeInfo) throws OpenEJBException {
    // Skip arrays since they can't define a variable-mapping element
    if (!javaClass.isArray()) {
        // if there is a variable-mapping, log a warning
        if (!javaXmlTypeMapping.getVariableMapping().isEmpty()) {
            log.warn("Ignoring variable-mapping defined for class " + javaClass + " which is an array.");
        }
        return;
    }
    // Index Java bean properties by name
    Map<String, Class> properties = new HashMap<String, Class>();
    try {
        PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(javaClass).getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            properties.put(propertyDescriptor.getName(), propertyDescriptor.getPropertyType());
        }
    } catch (IntrospectionException e) {
        throw new OpenEJBException("Class " + javaClass + " is not a valid javabean", e);
    }
    for (VariableMapping variableMapping : javaXmlTypeMapping.getVariableMapping()) {
        String fieldName = variableMapping.getJavaVariableName();
        if (variableMapping.getXmlAttributeName() != null) {
            JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo();
            fieldInfo.name = fieldName;
            // verify that the property exists on the java class
            Class javaType = properties.get(fieldName);
            if (javaType == null) {
                throw new OpenEJBException("field name " + fieldName + " not found in " + properties);
            }
            String attributeLocalName = variableMapping.getXmlAttributeName();
            QName xmlName = new QName("", attributeLocalName);
            fieldInfo.xmlName = xmlName;
            fieldInfo.xmlType = xmlTypeInfo.attributes.get(attributeLocalName);
            if (fieldInfo.xmlType == null) {
                throw new OpenEJBException("attribute " + xmlName + " not found in schema " + xmlTypeInfo.qname);
            }
            typeInfo.fields.add(fieldInfo);
        } else {
            JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo();
            fieldInfo.isElement = true;
            fieldInfo.name = fieldName;
            // verify that the property exists on the java class or there is a public field
            Class javaType = properties.get(fieldName);
            if (javaType == null) {
                //see if it is a public field
                try {
                    Field field = javaClass.getField(fieldName);
                    javaType = field.getType();
                } catch (NoSuchFieldException e) {
                    throw new OpenEJBException("field name " + fieldName + " not found in " + properties, e);
                }
            }
            QName xmlName = new QName("", variableMapping.getXmlElementName());
            XmlElementInfo nestedElement = xmlTypeInfo.elements.get(xmlName);
            if (nestedElement == null) {
                String ns = xmlTypeInfo.qname.getNamespaceURI();
                xmlName = new QName(ns, variableMapping.getXmlElementName());
                nestedElement = xmlTypeInfo.elements.get(xmlName);
                if (nestedElement == null) {
                    throw new OpenEJBException("element " + xmlName + " not found in schema " + xmlTypeInfo.qname);
                }
            }
            fieldInfo.isNillable = nestedElement.nillable || hasEncoded;
            fieldInfo.xmlName = xmlName;
            // xml type
            if (nestedElement.xmlType != null) {
                fieldInfo.xmlType = nestedElement.xmlType;
            } else {
                QName anonymousName;
                if (xmlTypeInfo.anonymous) {
                    anonymousName = new QName(xmlTypeInfo.qname.getNamespaceURI(), xmlTypeInfo.qname.getLocalPart() + ">" + nestedElement.qname.getLocalPart());
                } else {
                    anonymousName = new QName(xmlTypeInfo.qname.getNamespaceURI(), ">" + xmlTypeInfo.qname.getLocalPart() + ">" + nestedElement.qname.getLocalPart());
                }
                fieldInfo.xmlType = anonymousName;
            }
            if (javaType.isArray()) {
                fieldInfo.minOccurs = nestedElement.minOccurs;
                fieldInfo.maxOccurs = nestedElement.maxOccurs;
            }
            typeInfo.fields.add(fieldInfo);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) VariableMapping(org.apache.openejb.jee.VariableMapping) QName(javax.xml.namespace.QName) IntrospectionException(java.beans.IntrospectionException) Field(java.lang.reflect.Field)

Aggregations

QName (javax.xml.namespace.QName)3 CollapsedStringAdapter (javax.xml.bind.annotation.adapters.CollapsedStringAdapter)2 VariableMapping$JAXB.readVariableMapping (org.apache.openejb.jee.VariableMapping$JAXB.readVariableMapping)2 VariableMapping$JAXB.writeVariableMapping (org.apache.openejb.jee.VariableMapping$JAXB.writeVariableMapping)2 RuntimeContext (org.metatype.sxc.jaxb.RuntimeContext)2 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 VariableMapping (org.apache.openejb.jee.VariableMapping)1 Attribute (org.metatype.sxc.util.Attribute)1 XoXMLStreamReader (org.metatype.sxc.util.XoXMLStreamReader)1