From: Pieter v. Z. <pv...@us...> - 2005-08-03 08:47:36
|
Update of /cvsroot/coefficient/coefficient/src-test/za/org/coefficient/util/common In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5736/src-test/za/org/coefficient/util/common Added Files: XMLJndiUtil.java Log Message: This class reads in a server.xml file and uses the Resource tags/elements to create a lookup table (mock jndi) --- NEW FILE: XMLJndiUtil.java --- /** * */ package za.org.coefficient.util.common; import java.io.IOException; import java.io.InputStream; import java.util.Hashtable; import java.util.Iterator; import org.apache.velocity.anakia.NodeList; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * @author pieter20 Aug 2, 2005 * This class reads in a server.xml file and uses the Resource tags/elements to * create a lookup table (mock jndi) */ public class XMLJndiUtil { /** * @author pieter20 * Aug 3, 2005 * @param fileName * @return */ public static InputStream getFileInputStream(String fileName) { InputStream xmlFile = null; // See if it is in the classpath xmlFile = XMLJndiUtil.class.getResourceAsStream("/WEB-INF/" + fileName); // See if it is in the jar if (xmlFile == null) { xmlFile = XMLJndiUtil.class.getResourceAsStream("/" + fileName); if (xmlFile == null) { xmlFile = XMLJndiUtil.class.getResourceAsStream(fileName); } }// end nested if if (xmlFile == null) { System.out.println("XMLReaderUtil.getFileInputStream()-> File not found!!!"); } return xmlFile; } /** * @author pieter20 * Aug 3, 2005 * This method reads in a xml file: server.xml and creates a mock JNDI * lookup table. The table is implemented currently as a hashtable. The "Resource" * tag element and its attributes are used. A DOM parser is used. * @param xmlFileName * @return Hashtable containing some */ public static Hashtable createMockJNDI(String xmlFileName) { Hashtable xmlTable = new Hashtable(); DOMParser domParser = new DOMParser(); InputStream xmlFile = getFileInputStream(xmlFileName); InputSource xmlSource = new InputSource(xmlFile); try { domParser.parse(xmlSource); } catch (IOException io) { io.printStackTrace(); } catch (SAXException sax) { sax.printStackTrace(); } Document doc = domParser.getDocument(); org.w3c.dom.NodeList list = doc.getElementsByTagName("Resource"); for (int i = 0; i < list.getLength(); i++) { Element resourceElement = (Element)list.item(i); Attr name = resourceElement.getAttributeNode("name"); Attr className = resourceElement.getAttributeNode("type"); xmlTable.put(name.getValue(),className.getValue()); }//end for return xmlTable; } /** * @author pieter20 Aug 2, 2005 * @param args */ public static void main(String[] args) { Hashtable mockJNDI = createMockJNDI("server.xml.snippit"); Iterator jndiItems = mockJNDI.keySet().iterator(); while (jndiItems.hasNext()) { String name = (String) jndiItems.next(); String classType = (String)mockJNDI.get(name); System.out.println("name: "+name+" -> type: "+classType ); } } } |