spase.parser110
Class XMLParser

java.lang.Object
  extended by spase.parser110.XMLParser
Direct Known Subclasses:
AccessInformation, AzimuthalAngleRange, Bin, Contact, CoordinateSystem, Dimension, EnergyRange, Field, FrequencyRange, InformationURL, Measured, Particle, Photon, PhysicalParameter, PolarAngleRange, Positional, Resource, ResourceHeader, Support, TemporalDescription, TimeSpan

public class XMLParser
extends java.lang.Object

An example XML parser.

Since:
1.0.0

Constructor Summary
XMLParser()
          Creates an instance of a XML
 
Method Summary
 void dumpData(java.io.PrintStream out)
          Dump the CData sections of the XML
static void dumpList(java.lang.String label, java.util.ArrayList<java.lang.String> list)
          Print a list of values.
 void dumpNode(java.io.PrintStream out, org.w3c.dom.Node node)
          Dump the nodes of the XML
 org.w3c.dom.Node findNode(org.w3c.dom.Node node, java.lang.String name)
          Find the next node with the given name.
 java.util.ArrayList<java.lang.String> getAllValues(java.lang.String path)
          Return the list of values of a member given the XPath like reference to the member.
static java.lang.String getBranchText(org.w3c.dom.Node node)
          Concatenate all the text under a node.
 java.lang.String getClassName()
           
static javax.xml.transform.stream.StreamSource getDefaultStyleSheet()
          Obtain a StreamSource to the default XML Style Sheet.
 org.w3c.dom.Document getDocument()
          Obtain the Document containing the representation of the parsed XML files.
 java.lang.String getElementPath(java.lang.String base, int key)
          Construct an element path.
 java.lang.String getMemberName(java.lang.String name)
          Extract the member name from a string.
 XMLParser getMemberNode(java.lang.String name)
          Locates a node in a structure based on the the "name" associated with the member.
 int getMemberNodeIndex(java.lang.String name)
          Extracts the index portion of node reference.
 java.lang.String getMemberNodeName(java.lang.String name)
          Extracts the name portion of node reference.
 XMLParser getMemberParent(java.lang.String name)
          Find the parent class for a member.
 java.util.ArrayList<XMLParser> getNodes(java.lang.String path)
          Return the list of nodes of a member given the XPath like reference to the member.
 java.util.ArrayList<XMLParser> getNodes(java.lang.String path, boolean alwaysOne)
          Return the list of nodes of a member given the XPath like reference to the member.
 java.util.ArrayList<XMLParser> getNodes(XMLParser parent, java.lang.String name)
          Return the list of nodes given the name of the member.
static java.lang.String getNodeText(org.w3c.dom.Node node)
          Concatenate all the text under a node.
 java.lang.String getPath()
          Return the path to the parsed document
 java.lang.String getPathName()
          Return the pathname of the parsed document
 java.io.StringReader getStringReader()
          Return a StringReader for an XML representation of the document.
static java.lang.String getTagClose(int level, java.lang.String tagName)
          Return an close tag with the proper indentation.
static java.lang.String getTaggedList(int level, java.lang.String tagName, java.util.ArrayList<java.lang.String> list)
          Return a list of tagged values, properly indented.
static java.lang.String getTaggedValue(int level, java.lang.String tagName, java.lang.String value)
          Return a value enclosed in a tag and properly indented.
static java.lang.String getTagOpen(int level, java.lang.String tagName)
          Return an open tag with the proper indentation.
 XMLParser getTopParent(java.lang.String name)
          Find the top level parent class for a member.
 java.util.ArrayList<java.lang.String> getValues(java.lang.String path)
          Return the list of values of a member given the XPath like reference to the member.
 java.util.ArrayList<java.lang.String> getValues(java.lang.String path, boolean alwaysOne)
          Return the list of values of a member given the XPath like reference to the member.
 java.lang.String getXMLDocument(int n)
          Return a string containing an XML representation of this isntance.
 java.lang.String getXMLDocument(int n, java.lang.String path, int key)
          Return a string containing an XML representation of this isntance.
static java.lang.String indent(int level)
          Return a string with spaces to format to indicated indentation level.
 boolean isInList(java.lang.String base, java.lang.String value, java.util.ArrayList list, int index)
          Determine if a list is of the proper type and the index is within the range of the list.
 boolean isMatch(java.lang.String base, java.lang.String value)
          Return a list of tagged values, properly indented.
 void load(org.w3c.dom.Document document, java.lang.String root)
          Parses a Document which contains a parsed XML file and set internal variables with the contents of the file.
 void load(java.lang.String pathName)
          Parses a file containing XML into its constitute elments and sets internal variables with the contents of the file.
 void load(java.lang.String pathName, java.lang.String root)
          Parses a file containing XML into its constitute elments and sets internal variables with the contents of the file.
static void main(java.lang.String[] args)
          Entry point for testing
 void makeEditNodes()
          Create an instance of all nodes for use when editing.
 void makeNewMember(java.lang.String name)
          Create a new member.
 java.lang.String makeTagContent(java.lang.String tag, java.lang.String content)
          Return a string with content enclosed in tags with the passed name.
 boolean parseXML(java.io.InputStream stream)
          Parses a file containing XML into its constitute elments.
 boolean parseXML(java.lang.String pathName)
          Parses a file containing XML into its constitute elments.
 void printXML(java.io.PrintStream out)
          Generates an XML representation of the label and stream it to the print stream.
 void processNode(org.w3c.dom.Node node)
          Walk the nodes of the Document and populate the resource classes.
 void removeMember(java.lang.String name, int index)
          Remove an elment of a member.
 void setClassName(java.lang.String name)
           
 void setMember(java.lang.String name, org.w3c.dom.Node value)
          Call the set() method with a given name suffix and a DOM Node as an argument.
 java.lang.String setMember(java.lang.String name, java.lang.String value)
          Call the set() method with a given name suffix and a String as an argument.
 void setMember(java.lang.String name, java.lang.String[] value)
          Call the set() method with a given name suffix and an array of String values as an argument.
static java.lang.String toImproperCase(java.lang.String value)
          Convert a string to "improper" case (make first letter lower case).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser

public XMLParser()
Creates an instance of a XML

Method Detail

main

public static void main(java.lang.String[] args)
Entry point for testing

Since:
1.0

load

public void load(java.lang.String pathName)
          throws java.lang.Exception
Parses a file containing XML into its constitute elments and sets internal variables with the contents of the file. The path and name of the file are passed to the method which is opened and parsed.

Parameters:
pathName - the fully qualified path and name of the file to parse.
Throws:
java.lang.Exception
Since:
1.0

load

public void load(java.lang.String pathName,
                 java.lang.String root)
          throws java.lang.Exception
Parses a file containing XML into its constitute elments and sets internal variables with the contents of the file. The path and name of the file are passed to the method which is opened and parsed.

Parameters:
pathName - the fully qualified path and name of the file to parse.
root - the name of the tag containg elements to process.
Throws:
java.lang.Exception
Since:
1.0

load

public void load(org.w3c.dom.Document document,
                 java.lang.String root)
          throws java.lang.Exception
Parses a Document which contains a parsed XML file and set internal variables with the contents of the file.

Parameters:
document - the Document representation of a parsed XML file.
root - the name of the tag containg elements to process.
Throws:
java.lang.Exception
Since:
1.0

parseXML

public boolean parseXML(java.lang.String pathName)
                 throws java.lang.Exception
Parses a file containing XML into its constitute elments. The path and name of the file are passed to the method which is opened and parsed.

Parameters:
pathName - the fully qualified path and name of the file to parse.
Returns:
true if the file could be opened; false otherwise.
Throws:
java.lang.Exception
Since:
1.0

parseXML

public boolean parseXML(java.io.InputStream stream)
                 throws java.lang.Exception
Parses a file containing XML into its constitute elments. The file to parse must be previously opened and a InputStream pointing to the file is passed.

Parameters:
stream - a connection to a pre-opened file.
Returns:
true if the file could be read; false otherwise.
Throws:
java.lang.Exception
Since:
1.0

printXML

public void printXML(java.io.PrintStream out)
              throws java.lang.Exception
Generates an XML representation of the label and stream it to the print stream.

Parameters:
out - the stream to print the element to.
Throws:
java.lang.Exception
Since:
1.0

dumpData

public void dumpData(java.io.PrintStream out)
              throws java.lang.Exception
Dump the CData sections of the XML

Parameters:
out - the stream to print the element to.
Throws:
java.lang.Exception
Since:
1.0

dumpNode

public void dumpNode(java.io.PrintStream out,
                     org.w3c.dom.Node node)
Dump the nodes of the XML

Parameters:
out - the stream to print the element to.
Since:
1.0

findNode

public org.w3c.dom.Node findNode(org.w3c.dom.Node node,
                                 java.lang.String name)
                          throws java.lang.Exception
Find the next node with the given name.

Parameters:
node - the Node to start at.
name - the name of the node to look for.
Throws:
java.lang.Exception
Since:
1.0

getDefaultStyleSheet

public static javax.xml.transform.stream.StreamSource getDefaultStyleSheet()
Obtain a StreamSource to the default XML Style Sheet.

Returns:
a StreamSource which can be used to read the default style sheet.
Since:
1.0

getDocument

public org.w3c.dom.Document getDocument()
Obtain the Document containing the representation of the parsed XML files.

Returns:
a Document containing the representation of the parsed XML file.
Since:
1.0

processNode

public void processNode(org.w3c.dom.Node node)
                 throws java.lang.Exception
Walk the nodes of the Document and populate the resource classes. The name of each element encountered is used to find a method to store the value. The method search for has the element name with the prefix "set" and an parameter of type "String". If such a method is found the text contained by the element tags is passed to the method. Then a method with the prefix of "set" is with a parameter of of type "Node" is attempted. If such a method is found then method is called with the Node representing the element is passed as the argument.

Parameters:
node - the Node.
Throws:
java.lang.Exception
Since:
1.0

setMember

public java.lang.String setMember(java.lang.String name,
                                  java.lang.String value)
Call the set() method with a given name suffix and a String as an argument.

Parameters:
value - the String value to set.
Since:
1.0

setMember

public void setMember(java.lang.String name,
                      java.lang.String[] value)
Call the set() method with a given name suffix and an array of String values as an argument.

Parameters:
value - the String value to set.
Since:
1.0

setMember

public void setMember(java.lang.String name,
                      org.w3c.dom.Node value)
Call the set() method with a given name suffix and a DOM Node as an argument.

Parameters:
name - the name of the member (suffix for setXXX() method).
value - the String value to set.
Since:
1.0

getMemberParent

public XMLParser getMemberParent(java.lang.String name)
Find the parent class for a member. The syntax is [parent.]member where "parent." can have multiple levels. If "parent" is blank, then its self-referential.

Parameters:
node - the Node.
Since:
1.0

getTopParent

public XMLParser getTopParent(java.lang.String name)
Find the top level parent class for a member. The syntax is [parent.]member where "parent." can have multiple levels. If "parent" is blank, then its self-referential.

Parameters:
name - the XPath name for for the member.
Since:
1.0

getMemberName

public java.lang.String getMemberName(java.lang.String name)
Extract the member name from a string. The memeber name is the text following the last node delimiter. The syntax is [parent.]member

Parameters:
node - the Node.
Since:
1.0

getMemberNode

public XMLParser getMemberNode(java.lang.String name)
Locates a node in a structure based on the the "name" associated with the member. A "node" is a member of a class which also has members. To be implemented in each derived class. A name may have an option index. The full syntax is: "name[index]"

Parameters:
name - the name associated with a member.
Since:
1.0

getMemberNodeName

public java.lang.String getMemberNodeName(java.lang.String name)
Extracts the name portion of node reference. A node reference has the syntax "name[index]". The index ("[index]") is optional.

Parameters:
name - the name associated with a member.
Since:
1.0

getMemberNodeIndex

public int getMemberNodeIndex(java.lang.String name)
Extracts the index portion of node reference. A node reference has the syntax "name[index]". The index ("[index]") is optional. The default index is zero.

Parameters:
name - the name associated with a member.
Since:
1.0

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node node)
Concatenate all the text under a node.

Parameters:
node - the Node.
Since:
1.0

getBranchText

public static java.lang.String getBranchText(org.w3c.dom.Node node)
                                      throws java.lang.Exception
Concatenate all the text under a node.

Parameters:
node - the Node.
Throws:
java.lang.Exception
Since:
1.0

getPathName

public java.lang.String getPathName()
Return the pathname of the parsed document

Since:
1.0

getPath

public java.lang.String getPath()
Return the path to the parsed document

Since:
1.0

makeTagContent

public java.lang.String makeTagContent(java.lang.String tag,
                                       java.lang.String content)
Return a string with content enclosed in tags with the passed name.

Since:
1.0

indent

public static java.lang.String indent(int level)
Return a string with spaces to format to indicated indentation level.

Since:
1.0

getXMLDocument

public java.lang.String getXMLDocument(int n)
Return a string containing an XML representation of this isntance. To be implemented in each derived class.

Parameters:
n - the number of levels to indent the document.
Since:
1.0

getXMLDocument

public java.lang.String getXMLDocument(int n,
                                       java.lang.String path,
                                       int key)
Return a string containing an XML representation of this isntance. To be implemented in each derived class.

Parameters:
n - the number of levels to indent the document.
path - the path to the element.
key - the index (key) of the element at the path.
Since:
1.0

makeNewMember

public void makeNewMember(java.lang.String name)
Create a new member. To be implemented in each derived class.

Parameters:
name - the name of the member to create.
Since:
1.0

makeEditNodes

public void makeEditNodes()
Create an instance of all nodes for use when editing. To be implemented in each derived class.

Parameters:
name - the name of the member to create.
Since:
1.0

removeMember

public void removeMember(java.lang.String name,
                         int index)
Remove an elment of a member. To be implemented in each derived class.

Parameters:
name - the name of the member to create.
index - the index of the element to remove.
Since:
1.0

getNodes

public java.util.ArrayList<XMLParser> getNodes(java.lang.String path)
Return the list of nodes of a member given the XPath like reference to the member. For example, if you want a list of "AccessURL" nodes in the "ResourceHeader" then the path would be "ResourceHeader.AccessURL"

Only returns nodes for items that are nodes.

Always returns a list with at least one entry. If no nodes are found then the returned entry is blank.

Since:
1.0

getNodes

public java.util.ArrayList<XMLParser> getNodes(java.lang.String path,
                                               boolean alwaysOne)
Return the list of nodes of a member given the XPath like reference to the member. For example, if you want a list of "AccessURL" nodes in the "ResourceHeader" then the path would be "ResourceHeader.AccessURL"

Only returns nodes for items that are nodes.

If no nodes are found then the returned list is empty unless alwaysOne is set to true.

Since:
1.0

getNodes

public java.util.ArrayList<XMLParser> getNodes(XMLParser parent,
                                               java.lang.String name)
Return the list of nodes given the name of the member.

Only returns nodes for items that are nodes.

If no nodes are found then the returned list is empty.

Since:
1.0

getAllValues

public java.util.ArrayList<java.lang.String> getAllValues(java.lang.String path)
Return the list of values of a member given the XPath like reference to the member. The XPath is a canonical path which includes node names only (no array indexes) For example, if you want all instances of the "URL" in "AccessURL" under "ResourceHeader" then the path would be "ResourceHeader.AccessURL.URL"

Only returns values for items that return String values.

Always returns a list with at least one entry. If no nodes are found then the returned entry is blank.

Since:
1.0

getValues

public java.util.ArrayList<java.lang.String> getValues(java.lang.String path)
Return the list of values of a member given the XPath like reference to the member. For example, if you want the "URL" in the second "AccessURL" item in the "ResourceHeader" then the path would be "ResourceHeader.AccessURL[1].URL"

Only returns values for items that return String values.

Always returns a list with at least one entry. If no nodes are found then the returned entry is blank.

Since:
1.0

getValues

public java.util.ArrayList<java.lang.String> getValues(java.lang.String path,
                                                       boolean alwaysOne)
Return the list of values of a member given the XPath like reference to the member. For example, if you want the "URL" in the second "AccessURL" item in the "ResourceHeader" then the path would be "ResourceHeader.AccessURL[1].URL"

Only returns values for items that return String values.

If no nodes are found then the returned list is empty unless alwaysOne is set to true.

Since:
1.0

getStringReader

public java.io.StringReader getStringReader()
Return a StringReader for an XML representation of the document. Commonly used to pass the document through an XML stylesheet transformation using Transform.

Since:
1.0

getTaggedValue

public static java.lang.String getTaggedValue(int level,
                                              java.lang.String tagName,
                                              java.lang.String value)
Return a value enclosed in a tag and properly indented.

Since:
1.0

getTagOpen

public static java.lang.String getTagOpen(int level,
                                          java.lang.String tagName)
Return an open tag with the proper indentation.

Since:
1.0

getTagClose

public static java.lang.String getTagClose(int level,
                                           java.lang.String tagName)
Return an close tag with the proper indentation.

Since:
1.0

getTaggedList

public static java.lang.String getTaggedList(int level,
                                             java.lang.String tagName,
                                             java.util.ArrayList<java.lang.String> list)
Return a list of tagged values, properly indented.

Since:
1.0

isMatch

public boolean isMatch(java.lang.String base,
                       java.lang.String value)
Return a list of tagged values, properly indented.

Since:
1.0

getElementPath

public java.lang.String getElementPath(java.lang.String base,
                                       int key)
Construct an element path. A path has the form:
parent.className[key]
where "parent." and "[key]" is optional

Since:
1.0

isInList

public boolean isInList(java.lang.String base,
                        java.lang.String value,
                        java.util.ArrayList list,
                        int index)
Determine if a list is of the proper type and the index is within the range of the list.

Since:
1.0

toImproperCase

public static java.lang.String toImproperCase(java.lang.String value)
Convert a string to "improper" case (make first letter lower case).


dumpList

public static void dumpList(java.lang.String label,
                            java.util.ArrayList<java.lang.String> list)
Print a list of values.

Parameters:
node - the Node.
Since:
1.0

setClassName

public void setClassName(java.lang.String name)

getClassName

public java.lang.String getClassName()