com.sage.orion.sdata.servlet
Class XMLHelper

java.lang.Object
  extended by com.sage.orion.sdata.servlet.XMLHelper

public final class XMLHelper
extends java.lang.Object

group of helper routines to make processing XML files easier.

Author:
cliwa01

Field Summary
static javax.xml.parsers.DocumentBuilderFactory DOC_BUILDER_FACTORY
           
 
Method Summary
static java.lang.String convertElementToString(org.w3c.dom.Element element)
          Converts the Element to its string representation.
static org.w3c.dom.Document createDocument()
          Create a new Document
static void createElementResponse(org.w3c.dom.Element element, javax.servlet.http.HttpServletResponse response)
          Add the provided XML element as the document element onto the response.
static org.w3c.dom.Element createTextElement(org.w3c.dom.Document doc, java.lang.String name, java.lang.String value)
          Create a new Element and text node
static org.w3c.dom.Element getChildElement(org.w3c.dom.Node node, java.lang.String name)
          returns the Element that is a direct child of the provided node and has the given tag name.
static java.lang.String getTextValue(org.w3c.dom.Element node)
           Returns the text value of the given element node, or null if there is no true content.
static boolean isUsableElementName(java.lang.String name)
          checks if the given name can be used as an XML element name
static org.w3c.dom.Document loadXMLDocument(java.lang.String filename)
          Loads the document object from an XML file.
static java.lang.String makeUsableElementName(java.lang.String name)
          Returns a modification of the given name such that isUsableElementName(java.lang.String) returns true.
static java.lang.String removeTabsandLineFeeds(java.lang.String s)
          Returns the given string with the tabs ('\t'), end of line ('\n') and carriage return ('\r') characters removed.
static void writeToStream(org.w3c.dom.Element element, java.lang.String charset, java.io.OutputStream outputStream)
          writes the provided XML element as the document element to the given stream using the given character set
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DOC_BUILDER_FACTORY

public static final javax.xml.parsers.DocumentBuilderFactory DOC_BUILDER_FACTORY
Method Detail

removeTabsandLineFeeds

public static java.lang.String removeTabsandLineFeeds(java.lang.String s)
Returns the given string with the tabs ('\t'), end of line ('\n') and carriage return ('\r') characters removed.

Parameters:
s - the input string.
Returns:
the output string.

getTextValue

public static java.lang.String getTextValue(org.w3c.dom.Element node)

Returns the text value of the given element node, or null if there is no true content.

The text value is a concatenation of right-trimmed text nodes (which includes spaces being trimmed even within quotes) and any CDATA nodes (whose contents left as-is) that are children of the node under which the text value lives.

NOTE: Unlike Node.getTextContent, this method does right-trimming of the text nodes (though it does not trim CDATA nodes) and does not look at text of nested children (for example, this method would ignore a child element that itself has some text). One other difference is that this method returns null if there is no true content (in other words, if all we have is whitespace).

Parameters:
node - the node under which the text value is
Returns:
the text value under the given node, or null if there is none

createDocument

public static org.w3c.dom.Document createDocument()
Create a new Document

Returns:
a new Document

createTextElement

public static org.w3c.dom.Element createTextElement(org.w3c.dom.Document doc,
                                                    java.lang.String name,
                                                    java.lang.String value)
Create a new Element and text node

Parameters:
doc - the document to which to add the node
name - the name of the node (which must not be null)
value - the value of the text. If this is null then no text is added.
Returns:
a new Element with the text under it.

createElementResponse

public static void createElementResponse(org.w3c.dom.Element element,
                                         javax.servlet.http.HttpServletResponse response)
Add the provided XML element as the document element onto the response.

Parameters:
element - the element that is the body of the response
response - the http servlet response to which to add the element

writeToStream

public static void writeToStream(org.w3c.dom.Element element,
                                 java.lang.String charset,
                                 java.io.OutputStream outputStream)
                          throws java.io.IOException
writes the provided XML element as the document element to the given stream using the given character set

Parameters:
element - the Element to be written
charset - the name of the character set to be used
outputStream - the OutputStream to which to write
Throws:
java.io.IOException - on any input/output failure

loadXMLDocument

public static org.w3c.dom.Document loadXMLDocument(java.lang.String filename)
Loads the document object from an XML file.

Parameters:
filename - the name of the file
Returns:
a Document containing the XML loaded from the file whose name is given

getChildElement

public static org.w3c.dom.Element getChildElement(org.w3c.dom.Node node,
                                                  java.lang.String name)
returns the Element that is a direct child of the provided node and has the given tag name.

This is frequently what we want rather than iterating over all the children or getting all the elements with the given tag name (which does not guarantee to return a direct child)

Parameters:
node - the node that is the parent of the requested Element.
name - the name of the node
Returns:
the direct child Element with the given name, or null if there is none.

isUsableElementName

public static boolean isUsableElementName(java.lang.String name)
checks if the given name can be used as an XML element name

Parameters:
name - the name
Returns:
true if the name can be used as an XML element name

makeUsableElementName

public static java.lang.String makeUsableElementName(java.lang.String name)
Returns a modification of the given name such that isUsableElementName(java.lang.String) returns true.

The scheme for changing the name is currently only sufficient for normal use. There are names that cannot currently be changed; for these an exception is thrown.

Parameters:
name - the name
Returns:
a modified name that can be used as an XML element name
Throws:
java.lang.IllegalArgumentException - if the name cannot be made XML compatible with the current scheme

convertElementToString

public static java.lang.String convertElementToString(org.w3c.dom.Element element)
Converts the Element to its string representation.

Parameters:
element - The Element.
Returns:
The string representation of the Element.


Copyright © 2011 Sage Software, Inc. All rights reserved.