Search in sources :

Example 1 with XMLRoot

use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.

the class JAXBElementRootConverter method convertObjectValueToDataValue.

@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session, XMLMarshaller marshaller) {
    if (null != nestedConverter) {
        objectValue = nestedConverter.convertObjectValueToDataValue(objectValue, session, marshaller);
    }
    if (objectValue instanceof JAXBElement) {
        ClassDescriptor desc = session.getDescriptor(objectValue);
        if (desc == null || objectValue instanceof WrappedValue) {
            JAXBElement element = (JAXBElement) objectValue;
            Root root = new XMLRoot();
            root.setLocalName(element.getName().getLocalPart());
            root.setNamespaceURI(element.getName().getNamespaceURI());
            root.setObject(element.getValue());
            root.setDeclaredType(element.getDeclaredType());
            root.setNil(element.isNil());
            return root;
        }
    }
    return objectValue;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Root(org.eclipse.persistence.internal.oxm.Root) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 2 with XMLRoot

use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.

the class JAXBTestCases method testObjectToOutputStream.

@Override
public void testObjectToOutputStream() throws Exception {
    Object objectToWrite = getWriteControlObject();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    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());
    }
    int sizeBefore = getNamespaceResolverSize(desc);
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    try {
        jaxbMarshaller.marshal(objectToWrite, stream);
    } catch (Exception e) {
        assertMarshalException(e);
        return;
    }
    if (expectsMarshalException) {
        fail("An exception should have occurred but didn't.");
        return;
    }
    int sizeAfter = getNamespaceResolverSize(desc);
    assertEquals(sizeBefore, sizeAfter);
    InputStream is = new ByteArrayInputStream(stream.toByteArray());
    Document testDocument = getTestDocument(is);
    stream.close();
    is.close();
    objectToXMLDocumentTest(testDocument);
    if (getProperties() != null) {
        log("************test with JSON bindings*********");
        ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
        JAXBContext jaxbContextFromJSONBindings = createJaxbContextFromJSONBindings();
        Marshaller jaxbMarshallerFromJSONBindings = jaxbContextFromJSONBindings.createMarshaller();
        jaxbMarshallerFromJSONBindings.setAttachmentMarshaller(jaxbMarshaller.getAttachmentMarshaller());
        jaxbMarshallerFromJSONBindings.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, jaxbMarshaller.getProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER));
        jaxbMarshallerFromJSONBindings.setProperty(MarshallerProperties.OBJECT_GRAPH, jaxbMarshaller.getProperty(MarshallerProperties.OBJECT_GRAPH));
        jaxbMarshallerFromJSONBindings.marshal(objectToWrite, stream2);
        InputStream is2 = new ByteArrayInputStream(stream2.toByteArray());
        Document testDocument2 = parser.parse(is2);
        stream2.close();
        is2.close();
        objectToXMLDocumentTest(testDocument2);
    }
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Marshaller(jakarta.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(jakarta.xml.bind.JAXBException)

Example 3 with XMLRoot

use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.

the class JAXBTestCases method testObjectToXMLDocument.

@Override
public void testObjectToXMLDocument() throws Exception {
    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);
    Document testDocument = XMLPlatformFactory.getInstance().getXMLPlatform().createDocument();
    try {
        jaxbMarshaller.marshal(objectToWrite, testDocument);
    } catch (Exception e) {
        assertMarshalException(e);
        return;
    }
    if (expectsMarshalException) {
        fail("An exception should have occurred but didn't.");
        return;
    }
    int sizeAfter = getNamespaceResolverSize(desc);
    assertEquals(sizeBefore, sizeAfter);
    objectToXMLDocumentTest(testDocument);
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(jakarta.xml.bind.JAXBException)

Example 4 with XMLRoot

use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.

the class MapNamespaceTestCases method testObjectToXMLWithContentHandler.

public void testObjectToXMLWithContentHandler() throws Exception {
    Object objectToWrite = getWriteControlObject();
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", new DummyCharacterEscapeHandler());
    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());
    }
    int sizeBefore = getNamespaceResolverSize(desc);
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    jaxbMarshaller.setProperty("org.glassfish.jaxb.xmlDeclaration", Boolean.FALSE);
    try {
        jaxbMarshaller.marshal(objectToWrite, dataWriter);
    } catch (Exception e) {
        assertMarshalException(e);
        return;
    }
    if (expectsMarshalException) {
        fail("An exception should have occurred but didn't.");
        return;
    }
    int sizeAfter = getNamespaceResolverSize(desc);
    assertEquals(sizeBefore, sizeAfter);
    Document testDocument = getTestDocument(writer.toString());
    writer.close();
    objectToXMLDocumentTest(testDocument);
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Document(org.w3c.dom.Document) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) DataWriter(org.glassfish.jaxb.core.marshaller.DataWriter)

Example 5 with XMLRoot

use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.

the class JAXBTestCases method objectToXMLStringWriter.

public void objectToXMLStringWriter(Object objectToWrite) throws Exception {
    StringWriter writer = new StringWriter();
    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());
    }
    int sizeBefore = getNamespaceResolverSize(desc);
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    try {
        jaxbMarshaller.marshal(objectToWrite, writer);
    } catch (Exception e) {
        assertMarshalException(e);
        return;
    }
    if (expectsMarshalException) {
        fail("An exception should have occurred but didn't.");
        return;
    }
    int sizeAfter = getNamespaceResolverSize(desc);
    assertEquals(sizeBefore, sizeAfter);
    Document testDocument = getTestDocument(writer.toString());
    writer.close();
    objectToXMLDocumentTest(testDocument);
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(jakarta.xml.bind.JAXBException)

Aggregations

XMLRoot (org.eclipse.persistence.oxm.XMLRoot)126 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)34 Document (org.w3c.dom.Document)28 SAXException (org.xml.sax.SAXException)17 StringWriter (java.io.StringWriter)16 Person (org.eclipse.persistence.testing.oxm.xmlroot.Person)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 Vector (java.util.Vector)11 Node (org.w3c.dom.Node)11 QName (javax.xml.namespace.QName)10 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)10 Element (org.w3c.dom.Element)10 StringReader (java.io.StringReader)9 InputSource (org.xml.sax.InputSource)9 DataObject (commonj.sdo.DataObject)8 JAXBException (jakarta.xml.bind.JAXBException)8 InputStream (java.io.InputStream)8 MalformedURLException (java.net.MalformedURLException)8 XMLStreamException (javax.xml.stream.XMLStreamException)8