/* * Copyright (c) 2010-2012 Research In Motion Limited. All rights reserved. * * This program and the accompanying materials are made available * under the terms of the Apache License, Version 2.0, * which accompanies this distribution and is available at * * http://www.apache.org/licenses/LICENSE-2.0 * */ package net.rim.ejde.installer; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class XMLHelper { public static void writeXmlFile(Document doc, String filename) { try { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException ex) { System.out.println(ex.getMessage()); } catch (TransformerException ex) { System.out.println(ex.getMessage()); } } public static Document openXmlFile(String filename, boolean validating) { return openXmlFile(new File(filename), validating); } public static Document openXmlFile(File file, boolean validating) { try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validating); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(file); return doc; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid } catch (ParserConfigurationException ex) { System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println(ex.getMessage()); } return null; } public static String evaluate(File xmlFile, String xPathExpression) { String result=null; try { XPathFactory factory=XPathFactory.newInstance(); XPath xPath=factory.newXPath(); result=xPath.evaluate(xPathExpression, new InputSource(new FileInputStream(xmlFile))); }catch (Exception ex) { System.out.println(ex.getMessage()); } return result; } }