View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.util;
20  
21  import java.io.StringReader;
22  import java.net.URL;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.Map;
28  
29  import net.sf.jameleon.exception.JameleonScriptException;
30  
31  import org.dom4j.Document;
32  import org.dom4j.DocumentException;
33  import org.dom4j.DocumentFactory;
34  import org.dom4j.Node;
35  import org.dom4j.io.SAXReader;
36  
37  public class XMLHelper {
38  
39      protected SAXReader parser;
40      protected Document document;
41  
42      /***
43       * Contstructor
44       * @param xml - The XML to parse
45       */
46      public XMLHelper(String xml){
47          setUpSaxReader();
48          try{
49              document = parser.read(new StringReader(xml));
50          }catch(DocumentException de){
51              throw new JameleonScriptException(cleanNestedMessage(de), de);
52          }
53      }
54  
55      /***
56       * Contstructor
57       * @param xmlFile - The XML to parse
58       */
59      public XMLHelper(URL xmlFile){
60          setUpSaxReader();
61          try{
62              document = parser.read(xmlFile);
63          }catch(DocumentException de){
64              throw new JameleonScriptException(cleanNestedMessage(de), de);
65          }
66      }
67  
68      /***
69       * Set up the SAXReader such that it understands the jameleon namespace
70       */
71      protected void setUpSaxReader(){
72          parser = new SAXReader();
73          Map uris = new HashMap();
74          uris.put("jm", "jelly:jameleon");
75          DocumentFactory.getInstance().setXPathNamespaceURIs(uris);
76      }
77  
78      /***
79       * Gets the text value of the given tag
80       * @param xpath - the xpath representing a tag
81       * @return The text representing the tag
82       */
83      public String getValueFromXPath(String xpath){
84          String value = null;
85          Node node = document.selectSingleNode(xpath);
86          if (node != null) {
87              value = node.getText();
88          }
89          if ("null".equals(value)) {
90              value = null;
91          }
92          return value;
93      }
94  
95      /***
96       * Gets the boolean value of the given tag
97       * @param xpath - the xpath representing a tag
98       * @return a boolean value of the given tag
99       */
100     public boolean getBooleanValueFromXPath(String xpath){
101         String value = document.selectSingleNode( xpath ).getText();
102         boolean bValue = false;
103         if ("true".equals(value) || "yes".equals(value)) {
104             bValue = true;
105         }
106         return bValue;
107     }
108 
109     /***
110      * Gets the Document for the XML represented by the provided text
111      * @return The Document for the XML represented by the provided text
112      */
113     public Document getDocument(){
114         return document;
115     }
116 
117     /***
118      * Gets a list of Values from the xml
119      * @param xpath - the xpath representing the desired tags
120      * @return a List of nodes matching the xpath
121      */
122     public List getValuesFromXPath(String xpath){
123         List nodes = getListFromXPath(xpath);
124         List values = new LinkedList();
125         if (nodes != null) {
126             Iterator it = nodes.iterator();
127             Node node = null;
128             while (it.hasNext()) {
129                 node = (Node)it.next();
130                 values.add(node.getText());
131             }
132         }
133         return values;
134     }
135 
136     /***
137      * Gets a list of Nodes from the xml
138      * @param xpath - the xpath representing the desired tags
139      * @return a List of nodes matching the xpath
140      */
141     public List getListFromXPath(String xpath){
142         return document.selectNodes(xpath);
143     }
144 
145     protected String cleanNestedMessage(Throwable t){
146         String msg = t.getMessage();
147         if (msg != null) {
148             msg = msg.replaceAll(" Nested exception:.*", "");
149             msg = msg.replaceAll(": ", ":\n");
150         }
151         return msg;
152     }
153 
154 }