/* +----------------------------------------------------------------------+ | Class: CxmlParser | | | | Developper: Eric Gavaldo (egavaldo@xqual.com) | +----------------------------------------------------------------------+ */ package com.xqual.xlauncher.testpartner; //JAXP import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringReader; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * The CXmlParser/code> class defines a XML parsing utility * @author egavaldo */ public class CXmlParser { // +==============================================================+ // | Members | // +==============================================================+ private static final String OUTPUT_ENCODING = "ISO-8859-1"; private static final XPath xpath; private static DocumentBuilderFactory factory; private static TransformerFactory tFactory; private static DocumentBuilder builder; private static OutputStreamWriter errorWriter; // +==============================================================+ // | Static Constructor | // +==============================================================+ static { factory = DocumentBuilderFactory.newInstance(); tFactory = TransformerFactory.newInstance(); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.out.println("document builder: " + pce); System.exit(1); } // Set an ErrorHandler before parsing try { errorWriter = new OutputStreamWriter(System.err, OUTPUT_ENCODING); } catch (UnsupportedEncodingException uee) { System.out.println("OutputStreamWriter: " + uee); System.exit(1); } builder.setErrorHandler(new CErrorHandler(new PrintWriter(errorWriter, true))); xpath = XPathFactory.newInstance().newXPath(); } // +==============================================================+ // | Methods | // +==============================================================+ /** * @param xmlFile File used as the source of the XML Doc */ public static Document createXMLDoc(File xmlFile) { Document xmlDocument = null; try { xmlDocument = builder.parse(xmlFile); } catch (SAXException se) { System.out.println("builder.parse: " + se.getMessage()); } catch (IOException ioe) { System.out.println("builder.parse: " + ioe); } return xmlDocument; } /** * @param xmlString XML string used as the source of the XML Doc */ public static Document createXMLDoc(String xmlString) { Document xmlDocument = null; try { StringReader stringReader = new StringReader(xmlString); InputSource inputSource = new InputSource(stringReader); xmlDocument = builder.parse(inputSource); } catch (SAXException se) { System.out.println("builder.parse: " + se.getMessage()); } catch (IOException ioe) { System.out.println("builder.parse: " + ioe); } catch (Exception e) { System.out.println("builder.parse: " + e); } catch (Throwable t) { System.out.println("builder.parse: " + t); } return xmlDocument; } /** * Get the documentElement of the xml document */ public static Element getDocumentElement(Document xmlDocument) { Element thisDocumentElement = xmlDocument.getDocumentElement(); return thisDocumentElement; } /** * Get a list of nodes based on a unique name */ public static NodeList getNodeObjList( Document xmlDocument, String nodeName) { NodeList thisNodeObjList = xmlDocument.getElementsByTagName(nodeName); return thisNodeObjList; } /** * Get a single node based on its name */ public static Node getNodeObj(Document xmlDocument, String nodeName) { NodeList thisNodeObjList = getNodeObjList(xmlDocument, nodeName); Node thisNodeObj = null; if (thisNodeObjList.getLength() > 0) { thisNodeObj = thisNodeObjList.item(0); } return thisNodeObj; } /** * Get a list of nodes based on a xPath */ public static NodeList getNodeObjListFromXPath( Document xmlDocument, String xPath) { NodeList thisNodeList = null; try { thisNodeList = (NodeList)xpath.evaluate(xPath, xmlDocument, XPathConstants.NODESET); } catch (XPathExpressionException e) { System.out.println("evaluate " + xpath + ": " + e); } return thisNodeList; } /** * Get a list of nodes based on a xPath */ public static NodeList getNodeObjListFromXPath(Node node, String xPath) { NodeList thisNodeList = null; try { thisNodeList = (NodeList)xpath.evaluate(xPath, node, XPathConstants.NODESET); } catch (XPathExpressionException e) { System.out.println("evaluate " + xpath + ": " + e); } return thisNodeList; } /** * Get a single node based on a xPath */ public static Node getNodeObjFromXPath( Document xmlDocument, String xPath) { Node thisNode = null; try { thisNode = (Node)xpath.evaluate(xPath, xmlDocument, XPathConstants.NODE); } catch (XPathExpressionException e) { System.out.println("evaluate " + xpath + ": " + e); } return thisNode; } /** * Get a single node based on a xPath */ public static Node getNodeObjFromXPath(Node node, String xPath) { Node thisNode = null; try { thisNode = (Node)xpath.evaluate(xPath, node, XPathConstants.NODE); } catch (XPathExpressionException e) { System.out.println("evaluate " + xpath + ": " + e); } return thisNode; } /** * Get a single node content based on its name */ public static String getNodeValue(Document xmlDocument, String nodeName) { Node thisNodeObj = getNodeObj(xmlDocument, nodeName); String thisNodeValue = null; if (thisNodeObj != null) { thisNodeValue = thisNodeObj.getFirstChild().getNodeValue(); } return thisNodeValue; } /** * Get a single node content based on a xPath */ public static String getNodeValueFromXPath(Document xmlDocument, String xPath) { Node thisNodeObj = getNodeObjFromXPath(xmlDocument, xPath); String thisNodeValue = null; if (thisNodeObj != null) { thisNodeValue = thisNodeObj.getFirstChild().getNodeValue(); } return thisNodeValue; } /** * Get a single node content based on a xPath */ public static String getNodeValueFromXPath(Node node, String xPath) { Node thisNodeObj = getNodeObjFromXPath(node, xPath); String thisNodeValue = null; if (thisNodeObj != null) { thisNodeValue = thisNodeObj.getFirstChild().getNodeValue(); } return thisNodeValue; } /* +----------------------------------------------------------------------+ | Class: CErrorHandler | +----------------------------------------------------------------------+ */ /** * A class representing an Error Handler for XML Parsing */ private static class CErrorHandler implements ErrorHandler { // +==============================================================+ // | Members | // +==============================================================+ private PrintWriter out; // +==============================================================+ // | Constructors | // +==============================================================+ CErrorHandler(PrintWriter out) { this.out = out; } // +==============================================================+ // | Methods | // +==============================================================+ private String getParseExceptionInfo(SAXParseException spe) { String systemId = spe.getSystemId(); if (systemId == null) { systemId = "null"; } String info = "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage(); return info; } public void warning(SAXParseException spe) throws SAXException { out.println("Warning: " + getParseExceptionInfo(spe)); } public void error(SAXParseException spe) throws SAXException { String message = "Error: " + getParseExceptionInfo(spe); throw new SAXException(message); } public void fatalError(SAXParseException spe) throws SAXException { String message = "Fatal Error: " + getParseExceptionInfo(spe); throw new SAXException(message); } } }