Search in sources :

Example 26 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project aai-aai-common by onap.

the class NodeIngestorWiringTest method test.

@Test
public void test() {
    DynamicJAXBContext ctx10 = ni.getContextForVersion(new SchemaVersion("v10"));
    // should work bc Bar is valid in test_business_v10 schema
    DynamicEntity bar10 = ctx10.newDynamicEntity("Bar");
    bar10.set("barId", "bar2");
    assertTrue("bar2".equals(bar10.get("barId")));
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) SchemaVersion(org.onap.aai.setup.SchemaVersion) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) Test(org.junit.Test) ConfigTranslatorForWiringTest(org.onap.aai.testutils.ConfigTranslatorForWiringTest) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 27 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class XRServiceFactory method loadOXMetadata.

/**
 * <p>INTERNAL:
 * Create a Project using OXM metadata.  The given classloader is expected
 * to successfully load 'META-INF/eclipselink-dbws-ox.xml'.
 * @param xrdecl  {@link ClassLoader} used to search for {@code eclipselink-dbws-ox.xml}.
 * @param session OXM session (only for logging).
 */
protected Project loadOXMetadata(final ClassLoader xrdecl, final Session session) {
    Project oxProject = null;
    InputStream inStream = null;
    String searchPath;
    // try "META-INF/" and "/META-INF/"
    for (String prefix : META_INF_PATHS) {
        searchPath = prefix + Util.DBWS_OX_XML;
        inStream = xrdecl.getResourceAsStream(searchPath);
        if (inStream != null) {
            break;
        }
    }
    if (inStream != null) {
        Map<String, OXMMetadataSource> metadataMap = null;
        StreamSource xml = new StreamSource(inStream);
        try {
            JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
            XmlBindingsModel model = jaxbElt.getValue();
            if (model.getBindingsList() != null) {
                metadataMap = new HashMap<>();
                for (XmlBindings xmlBindings : model.getBindingsList()) {
                    metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
                }
            }
        } catch (JAXBException jaxbex) {
            /* could be legacy project, or none set, so just log */
            session.getSessionLog().log(SessionLog.FINE, SessionLog.DBWS, "dbws_oxm_metadata_read_error", jaxbex.getLocalizedMessage());
            return null;
        }
        if (metadataMap != null) {
            Map<String, Map<String, OXMMetadataSource>> properties = new HashMap<>();
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
            try {
                DynamicJAXBContext jCtx = DynamicJAXBContextFactory.createContextFromOXM(xrdecl, properties);
                oxProject = jCtx.getXMLContext().getSession(0).getProject();
                oxProject.setName(xrService.getName().concat(OX_PRJ_SUFFIX));
            } catch (JAXBException e) {
                throw new DBWSException(OXM_PROCESSING_EX, e);
            }
        }
    }
    return oxProject;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) JAXBContext(jakarta.xml.bind.JAXBContext) SimpleXMLFormatProject(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DBWSException(org.eclipse.persistence.exceptions.DBWSException) Unmarshaller(jakarta.xml.bind.Unmarshaller) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Map(java.util.Map) HashMap(java.util.HashMap)

Example 28 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class StaticWithDynamicTestCases method getControlObject.

@Override
protected Object getControlObject() {
    DynamicEntity customer = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "Customer");
    DynamicEntity phone1 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "PhoneNumber");
    DynamicEntity phone2 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "PhoneNumber");
    DynamicEntity phone3 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "PhoneNumber");
    phone1.set("value", "555-WORK");
    phone1.set("type", "work");
    phone2.set("value", "555-HOME");
    phone2.set("type", "home");
    phone3.set("value", "555-CELL");
    phone3.set("type", "cell");
    ArrayList phones = new ArrayList();
    phones.add(phone1);
    phones.add(phone2);
    customer.set("phoneNumber", phones);
    AddressWithPhone address = new AddressWithPhone();
    address.city = "Any Town";
    address.street = "123 Some Street";
    address.phone = phone3;
    customer.set("address", address);
    customer.set("name", "Jane Doe");
    return customer;
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) ArrayList(java.util.ArrayList)

Example 29 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBFromOXMTestCases method testUnmarshalWithClass.

/**
 * Ensure that using unmarshal(XMLStreamReader, Class) works in a Dynamic context.
 * Create a dummy Descriptor with a concrete Java class, add it to the session, and then
 * try to unmarshal using that class.  An empty instance of that concrete class should be returned.
 */
public void testUnmarshalWithClass() throws Exception {
    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(OXM_METADATA);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
    DynamicJAXBContext jc = DynamicJAXBContextFactory.createContextFromOXM(classLoader, properties);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    InputStream xmlStream = classLoader.getResourceAsStream(XML_RESOURCE_BEFORE);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    Document xsdDocument = docFactory.newDocumentBuilder().parse(xmlStream);
    Source domSource = new DOMSource(xsdDocument);
    XMLDescriptor dummy = new XMLDescriptor();
    dummy.setJavaClass(org.eclipse.persistence.testing.jaxb.employee.Employee.class);
    jc.getXMLContext().getSession(0).addDescriptor(dummy);
    Object o = unmarshaller.unmarshal(domSource, org.eclipse.persistence.testing.jaxb.employee.Employee.class);
    JAXBElement jelem = (JAXBElement) o;
    assertEquals(org.eclipse.persistence.testing.jaxb.employee.Employee.class, jelem.getValue().getClass());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) InputStream(java.io.InputStream) LinkAdapterString(org.eclipse.persistence.testing.jaxb.dynamic.util.LinkAdapterString) JAXBElement(jakarta.xml.bind.JAXBElement) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 30 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBFromSessionsXMLTestCases method getControlObject.

@Override
protected Object getControlObject() {
    DynamicEntity docWrapper = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(DOCWRAPPER_CLASS_NAME);
    DynamicEntity root = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(ROOT_CLASS_NAME);
    DynamicEntity compObj = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(COMPOBJ_CLASS_NAME);
    compObj.set("value", null);
    Vector anyColl = new Vector(2);
    DynamicEntity anyItem1 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(ANYCOLL_CLASS_NAME);
    anyItem1.set("value", "aNyOne");
    anyColl.add(anyItem1);
    DynamicEntity anyItem2 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(ANYCOLL_CLASS_NAME);
    anyItem2.set("value", "aNyTwo");
    anyColl.add(anyItem2);
    compObj.set("anyColl", anyColl);
    root.set("compObj", compObj);
    ArrayList compColl = new ArrayList();
    DynamicEntity item1 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(COMPCOLL_CLASS_NAME);
    item1.set("value", "123");
    DynamicEntity item2 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(COMPCOLL_CLASS_NAME);
    item2.set("value", "456");
    item1.set("invRef", root);
    item2.set("invRef", root);
    compColl.add(item1);
    compColl.add(item2);
    root.set("compColl", compColl);
    Vector comDirColl = new Vector(2);
    comDirColl.add("qWe");
    comDirColl.add("rTy");
    root.set("compDirColl", comDirColl);
    root.set("binData", new byte[] { 'a', 'b', 'c', 'd', 'e' });
    Vector binDataColl = new Vector();
    binDataColl.add(new byte[] { '1', '2', '3' });
    binDataColl.add(new byte[] { '4', '5', '6' });
    root.set("binDataColl", binDataColl);
    DynamicEntity anyObj = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(ANYOBJ_CLASS_NAME);
    anyObj.set("value", "aNy");
    root.set("anyObj", anyObj);
    DynamicEntity anyAtt = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(ANYATT_CLASS_NAME);
    Properties anyAttProps = new Properties();
    anyAttProps.put(new QName("", "anyAtt1"), "aNyaTT1");
    anyAttProps.put(new QName("", "anyAtt2"), "aNyAttTWO");
    anyAtt.set("value", anyAttProps);
    root.set("anyAtt", anyAtt);
    String[] transform = new String[2];
    transform[0] = "transformValue1";
    transform[1] = "transformValue2";
    root.set("transform", transform);
    Document doc;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    Element elem = doc.createElementNS("", "frag");
    Text text = doc.createTextNode("XML Fragment Text Content");
    elem.appendChild(text);
    root.set("frag", elem);
    Vector fragColl = new Vector(2);
    Element elem1 = doc.createElementNS("", "frag-coll");
    Text text1 = doc.createTextNode("XML Fragment Collection One");
    elem1.appendChild(text1);
    Element elem2 = doc.createElementNS("", "frag-coll");
    Text text2 = doc.createTextNode("XML Fragment Collection Two");
    elem2.appendChild(text2);
    fragColl.add(elem1);
    fragColl.add(elem2);
    root.set("fragColl", fragColl);
    DynamicEntity objRef = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(OBJREFSUB_CLASS_NAME);
    objRef.set("id", "13579");
    objRef.set("superclassValue", "SUPER");
    objRef.set("subclassValue", "SUB");
    root.set("objRef", objRef);
    Vector collRef = new Vector(2);
    DynamicEntity collRef1 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(COLLREF_CLASS_NAME);
    collRef1.set("id", "112233");
    collRef1.set("value", "collRefVal1");
    DynamicEntity collRef2 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(COLLREF_CLASS_NAME);
    collRef2.set("id", "445566");
    collRef2.set("value", "collRefVal2");
    collRef.add(collRef1);
    collRef.add(collRef2);
    root.set("collRef", collRef);
    root.set("choice", 2112);
    Vector choiceColl = new Vector(3);
    choiceColl.add(3.14159);
    choiceColl.add("Pi");
    choiceColl.add(Boolean.TRUE);
    root.set("choiceColl", choiceColl);
    docWrapper.set("root", root);
    docWrapper.set("objRefTarget", objRef);
    docWrapper.set("collRefTarget", collRef);
    return docWrapper;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Text(org.w3c.dom.Text) JAXBContextProperties(org.eclipse.persistence.jaxb.JAXBContextProperties) Properties(java.util.Properties) Document(org.w3c.dom.Document) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Vector(java.util.Vector)

Aggregations

DynamicJAXBContext (org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)44 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)32 InputStream (java.io.InputStream)21 HashMap (java.util.HashMap)17 Document (org.w3c.dom.Document)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)10 StreamSource (javax.xml.transform.stream.StreamSource)10 Source (javax.xml.transform.Source)8 JAXBException (jakarta.xml.bind.JAXBException)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 DocumentBuilder (javax.xml.parsers.DocumentBuilder)6 DOMSource (javax.xml.transform.dom.DOMSource)6 SchemaVersion (org.onap.aai.setup.SchemaVersion)6 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 Element (org.w3c.dom.Element)5 JAXBElement (jakarta.xml.bind.JAXBElement)4 Map (java.util.Map)4 DynamicException (org.eclipse.persistence.exceptions.DynamicException)4