Search in sources :

Example 6 with DynamicJAXBContext

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

the class DynamicJAXBRefreshTestCases method testRefreshOXM.

public void testRefreshOXM() throws Exception {
    ClassLoader classLoader = this.getClass().getClassLoader();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputStream metadataStream = classLoader.getResourceAsStream(OXM_METADATA);
    Document metadataDocument = db.parse(metadataStream);
    metadataStream.close();
    Map<String, Object> props = new HashMap<String, Object>(1);
    props.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataDocument);
    DynamicJAXBContext jc = DynamicJAXBContextFactory.createContextFromOXM(classLoader, props);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    DynamicEntity controlRoot = jc.newDynamicEntity("org.eclipse.persistence.testing.jaxb.dynamic.Root");
    controlRoot.set("name", "R");
    InputStream xmlBeforeStream = classLoader.getResourceAsStream(XML_RESOURCE_BEFORE);
    DynamicEntity rootBefore = (DynamicEntity) unmarshaller.unmarshal(xmlBeforeStream);
    assertEquals(controlRoot.get("name"), rootBefore.get("name"));
    Element xmlElementElement = (Element) metadataDocument.getElementsByTagNameNS("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element").item(0);
    xmlElementElement.setAttribute("name", "after-name");
    JAXBHelper.getJAXBContext(jc).refreshMetadata();
    InputStream xmlAfterStream = classLoader.getResourceAsStream(XML_RESOURCE_AFTER);
    DynamicEntity rootAfter = (DynamicEntity) unmarshaller.unmarshal(xmlAfterStream);
    assertEquals(controlRoot.get("name"), rootAfter.get("name"));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) JAXBElement(jakarta.xml.bind.JAXBElement) Document(org.w3c.dom.Document) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 7 with DynamicJAXBContext

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

the class DynamicJAXBUsingXMLNamesTestCases method testCreateEntityByXPathNameCollision2.

public void testCreateEntityByXPathNameCollision2() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_XSD_2);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + EXAMPLE_XSD_2 + "]");
    }
    Map<String, InputStream> properties = new HashMap<String, InputStream>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, iStream);
    NamespaceResolver nsResolver = new NamespaceResolver();
    nsResolver.put("ns0", "mynamespace");
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    DynamicEntity person = (DynamicEntity) jaxbContext.createByQualifiedName("mynamespace", "person", false);
    assertNotNull("Could not create Dynamic Entity.", person);
    // Set name by XPath
    jaxbContext.setValueByXPath(person, "ns0:first-name/text()", nsResolver, "Larry");
    // Create address by XPath, set street by XPath
    DynamicEntity address = jaxbContext.createByXPath(person, "ns0:address", nsResolver, DynamicEntity.class);
    jaxbContext.setValueByXPath(address, "ns0:street/text()", nsResolver, "400 CNN Plaza");
    // Create person's address by XPath
    jaxbContext.setValueByXPath(person, "ns0:address", nsResolver, address);
    Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    jaxbContext.createMarshaller().marshal(person, marshalDoc);
    DynamicEntity person2 = (DynamicEntity) jaxbContext.createUnmarshaller().unmarshal(marshalDoc);
    DynamicEntity address2 = jaxbContext.getValueByXPath(person2, "ns0:address", nsResolver, DynamicEntity.class);
    String newName = jaxbContext.getValueByXPath(person2, "ns0:first-name/text()", nsResolver, String.class);
    String newStreet = jaxbContext.getValueByXPath(address2, "ns0:street/text()", nsResolver, String.class);
    String newStreet2 = jaxbContext.getValueByXPath(person2, "ns0:address/ns0:street/text()", nsResolver, String.class);
    assertEquals("Larry", newName);
    assertEquals("400 CNN Plaza", newStreet);
    assertEquals("400 CNN Plaza", newStreet2);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) Document(org.w3c.dom.Document)

Example 8 with DynamicJAXBContext

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

the class DynamicJAXBUsingXMLNamesTestCases method testCreateEntityByXMLName.

public void testCreateEntityByXMLName() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_OXM);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + EXAMPLE_OXM + "]");
    }
    HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.dynamic", new StreamSource(iStream));
    Map<String, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    DynamicEntity employee = (DynamicEntity) jaxbContext.createByQualifiedName(EMPLOYEE_NAMESPACE, EMPLOYEE_TYPE_NAME, false);
    assertNotNull("Could not create Dynamic Entity.", employee);
    NamespaceResolver nsResolver = new NamespaceResolver();
    nsResolver.put("ns0", "mynamespace");
    jaxbContext.setValueByXPath(employee, "name/text()", nsResolver, "Larry King");
    jaxbContext.setValueByXPath(employee, "employee-id/text()", nsResolver, "CA34287");
    Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    jaxbContext.createMarshaller().marshal(employee, marshalDoc);
    DynamicEntity employee2 = (DynamicEntity) jaxbContext.createUnmarshaller().unmarshal(marshalDoc);
    String newName = jaxbContext.getValueByXPath(employee2, "name/text()", nsResolver, String.class);
    String newId = jaxbContext.getValueByXPath(employee2, "employee-id/text()", nsResolver, String.class);
    assertEquals("Larry King", newName);
    assertEquals("CA34287", newId);
}
Also used : HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceStringLoaderProps.

public void testNewInstanceStringLoaderProps() throws JAXBException {
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance(SESSION_NAMES, Thread.currentThread().getContextClassLoader(), new HashMap());
    DynamicEntity docWrapper = jaxbContext.newDynamicEntity(DOCWRAPPER_CLASS_NAME);
    assertNotNull(docWrapper);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity)

Example 10 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceXSDExternalBindings.

public void testNewInstanceXSDExternalBindings() throws Exception {
    System.clearProperty(JAXBContext.JAXB_CONTEXT_FACTORY);
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    InputStream xsdStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    Source xsdSource = new StreamSource(xsdStream);
    // Set SYSTEM_ID to the filename part of the fully qualified EXAMPLE_XSD
    xsdSource.setSystemId(EXAMPLE_XSD.substring(EXAMPLE_XSD.lastIndexOf('/') + 1));
    InputStream xjbStream = classLoader.getResourceAsStream(EXTERNAL_BINDINGS);
    Source xjbSource = new StreamSource(xjbStream);
    // Set SYSTEM_ID to be the same as the XSD
    xjbSource.setSystemId(xsdSource.getSystemId());
    InputStream xjbStream2 = classLoader.getResourceAsStream(EXTERNAL_BINDINGS_2);
    Source xjbSource2 = new StreamSource(xjbStream2);
    // Set SYSTEM_ID to be the same as the XSD
    xjbSource2.setSystemId(xsdSource.getSystemId());
    ArrayList<Source> extBindings = new ArrayList<Source>(3);
    extBindings.add(xjbSource);
    extBindings.add(xjbSource2);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, xsdSource);
    properties.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, extBindings);
    properties.put(JAXBContextProperties.MOXY_FACTORY, JAXBContextProperties.Factory.DYNAMIC);
    // Have to include a path to a jaxb.properties, so just reusing a context path that does contain one.
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    DynamicEntity emp = jaxbContext.newDynamicEntity(ALT_EMPLOYEE_CLASS_NAME);
    assertNotNull(emp);
    try {
        // These sets will fail if the external bindings file was not read properly
        emp.set("empId", "747");
        emp.set("lastNameCommaFirstName", "Neilson, Leslie");
    } catch (DynamicException e) {
        fail("External bindings file not applied.");
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DynamicException(org.eclipse.persistence.exceptions.DynamicException) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)

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