Search in sources :

Example 16 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class XMLProcessor method processXmlValue.

private Property processXmlValue(XmlValue xmlValue, Property oldProperty, TypeInfo info, JavaType javaType) {
    // reset any existing values
    resetProperty(oldProperty, info);
    oldProperty.setIsXmlValue(true);
    oldProperty.setIsXmlValueExtension(true);
    info.setXmlValueProperty(oldProperty);
    // handle get/set methods
    if (xmlValue.getXmlAccessMethods() != null) {
        oldProperty.setMethodProperty(true);
        oldProperty.setGetMethodName(xmlValue.getXmlAccessMethods().getGetMethod());
        oldProperty.setSetMethodName(xmlValue.getXmlAccessMethods().getSetMethod());
    }
    // check for container type
    if (!xmlValue.getContainerType().equals(DEFAULT)) {
        setContainerType(oldProperty, xmlValue.getContainerType());
    }
    // set type
    if (!xmlValue.getType().equals(DEFAULT)) {
        JavaClass pType = jModelInput.getJavaModel().getClass(xmlValue.getType());
        if (aProcessor.getHelper().isCollectionType(oldProperty.getType())) {
            oldProperty.setGenericType(pType);
        } else {
            oldProperty.setType(pType);
        }
        oldProperty.setHasXmlElementType(true);
        // may need to generate a type info for the type
        if (aProcessor.shouldGenerateTypeInfo(pType) && aProcessor.getTypeInfos().get(pType.getQualifiedName()) == null) {
            aProcessor.buildNewTypeInfo(new JavaClass[] { pType });
        }
    }
    reapplyPackageAndClassAdapters(oldProperty, info);
    // handle XmlJavaTypeAdapter
    if (xmlValue.getXmlJavaTypeAdapter() != null) {
        try {
            oldProperty.setXmlJavaTypeAdapter(xmlValue.getXmlJavaTypeAdapter());
        } catch (JAXBException e) {
            throw JAXBException.invalidPropertyAdapterClass(xmlValue.getXmlJavaTypeAdapter().getValue(), xmlValue.getJavaAttribute(), javaType.getName());
        }
    }
    // handle read-only
    if (xmlValue.isSetReadOnly()) {
        oldProperty.setReadOnly(xmlValue.isReadOnly());
    }
    // handle write-only
    if (xmlValue.isSetWriteOnly()) {
        oldProperty.setWriteOnly(xmlValue.isWriteOnly());
    }
    // handle cdata
    if (xmlValue.isSetCdata()) {
        oldProperty.setCdata(xmlValue.isCdata());
    }
    // handle null policy
    if (xmlValue.getXmlAbstractNullPolicy() != null) {
        JAXBElement<? extends XmlAbstractNullPolicy> jaxbElt = xmlValue.getXmlAbstractNullPolicy();
        oldProperty.setNullPolicy(jaxbElt.getValue());
    }
    // set user-defined properties
    if (xmlValue.getXmlProperties() != null && xmlValue.getXmlProperties().getXmlProperty().size() > 0) {
        oldProperty.setUserProperties(createUserPropertyMap(xmlValue.getXmlProperties().getXmlProperty()));
    }
    return oldProperty;
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 17 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class XMLProcessor method processXmlElement.

/**
 * XmlElement override will completely replace the existing values.
 */
private Property processXmlElement(XmlElement xmlElement, Property oldProperty, TypeInfo typeInfo, NamespaceInfo nsInfo, JavaType javaType) {
    // reset any existing values
    resetProperty(oldProperty, typeInfo);
    if (xmlElement.getXmlMap() != null) {
        processXmlMap(xmlElement.getXmlMap(), oldProperty);
    }
    if (xmlElement.isXmlLocation()) {
        if (!aProcessor.getHelper().getJavaClass(Constants.LOCATOR_CLASS).isAssignableFrom(oldProperty.getType())) {
            throw JAXBException.invalidXmlLocation(oldProperty.getPropertyName(), oldProperty.getType().getName());
        }
        oldProperty.setXmlLocation(true);
    }
    // handle xml-id
    if (xmlElement.isXmlId()) {
        oldProperty.setIsXmlId(true);
        oldProperty.setIsXmlIdExtension(true);
        typeInfo.setIDProperty(oldProperty);
    } else {
        // account for XmlID un-set via XML
        if (typeInfo.getIDProperty() != null && typeInfo.getIDProperty().getPropertyName().equals(oldProperty.getPropertyName())) {
            typeInfo.setIDProperty(null);
        }
    }
    if (xmlElement.getXmlInverseReference() != null) {
        String mappedBy = xmlElement.getXmlInverseReference().getMappedBy();
        oldProperty.setInverseReference(true, true);
        oldProperty.setInverseReferencePropertyName(mappedBy);
    }
    // handle xml-idref
    oldProperty.setIsXmlIdRef(xmlElement.isXmlIdref());
    // handle xml-key
    if (xmlElement.isXmlKey()) {
        typeInfo.addXmlKeyProperty(oldProperty);
    }
    // set required
    oldProperty.setIsRequired(xmlElement.isRequired());
    // set xml-inline-binary-data
    oldProperty.setisInlineBinaryData(xmlElement.isXmlInlineBinaryData());
    // set nillable
    oldProperty.setNillable(xmlElement.isNillable());
    // set defaultValue
    if (xmlElement.getDefaultValue().equals("\u0000")) {
        oldProperty.setDefaultValue(null);
    } else {
        oldProperty.setDefaultValue(xmlElement.getDefaultValue());
    }
    String name;
    String namespace;
    // if xml-path is set, we ignore xml-element-wrapper, as well as name/namespace on xml-element
    if (xmlElement.getXmlPath() != null) {
        oldProperty.setXmlPath(xmlElement.getXmlPath());
        name = getNameFromXPath(xmlElement.getXmlPath(), oldProperty.getPropertyName(), false);
        namespace = DEFAULT;
    } else {
        // no xml-path, so use name/namespace from xml-element, and process wrapper
        name = xmlElement.getName();
        namespace = xmlElement.getNamespace();
        XmlElementWrapper xmlElementWrapper = xmlElement.getXmlElementWrapper();
        if (xmlElementWrapper != null) {
            if (DEFAULT.equals(xmlElementWrapper.getName())) {
                xmlElementWrapper.setName(typeInfo.getXmlNameTransformer().transformElementName(oldProperty.getPropertyName()));
            }
            oldProperty.setXmlElementWrapper(xmlElement.getXmlElementWrapper());
            if (oldProperty.isMap()) {
                name = xmlElement.getXmlElementWrapper().getName();
                namespace = xmlElement.getXmlElementWrapper().getNamespace();
            }
        }
    }
    if (javaType.getXmlType() != null && javaType.getXmlType().getNamespace() != null && (xmlElement.getNamespace() != null && xmlElement.getNamespace().equals(DEFAULT))) {
        // Inherit type-level namespace if there is one
        namespace = javaType.getXmlType().getNamespace();
    }
    // set schema name
    QName qName;
    if (name.equals(DEFAULT)) {
        name = typeInfo.getXmlNameTransformer().transformElementName(oldProperty.getPropertyName());
    }
    if (namespace.equals(DEFAULT)) {
        if (nsInfo.isElementFormQualified()) {
            qName = new QName(nsInfo.getNamespace(), name);
        } else {
            qName = new QName(name);
        }
    } else {
        qName = new QName(namespace, name);
    }
    oldProperty.setSchemaName(qName);
    // check for container type
    if (!xmlElement.getContainerType().equals(DEFAULT)) {
        setContainerType(oldProperty, xmlElement.getContainerType());
    }
    // set type
    if (xmlElement.getType().equals("jakarta.xml.bind.annotation.XmlElement.DEFAULT")) {
        // @XmlElement, reset it to the original value
        if (oldProperty.isXmlElementType()) {
            oldProperty.setType(oldProperty.getOriginalType());
        }
    } else if (xmlElement.getXmlMap() != null) {
        getLogger().logWarning(JAXBMetadataLogger.INVALID_TYPE_ON_MAP, new Object[] { xmlElement.getName() });
    } else {
        JavaClass pType = jModelInput.getJavaModel().getClass(xmlElement.getType());
        if (aProcessor.getHelper().isCollectionType(oldProperty.getType())) {
            oldProperty.setGenericType(pType);
        } else {
            oldProperty.setType(pType);
        }
        oldProperty.setHasXmlElementType(true);
        // may need to generate a type info for the type
        if (aProcessor.shouldGenerateTypeInfo(pType) && aProcessor.getTypeInfos().get(pType.getQualifiedName()) == null) {
            aProcessor.buildNewTypeInfo(new JavaClass[] { pType });
        }
    }
    reapplyPackageAndClassAdapters(oldProperty, typeInfo);
    // handle XmlJavaTypeAdapter
    if (xmlElement.getXmlJavaTypeAdapter() != null) {
        try {
            oldProperty.setXmlJavaTypeAdapter(xmlElement.getXmlJavaTypeAdapter());
        } catch (JAXBException e) {
            throw JAXBException.invalidPropertyAdapterClass(xmlElement.getXmlJavaTypeAdapter().getValue(), xmlElement.getJavaAttribute(), javaType.getName());
        }
    }
    // for primitives we always set required, a.k.a. minOccurs="1"
    if (!oldProperty.isRequired()) {
        JavaClass ptype = oldProperty.getActualType();
        oldProperty.setIsRequired(ptype.isPrimitive() || ptype.isArray() && ptype.getComponentType().isPrimitive());
    }
    // handle xml-list
    if (xmlElement.isSetXmlList()) {
        // Make sure XmlList annotation is on a collection or array
        if (!aProcessor.getHelper().isCollectionType(oldProperty.getType()) && !oldProperty.getType().isArray()) {
            throw JAXBException.invalidList(oldProperty.getPropertyName());
        }
        oldProperty.setIsXmlList(xmlElement.isXmlList());
    }
    // handle xml-mime-type
    if (xmlElement.getXmlMimeType() != null) {
        oldProperty.setMimeType(xmlElement.getXmlMimeType());
    }
    // handle xml-attachment-ref
    if (xmlElement.isXmlAttachmentRef()) {
        oldProperty.setIsSwaAttachmentRef(true);
        oldProperty.setSchemaType(Constants.SWA_REF_QNAME);
    } else if (aProcessor.isMtomAttachment(oldProperty)) {
        oldProperty.setIsMtomAttachment(true);
        oldProperty.setSchemaType(Constants.BASE_64_BINARY_QNAME);
    }
    // handle xml-schema-type
    if (xmlElement.getXmlSchemaType() != null) {
        oldProperty.setSchemaType(new QName(xmlElement.getXmlSchemaType().getNamespace(), xmlElement.getXmlSchemaType().getName()));
    }
    // handle get/set methods
    if (xmlElement.getXmlAccessMethods() != null) {
        oldProperty.setMethodProperty(true);
        oldProperty.setGetMethodName(xmlElement.getXmlAccessMethods().getGetMethod());
        oldProperty.setSetMethodName(xmlElement.getXmlAccessMethods().getSetMethod());
    }
    // handle read-only
    if (xmlElement.isSetReadOnly()) {
        oldProperty.setReadOnly(xmlElement.isReadOnly());
    }
    // handle write-only
    if (xmlElement.isSetWriteOnly()) {
        oldProperty.setWriteOnly(xmlElement.isWriteOnly());
    }
    // handle cdata
    if (xmlElement.isSetCdata()) {
        oldProperty.setCdata(xmlElement.isCdata());
    }
    // handle null policy
    if (xmlElement.getXmlAbstractNullPolicy() != null) {
        JAXBElement<? extends XmlAbstractNullPolicy> jaxbElt = xmlElement.getXmlAbstractNullPolicy();
        oldProperty.setNullPolicy(jaxbElt.getValue());
    }
    // set user-defined properties
    if (xmlElement.getXmlProperties() != null && xmlElement.getXmlProperties().getXmlProperty().size() > 0) {
        oldProperty.setUserProperties(createUserPropertyMap(xmlElement.getXmlProperties().getXmlProperty()));
    }
    return oldProperty;
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) QName(javax.xml.namespace.QName) JAXBException(org.eclipse.persistence.exceptions.JAXBException) XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper)

Example 18 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class ExceptionHandlingTestCases method testInvalidLocation2.

/**
 * Tests declaration of a non-existent class via eclipselink-oxm.xml
 *
 * Negative test.
 */
public void testInvalidLocation2() {
    String metadataFile = PATH + "eclipselink_doesnt_exist-oxm.xml";
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put(CONTEXT_PATH, new StreamSource(metadataFile));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    try {
        JAXBContextFactory.createContext(CONTEXT_PATH, getClass().getClassLoader(), properties);
    } catch (JAXBException e) {
        return;
    } catch (Exception x) {
        x.printStackTrace();
    }
    fail("The expected JAXBException was not thrown.");
}
Also used : HashMap(java.util.HashMap) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(org.eclipse.persistence.exceptions.JAXBException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 19 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class ExceptionHandlingTestCases method testInvalidClassName.

/**
 * Tests declaration of a non-existent class via eclipselink-oxm.xml
 *
 * Negative test.
 */
public void testInvalidClassName() {
    String metadataFile = PATH + "eclipselink-oxm.xml";
    InputStream iStream = getClass().getClassLoader().getResourceAsStream(metadataFile);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + metadataFile + "]");
    }
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put(CONTEXT_PATH, new StreamSource(iStream));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    try {
        JAXBContextFactory.createContext(CONTEXT_PATH, getClass().getClassLoader(), properties);
    } catch (JAXBException e) {
        return;
    } catch (Exception x) {
    }
    fail("The expected JAXBException was not thrown.");
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(org.eclipse.persistence.exceptions.JAXBException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 20 with JAXBException

use of org.eclipse.persistence.exceptions.JAXBException in project eclipselink by eclipse-ee4j.

the class ExceptionHandlingTestCases method testInvalidPackageAsKey.

/**
 * Tests declaration of a non-existent package
 *
 * Negative test.
 */
public void testInvalidPackageAsKey() {
    String validPath = "org/eclipse/persistence/testing/jaxb/externalizedmetadata/jaxbcontextfactory/";
    String contextPath = "org.eclipse.persistence.testing.jaxb.externalizedmetadata.jaxbcontextfactory";
    String metadataFile = validPath + "eclipselink-oxm.xml";
    InputStream iStream = getClass().getClassLoader().getResourceAsStream(metadataFile);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + metadataFile + "]");
    }
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put("java.util", new StreamSource(iStream));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    try {
        JAXBContextFactory.createContext(contextPath, getClass().getClassLoader(), properties);
    } catch (jakarta.xml.bind.JAXBException e) {
        assertTrue(e.getLinkedException() instanceof JAXBException);
        assertEquals(JAXBException.JAVATYPE_NOT_ALLOWED_IN_BINDINGS_FILE, ((JAXBException) e.getLinkedException()).getErrorCode());
        return;
    }
    fail("The expected JAXBException was not thrown.");
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(org.eclipse.persistence.exceptions.JAXBException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

JAXBException (org.eclipse.persistence.exceptions.JAXBException)24 HashMap (java.util.HashMap)11 Source (javax.xml.transform.Source)9 StreamSource (javax.xml.transform.stream.StreamSource)9 InputStream (java.io.InputStream)7 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)7 JAXBContext (jakarta.xml.bind.JAXBContext)6 ArrayList (java.util.ArrayList)3 QName (javax.xml.namespace.QName)3 XmlProperty (org.eclipse.persistence.oxm.annotations.XmlProperty)3 XmlTransient (jakarta.xml.bind.annotation.XmlTransient)2 Type (java.lang.reflect.Type)2 TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)2 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)2 XmlAnyElement (jakarta.xml.bind.annotation.XmlAnyElement)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collection (java.util.Collection)1