Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sonoma/XMLUtility.java


1   
2   package com.sonoma;
3   import java.io.FileInputStream;
4   import java.io.FileOutputStream;
5   import java.io.PrintWriter;
6   import java.io.FileNotFoundException;
7   import java.util.Properties;
8   import org.apache.xerces.parsers.DOMParser;
9   import org.apache.xpath.XPathAPI;
10  import org.apache.xml.utils.TreeWalker;
11  import org.apache.xml.utils.DOMBuilder;
12  import org.w3c.dom.Document;
13  import org.w3c.dom.Element;
14  import org.w3c.dom.DocumentFragment;
15  import org.w3c.dom.Node;
16  import org.w3c.dom.NamedNodeMap;
17  import org.w3c.dom.NodeList;
18  import org.w3c.dom.traversal.NodeIterator;
19  import org.xml.sax.SAXException;
20  import org.xml.sax.InputSource;
21  
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.parsers.ParserConfigurationException; 
25  import javax.xml.transform.OutputKeys;
26  
27  import org.apache.xalan.serialize.Serializer;
28  import org.apache.xalan.serialize.SerializerFactory;
29  
30  import org.apache.xalan.templates.OutputProperties;
31  
32  /**
33  * Utility 'helper' that handles XML operations to create, load, save, and add to documents.
34  * Other classes and JSP pages use this object instead of Xerces or XML.
35  * XMLUtility.java
36  * @author Roy Hoobler
37  * @version 1.1 04/7/2002
38  */
39  public class XMLUtility extends Object {
40      protected String filename = new String("/java/jakarta-tomcat-3.2.2/webapps/logins.xml");
41      private Node ndDocRoot;
42      private Document doc;
43  
44      /** Creates new XMLUtility */
45      public XMLUtility() {
46      }
47      /**Set a document root and return a node - not supported!
48       *@deprecated
49       */
50      public Node setDocRootNode(){
51          return setDocRootNode(filename);
52      }
53          
54       private Node setDocRootNode(String fileName){
55           Node nRoot;
56           nRoot = (Node) loadDoc(fileName).getDocumentElement();
57           return nRoot;
58      }
59       /** Loads an XML file
60       *@param FileName full path to the xml file
61       */
62       public Document loadDoc(String strFileName){
63        //InputSource in;
64        try
65        {
66            InputSource in = new InputSource(new FileInputStream(strFileName));
67            System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
68  
69          //com.sun.xml.parser.DocumentBuilderFactoryImpl dfactory = new com.sun.xml.parser.DocumentBuilderFactoryImpl();
70          //  Document doc = dfactory.newDocumentBuilder().parse(in);
71            
72            javax.xml.parsers.DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
73            Document doc = dfactory.newDocumentBuilder().parse(in);
74            filename = strFileName;
75        return doc;
76        }
77        catch (Exception fnf)
78        {
79              System.out.println("FileInputStream of " + strFileName + " threw: " + fnf.toString());
80              fnf.printStackTrace();
81              return null;
82        }
83    }
84    /** setLogin should be moved to a security class
85     *@param sFileName full path to xml file
86     *@param userName the name of the user
87     *@return Returns the user directory (if found) or null (if not found)
88     */
89    public String setLogin(String sFileName, String userName){
90      String xPath = new String("");
91      String strReturn = new String("");
92            filename = sFileName;
93      try{
94                  xPath = "/application/login[@username='" + userName + "']";
95                  ndDocRoot = setDocRootNode();
96                  Node n = XPathAPI.selectSingleNode(ndDocRoot, xPath);
97                  NamedNodeMap nattr;
98                          nattr = n.getAttributes();
99                          strReturn = nattr.item(0).getNodeValue();
100                 return strReturn;
101                 }catch(Exception e){
102                         e.printStackTrace();
103                         return null;
104                 }
105   }
106 
107 
108   /** setLogin should be moved to a security class 
109    *Different from set Login.  Using a password actually gives permissions to do stuff
110    *@param sFileName full path to xml file
111    *@param userName the name of the user
112    *@param password the user's password
113    *@return Returns the user directory (if found) or null (if not found)
114    */
115  public String setAdmin(String sFileName, String userName, String password){
116     String xPath = new String("");
117     String strReturn = new String("");
118           filename = sFileName;
119     xPath = "/application/login[@username='"  + userName + "' and @password='" + password + "']";
120     try{
121                 ndDocRoot = setDocRootNode();
122                 Node nI = XPathAPI.selectSingleNode(ndDocRoot, xPath);
123                 NamedNodeMap nattr;
124                 Node n = XPathAPI.selectSingleNode(ndDocRoot,xPath);
125                 nattr = n.getAttributes();
126                 strReturn = nattr.item(2).getNodeValue();
127                 return strReturn;
128         }catch(Exception e){
129                 e.printStackTrace();
130                 return null;
131             }
132          }
133 
134          /** Deprecated.  Creates a new node based on the partName and value
135           *@deprecated
136           */
137   public Node createTextElement(String partName, String value){
138     Node ndNew;
139     ndNew = doc.createElement(partName);
140     ndNew.appendChild(doc.createTextNode(value));
141     return ndNew;
142   }
143          /** creates a new node based on the partName and value
144           */
145   public Node createTextElement(Document doc, String partName, String value){
146     Node ndNew;
147     ndNew = doc.createElement(partName);
148     ndNew.appendChild(doc.createTextNode(value));
149     return ndNew;
150   }
151         /** Returns an XML node object from the XPath query
152          *@param doc the xml document to be queried
153          *@param XPath the XPath query to find the desired node
154          *@return The node if found or null
155          */
156         public Node getNode(Document doc, String XPathQuery){
157             Node n;
158             
159             try{
160                       NodeIterator nl = XPathAPI.selectNodeIterator(doc, XPathQuery);
161 
162                     n = XPathAPI.selectSingleNode(doc, XPathQuery);
163                     return n;
164             }catch(Exception e){
165                     System.out.println( " Node Iterator: " + e.getMessage());
166                     return null;
167             }
168     
169   }
170     /** This function is not yet working!  There seems to be some buffer size issues
171      *Use the XMLDocumentWriter instead (for now)
172      */
173     public boolean saveDoc(Document doc, String strFileName){
174         try{
175             FileOutputStream OS = new FileOutputStream(strFileName);
176             OutputProperties OP = new OutputProperties();
177             OP.getDefaultMethodProperties("xml");
178             OP.setProperty(OutputKeys.METHOD,"xml");
179             OP.setProperty(OutputKeys.INDENT,"yes");
180             org.apache.xalan.serialize.Serializer serializer = org.apache.xalan.serialize.SerializerFactory.getSerializer
181                                    (OutputProperties.getDefaultMethodProperties("xml"));
182             serializer.setOutputStream(OS);
183             serializer.asDOMSerializer().serialize(doc);
184             return true;
185         }catch(Exception e){
186             e.printStackTrace();
187             System.out.println(strFileName + "  Not Saved!!!");
188             return false;
189         }
190     }
191 /*
192     public static boolean saveDoc(Document xmlNode){
193         return saveDoc(xmlNode, filename);
194     }        
195 */
196 }