Search in sources :

Example 26 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project eclipselink by eclipse-ee4j.

the class DefaultNamespace2TestCases method testObjectToXMLStreamWriterRepairingRecord.

public void testObjectToXMLStreamWriterRepairingRecord() throws Exception {
    if (XML_OUTPUT_FACTORY != null) {
        StringWriter writer = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
        XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);
        Object objectToWrite = getWriteControlObject();
        XMLDescriptor desc = null;
        if (objectToWrite instanceof XMLRoot) {
            desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
        } else {
            desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass());
        }
        jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
        int sizeBefore = getNamespaceResolverSize(desc);
        XMLStreamWriterRecord record = new XMLStreamWriterRecord(streamWriter);
        try {
            ((org.eclipse.persistence.jaxb.JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, record);
        } catch (Exception e) {
            assertMarshalException(e);
            return;
        }
        if (expectsMarshalException) {
            fail("An exception should have occurred but didn't.");
            return;
        }
        streamWriter.flush();
        int sizeAfter = getNamespaceResolverSize(desc);
        assertEquals(sizeBefore, sizeAfter);
        Document testDocument = getTestDocument(writer.toString());
        writer.close();
        objectToXMLDocumentTest(testDocument);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Document(org.w3c.dom.Document) XMLStreamWriterRecord(org.eclipse.persistence.oxm.record.XMLStreamWriterRecord) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 27 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project eclipselink by eclipse-ee4j.

the class InheritanceImportsTestCases method getJSONMarshaller.

@Override
protected JAXBMarshaller getJSONMarshaller() throws Exception {
    JAXBMarshaller jsonMarshaller = (JAXBMarshaller) jaxbContext.createMarshaller();
    jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    Map<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("childnamespace", "ns0");
    jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
    return jsonMarshaller;
}
Also used : HashMap(java.util.HashMap) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 28 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project eclipselink by eclipse-ee4j.

the class JAXBInheritanceNSSeparatorTestCases method getJSONMarshaller.

@Override
public JAXBMarshaller getJSONMarshaller() throws Exception {
    JAXBMarshaller m = super.getJSONMarshaller();
    m.setProperty(MarshallerProperties.JSON_NAMESPACE_SEPARATOR, '*');
    return m;
}
Also used : JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 29 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project eclipselink by eclipse-ee4j.

the class JAXBInheritanceNSTestCases method getJSONMarshaller.

@Override
public JAXBMarshaller getJSONMarshaller() throws Exception {
    Map<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("rootNamespace", "ns0");
    namespaces.put("someNamespace", "ns1");
    namespaces.put(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi");
    JAXBMarshaller jsonMarshaller = (JAXBMarshaller) jaxbContext.createMarshaller();
    jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
    jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    return jsonMarshaller;
}
Also used : HashMap(java.util.HashMap) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 30 with JAXBMarshaller

use of org.eclipse.persistence.jaxb.JAXBMarshaller in project eclipselink by eclipse-ee4j.

the class JAXBInheritanceSubTypeNoParentRootTestCases method getJSONMarshaller.

@Override
public JAXBMarshaller getJSONMarshaller() throws Exception {
    Map<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("rootNamespace", "ns0");
    namespaces.put("someNamespace", "ns1");
    namespaces.put(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi");
    JAXBMarshaller jsonMarshaller = (JAXBMarshaller) jaxbContext.createMarshaller();
    jsonMarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
    jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    return jsonMarshaller;
}
Also used : HashMap(java.util.HashMap) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Aggregations

JAXBMarshaller (org.eclipse.persistence.jaxb.JAXBMarshaller)41 HashMap (java.util.HashMap)16 StringWriter (java.io.StringWriter)14 JAXBUnmarshaller (org.eclipse.persistence.jaxb.JAXBUnmarshaller)7 JAXBContext (jakarta.xml.bind.JAXBContext)6 JAXBElement (jakarta.xml.bind.JAXBElement)6 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)6 Map (java.util.Map)5 ConstraintViolationWrapper (org.eclipse.persistence.jaxb.ConstraintViolationWrapper)5 JAXBContext (org.eclipse.persistence.jaxb.JAXBContext)5 JAXBTypeElement (org.eclipse.persistence.jaxb.JAXBTypeElement)5 Marshaller (jakarta.xml.bind.Marshaller)4 StringReader (java.io.StringReader)4 StreamResult (javax.xml.transform.stream.StreamResult)4 QName (javax.xml.namespace.QName)3 TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)3 XmlList (jakarta.xml.bind.annotation.XmlList)2 Annotation (java.lang.annotation.Annotation)2 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)2 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)2