tcMenuJavaAPI
Static Public Member Functions | List of all members
com.thecoderscorner.menu.persist.XMLDOMHelper Class Reference

Static Public Member Functions

static Document loadDocumentFromPath (Path filePath) throws IOException, ParserConfigurationException, SAXException
 
static Document loadDocumentFromData (String data) throws IOException, ParserConfigurationException, SAXException
 
static Document loadDocumentStream (InputStream stream) throws ParserConfigurationException, IOException, SAXException
 
static List< Element > getChildElementsWithName (Element ele, String name)
 
static String textOfElementByName (Element elem, String child)
 
static int integerOfElementByName (Element element, String child, int def)
 
static Element elementWithName (Element elem, String child)
 
static String getAttrOrNull (Element ele, String attr)
 
static String getAttributeOrDefault (Element elem, String val, Object def)
 
static< T > List< T > transformElements (Element root, String eleName, String childName, Function< Element, T > transform)
 
static< T > List< T > transformElements (Element root, String childName, Function< Element, T > transform)
 
static void writeXml (Document doc, OutputStream output) throws TransformerException
 
static void writeXml (Document doc, OutputStream output, boolean prettyPrint) throws TransformerException
 
static Document newDocumentRoot (String name) throws ParserConfigurationException
 
static Element appendElementWithNameValue (Element parent, String name, Object value)
 

Detailed Description

A series of static helper method that make dealing with XML easier, this is because many areas of tcMenu deal with XML documents, and these functions are generally useful.

Member Function Documentation

◆ appendElementWithNameValue()

static Element com.thecoderscorner.menu.persist.XMLDOMHelper.appendElementWithNameValue ( Element  parent,
String  name,
Object  value 
)
static

Append a new element into the parent provided with the name given.

Parameters
parentthe parent where the new element is to be placed
namethe name of the new element
valuethe value to set as text (can be null)
Returns
the newly created element

◆ elementWithName()

static Element com.thecoderscorner.menu.persist.XMLDOMHelper.elementWithName ( Element  elem,
String  child 
)
static

Gets an element with a given name or null

Parameters
elemthe parent element
childthe child to locate
Returns
the element or null

◆ getAttributeOrDefault()

static String com.thecoderscorner.menu.persist.XMLDOMHelper.getAttributeOrDefault ( Element  elem,
String  val,
Object  def 
)
static

Gets the text of an attribute or a default value.

Parameters
elemthe element
valthe attribute name
defthe default value
Returns
the attribute text or otherwise the default

◆ getAttrOrNull()

static String com.thecoderscorner.menu.persist.XMLDOMHelper.getAttrOrNull ( Element  ele,
String  attr 
)
static

Gets the text of attribute if it is available, or null

Parameters
elethe element
attrthe attribute name
Returns
the text of the attribute or null

◆ getChildElementsWithName()

static List<Element> com.thecoderscorner.menu.persist.XMLDOMHelper.getChildElementsWithName ( Element  ele,
String  name 
)
static

Find all child elements with a given name

Parameters
elethe parent element
namethe name to search for
Returns
a list of elements directly under ele that are named name

◆ integerOfElementByName()

static int com.thecoderscorner.menu.persist.XMLDOMHelper.integerOfElementByName ( Element  element,
String  child,
int  def 
)
static

Gets the int value associated with the named child, or the default if it does not exist or can't be parsed.

Parameters
elementthe parent element
childthe name of the child element
defthe default value in case of problem

◆ loadDocumentFromData()

static Document com.thecoderscorner.menu.persist.XMLDOMHelper.loadDocumentFromData ( String  data) throws IOException, ParserConfigurationException, SAXException
static

Load an XML document from a string of data

Parameters
datathe data to load from
Returns
an XML document if successful, otherwise throws
Exceptions
IOExceptionfor any IO problems
ParserConfigurationExceptionif the document does not parse
SAXExceptionif the document does not parse

◆ loadDocumentFromPath()

static Document com.thecoderscorner.menu.persist.XMLDOMHelper.loadDocumentFromPath ( Path  filePath) throws IOException, ParserConfigurationException, SAXException
static

Load an XML document from a file system path

Parameters
filePaththe path to load from
Returns
an XML document if successful, otherwise throws
Exceptions
IOExceptionfor any IO problems
ParserConfigurationExceptionif the document does not parse
SAXExceptionif the document does not parse

◆ loadDocumentStream()

static Document com.thecoderscorner.menu.persist.XMLDOMHelper.loadDocumentStream ( InputStream  stream) throws ParserConfigurationException, IOException, SAXException
static

Load an XML document from a stream

Parameters
streamthe steam to load from
Returns
an XML document if successful, otherwise throws
Exceptions
IOExceptionfor any IO problems
ParserConfigurationExceptionif the document does not parse
SAXExceptionif the document does not parse

◆ newDocumentRoot()

static Document com.thecoderscorner.menu.persist.XMLDOMHelper.newDocumentRoot ( String  name) throws ParserConfigurationException
static

Creates a new document that has a root element with the name given

Parameters
namethe root element name
Returns
a document with root node attached
Exceptions
ParserConfigurationExceptionif the document cannot be created

◆ textOfElementByName()

static String com.thecoderscorner.menu.persist.XMLDOMHelper.textOfElementByName ( Element  elem,
String  child 
)
static

Get the text of an element with the name child. Saves looking up the element and then getting text.

Parameters
elemthe parent element
childthe name of the child to get the text of
Returns
the text content or blank.

◆ transformElements() [1/2]

static <T> List<T> com.thecoderscorner.menu.persist.XMLDOMHelper.transformElements ( Element  root,
String  childName,
Function< Element, T >  transform 
)
static

Transforms all elements that match childName under root, there is another overload that provides indirection. You provide the transformation function that takes an Element and returns the desired result.

Parameters
rootthe root element to start at
childNamethe items to filter for
transformthe transformation to apply - returns T, takes an element
<T>can be any type you wish to return in your transformation
Returns
the transformed list or an empty list.

◆ transformElements() [2/2]

static <T> List<T> com.thecoderscorner.menu.persist.XMLDOMHelper.transformElements ( Element  root,
String  eleName,
String  childName,
Function< Element, T >  transform 
)
static

Transforms all elements that match childName under either root if eleName is null, or under root/eleName if eleName is provided. You provide the transformation function that takes an Element and returns the desired result

Parameters
rootthe root element to start at
eleNameoptionally, another level of indirection (can be null) EG root/eleName
childNamethe items to filter for
transformthe transformation to apply - returns T, takes an element
<T>can be any type you wish to return in your transformation
Returns
the transformed list or an empty list.

◆ writeXml() [1/2]

static void com.thecoderscorner.menu.persist.XMLDOMHelper.writeXml ( Document  doc,
OutputStream  output 
) throws TransformerException
static

Writes an XML document without reformatting it, this is good if you are working with a DOM that was previously loaded from a stream, where it may be already formatted.

Parameters
docthe document
outputthe output stream to write to
Exceptions
TransformerExceptionif the XML cannot be transformed

◆ writeXml() [2/2]

static void com.thecoderscorner.menu.persist.XMLDOMHelper.writeXml ( Document  doc,
OutputStream  output,
boolean  prettyPrint 
) throws TransformerException
static

Writes an XML document to a stream

Parameters
docthe document
outputthe stream to write to
prettyPrintif the document should be formatted
Exceptions
TransformerExceptionif it cannot be written

The documentation for this class was generated from the following file: