org.stanwood.media.xml
Class XMLParser

java.lang.Object
  extended by org.stanwood.media.xml.XMLParser
Direct Known Subclasses:
AtomicParsleyOutputParser, BaseConfigReader, BaseXMLStore, SeenDatabase, TagChimpSource, VideoInfoParser, XBMCAddon, XBMCExtension, XBMCSource, XBMCWebUpdater, XMLSeason, XMLShow, XMLVideo

public class XMLParser
extends java.lang.Object

This is a helper class that should be extend by classes that need to parse XML


Field Summary
static java.lang.String DTD_WEB_LOCATION
          The (@link XMLStore2 schema
static java.lang.String SCHEMA_WEB_LOCATION
          The application configuration schema
 
Constructor Summary
XMLParser()
           
 
Method Summary
static javax.xml.parsers.DocumentBuilder createDocBuilder(javax.xml.parsers.DocumentBuilderFactory factory)
          Create a XML document builder.
static javax.xml.parsers.DocumentBuilderFactory createFactory(java.lang.String schemaName)
          Used to create a DOM document builder factory
protected  void deleteNode(org.w3c.dom.Element parent, java.lang.String name)
           
static java.lang.String domToStr(org.w3c.dom.Document document)
          Used to convert a DOM document to a string
static java.lang.String encodeAttributeValue(java.lang.String value)
          This is used to make sure a value can be written to a XML document by encoding the characters that should be XML entities.
static org.w3c.dom.Element firstChild(org.w3c.dom.Node parent)
          Used to get the first XML element under a parent element
protected  java.lang.String getAttribute(org.w3c.dom.Element node, java.lang.String attributeName)
           
protected  org.w3c.dom.Element getElement(org.w3c.dom.Element parent, java.lang.String name)
           
protected  org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Node parent, java.lang.String name)
           
protected  float getFloatFromXML(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a float from the XML
protected  java.lang.Integer getIntegerFromXML(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a integer from the XML
protected  org.w3c.dom.Element getLastChildElement(org.w3c.dom.Node parent, java.lang.String name)
           
protected  java.lang.Long getLongFromXML(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a long from the XML
static javax.xml.validation.Schema getSchema(java.lang.String name)
          Used to create a Schema that can be used to validate XML documents
protected  java.lang.String getStringFromXML(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a string from the XML
protected  java.lang.String getStringFromXMLOrNull(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a string from the XML, if it can't be found then return null
protected  java.net.URL getURLFromXML(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a URL from the XML
protected  java.net.URL getURLFromXMLOrNull(org.w3c.dom.Node parent, java.lang.String path)
          Used to read a URL; from the XML, if it can't be found then return null
static boolean hasNode(org.w3c.dom.Document document, java.lang.String path)
          Used to see if a XML document contains a node
static org.w3c.dom.Document parse(java.io.File file, java.lang.String schemaName)
          Used to convert a XML file to a XML document.
static org.w3c.dom.Document parse(java.io.InputStream is, java.lang.String schemaName)
          Used to convert a XML InputStream to a XML document.
static org.w3c.dom.Document parse(java.lang.String contents, java.lang.String schemaName)
          Used to convert a XML String to a XML document.
 java.lang.String quoteXPathQuery(java.lang.String s)
          Used to quote xpath queries
static java.util.List<org.w3c.dom.Element> selectChildNodes(org.w3c.dom.Node parent, java.lang.String childName)
          Used to get a list of children nodes under a parent XML node that match a given name
static IterableNodeList selectNodeList(org.w3c.dom.Node contextNode, java.lang.String path)
          Used to get a list of XML nodes using a XPath path
static org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node contextNode, java.lang.String path)
          Used to return a single XML node using a XPath path
static org.w3c.dom.Document strToDom(java.io.File file)
          Used to convert a XML file to a DOM document
static org.w3c.dom.Document strToDom(java.lang.String str)
          Used to convert a XML string to a DOM document
static org.w3c.dom.Document strToDom(java.lang.String str, java.lang.String schemaName)
          Used to convert a XML string to a DOM document
static void writeXML(java.io.File file, org.w3c.dom.Document doc)
          Used to write a XML document to a file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DTD_WEB_LOCATION

public static final java.lang.String DTD_WEB_LOCATION
The (@link XMLStore2 schema

See Also:
Constant Field Values

SCHEMA_WEB_LOCATION

public static final java.lang.String SCHEMA_WEB_LOCATION
The application configuration schema

See Also:
Constant Field Values
Constructor Detail

XMLParser

public XMLParser()
Method Detail

getIntegerFromXML

protected java.lang.Integer getIntegerFromXML(org.w3c.dom.Node parent,
                                              java.lang.String path)
                                       throws XMLParserException
Used to read a integer from the XML

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the integer
Returns:
The integer value
Throws:
XMLParserException - Thrown if their is a XML problem
XMLParserNotFoundException - Thrown if the value can't be read

getLongFromXML

protected java.lang.Long getLongFromXML(org.w3c.dom.Node parent,
                                        java.lang.String path)
                                 throws XMLParserException
Used to read a long from the XML

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the long
Returns:
The long value
Throws:
XMLParserException - Thrown if their is a XML problem
XMLParserNotFoundException - Thrown if the value can't be read

getStringFromXML

protected java.lang.String getStringFromXML(org.w3c.dom.Node parent,
                                            java.lang.String path)
                                     throws XMLParserException
Used to read a string from the XML

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the string
Returns:
The string value
Throws:
XMLParserException - Thrown if their is a XML problem
XMLParserNotFoundException - Thrown if the value can't be read

getStringFromXMLOrNull

protected java.lang.String getStringFromXMLOrNull(org.w3c.dom.Node parent,
                                                  java.lang.String path)
                                           throws XMLParserException
Used to read a string from the XML, if it can't be found then return null

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the string
Returns:
The string value
Throws:
XMLParserException - Thrown if their is a XML problem

getURLFromXML

protected java.net.URL getURLFromXML(org.w3c.dom.Node parent,
                                     java.lang.String path)
                              throws java.net.MalformedURLException,
                                     XMLParserException
Used to read a URL from the XML

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the URL
Returns:
The string value
Throws:
XMLParserException - Thrown if their is a XML problem
XMLParserNotFoundException - Thrown if the value can't be read
java.net.MalformedURLException

getURLFromXMLOrNull

protected java.net.URL getURLFromXMLOrNull(org.w3c.dom.Node parent,
                                           java.lang.String path)
                                    throws java.net.MalformedURLException,
                                           XMLParserException
Used to read a URL; from the XML, if it can't be found then return null

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the URL
Returns:
The string value
Throws:
XMLParserException - Thrown if their is a XML problem
java.net.MalformedURLException

getFloatFromXML

protected float getFloatFromXML(org.w3c.dom.Node parent,
                                java.lang.String path)
                         throws XMLParserException
Used to read a float from the XML

Parameters:
parent - The parent node it's the path is to be used from
path - The xpath location of the float
Returns:
The float value
Throws:
XMLParserException - Thrown if their is a XML problem
XMLParserNotFoundException - Thrown if the value can't be read

strToDom

public static org.w3c.dom.Document strToDom(java.lang.String str,
                                            java.lang.String schemaName)
                                     throws XMLParserException
Used to convert a XML string to a DOM document

Parameters:
str - The string to convert
schemaName - the name of the schema, or null if one should not be used
Returns:
The DOM Document
Throws:
XMLParserException - Thrown if their is a parsing problem

strToDom

public static org.w3c.dom.Document strToDom(java.lang.String str)
                                     throws XMLParserException
Used to convert a XML string to a DOM document

Parameters:
str - The string to convert
Returns:
The DOM Document
Throws:
XMLParserException - Thrown if their is a parsing problem

strToDom

public static org.w3c.dom.Document strToDom(java.io.File file)
                                     throws XMLParserException,
                                            java.io.IOException
Used to convert a XML file to a DOM document

Parameters:
file - the XML file
Returns:
The DOM Document
Throws:
XMLParserException - Thrown if their is a parsing problem
java.io.IOException - Thrown if their is a problem reading the file

domToStr

public static java.lang.String domToStr(org.w3c.dom.Document document)
                                 throws XMLParserException
Used to convert a DOM document to a string

Parameters:
document - The DOM document
Returns:
The XML as a string
Throws:
XMLParserException - Thrown if their is a parsing problem

getFirstChildElement

protected org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Node parent,
                                                   java.lang.String name)

getLastChildElement

protected org.w3c.dom.Element getLastChildElement(org.w3c.dom.Node parent,
                                                  java.lang.String name)

firstChild

public static org.w3c.dom.Element firstChild(org.w3c.dom.Node parent)
Used to get the first XML element under a parent element

Parameters:
parent - The parent element
Returns:
The first XML element or null if none could be found

selectChildNodes

public static java.util.List<org.w3c.dom.Element> selectChildNodes(org.w3c.dom.Node parent,
                                                                   java.lang.String childName)
Used to get a list of children nodes under a parent XML node that match a given name

Parameters:
parent - The parent name
childName - The name of the children to return
Returns:
The children nodes

selectNodeList

public static IterableNodeList selectNodeList(org.w3c.dom.Node contextNode,
                                              java.lang.String path)
                                       throws XMLParserException
Used to get a list of XML nodes using a XPath path

Parameters:
contextNode - The context that the path should be used with
path - The XPath path
Returns:
The node list
Throws:
XMLParserException - Thrown if their is a XML problem

selectSingleNode

public static org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node contextNode,
                                                java.lang.String path)
                                         throws XMLParserException
Used to return a single XML node using a XPath path

Parameters:
contextNode - The context that the path should be used with
path - The XPath path
Returns:
The node, or null if it could not be found
Throws:
XMLParserException - Thrown if their is a XML problem

createDocBuilder

public static javax.xml.parsers.DocumentBuilder createDocBuilder(javax.xml.parsers.DocumentBuilderFactory factory)
                                                          throws javax.xml.parsers.ParserConfigurationException
Create a XML document builder. This takes care of fetching DTD's locally instead of downloading them from the Internet.

Parameters:
factory - The document builder factory
Returns:
The document builder
Throws:
javax.xml.parsers.ParserConfigurationException - Thrown if their are any problems

parse

public static org.w3c.dom.Document parse(java.lang.String contents,
                                         java.lang.String schemaName)
                                  throws XMLParserException
Used to convert a XML String to a XML document. If the schemaName is not null, then the XML string is validated against it. It will also attempt to find the schema locally if possible

Parameters:
contents - The XML String
schemaName - The schema name, or null if validation is not required
Returns:
The XML Document
Throws:
XMLParserException - Thrown if their was a problem converting the string to a document

parse

public static org.w3c.dom.Document parse(java.io.InputStream is,
                                         java.lang.String schemaName)
                                  throws XMLParserException
Used to convert a XML InputStream to a XML document. If the schemaName is not null, then the XML string is validated against it. It will also attempt to find the schema locally if possible

Parameters:
is - The XML InputStream
schemaName - The schema name, or null if validation is not required
Returns:
The XML Document
Throws:
XMLParserException - Thrown if their was a problem converting the string to a document

createFactory

public static javax.xml.parsers.DocumentBuilderFactory createFactory(java.lang.String schemaName)
Used to create a DOM document builder factory

Parameters:
schemaName - The schema name, or null if one should not be used
Returns:
The factory

parse

public static org.w3c.dom.Document parse(java.io.File file,
                                         java.lang.String schemaName)
                                  throws XMLParserException
Used to convert a XML file to a XML document. If the schemaName is not null, then the XML string is validated against it. It will also attempt to find the schema locally if possible

Parameters:
file - The XML file
schemaName - The schema name, or null if validation is not required
Returns:
The XML Document
Throws:
XMLParserException - Thrown if their was a problem converting the file to a document

getSchema

public static javax.xml.validation.Schema getSchema(java.lang.String name)
                                             throws XMLParserException
Used to create a Schema that can be used to validate XML documents

Parameters:
name - The name of the schema
Returns:
The schema
Throws:
XMLParserException - Thrown if their are any problems

encodeAttributeValue

public static java.lang.String encodeAttributeValue(java.lang.String value)
This is used to make sure a value can be written to a XML document by encoding the characters that should be XML entities.

Parameters:
value - The value to encode
Returns:
The encoded value

writeXML

public static void writeXML(java.io.File file,
                            org.w3c.dom.Document doc)
                     throws java.io.IOException
Used to write a XML document to a file

Parameters:
file - The file to write
doc - The XML contents
Throws:
java.io.IOException - Thrown if thier is aproblem writing the file

quoteXPathQuery

public java.lang.String quoteXPathQuery(java.lang.String s)
Used to quote xpath queries

Parameters:
s - The query
Returns:
the quoted query

getAttribute

protected java.lang.String getAttribute(org.w3c.dom.Element node,
                                        java.lang.String attributeName)
                                 throws XMLParserNotFoundException
Throws:
XMLParserNotFoundException

deleteNode

protected void deleteNode(org.w3c.dom.Element parent,
                          java.lang.String name)

getElement

protected org.w3c.dom.Element getElement(org.w3c.dom.Element parent,
                                         java.lang.String name)

hasNode

public static boolean hasNode(org.w3c.dom.Document document,
                              java.lang.String path)
                       throws XMLParserException
Used to see if a XML document contains a node

Parameters:
document - The XML
path - The node to look for ( via a xpath path)
Returns:
True if found, otherwise false
Throws:
XMLParserException - Thrown if their is a parser problem