Search in sources :

Example 6 with XMLRoot

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

the class JAXBTestCases method testObjectToContentHandler.

@Override
public void testObjectToContentHandler() throws Exception {
    SAXDocumentBuilder builder = new SAXDocumentBuilder();
    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());
    }
    int sizeBefore = getNamespaceResolverSize(desc);
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    try {
        jaxbMarshaller.marshal(objectToWrite, builder);
    } 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 controlDocument = getWriteControlDocument();
    Document testDocument = builder.getDocument();
    log("**testObjectToContentHandler**");
    log("Expected:");
    log(controlDocument);
    log("\nActual:");
    log(testDocument);
    // Diff diff = new Diff(controlDocument, testDocument);
    // this.assertXMLEqual(diff, true);
    assertXMLIdentical(controlDocument, testDocument);
}
Also used : SAXDocumentBuilder(org.eclipse.persistence.platform.xml.SAXDocumentBuilder) 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 7 with XMLRoot

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

the class ExternalMetadataTestCases 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");
    jaxbMarshaller.marshal(objectToWrite, stream);
    int sizeAfter = getNamespaceResolverSize(desc);
    assertEquals(sizeBefore, sizeAfter);
    InputStream is = new ByteArrayInputStream(stream.toByteArray());
    Document testDocument = parser.parse(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));
        // before marshalling object again, need to reset it's triggered events
        ((Employee) objectToWrite).triggeredEvents = new ArrayList();
        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) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document)

Example 8 with XMLRoot

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

the class JAXBInheritanceNSSeparatorTestCases method testObjectToXMLStreamWriterRecordJSON.

public void testObjectToXMLStreamWriterRecordJSON() throws Exception {
    if (XML_OUTPUT_FACTORY != null) {
        StringWriter writer = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
        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/json");
        int sizeBefore = getNamespaceResolverSize(desc);
        XMLStreamWriterRecord record = new XMLStreamWriterRecord(streamWriter);
        ((org.eclipse.persistence.jaxb.JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, record);
        streamWriter.flush();
        int sizeAfter = getNamespaceResolverSize(desc);
        assertEquals(sizeBefore, sizeAfter);
        StringReader reader = new StringReader(writer.toString());
        InputSource inputSource = new InputSource(reader);
        Document testDocument = parser.parse(inputSource);
        writer.close();
        reader.close();
        objectToXMLDocumentTest(testDocument);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) InputSource(org.xml.sax.InputSource) 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) StringReader(java.io.StringReader) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Example 9 with XMLRoot

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

the class TypeMappingInfoTestCases method testObjectToXMLStreamWriterWithTypeMappingInfo.

public void testObjectToXMLStreamWriterWithTypeMappingInfo() throws Exception {
    if (XML_OUTPUT_FACTORY != null) {
        StringWriter writer = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
        XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);
        Object objectToWrite = getWriteControlObject();
        XMLDescriptor desc = null;
        if (objectToWrite instanceof XMLRoot) {
            desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
        } else {
            desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(objectToWrite.getClass());
        }
        int sizeBefore = getNamespaceResolverSize(desc);
        ((JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, streamWriter, getTypeMappingInfo());
        streamWriter.flush();
        int sizeAfter = getNamespaceResolverSize(desc);
        assertEquals(sizeBefore, sizeAfter);
        StringReader reader = new StringReader(writer.toString());
        InputSource inputSource = new InputSource(reader);
        Document testDocument = parser.parse(inputSource);
        writer.close();
        reader.close();
        objectToXMLDocumentTest(testDocument);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) InputSource(org.xml.sax.InputSource) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Document(org.w3c.dom.Document) org.eclipse.persistence.jaxb(org.eclipse.persistence.jaxb) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader)

Example 10 with XMLRoot

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

the class TypeMappingInfoTestCases method testObjectToResultWithTypeMappingInfoXML.

public void testObjectToResultWithTypeMappingInfoXML() throws Exception {
    Object objectToWrite = getWriteControlObject();
    XMLDescriptor desc = null;
    if (objectToWrite instanceof XMLRoot) {
        desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
    } else {
        desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(objectToWrite.getClass());
    }
    int sizeBefore = getNamespaceResolverSize(desc);
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult(stringWriter);
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    ((JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, result, getTypeMappingInfo());
    int sizeAfter = getNamespaceResolverSize(desc);
    assertEquals(sizeBefore, sizeAfter);
    StringReader reader = new StringReader(stringWriter.toString());
    InputSource inputSource = new InputSource(reader);
    Document testDocument = parser.parse(inputSource);
    stringWriter.close();
    reader.close();
    objectToXMLDocumentTest(testDocument);
}
Also used : org.eclipse.persistence.jaxb(org.eclipse.persistence.jaxb) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

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