public class XMLParser extends Object
Constructor and Description |
---|
XMLParser()
Creates an instance of a XML
|
Modifier and Type | Method and Description |
---|---|
void |
addRequired(String value) |
void |
dump()
Prints the XML document to the currently define System.out.
|
void |
dumpData(PrintStream out)
Dump the CData sections of the XML
|
void |
dumpNode(PrintStream out,
Node node)
Dump the nodes of the XML
|
static String |
entityEncode(String text)
Convert special characters in a string for use in an
HTML document.
|
Node |
findNode(Node node,
String name)
Find the next node with the given name.
|
ArrayList<String> |
getAllValues(String path)
Return the list of values of a member given the XPath like reference to the member.
|
static String |
getBranchText(Node node)
Concatenate all the text under a node.
|
String |
getClassName() |
static StreamSource |
getDefaultStyleSheet()
Obtain a StreamSource to the default XML Style Sheet.
|
Document |
getDocument()
Obtain the Document containing the representation of the parsed XML files.
|
String |
getElementPath(String base,
int key)
Construct an abbreviated XPath for a a class node.
|
Integer |
getIndent()
Return the indent value.
|
String |
getMemberName(String name)
Extract the member name from an XPath string.
|
XMLParser |
getMemberNode(String name)
Locates a node in a structure based on the the "name" associated
with the member.
|
int |
getMemberNodeIndex(String name)
Extracts the index portion of node reference.
|
String |
getMemberNodeName(String name)
Extracts the name portion of node reference.
|
XMLParser |
getMemberParent(String name)
Find the parent class for a member.
|
String |
getMemberPath(String name)
Extract the path to a member from an XPath string.
|
ArrayList<XMLParser> |
getNodes(String path)
Return the list of nodes of a member given the XPath like reference to the member.
|
ArrayList<XMLParser> |
getNodes(String path,
boolean alwaysOne)
Return the list of nodes of a member given the XPath like reference to the member.
|
ArrayList<XMLParser> |
getNodes(XMLParser parent,
String name)
Return the list of nodes given the name of the member.
|
static String |
getNodeText(Node node)
Concatenate all the text under a node.
|
String |
getPath()
Return the path to the parsed document
|
String |
getPathName()
Return the pathname of the parsed document
|
ArrayList<String> |
getRequired() |
StringReader |
getStringReader()
Return a
StringReader for an XML representation of the document. |
static String |
getTagClose(int level,
String tagName)
Return an close tag with the proper indentation.
|
static String |
getTaggedList(int level,
String tagName,
ArrayList<String> list)
Return a list of tagged values, properly indented.
|
String |
getTaggedList(int level,
String tagName,
ArrayList<String> list,
boolean inUseOnly)
Return a list of tagged values, properly indented.
|
static String |
getTaggedValue(int level,
String tagName,
String value)
Return a value enclosed in a tag and properly indented.
|
String |
getTaggedValue(int level,
String tagName,
String value,
boolean inUseOnly)
Return a value enclosed in a tag and properly indented.
|
static String |
getTagOpen(int level,
String tagName)
Return an open tag with the proper indentation.
|
XMLParser |
getTopParent(String name)
Find the top level parent class for a member.
|
ArrayList<String> |
getValues(String path)
Return the list of values of a member given the XPath like reference to the member.
|
ArrayList<String> |
getValues(String path,
boolean alwaysOne)
Return the list of values of a member given the XPath like reference to the member.
|
ArrayList<String> |
getWords()
Walk the internals of a class and collect a unique list
of words from all String fields.
|
String |
getXMLDocument(int n)
Return a string containing an XML representation of this instance.
|
String |
getXMLDocument(int n,
boolean inUseOnly)
Return a string containing an XML representation of this instance.
|
String |
getXMLDocument(int n,
String path,
int key)
Return a string containing an XML representation of this isntance.
|
String |
getXMLDocument(int n,
String path,
int key,
boolean inUseOnly)
Return a string containing an XML representation of this isntance.
|
static String |
getXMLHeader()
Returns the header line required for XML files.
|
static ArrayList<Pair> |
getXPathList(String prefix,
ArrayList<String> list)
Get a list of values labeled with an XPath.
|
ArrayList<Pair> |
getXPathPairs()
Return an ArrayList of string Pairs which contains for
each node in the XML document an XPath as the "left" value
of the pair and the value as the "right" value of pair.
|
ArrayList<Pair> |
getXPathPairs(String prefix,
int index)
Return an ArrayList of string Pairs which contains for
each node in the XML document an XPath as the "left" value
of the pair and the value as the "right" value of pair.
|
static String |
indent(int level)
Return a string with spaces to format to indicated indentation level.
|
boolean |
isCommonWord(String value)
Determine if a word is a common word.
|
boolean |
isInUse(String name,
String value)
Determine if an element is in use.
|
boolean |
isRequired(String name)
Determine if an element is required.
|
void |
load(Document document,
String root)
Parses a
Document which contains a parsed XML file
and set internal variables with the contents of the file. |
void |
load(InputStream stream)
Parses an InputStream of XML into its constitute elments and
sets internal variables with the contents of the file.
|
void |
load(InputStream stream,
String root)
Parses an InputStream of XML into its constitute elments and
sets internal variables with the contents of the file.
|
void |
load(String pathName)
Parses a file containing XML into its constitute elements and
sets internal variables with the contents of the file.
|
void |
load(String pathName,
String root)
Parses a file containing XML into its constitute elments and
sets internal variables with the contents of the file.
|
static void |
main(String[] args)
Entry point for testing
|
void |
makeEditNodes()
Create an instance of all nodes for use when editing.
|
void |
makeNewMember(String name)
Create a new member.
|
void |
makeSkeletonNodes()
Create all nodes for use when editing which currently
do not exist.
|
String |
makeTagContent(String tag,
String content)
Return a string with content enclosed in tags with the passed name.
|
ArrayList<String> |
parseWords(String text)
Parse a string into words and return a list of unique words.
|
boolean |
parseXML(InputStream stream)
Parses a file containing XML into its constitute elments.
|
boolean |
parseXML(String pathName)
Parses a file containing XML into its constitute elements.
|
boolean |
parseXMLString(String text)
Parses a string containing XML into its constitute elements.
|
void |
printXML(PrintStream out)
Generates an XML representation of the label and stream it to the
print stream.
|
void |
processNode(Node node)
Walk the nodes of the Document and populate the resource classes.
|
void |
removeMember(String name,
int index)
Remove an elment of a member.
|
void |
setClassName(String name) |
void |
setIndent(int n)
Set the indent value.
|
void |
setMember(String name,
Node value)
Call the set() method with a given name suffix and a DOM
Node
as an argument. |
String |
setMember(String name,
String value)
Call the set() method with a given name suffix and a
String
as an argument. |
void |
setMember(String name,
String[] value)
Call the set() method with a given name suffix and an array of
String
values as an argument. |
String[] |
splitMixed(String text)
Divide a string on capital letters that follow lowercase letters
|
String |
toString()
Generates an XML representation of the content and return it as a string.
|
void |
trim()
Trim all text nodes by removing leading and trailing spaces.
|
void |
trimNode(Node node)
Trim the text in a node by removing leading and trailing spaces.
|
public static void main(String[] args)
public void load(String pathName) throws Exception
getClassName()
.pathName
- the fully qualified path and name of the file to parse.Exception
public void load(String pathName, String root) throws Exception
pathName
- the fully qualified path and name of the file to parse.root
- the name of the tag containg elements to process.Exception
public void load(InputStream stream) throws Exception
getClassName()
.stream
- the opened InputStream for the XML.Exception
public void load(InputStream stream, String root) throws Exception
stream
- the opened InputStream for the XML.root
- the name of the tag containg elements to process.Exception
public void load(Document document, String root) throws Exception
Document
which contains a parsed XML file
and set internal variables with the contents of the file.public boolean parseXML(String pathName) throws Exception
pathName
- the fully qualified path and name of the file to parse.true
if the file could be opened;
false
otherwise.Exception
public boolean parseXMLString(String text) throws Exception
text
- the String containing the XML text.true
if the file could be opened;
false
otherwise.Exception
public boolean parseXML(InputStream stream) throws Exception
stream
- a connection to a pre-opened file.true
if the file could be read;
false
otherwise.Exception
public String toString()
public void printXML(PrintStream out) throws Exception
out
- the stream to print the element to.Exception
public void trim() throws Exception
Exception
public void trimNode(Node node)
node
- the Node
of the document.public void dumpData(PrintStream out) throws Exception
out
- the stream to print the element to.Exception
public void dumpNode(PrintStream out, Node node)
out
- the stream to print the element to.public Node findNode(Node node, String name) throws Exception
public static StreamSource getDefaultStyleSheet()
StreamSource
which can be used to read the default
style sheet.public Document getDocument()
Document
containing the representation of the parsed XML file.public void processNode(Node node) throws Exception
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.public String setMember(String name, String value)
String
as an argument.value
- the String
value to set.public void setMember(String name, String[] value)
String
values as an argument.value
- the String
value to set.public void setMember(String name, Node value)
Node
as an argument.name
- the name of the member (suffix for setXXX() method).value
- the String
value to set.public XMLParser getMemberParent(String name)
name
- the XPath name for the member.public XMLParser getTopParent(String name)
name
- the XPath name for the member.public String getMemberPath(String name)
name
- the XPath name for the member.public String getMemberName(String name)
name
- the XPath name for the member.public XMLParser getMemberNode(String name)
name
- the name associated with a member.public String getMemberNodeName(String name)
name
- the name associated with a member.public int getMemberNodeIndex(String name)
name
- the name associated with a member.public static String getNodeText(Node node)
node
- the Node
.public static String getBranchText(Node node) throws Exception
public String getPathName()
public String getPath()
public Integer getIndent()
public void setIndent(int n)
public String makeTagContent(String tag, String content)
public static String indent(int level)
public void dump()
public ArrayList<Pair> getXPathPairs()
public ArrayList<Pair> getXPathPairs(String prefix, int index)
prefix
- the leading path to add to each XPath.index
- the index of the item if part of an array.
Indexes start a 1, a value of 0 indicates it is
not part of an array.public String getXMLDocument(int n)
n
- the number of levels to indent the document.public String getXMLDocument(int n, boolean inUseOnly)
n
- the number of levels to indent the document.inUseOnly
- indicates whether to include only those elements currently in use.public String getXMLDocument(int n, String path, int key)
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.public String getXMLDocument(int n, String path, int key, boolean inUseOnly)
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.public void makeNewMember(String name)
name
- the name of the member to create.public void makeEditNodes()
public void makeSkeletonNodes()
public void removeMember(String name, int index)
name
- the name of the member to create.index
- the index of the element to remove.public ArrayList<XMLParser> getNodes(String path)
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.
public ArrayList<XMLParser> getNodes(String path, boolean alwaysOne)
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.
public ArrayList<XMLParser> getNodes(XMLParser parent, String name)
Only returns nodes for items that are nodes.
If no nodes are found then the returned list is empty.
public ArrayList<String> getAllValues(String path)
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.
public ArrayList<String> getValues(String path)
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.
public ArrayList<String> getValues(String path, boolean alwaysOne)
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.
public StringReader getStringReader()
StringReader
for an XML representation of the document.
Commonly used to pass the document through an XML stylesheet transformation
using Transform
.public static String getTaggedValue(int level, String tagName, String value)
public String getTaggedValue(int level, String tagName, String value, boolean inUseOnly)
public static String getTagOpen(int level, String tagName)
public static String getTagClose(int level, String tagName)
public static String getTaggedList(int level, String tagName, ArrayList<String> list)
public String getTaggedList(int level, String tagName, ArrayList<String> list, boolean inUseOnly)
public String getElementPath(String base, int key)
public boolean isCommonWord(String value)
public ArrayList<String> parseWords(String text)
public String[] splitMixed(String text)
public ArrayList<String> getWords()
public boolean isInUse(String name, String value)
name
- the name of the element.value
- the value associated with the element.public boolean isRequired(String name)
name
- the name of the element.public static ArrayList<Pair> getXPathList(String prefix, ArrayList<String> list)
prefix
- the XPath to the desired members.list
- the list of member names to retrieve.public static String getXMLHeader()
public static String entityEncode(String text)
public void setClassName(String name)
public String getClassName()
public void addRequired(String value)
Copyright © 2008-2015 Regents University of California. All Rights Reserved.