/** *

* File XMLUtil.java *

*

* Package fr.interfacesi.gpm.util *

*

* Created on May 8, 2006 *

*

* @author AdrianS *

*/ package fr.interfacesi.gpm.util; import java.util.List; import java.util.Map; import java.util.HashMap; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.apache.xpath.XPathAPI; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.iso_relax.verifier.*; /** *

* This class implements xml files tasks *

*

* Created on May 8, 2006 *

*

* @author AdrianS *

*

* @version $Revision: 1.1 $ *

*/ public class XMLUtil { /** * logger object */ static Log logger = LogFactory.getLog(XMLUtil.class); /** * Initializes a DOM xml tree from a path * @param xmlPath * @return an instance to a DOM Document */ public static org.w3c.dom.Document initXMLDoc(String xmlPath) { Document doc = null; GPMUtil.writeLog("XMLUtil.initXMLDoc",Messages.getString("templatesGui.log.message63"),null); try { File file = new File(xmlPath); if (!file.exists()) return null; FileInputStream fileInputStream = new FileInputStream(file); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(fileInputStream); doc = builder.parse(inputSource); } catch(Exception ex) { GPMUtil.writeLog("XMLUtil.initXMLDoc","",ex); logger.error("initXMLDoc: ", ex); } return doc; } /** * Initializes a DOM xml tree from a buffer * @param xmlBuff * @return an instance to a DOM Document */ public static org.w3c.dom.Document initXMLDoc(byte[] xmlBuff) { Document doc = null; GPMUtil.writeLog("XMLUtil.initXMLDoc",Messages.getString("templatesGui.log.message64"),null); try { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlBuff); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(byteArrayInputStream); doc = builder.parse(inputSource); } catch(Exception ex) { GPMUtil.writeLog("XMLUtil.initXMLDoc","",ex); logger.error("initXMLDoc: ", ex); } return doc; } /** * Verify a DOM document with a RelaxNG type schema * @param doc * @param rngFile * @return xml file has been validated or not */ public static boolean validateXmlWithNG(Document doc, String rngFile) { boolean ret = true; GPMUtil.writeLog("XMLUtil.validateXmlWithNG",Messages.getString("templatesGui.log.message65"),null); //no .rng file, assume xml file is valid if (!GPMUtil.existsFile(rngFile)) return ret; try { // setup JARV and compile a schema. VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl(); Verifier verifier = factory.compileSchema(rngFile).newVerifier(); ret = verifier.verify(doc); } catch(Exception ex) { GPMUtil.writeLog("XMLUtil.validateXmlWithNG","",ex); ret = false; logger.info(ex); } return ret; } /** * XPATH-based function to search an xml node inside an xml tree * @param xpathFilter * XPATH based query string * @return Node */ public static Node findNode(Node node, String xpathFilter) { Node nodeRet = null; try { nodeRet = XPathAPI.selectSingleNode(node,xpathFilter); } catch (Exception ex) { logger.error("initXMLDoc: ", ex); } return nodeRet; } /** * XPATH-based function to search xml nodes inside an xml tree * @param xpathFilter * XPATH based query string * @return NodeList */ public static NodeList findNodes(Node node, String xpathFilter) { NodeList list = null; try { list = XPathAPI.selectNodeList(node,xpathFilter); } catch (Exception ex) { logger.error("findNodes: ", ex); } return list; } /** * * @param doc * @param node * @param param */ public static void editNode(Document doc,Node node,List param) { String nodeType = (String)param.get(0); String nodeText = (String)param.get(1); Attr attr = null; String attrName = null; String attrValue = null; try { for (int i = 2;i0) { Node newTextNode = doc.createTextNode(nodeText); Node oldTextNode = null; NodeList list = node.getChildNodes(); for (int j=0;j0) child = doc.createElement(nodeType); else child = node; Attr attr = null; String attrName = null; String attrValue = null; boolean found = false; try { for (int i = 2;i0) { if (nodeText.length()>0) { Node textNode = doc.createTextNode(nodeText); child.appendChild(textNode); } node.appendChild(child); } else { if (nodeText.length()>0) { Node newTextNode = doc.createTextNode(nodeText); Node oldTextNode = null; NodeList list = node.getChildNodes(); for (int j=0;j-1) { doc = XMLUtil.initXMLDoc(path + File.separator + name); break; } } return doc; } /** * * @param doc * @return map with template control info */ public static Map getTemplateControlInfo(Document doc) { Node n = null; Node child = null; Node child1 = null; String name = null; String value = null; Map tplMap = new HashMap(); n = XMLUtil.findNode(doc,GPMUtil.XPATH_TEMPLATE); if(null == n) { return null; } //first its attributes, then the child nodes for (int j=0;j