Search in sources :

Example 11 with JAXBException

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

the class ExceptionHandlingTestCases method testInvalidLocation.

/**
 * Tests declaration of a non-existent class via eclipselink-oxm.xml
 *
 * Negative test.
 */
public void testInvalidLocation() {
    String metadataFile = PATH + "eclipselink_doesnt_exist-oxm.xml";
    InputStream iStream = getClass().getClassLoader().getResourceAsStream(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) {
        x.printStackTrace();
    }
    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 12 with JAXBException

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

the class ExceptionHandlingTestCases method testInvalidMapParameterTypeNullKey.

/**
 * Tests an invalid parameter type by setting a null Key.
 *
 * Negative test.
 */
public void testInvalidMapParameterTypeNullKey() {
    Map<Class<?>, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put(null, new StreamSource());
    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) 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 13 with JAXBException

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

the class NonRefreshableMetadataTestCases method testNonRefreshableMetadata.

public void testNonRefreshableMetadata() throws Exception {
    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream metadataStream = classLoader.getResourceAsStream(XML_METADATA);
    Map<String, Object> props = new HashMap<String, Object>(1);
    props.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataStream);
    JAXBContext jc = JAXBContextFactory.createContext(new Class<?>[] { Root.class }, props);
    try {
        JAXBHelper.getJAXBContext(jc).refreshMetadata();
    } catch (JAXBException e) {
        assertEquals(JAXBException.COULD_NOT_UNMARSHAL_METADATA, e.getErrorCode());
        return;
    }
    fail();
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) JAXBException(org.eclipse.persistence.exceptions.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext)

Example 14 with JAXBException

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

the class AnnotationsProcessor method buildNewProperty.

/*
    * Create a new Property Object and process the annotations that are common
    * to fields and methods
     */
Property buildNewProperty(TypeInfo info, JavaClass cls, JavaHasAnnotations javaHasAnnotations, String propertyName, JavaClass ptype) {
    Property property = null;
    if (helper.isAnnotationPresent(javaHasAnnotations, XmlElements.class)) {
        property = buildChoiceProperty(javaHasAnnotations);
    } else if (helper.isAnnotationPresent(javaHasAnnotations, XmlElementRef.class) || helper.isAnnotationPresent(javaHasAnnotations, XmlElementRefs.class)) {
        findAndProcessObjectFactory(cls);
        property = buildReferenceProperty(info, javaHasAnnotations, propertyName, ptype);
        if (helper.isAnnotationPresent(javaHasAnnotations, XmlAnyElement.class)) {
            XmlAnyElement anyElement = (XmlAnyElement) helper.getAnnotation(javaHasAnnotations, XmlAnyElement.class);
            property.setIsAny(true);
            if (anyElement.value() != null) {
                property.setDomHandlerClassName(anyElement.value().getName());
            }
            property.setLax(anyElement.lax());
            info.setAnyElementPropertyName(propertyName);
        }
    } else if (helper.isAnnotationPresent(javaHasAnnotations, XmlAnyElement.class)) {
        findAndProcessObjectFactory(cls);
        XmlAnyElement anyElement = (XmlAnyElement) helper.getAnnotation(javaHasAnnotations, XmlAnyElement.class);
        property = new Property(helper);
        property.setIsAny(true);
        if (anyElement.value() != null) {
            property.setDomHandlerClassName(anyElement.value().getName());
        }
        property.setLax(anyElement.lax());
        info.setAnyElementPropertyName(propertyName);
    } else if (helper.isAnnotationPresent(javaHasAnnotations, org.eclipse.persistence.oxm.annotations.XmlTransformation.class) || helper.isAnnotationPresent(javaHasAnnotations, org.eclipse.persistence.oxm.annotations.XmlReadTransformer.class) || helper.isAnnotationPresent(javaHasAnnotations, org.eclipse.persistence.oxm.annotations.XmlWriteTransformer.class) || helper.isAnnotationPresent(javaHasAnnotations, XmlWriteTransformers.class)) {
        property = buildTransformationProperty(javaHasAnnotations, cls);
    } else {
        property = new Property(helper);
    }
    property.setPropertyName(propertyName);
    property.setElement(javaHasAnnotations);
    // check the class
    if (helper.isCollectionType(ptype)) {
        JavaClass componentType = helper.getJavaClass(Object.class);
        Collection typeArgs = ptype.getActualTypeArguments();
        if (!typeArgs.isEmpty()) {
            componentType = (JavaClass) typeArgs.iterator().next();
        }
        updatePropertyType(property, ptype, componentType);
    } else {
        updatePropertyType(property, ptype, ptype);
    }
    if (helper.isAnnotationPresent(javaHasAnnotations, XmlVariableNode.class)) {
        XmlVariableNode variableNode = (XmlVariableNode) helper.getAnnotation(javaHasAnnotations, XmlVariableNode.class);
        if (variableNode.type() != XmlVariableNode.DEFAULT.class) {
            property.setVariableClassName(variableNode.type().getName());
            JavaClass componentType = helper.getJavaClass(variableNode.type());
            if (helper.isCollectionType(ptype)) {
                property.setGenericType(componentType);
            } else {
                property.setType(componentType);
            }
        }
        if (!variableNode.value().equals("##default")) {
            property.setVariableAttributeName(variableNode.value());
        }
        property.setVariableNodeAttribute(variableNode.attribute());
    }
    if ((ptype.isArray() && !areEquals(ptype, byte[].class)) || (helper.isCollectionType(ptype) && !helper.isAnnotationPresent(javaHasAnnotations, XmlList.class))) {
        property.setNillable(true);
    }
    processPropertyAnnotations(info, cls, javaHasAnnotations, property);
    if (helper.isAnnotationPresent(javaHasAnnotations, XmlPath.class)) {
        XmlPath xmlPath = (XmlPath) helper.getAnnotation(javaHasAnnotations, XmlPath.class);
        property.setXmlPath(xmlPath.value());
        Field<XMLConversionManager, NamespaceResolver> tempField = new XMLField(xmlPath.value());
        boolean isAttribute = tempField.getLastXPathFragment().isAttribute();
        property.setIsAttribute(isAttribute);
        // set schema name
        String schemaName = XMLProcessor.getNameFromXPath(xmlPath.value(), property.getPropertyName(), isAttribute);
        QName qName;
        NamespaceInfo nsInfo = getPackageInfoForPackage(cls).getNamespaceInfo();
        if (isAttribute) {
            if (nsInfo.isAttributeFormQualified()) {
                qName = new QName(nsInfo.getNamespace(), schemaName);
            } else {
                qName = new QName(schemaName);
            }
        } else {
            if (nsInfo.isElementFormQualified()) {
                qName = new QName(nsInfo.getNamespace(), schemaName);
            } else {
                qName = new QName(schemaName);
            }
        }
        property.setSchemaName(qName);
        // create properties for any predicates
        XPathFragment fragment = tempField.getXPathFragment();
        String currentPath = "";
        while (fragment != null && !(fragment.nameIsText()) && !(fragment.isAttribute())) {
            if (fragment.getPredicate() != null) {
                // can't append xpath directly since it will contain the predicate
                String fragmentPath = fragment.getLocalName();
                if (fragment.getPrefix() != null && !(Constants.EMPTY_STRING.equals(fragment.getPrefix()))) {
                    fragmentPath = fragment.getPrefix() + ":" + fragmentPath;
                }
                currentPath += fragmentPath;
                String predicatePath = currentPath;
                TypeInfo targetInfo = info;
                if (fragment.getNextFragment() == null) {
                    // if this is the last fragment, and there's no text after, then this is
                    // complex. May need to add the attribute property to the target type.
                    processReferencedClass(ptype);
                    TypeInfo predicateTypeInfo = typeInfos.get(ptype.getQualifiedName());
                    if (predicateTypeInfo != null) {
                        targetInfo = predicateTypeInfo;
                        predicatePath = "";
                    }
                }
                Property predicateProperty = new Property(helper);
                predicateProperty.setType(helper.getJavaClass("java.lang.String"));
                if (predicatePath.length() > 0) {
                    predicatePath += "/";
                }
                predicatePath += fragment.getPredicate().getXPathFragment().getXPath();
                predicateProperty.setXmlPath(predicatePath);
                predicateProperty.setIsAttribute(true);
                String predschemaName = XMLProcessor.getNameFromXPath(predicatePath, property.getPropertyName(), true);
                QName predQname;
                if (nsInfo.isAttributeFormQualified()) {
                    predQname = new QName(nsInfo.getNamespace(), predschemaName);
                } else {
                    predQname = new QName(predschemaName);
                }
                predicateProperty.setSchemaName(predQname);
                if (!targetInfo.hasPredicateProperty(predicateProperty)) {
                    targetInfo.getPredicateProperties().add(predicateProperty);
                }
            } else {
                currentPath += fragment.getXPath();
            }
            currentPath += "/";
            fragment = fragment.getNextFragment();
        }
    } else {
        property.setSchemaName(getQNameForProperty(property, propertyName, javaHasAnnotations, getPackageInfoForPackage(cls).getNamespaceInfo(), info));
    }
    ptype = property.getActualType();
    if (ptype.isPrimitive()) {
        if (property.getType().isArray() && helper.isAnnotationPresent(javaHasAnnotations, XmlElement.class)) {
            XmlElement elemAnno = (XmlElement) helper.getAnnotation(javaHasAnnotations, XmlElement.class);
            property.setIsRequired(elemAnno.required());
        } else {
            property.setIsRequired(true);
        }
    }
    // apply class level adapters - don't override property level adapter
    if (!property.isSetXmlJavaTypeAdapter()) {
        TypeInfo refClassInfo = getTypeInfos().get(ptype.getQualifiedName());
        if (refClassInfo != null && refClassInfo.isSetXmlJavaTypeAdapter()) {
            org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter xmlJavaTypeAdapter = null;
            try {
                xmlJavaTypeAdapter = refClassInfo.getXmlJavaTypeAdapter();
                property.setXmlJavaTypeAdapter(refClassInfo.getXmlJavaTypeAdapter());
            } catch (JAXBException e) {
                throw JAXBException.invalidTypeAdapterClass(xmlJavaTypeAdapter.getValue(), cls.getName());
            }
        }
    }
    if (property.isXmlTransformation()) {
        referencedByTransformer.add(property.getType().getName());
    }
    return property;
}
Also used : XmlPath(org.eclipse.persistence.oxm.annotations.XmlPath) XmlVariableNode(org.eclipse.persistence.oxm.annotations.XmlVariableNode) XmlAnyElement(jakarta.xml.bind.annotation.XmlAnyElement) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XmlWriteTransformer(org.eclipse.persistence.jaxb.xmlmodel.XmlTransformation.XmlWriteTransformer) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty) XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) JAXBException(org.eclipse.persistence.exceptions.JAXBException) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) Collection(java.util.Collection) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XmlElement(jakarta.xml.bind.annotation.XmlElement) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 15 with JAXBException

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

the class AnnotationsProcessor method findAndProcessObjectFactory.

void findAndProcessObjectFactory(String objectFactoryClassName) {
    // need to make sure objectfactory gets processed.
    try {
        if (objectFactoryClassNames.contains(objectFactoryClassName)) {
            return;
        }
        JavaClass javaClass = helper.getJavaClass(objectFactoryClassName);
        if (isXmlRegistry(javaClass)) {
            JavaClass[] processed = this.processObjectFactory(javaClass, new ArrayList<>());
            preBuildTypeInfo(processed);
            buildTypeInfo(processed);
            updateGlobalElements(processed);
        }
    } catch (JAXBException ignored) {
    /* ignored */
    }
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

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