use of org.eclipse.persistence.jaxb.JAXBContext in project metro-jax-ws by eclipse-ee4j.
the class JAXBBond method marshal.
@Override
public void marshal(T object, Result result) throws JAXBException {
JAXBMarshaller marshaller = null;
try {
marshaller = parent.mpool.allocate();
marshaller.setAttachmentMarshaller(null);
marshaller.setProperty(jakarta.xml.bind.Marshaller.JAXB_FRAGMENT, true);
if (mappingInfo != null) {
if (isParameterizedType) {
JAXBTypeElement jte = new JAXBTypeElement(mappingInfo.getXmlTagName(), object, (ParameterizedType) mappingInfo.getType());
marshaller.marshal(jte, result, mappingInfo);
} else {
JAXBElement<T> elt = new JAXBElement<>(mappingInfo.getXmlTagName(), (Class<T>) mappingInfo.getType(), object);
// marshaller.marshal(elt, result);
marshaller.marshal(elt, result, mappingInfo);
}
} else {
TypeMappingInfo tmi = null;
if (object instanceof JAXBElement) {
QName q = ((JAXBElement) object).getName();
JAXBContext ctx = (JAXBContext) parent.getJAXBContext();
Map<TypeMappingInfo, QName> mtq = ctx.getTypeMappingInfoToSchemaType();
for (Map.Entry<TypeMappingInfo, QName> es : mtq.entrySet()) {
if (q.equals(es.getValue())) {
tmi = es.getKey();
break;
}
}
}
if (tmi != null) {
marshaller.marshal(object, result, tmi);
} else {
marshaller.marshal(object, result);
}
}
} finally {
if (marshaller != null) {
marshaller.setAttachmentMarshaller(null);
parent.mpool.replace(marshaller);
}
}
}
use of org.eclipse.persistence.jaxb.JAXBContext in project aai-aai-common by onap.
the class PojoUtilsTest method testGetJsonFromDynamicObject.
@Test
public void testGetJsonFromDynamicObject() throws Exception {
DynamicEntity dynamicEntity = Mockito.mock(DynamicEntity.class);
JAXBContext jaxbContext = Mockito.mock(JAXBContext.class);
JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
assertEquals("", output);
}
use of org.eclipse.persistence.jaxb.JAXBContext in project eclipselink by eclipse-ee4j.
the class PersistenceContext method createDynamicJAXBContext.
/**
* Create a JAXBContext based on the EntityManagerFactory for this PersistenceContext.
*/
protected JAXBContext createDynamicJAXBContext(Session session) throws JAXBException, IOException {
final ServiceVersion cachedContextVersion = (ServiceVersion) session.getProperty(SESSION_VERSION_PROPERTY);
final JAXBContext cachedContext = (JAXBContext) session.getProperty(JAXBContext.class.getName());
if (cachedContext != null && cachedContextVersion != null && cachedContextVersion == version) {
return cachedContext;
}
final Map<String, Object> properties = createJAXBProperties(session);
final ClassLoader cl = session.getDatasourcePlatform().getConversionManager().getLoader();
final JAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(cl, properties);
session.setProperty(SESSION_VERSION_PROPERTY, version);
session.setProperty(JAXBContext.class.getName(), jaxbContext);
return jaxbContext;
}
use of org.eclipse.persistence.jaxb.JAXBContext in project eclipselink by eclipse-ee4j.
the class JAXBWithJSONTestCases method generateJSONSchema.
public void generateJSONSchema(List<InputStream> controlSchemas) throws Exception {
MyStreamSchemaOutputResolver outputResolver = new MyStreamSchemaOutputResolver();
Class<? extends Object> theClass = getWriteControlObject().getClass();
if (getWriteControlObject() instanceof JAXBElement) {
theClass = ((JAXBElement) getWriteControlObject()).getValue().getClass();
}
((JAXBContext) getJAXBContext()).generateJsonSchema(outputResolver, theClass);
List<Writer> generatedSchemas = outputResolver.getSchemaFiles();
assertEquals("Wrong Number of Schemas Generated", controlSchemas.size(), generatedSchemas.size());
for (int i = 0; i < controlSchemas.size(); i++) {
InputStream controlInputstream = controlSchemas.get(i);
Writer generated = generatedSchemas.get(i);
log(generated.toString());
String controlString = loadInputStreamToString(controlInputstream);
compareStrings("generateJSONSchema", generated.toString(), controlString, true);
}
}
use of org.eclipse.persistence.jaxb.JAXBContext in project eclipselink by eclipse-ee4j.
the class XmlListTestCases method _testArray.
public void _testArray(Object controlArray) throws Exception {
XmlList xmlList = new XmlList() {
@Override
public Class<? extends Annotation> annotationType() {
return XmlList.class;
}
};
Annotation[] a = { xmlList };
TypeMappingInfo t = new TypeMappingInfo();
t.setType(controlArray.getClass());
t.setXmlTagName(new QName("root"));
t.setAnnotations(a);
TypeMappingInfo[] types = { t };
JAXBContext jaxbContext = (JAXBContext) JAXBContextFactory.createContext(types, null, Thread.currentThread().getContextClassLoader());
StringReader stringReader = new StringReader(XML);
StreamSource streamSource = new StreamSource(stringReader);
JAXBUnmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement o = unmarshaller.unmarshal(streamSource, t);
Object testArray = o.getValue();
assertEquals(Array.get(controlArray, 0), Array.get(testArray, 0));
assertEquals(Array.get(controlArray, 1), Array.get(testArray, 1));
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
JAXBMarshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(o, streamResult, t);
assertEquals(XML, stringWriter.toString());
}
Aggregations