com.sun.faban.harness.util
Class XMLReader

java.lang.Object
  extended by com.sun.faban.harness.util.XMLReader

public class XMLReader
extends Object

Provides all XML accesses to the parameter repository and other XML files.


Constructor Summary
XMLReader(String file)
          Constructs the XML reader.
XMLReader(String file, boolean useFabanNS, boolean warnDeprecated)
          Constructs the XML reader.
 
Method Summary
 Element addNode(Element parent, String namespaceURI, String prefix, String nodeName)
          Adds a node.
 Element addNode(String baseXPath, String namespaceURI, String prefix, String nodeName)
          Adds a node.
 void deleteNode(Node deleteNode, Node parent)
          Deletes a node.
 List<String> getAttributeValues(String xpath, String attribute)
          Returns the list of attribute values for a given xpath and attribute.
 List<String> getAttributeValues(String xpath, String attribute, Node base)
          Returns the list of attribute values for a given xpath and attribute based on base node.
 List<NameValuePair<Integer>> getHostPorts(Element roleNode)
          Obtains the host:port pairs based on a role.
 List<NameValuePair<Integer>> getHostPorts(String xPathExpr)
          Obtains the host:port name value pair list from the element matching this XPath.
 Node getNode(String xpath)
          Returns node for the given xpath.
 Node getNode(String xpath, Node base)
          Returns node for the given xpath based on the base node.
 NodeList getNodeList(String xPathExpr)
          Returns NodeList for a given xpath string.
 NodeList getNodeListForTagName(String tagName)
          Returns nodelist for the given tag.
 NodeList getNodes(String xpath)
          Returns the nodelist for a given xpath.
 NodeList getNodes(String xpath, Node base)
          Returns the nodelist for a given xpath based on a base node.
 Element getRootNode()
          Returns the root element.
 NodeList getTopLevelElements()
          Returns nodelist for top level elements(Children of root element).
 String getValue(String xpath)
          Returns the value for the given xpath.
 String getValue(String xpath, Node base)
          Returns the value for the given xpath based on the base node.
 List<String> getValues(String xpath)
          Returns the list of values for a given xpath.
 List<String> getValues(String xpath, Node base)
          Returns the list of values for a given xpath based on base node.
static void main(String[] args)
          Runs some testing of the XML reader.
 void processHostPorts()
          Scans the XML for specific entries matching "//fa:hostConfig/fa:hostPorts" and processes them for later retrieval.
 void replaceValue(Element element, String value)
          Replaces the new value for the element text.
 boolean save(String backupFileName)
          This method saves the XML file if it was modified and if a back up file name is specified the original file is backed up.
 void setValue(Element element, String value)
          Sets the new value for the element.
 void setValue(String xpath, String newValue)
          Sets the new value for a given xpath.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLReader

public XMLReader(String file)
Constructs the XML reader.

Parameters:
file - The XML file

XMLReader

public XMLReader(String file,
                 boolean useFabanNS,
                 boolean warnDeprecated)
Constructs the XML reader.

Parameters:
file - The XML file
useFabanNS - Whether to apply Faban namespaces in this file
warnDeprecated - Whether to warn for deprecated Faban elements
Method Detail

getNodeListForTagName

public NodeList getNodeListForTagName(String tagName)
Returns nodelist for the given tag.

Parameters:
tagName -
Returns:
nodelist

getTopLevelElements

public NodeList getTopLevelElements()
Returns nodelist for top level elements(Children of root element).

Returns:
nodelist.

getRootNode

public Element getRootNode()
Returns the root element.

Returns:
element.

getNode

public Node getNode(String xpath)
Returns node for the given xpath.

Parameters:
xpath -
Returns:
node

getNode

public Node getNode(String xpath,
                    Node base)
Returns node for the given xpath based on the base node.

Parameters:
xpath -
base -
Returns:
node.

getNodes

public NodeList getNodes(String xpath)
Returns the nodelist for a given xpath.

Parameters:
xpath -
Returns:
nodelist.

getNodes

public NodeList getNodes(String xpath,
                         Node base)
Returns the nodelist for a given xpath based on a base node.

Parameters:
xpath -
base -
Returns:
nodelist.

getValue

public String getValue(String xpath)
Returns the value for the given xpath.

Parameters:
xpath -
Returns:
string.

getValue

public String getValue(String xpath,
                       Node base)
Returns the value for the given xpath based on the base node.

Parameters:
xpath -
base -
Returns:
string.

getValues

public List<String> getValues(String xpath)
Returns the list of values for a given xpath.

Parameters:
xpath -
Returns:
List.

getValues

public List<String> getValues(String xpath,
                              Node base)
Returns the list of values for a given xpath based on base node.

Parameters:
xpath -
base -
Returns:
List.

getAttributeValues

public List<String> getAttributeValues(String xpath,
                                       String attribute)
Returns the list of attribute values for a given xpath and attribute.

Parameters:
xpath -
attribute -
Returns:
List.

getAttributeValues

public List<String> getAttributeValues(String xpath,
                                       String attribute,
                                       Node base)
Returns the list of attribute values for a given xpath and attribute based on base node.

Parameters:
xpath -
attribute -
base -
Returns:
List.

setValue

public void setValue(String xpath,
                     String newValue)
Sets the new value for a given xpath.

Parameters:
xpath - The XPath to the element to set the value
newValue - The new value

setValue

public void setValue(Element element,
                     String value)
Sets the new value for the element.

Parameters:
element - The DOM element
value - The new value

replaceValue

public void replaceValue(Element element,
                         String value)
Replaces the new value for the element text.

Parameters:
element -
value -

deleteNode

public void deleteNode(Node deleteNode,
                       Node parent)
Deletes a node.

Parameters:
deleteNode -
parent -

addNode

public Element addNode(Element parent,
                       String namespaceURI,
                       String prefix,
                       String nodeName)
Adds a node.

Parameters:
parent -
namespaceURI -
prefix -
nodeName -
Returns:
Element

addNode

public Element addNode(String baseXPath,
                       String namespaceURI,
                       String prefix,
                       String nodeName)
Adds a node.

Parameters:
baseXPath -
namespaceURI -
prefix -
nodeName -
Returns:
Element

save

public boolean save(String backupFileName)
             throws Exception
This method saves the XML file if it was modified and if a back up file name is specified the original file is backed up.

Parameters:
backupFileName - - the name of the backup file
Returns:
Whether the save succeeded
Throws:
Exception - Error saving the file

processHostPorts

public void processHostPorts()
Scans the XML for specific entries matching "//fa:hostConfig/fa:hostPorts" and processes them for later retrieval.


getHostPorts

public List<NameValuePair<Integer>> getHostPorts(String xPathExpr)
Obtains the host:port name value pair list from the element matching this XPath.

Parameters:
xPathExpr -
Returns:
The list of host:port elements, or null if the XPath does not exist or does not point to a host:port node.

getHostPorts

public List<NameValuePair<Integer>> getHostPorts(Element roleNode)
Obtains the host:port pairs based on a role.

Parameters:
roleNode - The node representing the role
Returns:
The host:port pairs

getNodeList

public NodeList getNodeList(String xPathExpr)
Returns NodeList for a given xpath string.

Parameters:
xPathExpr -
Returns:
NodeList

main

public static void main(String[] args)
                 throws Exception
Runs some testing of the XML reader.

Parameters:
args - The command line arguments
Throws:
Exception - If there are errors with the test