Search in sources :

Example 16 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testCreateContextFromXSDNode.

/*
    public void testCreateContextInputStreamInvalidSchema() throws Exception {
        // 'createContextFromXSD' can catch some schema errors (e.g. undeclared namespace), but
        // other more basic syntax problems (e.g. a "weak correctness check") will end up getting thrown
        // as a SAXParseException and is uncatchable.

        InputStream inputStream = ClassLoader.getSystemResourceAsStream(INVALID_XSD);

        JAXBException caughtException = null;
        try {
            DynamicJAXBContextFactory.createContextFromXSD(inputStream, null, null, null);
        } catch (JAXBException e) {
            caughtException = e;
        }

        assertNotNull("Did not catch exception as expected.", caughtException);
        assertEquals("Incorrect exception thrown.", 50046, ((org.eclipse.persistence.exceptions.JAXBException) caughtException.getLinkedException()).getErrorCode());
    }
    */
public void testCreateContextFromXSDNode() throws Exception {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xsdDocument = docBuilder.parse(inputStream);
    Element xsdElement = xsdDocument.getDocumentElement();
    DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(xsdElement, null, null, null);
    DynamicEntity emp = jaxbContext.newDynamicEntity(EMPLOYEE_CLASS_NAME);
    assertNotNull(emp);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 17 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testCreateContextFromXSDSource.

public void testCreateContextFromXSDSource() throws Exception {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    Document xsdDocument = docFactory.newDocumentBuilder().parse(inputStream);
    Source domSource = new DOMSource(xsdDocument);
    DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(domSource, null, null, null);
    DynamicEntity emp = jaxbContext.newDynamicEntity(EMPLOYEE_CLASS_NAME);
    assertNotNull(emp);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 18 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testCreateContextFromXSDInputStream.

// ========================================================================
public void testCreateContextFromXSDInputStream() throws JAXBException {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(inputStream, null, null, null);
    DynamicEntity emp = jaxbContext.newDynamicEntity(EMPLOYEE_CLASS_NAME);
    assertNotNull(emp);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream)

Example 19 with DynamicJAXBContext

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

the class DynamicWithStaticAddressNoPropsTestCases method getControlObject.

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

Example 20 with DynamicJAXBContext

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

the class DynamicWithStaticOverrideTestCases method getControlObject.

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

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