Search in sources :

Example 1 with PropertyException

use of jakarta.xml.bind.PropertyException in project resteasy by resteasy.

the class JaxbNamespacePrefixTest method testNamespacePrefix.

/**
 * @tpTestDetails Create xml schema from provided class and set "namespacePrefixMapper" for the Marshaller
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testNamespacePrefix() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(JaxbNameSpacePrefixPurchaseOrderType.class);
    JaxbNameSpacePrefixPurchaseOrderType po = new JaxbNameSpacePrefixPurchaseOrderType();
    JaxbNameSpacePrefixItems jaxbNameSpacePrefixItems = new JaxbNameSpacePrefixItems();
    JaxbNameSpacePrefixItem jaxbNameSpacePrefixItem = new JaxbNameSpacePrefixItem();
    jaxbNameSpacePrefixItem.setComment("Tetsing");
    jaxbNameSpacePrefixItem.setPartNum("242-GZ");
    jaxbNameSpacePrefixItem.setProductName("My Thing");
    jaxbNameSpacePrefixItem.setQuantity(6);
    jaxbNameSpacePrefixItem.setUSPrice(new BigDecimal(13.99));
    jaxbNameSpacePrefixItems.getJaxbNameSpacePrefixItem().add(jaxbNameSpacePrefixItem);
    po.setJaxbNameSpacePrefixItems(jaxbNameSpacePrefixItems);
    Marshaller marshaller = RiHacks.createMarshaller(ctx);
    XmlSchema xmlSchema = JaxbNameSpacePrefixPurchaseOrderType.class.getPackage().getAnnotation(XmlSchema.class);
    Assert.assertNotNull("Couldn't create xml schema for JaxbNameSpacePrefixPurchaseOrderType class", xmlSchema);
    XmlNamespacePrefixMapper mapper = new XmlNamespacePrefixMapper(xmlSchema.xmlns());
    try {
        marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
    } catch (PropertyException e) {
        logger.error(e.getMessage(), e);
    }
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    ObjectFactory factory = new ObjectFactory();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    marshaller.marshal(factory.createPurchaseOrder(po), out);
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) XmlNamespacePrefixMapper(org.jboss.resteasy.plugins.providers.jaxb.XmlNamespacePrefixMapper) ObjectFactory(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.ObjectFactory) XmlSchema(jakarta.xml.bind.annotation.XmlSchema) JaxbNameSpacePrefixPurchaseOrderType(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixPurchaseOrderType) PropertyException(jakarta.xml.bind.PropertyException) JaxbNameSpacePrefixItems(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItems) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JaxbNameSpacePrefixItem(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItem) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with PropertyException

use of jakarta.xml.bind.PropertyException in project metro-jax-ws by eclipse-ee4j.

the class ExceptionBean method marshal.

/**
 * Converts the given {@link Throwable} into an XML representation
 * and put that as a DOM tree under the given node.
 */
public static void marshal(Throwable t, Node parent) throws JAXBException {
    Marshaller m = JAXB_CONTEXT.createMarshaller();
    try {
        m.setProperty("org.glassfish.jaxb.runtime.namespacePrefixMapper", nsp);
    } catch (PropertyException pe) {
    }
    m.marshal(new ExceptionBean(t), parent);
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) PropertyException(jakarta.xml.bind.PropertyException)

Example 3 with PropertyException

use of jakarta.xml.bind.PropertyException in project eclipselink by eclipse-ee4j.

the class JAXBUnmarshaller method getProperty.

/**
 * Get a property from the JAXBMarshaller. Attempting to get any unsupported
 * property will result in a jakarta.xml.bind.PropertyException
 * See <a href="#supportedProps">Supported Properties</a>.
 * @see org.eclipse.persistence.jaxb.UnmarshallerProperties
 */
@Override
public Object getProperty(String key) throws PropertyException {
    if (key == null) {
        throw new IllegalArgumentException();
    }
    if (key.equals(UnmarshallerProperties.MEDIA_TYPE)) {
        return xmlUnmarshaller.getMediaType();
    } else if (key.equals(UnmarshallerProperties.AUTO_DETECT_MEDIA_TYPE)) {
        return xmlUnmarshaller.isAutoDetectMediaType();
    } else if (key.equals(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE)) {
        return xmlUnmarshaller.isCaseInsensitive();
    } else if (key.equals(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX)) {
        return xmlUnmarshaller.getAttributePrefix();
    } else if (key.equals(UnmarshallerProperties.JSON_INCLUDE_ROOT)) {
        return xmlUnmarshaller.isIncludeRoot();
    } else if (key.equals(UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR)) {
        return xmlUnmarshaller.getNamespaceSeparator();
    } else if (key.equals(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER)) {
        if (xmlUnmarshaller.getNamespaceResolver() == null) {
            return null;
        }
        if (xmlUnmarshaller.getNamespaceResolver() instanceof PrefixMapperNamespaceResolver) {
            PrefixMapperNamespaceResolver wrapper = (PrefixMapperNamespaceResolver) xmlUnmarshaller.getNamespaceResolver();
            return wrapper.getPrefixMapper();
        } else {
            Map<String, String> nsMap = new HashMap<String, String>();
            Map<String, String> prefixesToNS = xmlUnmarshaller.getNamespaceResolver().getPrefixesToNamespaces();
            // Reverse the prefixesToNS map
            Iterator<Entry<String, String>> namesapcesIter = prefixesToNS.entrySet().iterator();
            for (int i = 0; i < prefixesToNS.size(); i++) {
                Entry<String, String> nextEntry = namesapcesIter.next();
                nsMap.put(nextEntry.getValue(), nextEntry.getKey());
            }
            return nsMap;
        }
    } else if (key.equals(UnmarshallerProperties.JSON_VALUE_WRAPPER)) {
        return xmlUnmarshaller.getValueWrapper();
    } else if (UnmarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
        return xmlUnmarshaller.getJsonTypeConfiguration().isUseXsdTypesWithPrefix();
    } else if (UnmarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
        return xmlUnmarshaller.getJsonTypeConfiguration().isJsonTypeCompatibility();
    } else if (MarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
        return xmlUnmarshaller.getJsonTypeConfiguration().getJsonTypeAttributeName();
    } else if (UnmarshallerProperties.ID_RESOLVER.equals(key)) {
        return xmlUnmarshaller.getIDResolver();
    } else if (SUN_ID_RESOLVER.equals(key) || SUN_JSE_ID_RESOLVER.equals(key)) {
        IDResolverWrapper wrapper = (IDResolverWrapper) xmlUnmarshaller.getIDResolver();
        if (wrapper == null) {
            return null;
        }
        return wrapper.getResolver();
    } else if (UnmarshallerProperties.OBJECT_GRAPH.equals(key)) {
        Object graph = xmlUnmarshaller.getUnmarshalAttributeGroup();
        if (graph instanceof CoreAttributeGroup) {
            return new ObjectGraphImpl((CoreAttributeGroup) graph);
        }
        return graph;
    } else if (UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
        return xmlUnmarshaller.isWrapperAsCollectionName();
    } else if (UnmarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
        return this.beanValidationMode;
    } else if (UnmarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
        return this.prefValidatorFactory;
    } else if (UnmarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
        return this.beanValidationGroups;
    } else if (UnmarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
        return this.bvNoOptimisation;
    } else if (UnmarshallerProperties.DISABLE_SECURE_PROCESSING.equals(key)) {
        return xmlUnmarshaller.isSecureProcessingDisabled();
    } else if (UnmarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
        return xmlUnmarshaller.isLogPayload();
    }
    throw new PropertyException(key);
}
Also used : CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) HashMap(java.util.HashMap) PropertyException(jakarta.xml.bind.PropertyException) ObjectGraphImpl(org.eclipse.persistence.internal.jaxb.ObjectGraphImpl) PrefixMapperNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver) Entry(java.util.Map.Entry) IDResolverWrapper(org.eclipse.persistence.internal.jaxb.IDResolverWrapper)

Example 4 with PropertyException

use of jakarta.xml.bind.PropertyException in project eclipselink by eclipse-ee4j.

the class JAXBUnmarshaller method setProperty.

/**
 * Set a property on the JAXBUnmarshaller. Attempting to set any unsupported
 * property will result in a jakarta.xml.bind.PropertyException.
 * @see org.eclipse.persistence.jaxb.UnmarshallerProperties
 */
@Override
public void setProperty(String key, Object value) throws PropertyException {
    if (key == null) {
        throw new IllegalArgumentException();
    }
    SessionLog logger = AbstractSessionLog.getLog();
    if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
        logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_set_unmarshaller_property", new Object[] { key, value });
    }
    if (MOXySystemProperties.moxyLogPayload != null && xmlUnmarshaller.isLogPayload() == null) {
        xmlUnmarshaller.setLogPayload(MOXySystemProperties.moxyLogPayload);
    }
    if (key.equals(UnmarshallerProperties.MEDIA_TYPE)) {
        MediaType mType = null;
        if (value instanceof MediaType) {
            mType = (MediaType) value;
        } else if (value instanceof String) {
            mType = MediaType.getMediaType((String) value);
        }
        if (mType == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        xmlUnmarshaller.setMediaType(mType);
    } else if (key.equals(UnmarshallerProperties.UNMARSHALLING_CASE_INSENSITIVE)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        xmlUnmarshaller.setCaseInsensitive((Boolean) value);
    } else if (key.equals(UnmarshallerProperties.AUTO_DETECT_MEDIA_TYPE)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        xmlUnmarshaller.setAutoDetectMediaType((Boolean) value);
    } else if (key.equals(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX)) {
        xmlUnmarshaller.setAttributePrefix((String) value);
    } else if (UnmarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        xmlUnmarshaller.setIncludeRoot((Boolean) value);
    } else if (UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER.equals(key)) {
        if (value == null) {
            xmlUnmarshaller.setNamespaceResolver(null);
        } else if (value instanceof Map) {
            Map<String, String> namespaces = (Map<String, String>) value;
            NamespaceResolver nr = new NamespaceResolver();
            Iterator<Entry<String, String>> namesapcesIter = namespaces.entrySet().iterator();
            for (int i = 0; i < namespaces.size(); i++) {
                Entry<String, String> nextEntry = namesapcesIter.next();
                nr.put(nextEntry.getValue(), nextEntry.getKey());
            }
            xmlUnmarshaller.setNamespaceResolver(nr);
        } else if (value instanceof NamespacePrefixMapper) {
            xmlUnmarshaller.setNamespaceResolver(new PrefixMapperNamespaceResolver((NamespacePrefixMapper) value, null));
        }
    } else if (UnmarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) {
        xmlUnmarshaller.setValueWrapper((String) value);
    } else if (UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        xmlUnmarshaller.setNamespaceSeparator((Character) value);
    } else if (UnmarshallerProperties.JSON_USE_XSD_TYPES_WITH_PREFIX.equals(key)) {
        xmlUnmarshaller.getJsonTypeConfiguration().setUseXsdTypesWithPrefix((Boolean) value);
    } else if (UnmarshallerProperties.JSON_TYPE_COMPATIBILITY.equals(key)) {
        xmlUnmarshaller.getJsonTypeConfiguration().setJsonTypeCompatibility((Boolean) value);
    } else if (UnmarshallerProperties.JSON_TYPE_ATTRIBUTE_NAME.equals(key)) {
        xmlUnmarshaller.getJsonTypeConfiguration().setJsonTypeAttributeName((String) value);
    } else if (UnmarshallerProperties.ID_RESOLVER.equals(key)) {
        setIDResolver((IDResolver) value);
    } else if (SUN_ID_RESOLVER.equals(key) || SUN_JSE_ID_RESOLVER.equals(key)) {
        if (value == null) {
            setIDResolver(null);
        } else {
            setIDResolver(new IDResolverWrapper(value));
        }
    } else if (UnmarshallerProperties.OBJECT_GRAPH.equals(key)) {
        if (value instanceof ObjectGraphImpl) {
            xmlUnmarshaller.setUnmarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup());
        } else if (value instanceof String || value == null) {
            xmlUnmarshaller.setUnmarshalAttributeGroup(value);
        } else {
            throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value);
        }
    } else if (UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) {
        xmlUnmarshaller.setWrapperAsCollectionName((Boolean) value);
    } else if (UnmarshallerProperties.BEAN_VALIDATION_MODE.equals(key)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        this.beanValidationMode = ((BeanValidationMode) value);
    } else if (UnmarshallerProperties.BEAN_VALIDATION_FACTORY.equals(key)) {
        // Null value is allowed
        this.prefValidatorFactory = value;
    } else if (UnmarshallerProperties.BEAN_VALIDATION_GROUPS.equals(key)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        this.beanValidationGroups = ((Class<?>[]) value);
    } else if (UnmarshallerProperties.BEAN_VALIDATION_NO_OPTIMISATION.equals(key)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        this.bvNoOptimisation = ((boolean) value);
    } else if (UnmarshallerProperties.DISABLE_SECURE_PROCESSING.equals(key)) {
        if (value == null) {
            throw new PropertyException(key, Constants.EMPTY_STRING);
        }
        boolean disabled = value instanceof String ? Boolean.parseBoolean((String) value) : (boolean) value;
        xmlUnmarshaller.setDisableSecureProcessing(disabled);
    } else if (UnmarshallerProperties.MOXY_LOG_PAYLOAD.equals(key)) {
        xmlUnmarshaller.setLogPayload(((boolean) value));
    } else if (UnmarshallerProperties.MOXY_LOGGING_LEVEL.equals(key)) {
        if (value instanceof String) {
            AbstractSessionLog.getLog().setLevel(LogLevel.toValue((String) value).getId(), SessionLog.MOXY);
        } else {
            AbstractSessionLog.getLog().setLevel(((LogLevel) value).getId(), SessionLog.MOXY);
        }
    } else {
        throw new PropertyException(key, value);
    }
}
Also used : IDResolver(org.eclipse.persistence.oxm.IDResolver) PropertyException(jakarta.xml.bind.PropertyException) ObjectGraphImpl(org.eclipse.persistence.internal.jaxb.ObjectGraphImpl) LogLevel(org.eclipse.persistence.logging.LogLevel) PrefixMapperNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) Entry(java.util.Map.Entry) IDResolverWrapper(org.eclipse.persistence.internal.jaxb.IDResolverWrapper) MediaType(org.eclipse.persistence.oxm.MediaType) PrefixMapperNamespaceResolver(org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) Map(java.util.Map) HashMap(java.util.HashMap) NamespacePrefixMapper(org.eclipse.persistence.oxm.NamespacePrefixMapper)

Example 5 with PropertyException

use of jakarta.xml.bind.PropertyException in project jaxb-ri by eclipse-ee4j.

the class PrefixExample method test.

private static void test(String fileName) throws Exception {
    JAXBContext context = JAXBContext.newInstance("foo:bar");
    // unmarshal a file specified by the command line argument
    Object o = context.createUnmarshaller().unmarshal(new File(fileName));
    Marshaller marshaller = context.createMarshaller();
    // to assign a prefix for a namespace.
    try {
        marshaller.setProperty("org.glassfish.jaxb.namespacePrefixMapper", new NamespacePrefixMapperImpl());
    } catch (PropertyException e) {
        // you can just continue marshalling without failing
        ;
    }
    // make the output indented. It looks nicer on screen.
    // this is a JAXB standard property, so it should work with any JAXB impl.
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    // print it out to the console since we are just testing the behavior.
    marshaller.marshal(o, System.out);
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) PropertyException(jakarta.xml.bind.PropertyException) JAXBContext(jakarta.xml.bind.JAXBContext) File(java.io.File)

Aggregations

PropertyException (jakarta.xml.bind.PropertyException)8 Marshaller (jakarta.xml.bind.Marshaller)3 HashMap (java.util.HashMap)3 ObjectGraphImpl (org.eclipse.persistence.internal.jaxb.ObjectGraphImpl)3 JAXBContext (jakarta.xml.bind.JAXBContext)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 IDResolverWrapper (org.eclipse.persistence.internal.jaxb.IDResolverWrapper)2 PrefixMapperNamespaceResolver (org.eclipse.persistence.internal.oxm.record.namespaces.PrefixMapperNamespaceResolver)2 AbstractSessionLog (org.eclipse.persistence.logging.AbstractSessionLog)2 LogLevel (org.eclipse.persistence.logging.LogLevel)2 SessionLog (org.eclipse.persistence.logging.SessionLog)2 MediaType (org.eclipse.persistence.oxm.MediaType)2 NamespacePrefixMapper (org.eclipse.persistence.oxm.NamespacePrefixMapper)2 XmlSchema (jakarta.xml.bind.annotation.XmlSchema)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1