protected Object parseUsingJaxb( Element element, ParserContext parserContext, Binder<Node> binder) { try { return binder.unmarshal(element); } catch (JAXBException e) { throw new ComponentDefinitionException("Failed to parse JAXB element: " + e, e); } }
/** * Write Data to the existing XML File. * * @param filePath the path to the file to which the data needs to be written * @param actualData the actual data that needs to be written to the file. */ @Override public void writeData(String filePath, Map<String, List<Map<String, Object>>> actualData) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); // File xml = new File(filePath); ResourceLoader resource = new ResourceLoader(filePath); Document document = db.parse(resource.getInputStream()); Binder<Node> binder = getJAXBContext().createBinder(); binder.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); InputTestData testData = (InputTestData) binder.unmarshal(document); updateTestMethods(testData, actualData); binder.updateXML(testData); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(resource.getFileOutputStream())); } catch (ParserConfigurationException e) { LOG.error( "Ignoring the write operation as ParserConfigurationException occured while parsing the file : " + filePath, e); } catch (SAXException e) { LOG.error( "Ignoring the write operation as SAXException occured while parsing the file : " + filePath, e); } catch (IOException e) { LOG.error( "Ignoring the write operation as IOException occured while parsing the file : " + filePath, e); } catch (JAXBException e) { LOG.error( "Ignoring the write operation as JAXBException occured while parsing the file : " + filePath, e); } catch (TransformerException e) { LOG.error( "Ignoring the write operation as TransformerException occured while parsing the file : " + filePath, e); } }
public void testNamespaceCollision() throws Exception { String xml = "<ns0:employee xmlns:ns1=\"mynamespace3\" xmlns:ns2=\"mynamespace2\" xmlns:ns0=\"mynamespace1\"><ns2:address><street>123 Fake Street</street></ns2:address><firstName>Matt</firstName><id>123</id></ns0:employee>"; String controlSource = "org/eclipse/persistence/testing/jaxb/binder/nscollision/employee.xml"; Document controlDocument = parser.parse(Thread.currentThread().getContextClassLoader().getResource(controlSource)); JAXBContext ctx = JAXBContextFactory.createContext(new Class[] {Employee.class}, null); Binder binder = ctx.createBinder(); JAXBElement elem = binder.unmarshal(parser.parse(new StringReader(xml)), Employee.class); Employee emp = (Employee) elem.getValue(); emp.address.city = "Toronto"; binder.updateXML(emp.address); JAXBXMLComparer comparer = new JAXBXMLComparer(); assertTrue( "Marshalled document does not match the control document.", comparer.isNodeEqual(controlDocument, ((Node) binder.getXMLNode(emp)).getOwnerDocument())); }
/** * Used for auto registering endpoints from the <tt>from</tt> or <tt>to</tt> DSL if they have an * id attribute set */ protected void registerEndpointsWithIdsDefinedInFromOrToTypes( Element element, ParserContext parserContext, String contextId, Binder<Node> binder) { NodeList list = element.getChildNodes(); int size = list.getLength(); for (int i = 0; i < size; i++) { Node child = list.item(i); if (child instanceof Element) { Element childElement = (Element) child; Object object = binder.getJAXBNode(child); // we only want from/to types to be registered as endpoints if (object instanceof FromDefinition || object instanceof SendDefinition) { registerEndpoint(childElement, parserContext, contextId); } // recursive registerEndpointsWithIdsDefinedInFromOrToTypes( childElement, parserContext, contextId, binder); } } }
protected void injectNamespaces(Element element, Binder<Node> binder) { NodeList list = element.getChildNodes(); Namespaces namespaces = null; int size = list.getLength(); for (int i = 0; i < size; i++) { Node child = list.item(i); if (child instanceof Element) { Element childElement = (Element) child; Object object = binder.getJAXBNode(child); if (object instanceof NamespaceAware) { NamespaceAware namespaceAware = (NamespaceAware) object; if (namespaces == null) { namespaces = new Namespaces(element); } namespaces.configure(namespaceAware); } injectNamespaces(childElement, binder); } } }