Package cz.martlin.xspf.util
Class XMLDocumentUtility
- java.lang.Object
-
- cz.martlin.xspf.util.XMLDocumentUtility
-
public class XMLDocumentUtility extends Object
An xml document utility. Provides some usefull methods to simplify manipulation with the XML file(s).- Author:
- martin
-
-
Constructor Summary
Constructors Constructor Description XMLDocumentUtility(String nsName, String nsURL)Creates instance for the given xml namespace.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddChildElement(Node owner, Element elem)Just adds the given element to the given owner.voidaddChildElements(Node owner, java.util.stream.Stream<Element> elems)Just adds all the given elements to the given owner.ElementcreateNewElement(Node context, String name)Just creates new element in the document given by the context (the whole document including).ElementgetElemClone(Element elem)Returns the copy of the given element.<T> TgetElementAttrOrNull(Element owner, String attrName, XMLDocumentUtilityHelper.NullableTextToValueMapper<T> mapper)Returns the value of the given element attribute of given name, converted by given mapper.<T> TgetElementValueOrNull(Element elem, XMLDocumentUtilityHelper.NullableTextToValueMapper<T> mapper)Returns the value of the given element, converted by given mapper.ElementgetChildElemCloneOrNull(Node owner, String elemName)Returns the copy of the child element of the given name of the given owner.<T> TgetChildElementValueOrNull(Element owner, String elemName, XMLDocumentUtilityHelper.NullableTextToValueMapper<T> mapper)Returns the value of the given element child of given name, converted by given mapper.ElementgetChildElemOrNull(Node owner, String elemName)Returns the child element of the given name of the given owner.ElementgetOrCreateChildElem(Node owner, String elemName)Returns the child element of the given name of the given owner.java.util.stream.Stream<Element>listChildrenElems(Node container)Lists all the children of the given container node (element or document).java.util.stream.Stream<Element>listChildrenElems(Node container, String elemName)Lists the children of the given name of the given container node (element or document).java.util.stream.Stream<Element>listChildrenElemsClones(Node container, String elemName)Lists the copies of the children of the given name of the given container node (element or document).voidregister(Element elem)Registers given element to this utility (populates he namespace).voidremoveChildElement(Node owner, String elemName)Removes one child element of the given name of the given owner.voidremoveChildElement(Node owner, Element elem)Just removes the given element from the given owner.voidremoveChildElements(Node owner, String elemName)Removes all child elements of the given name of the given owner.voidremoveChildElements(Node owner, java.util.stream.Stream<Element> elems)Just removes the given elements from the given owner.voidreplaceChildElement(Node owner, String elemName, Element replacement)Replaces one child element of the given name of the given owner with the another one.voidreplaceChildElement(Node owner, Element original, Element replacement)Just replaces the given element with another one.voidreplaceChildElementByClone(Node owner, String elemName, Element replacement)Replaces one child element of the given name of the given owner with the clone of another one.voidreplaceChildElements(Node owner, String elemName, java.util.stream.Stream<Element> replacements)Replaces all child elements of the given name of the given owner with the another ones.voidreplaceChildElements(Node owner, java.util.stream.Stream<Element> originals, java.util.stream.Stream<Element> replacements)Just replaces the given elements by another ones.voidreplaceChildElementsByClone(Node owner, String elemName, java.util.stream.Stream<Element> replacements)Replaces all child elements of the given name of the given owner with the clone of another ones.<T> voidsetElementAttr(Element owner, String attrName, T value, XMLDocumentUtilityHelper.NullableValueToTextMapper<T> mapper)Sets the value of the given element attribute of given name, converted by given mapper.<T> voidsetElementValue(Element elem, T value, XMLDocumentUtilityHelper.NullableValueToTextMapper<T> mapper)Sets the value of the given element, converted by given mapper.<T> voidsetChildElementValue(Element owner, String elemName, T value, XMLDocumentUtilityHelper.NullableValueToTextMapper<T> mapper)Sets the value of the given element child of given name, converted by given mapper.
-
-
-
Method Detail
-
register
public void register(Element elem) throws XSPFException
Registers given element to this utility (populates he namespace).- Parameters:
elem-- Throws:
XSPFException
-
getChildElementValueOrNull
public <T> T getChildElementValueOrNull(Element owner, String elemName, XMLDocumentUtilityHelper.NullableTextToValueMapper<T> mapper) throws XSPFException
Returns the value of the given element child of given name, converted by given mapper. If no such child, returns null.- Type Parameters:
T-- Parameters:
owner-elemName-mapper-- Returns:
- Throws:
XSPFException
-
setChildElementValue
public <T> void setChildElementValue(Element owner, String elemName, T value, XMLDocumentUtilityHelper.NullableValueToTextMapper<T> mapper) throws XSPFException
Sets the value of the given element child of given name, converted by given mapper. If no such child, creates it.- Type Parameters:
T-- Parameters:
owner-elemName-value-mapper-- Throws:
XSPFException
-
getElementValueOrNull
public <T> T getElementValueOrNull(Element elem, XMLDocumentUtilityHelper.NullableTextToValueMapper<T> mapper) throws XSPFException
Returns the value of the given element, converted by given mapper. If no content, returns null.- Type Parameters:
T-- Parameters:
elem-mapper-- Returns:
- Throws:
XSPFException
-
setElementValue
public <T> void setElementValue(Element elem, T value, XMLDocumentUtilityHelper.NullableValueToTextMapper<T> mapper) throws XSPFException
Sets the value of the given element, converted by given mapper. If no content, fails.- Type Parameters:
T-- Parameters:
elem-value-mapper-- Throws:
XSPFException
-
getElementAttrOrNull
public <T> T getElementAttrOrNull(Element owner, String attrName, XMLDocumentUtilityHelper.NullableTextToValueMapper<T> mapper) throws XSPFException
Returns the value of the given element attribute of given name, converted by given mapper. If no such attribute, returns null.- Type Parameters:
T-- Parameters:
owner-attrName-mapper-- Returns:
- Throws:
XSPFException
-
setElementAttr
public <T> void setElementAttr(Element owner, String attrName, T value, XMLDocumentUtilityHelper.NullableValueToTextMapper<T> mapper) throws XSPFException
Sets the value of the given element attribute of given name, converted by given mapper. If no such attribute, creates.- Type Parameters:
T-- Parameters:
owner-attrName-value-mapper-- Throws:
XSPFException
-
listChildrenElems
public java.util.stream.Stream<Element> listChildrenElems(Node container) throws XSPFException
Lists all the children of the given container node (element or document).- Parameters:
container-- Returns:
- Throws:
XSPFException
-
listChildrenElems
public java.util.stream.Stream<Element> listChildrenElems(Node container, String elemName) throws XSPFException
Lists the children of the given name of the given container node (element or document).- Parameters:
container-elemName-- Returns:
- Throws:
XSPFException
-
listChildrenElemsClones
public java.util.stream.Stream<Element> listChildrenElemsClones(Node container, String elemName) throws XSPFException
Lists the copies of the children of the given name of the given container node (element or document).- Parameters:
container-elemName-- Returns:
- Throws:
XSPFException
-
getChildElemOrNull
public Element getChildElemOrNull(Node owner, String elemName) throws XSPFException
Returns the child element of the given name of the given owner. Returns null if none, fails if more.- Parameters:
owner-elemName-- Returns:
- Throws:
XSPFException
-
getOrCreateChildElem
public Element getOrCreateChildElem(Node owner, String elemName) throws XSPFException
Returns the child element of the given name of the given owner. Creates and returns if none.- Parameters:
owner-elemName-- Returns:
- Throws:
XSPFException
-
getChildElemCloneOrNull
public Element getChildElemCloneOrNull(Node owner, String elemName) throws XSPFException
Returns the copy of the child element of the given name of the given owner. Returns null if none, fails if more.- Parameters:
owner-elemName-- Returns:
- Throws:
XSPFException
-
getElemClone
public Element getElemClone(Element elem)
Returns the copy of the given element.- Parameters:
elem-- Returns:
-
removeChildElement
public void removeChildElement(Node owner, String elemName) throws XSPFException
Removes one child element of the given name of the given owner. Fails if none or more.- Parameters:
owner-elemName-- Throws:
XSPFException
-
removeChildElements
public void removeChildElements(Node owner, String elemName) throws XSPFException
Removes all child elements of the given name of the given owner.- Parameters:
owner-elemName-- Throws:
XSPFException
-
replaceChildElement
public void replaceChildElement(Node owner, String elemName, Element replacement) throws XSPFException
Replaces one child element of the given name of the given owner with the another one. If none just adds, fails if more.- Parameters:
owner-elemName-replacement-- Throws:
XSPFException
-
replaceChildElements
public void replaceChildElements(Node owner, String elemName, java.util.stream.Stream<Element> replacements) throws XSPFException
Replaces all child elements of the given name of the given owner with the another ones.- Parameters:
owner-elemName-replacements-- Throws:
XSPFException
-
replaceChildElementByClone
public void replaceChildElementByClone(Node owner, String elemName, Element replacement) throws XSPFException
Replaces one child element of the given name of the given owner with the clone of another one. If none just adds, fails if more.- Parameters:
owner-elemName-replacement-- Throws:
XSPFException
-
replaceChildElementsByClone
public void replaceChildElementsByClone(Node owner, String elemName, java.util.stream.Stream<Element> replacements) throws XSPFException
Replaces all child elements of the given name of the given owner with the clone of another ones.- Parameters:
owner-elemName-replacements-- Throws:
XSPFException
-
createNewElement
public Element createNewElement(Node context, String name) throws XSPFException
Just creates new element in the document given by the context (the whole document including).- Parameters:
context-name-- Returns:
- Throws:
XSPFException
-
addChildElement
public void addChildElement(Node owner, Element elem) throws XSPFException
Just adds the given element to the given owner.- Throws:
XSPFException
-
addChildElements
public void addChildElements(Node owner, java.util.stream.Stream<Element> elems)
Just adds all the given elements to the given owner.- Parameters:
owner-elems-
-
removeChildElement
public void removeChildElement(Node owner, Element elem) throws XSPFException
Just removes the given element from the given owner.- Parameters:
owner-elem-- Throws:
XSPFException
-
removeChildElements
public void removeChildElements(Node owner, java.util.stream.Stream<Element> elems)
Just removes the given elements from the given owner.- Parameters:
owner-elems-
-
replaceChildElement
public void replaceChildElement(Node owner, Element original, Element replacement) throws XSPFException
Just replaces the given element with another one.- Parameters:
owner-original-replacement-- Throws:
XSPFException
-
replaceChildElements
public void replaceChildElements(Node owner, java.util.stream.Stream<Element> originals, java.util.stream.Stream<Element> replacements) throws XSPFException
Just replaces the given elements by another ones.- Parameters:
owner-originals-replacements-- Throws:
XSPFException
-
-