Search in sources :

Example 1 with JAXBContext

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);
        }
    }
}
Also used : JAXBTypeElement(org.eclipse.persistence.jaxb.JAXBTypeElement) QName(javax.xml.namespace.QName) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) Map(java.util.Map)

Example 2 with JAXBContext

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);
}
Also used : DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller) Test(org.junit.Test)

Example 3 with JAXBContext

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;
}
Also used : ServiceVersion(org.eclipse.persistence.jpa.rs.features.ServiceVersion) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext)

Example 4 with 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);
    }
}
Also used : InputStream(java.io.InputStream) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) JsonWriter(jakarta.json.JsonWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 5 with JAXBContext

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());
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) Annotation(java.lang.annotation.Annotation) XmlList(jakarta.xml.bind.annotation.XmlList) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JAXBUnmarshaller(org.eclipse.persistence.jaxb.JAXBUnmarshaller) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) JAXBMarshaller(org.eclipse.persistence.jaxb.JAXBMarshaller)

Aggregations

JAXBContext (org.eclipse.persistence.jaxb.JAXBContext)80 HashMap (java.util.HashMap)29 InputStream (java.io.InputStream)21 QName (javax.xml.namespace.QName)17 TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)17 File (java.io.File)16 StreamSource (javax.xml.transform.stream.StreamSource)16 JAXBException (jakarta.xml.bind.JAXBException)13 Type (java.lang.reflect.Type)13 ArrayList (java.util.ArrayList)10 XMLContext (org.eclipse.persistence.oxm.XMLContext)10 InputSource (org.xml.sax.InputSource)10 InputStreamReader (java.io.InputStreamReader)9 JAXBHelperContext (org.eclipse.persistence.sdo.helper.jaxb.JAXBHelperContext)9 StringWriter (java.io.StringWriter)7 Source (javax.xml.transform.Source)7 Unmarshaller (jakarta.xml.bind.Unmarshaller)6 JAXBElement (jakarta.xml.bind.JAXBElement)5 Marshaller (jakarta.xml.bind.Marshaller)5 IOException (java.io.IOException)5