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;
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations