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"));
}
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);
}
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);
}
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);
}
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.");
}
}
Aggregations