Package com.jalios.util
Class XmlUtil
- java.lang.Object
-
- com.jalios.util.XmlUtil
-
public class XmlUtil extends java.lang.Object
This class provides a set of static methods which perform various utility operations for XML.- Version:
- $Revision: 125018 $
- Author:
- Olivier Dedieu
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
REVISION
static java.lang.String
XML_HEADER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addLangElement(org.jdom.Element parentElt, java.lang.String eltName, java.util.Map<java.lang.String,java.lang.String> langMap)
Constructs a sub element base on the given language map.static java.lang.String
extractText(java.lang.String str)
Extract the text of the given XML/HTML string.static java.lang.String
extractText(java.lang.String str, boolean collapseBlank)
Extract the text of the given XML/HTML string.static java.util.HashMap<java.lang.String,java.lang.String>
getAttributesAsMap(org.jdom.Element elt)
Return a map of attribute (key, value) from the attributes of the JDom Elementstatic boolean
getBoolean(java.lang.Object node, java.lang.String xpathExpression, boolean def)
Gives a boolean representation of the first node wich if the result of the XPath expression into the given node in parameter.static org.jdom.input.SAXBuilder
getBuilder()
Get a SAXBuilderstatic java.util.Date
getDate(java.lang.Object node, java.lang.String xpathExpression, java.util.Date def)
Gives a Date representation of the first node wich if the result of the XPath expression into the given node in parameter.static org.jdom.Document
getDocument(java.io.File file)
Returns the JDOM document for the given file.static org.jdom.Document
getDocument(java.io.InputStream in)
Returns the JDOM document for the given input stream.static org.jdom.Document
getDocument(java.io.Reader reader)
Returns the JDOM document for the given reader.static org.jdom.Document
getDocument(java.lang.String text)
Returns a JDOM document for the given text.static org.jdom.Element
getFirstChild(org.jdom.Element parent, java.lang.String childName)
Returns the first child of the given parent with the given name.static org.jdom.Element
getLangChild(org.jdom.Element elt, java.lang.String childName, java.lang.String lang, java.lang.String defaultLang)
Returns the first child ofelt
if possible which useslang
ordefaultLang
as value for itsxml:lang
attribute.static java.util.List<org.jdom.Element>
getLangChildren(org.jdom.Element elt, java.lang.String childName, java.lang.String lang)
Returns all the children ofelt
which uselang
as value for theirxml:lang
attribute.static java.util.Map<java.lang.String,java.lang.String>
getLangChildrenMap(org.jdom.Element elt, java.lang.String childName)
Returns the map of the children ofelt
with theirxml:lang
as key.static java.lang.String
getLangChildText(org.jdom.Element elt, java.lang.String childName, java.lang.String lang)
Returns the text of the first child ofelt
which useslang
as value for itsxml:lang
attribute.static java.lang.String
getLangChildText(org.jdom.Element elt, java.lang.String childName, java.lang.String lang, java.lang.String defaultLang)
Returns the text of the first child ofelt
which useslang
ordefaultLang
as value for itsxml:lang
attribute.static long
getLong(java.lang.Object node, java.lang.String xpathExpression, long def)
Gives a long representation of the first node wich if the result of the XPath expression into the given node in parameter.static java.util.List<?>
getNodeList(java.lang.String xmlString, java.lang.String xpathExpression)
Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.static java.util.List<?>
getNodeList(org.jdom.Document doc, java.lang.String xpathExpression)
Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.static java.util.List<?>
getNodeList(org.jdom.Element element, java.lang.String xpathExpression)
Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.static java.lang.Object
getSingleNode(java.lang.Object doc, java.lang.String xpathExpression)
Provide XPath feature : returns the first of the Nodes in the xml in first argument, with the xPathExpression in second argument.static java.lang.Object
getSingleNode(java.lang.String xmlString, java.lang.String xpathExpression)
Provide XPath feature : returns the first of the Nodes in the xml in first argument, with the xPathExpression in second argument.static java.lang.String
getString(java.lang.Object node, java.lang.String xpathExpression)
Return the most pertinent String for the result of the XPath query in the body content of the given resource.static java.lang.String
getStringNode(java.lang.Object node)
Return the most pertinent String representation for this node.static java.lang.String
getTextValue(org.w3c.dom.Element parentElement, java.lang.String tagName)
Returns the text value of the given tag of the given DOM elementstatic java.lang.String
getTextValue(org.w3c.dom.Element parentElement, java.lang.String tagName, java.lang.String defaultValue)
Returns the text value of the given tag of the given DOM element or the default value.static java.lang.String
getXmlString(org.jdom.Element elt)
Returns a String representation of the given element.static int
lastIndexOfElement(org.jdom.Element parentElt, java.lang.String eltName)
Returns the position of the latest element with the given name in the parent's childrens or -1 if there is no elements.static java.lang.String
normalize(java.lang.String str)
Normalizes the String for XML.static void
removeEmptyElement(org.jdom.Element element)
Remove element if it is no longer a node.static org.jdom.Element
searchChild(org.jdom.Element elt, java.lang.String tagName, java.lang.String attName, java.lang.String attValue)
Returns the first child having a given attribute value.static java.util.List<org.jdom.Element>
searchChildren(org.jdom.Element elt, java.lang.String tagName, java.lang.String attName, java.lang.String attValue)
Returns all the children having the given attribute value.static org.jdom.Element
searchElement(org.jdom.Document document, java.lang.String tagName, java.lang.String attName, java.lang.String attValue)
Returns the first element having a given attribute valuestatic java.lang.String
searchValue(org.jdom.Element element, java.lang.String tagName, java.lang.String attName)
Returns the first element having a given attribute namestatic void
updateAttribute(org.jdom.Element elt, java.lang.String attr, java.lang.String value)
Updates a given attribute value on a given element.static void
updateElements(org.jdom.Element parent, java.lang.String childName, java.lang.String attName, java.util.Set<java.lang.String> attrSet)
Update all children attribute value, with the given name, under a given parent Element.static void
updateLangElement(org.jdom.Element parentElt, java.lang.String eltName, java.util.Map<java.lang.String,java.lang.String> langMap)
Constructs a sub element base on the given language map.static void
write(org.jdom.Document doc, java.io.File file)
-
-
-
Field Detail
-
REVISION
public static final java.lang.String REVISION
- See Also:
- Constant Field Values
-
XML_HEADER
public static final java.lang.String XML_HEADER
- See Also:
- Constant Field Values
-
-
Method Detail
-
normalize
public static java.lang.String normalize(java.lang.String str)
Normalizes the String for XML. Chars<
,>
,&
,\
,"
,\r
,\n
. All char < 0x20 are ignored except \t, \r, \n- Parameters:
str
- the string to normalize- Returns:
- the normalized String.
-
searchChild
public static org.jdom.Element searchChild(org.jdom.Element elt, java.lang.String tagName, java.lang.String attName, java.lang.String attValue)
Returns the first child having a given attribute value.- Parameters:
elt
- the element to start the search.tagName
- the elements to be searched for.attName
- the attributes to be searched for.attValue
- the value to be searched for.- Returns:
- the element found or null
- Since:
- jcms-4.0
-
searchChildren
public static java.util.List<org.jdom.Element> searchChildren(org.jdom.Element elt, java.lang.String tagName, java.lang.String attName, java.lang.String attValue)
Returns all the children having the given attribute value.- Parameters:
elt
- the element to start the search.tagName
- the elements to be searched for.attName
- the attributes to be searched for.attValue
- the value to be searched for.- Returns:
- a list (never null) of JDom elements.
- Since:
- jcms-5.7.2
-
searchElement
public static org.jdom.Element searchElement(org.jdom.Document document, java.lang.String tagName, java.lang.String attName, java.lang.String attValue)
Returns the first element having a given attribute value- Parameters:
document
- the document to start the search.tagName
- the elements to be searched for.attName
- the attributes to be searched for.attValue
- the value to be searched for.- Returns:
- the element found or null
- Since:
- jcms-2.2
-
searchValue
public static java.lang.String searchValue(org.jdom.Element element, java.lang.String tagName, java.lang.String attName)
Returns the first element having a given attribute name- Parameters:
element
- the element to search on.tagName
- the elements of this document to be searched.attName
- the attributes of this element to be searched.- Returns:
- the element value found or null
- Since:
- jcms-2.2
-
getAttributesAsMap
public static java.util.HashMap<java.lang.String,java.lang.String> getAttributesAsMap(org.jdom.Element elt)
Return a map of attribute (key, value) from the attributes of the JDom Element- Parameters:
elt
- the Element to get the attribute- Returns:
- a map
- Since:
- jcms-4.1
-
getBuilder
public static org.jdom.input.SAXBuilder getBuilder()
Get a SAXBuilder- Returns:
- a SAXBuilder
- Since:
- jcms-5.7.0
-
getDocument
public static org.jdom.Document getDocument(java.io.File file) throws org.jdom.JDOMException, java.io.IOException
Returns the JDOM document for the given file.- Parameters:
file
- the file.- Returns:
- the JDOM document for the given file.
- Throws:
org.jdom.JDOMException
- JDOMException thrown during parsingjava.io.IOException
- IOException thrown during parsing- Since:
- jcms-5.7.0
-
getDocument
public static org.jdom.Document getDocument(java.io.InputStream in) throws org.jdom.JDOMException, java.io.IOException
Returns the JDOM document for the given input stream.- Parameters:
in
- the input stream.- Returns:
- the JDOM document for the given input stream.
- Throws:
org.jdom.JDOMException
- JDOMException thrown during parsingjava.io.IOException
- IOException thrown during parsing- Since:
- jcms-5.7.0
-
getDocument
public static org.jdom.Document getDocument(java.io.Reader reader) throws org.jdom.JDOMException, java.io.IOException
Returns the JDOM document for the given reader.- Parameters:
reader
- the reader.- Returns:
- the JDOM document for the given reader.
- Throws:
org.jdom.JDOMException
- JDOMException thrown during parsingjava.io.IOException
- IOException thrown during parsing- Since:
- jcms-5.7.0
-
getDocument
public static org.jdom.Document getDocument(java.lang.String text) throws org.jdom.JDOMException, java.io.IOException
Returns a JDOM document for the given text.- Parameters:
text
- the text.- Returns:
- a JDOM document for the given text.
- Throws:
org.jdom.JDOMException
- JDOMException thrown during parsingjava.io.IOException
- IOException thrown during parsing- Since:
- jcms-5.7.2
-
write
public static void write(org.jdom.Document doc, java.io.File file) throws java.io.IOException
- Throws:
java.io.IOException
-
getXmlString
public static java.lang.String getXmlString(org.jdom.Element elt)
Returns a String representation of the given element.- Parameters:
elt
- the element- Returns:
- a String representation of the given element.
- Since:
- jcms-5.7.0
-
getLangChild
public static org.jdom.Element getLangChild(org.jdom.Element elt, java.lang.String childName, java.lang.String lang, java.lang.String defaultLang)
Returns the first child ofelt
if possible which useslang
ordefaultLang
as value for itsxml:lang
attribute.- Parameters:
elt
- the ElementchildName
- the name of the childlang
- the languagedefaultLang
- the default language- Returns:
- the first child of
elt
which useslang
as value for itsxml:lang
attribute. - Since:
- jcms-5.7.0
-
getLangChildText
public static java.lang.String getLangChildText(org.jdom.Element elt, java.lang.String childName, java.lang.String lang)
Returns the text of the first child ofelt
which useslang
as value for itsxml:lang
attribute.- Parameters:
elt
- the ElementchildName
- the name of the childlang
- the language- Returns:
- the text of the first child of
elt
which useslang
as value for itsxml:lang
attribute. - Since:
- jcms-5.7.0
- See Also:
getLangChildText(Element, String, String, String)
-
getLangChildText
public static java.lang.String getLangChildText(org.jdom.Element elt, java.lang.String childName, java.lang.String lang, java.lang.String defaultLang)
Returns the text of the first child ofelt
which useslang
ordefaultLang
as value for itsxml:lang
attribute.- Parameters:
elt
- the ElementchildName
- the name of the childlang
- the languagedefaultLang
- the default language- Returns:
- the text of the first child of
elt
which useslang
ordefaultLang
as value for itsxml:lang
attribute. - Since:
- jcms-5.7.0
-
getLangChildren
public static java.util.List<org.jdom.Element> getLangChildren(org.jdom.Element elt, java.lang.String childName, java.lang.String lang)
Returns all the children ofelt
which uselang
as value for theirxml:lang
attribute.- Parameters:
elt
- the ElementchildName
- the name of the childlang
- the language- Returns:
- the a list of child Element of
elt
which useslang
as value for itsxml:lang
attribute. - Since:
- jcms-5.7.0
-
getLangChildrenMap
public static java.util.Map<java.lang.String,java.lang.String> getLangChildrenMap(org.jdom.Element elt, java.lang.String childName)
Returns the map of the children ofelt
with theirxml:lang
as key.- Parameters:
elt
- the ElementchildName
- the name of the child- Returns:
- a map of {
lang
,elt
}. - Since:
- jcms-5.7.0
-
lastIndexOfElement
public static int lastIndexOfElement(org.jdom.Element parentElt, java.lang.String eltName)
Returns the position of the latest element with the given name in the parent's childrens or -1 if there is no elements.- Parameters:
parentElt
- the parent elementeltName
- the name of the sub element- Returns:
- int the last element index
- Since:
- jcms-5.7.1
-
addLangElement
public static void addLangElement(org.jdom.Element parentElt, java.lang.String eltName, java.util.Map<java.lang.String,java.lang.String> langMap)
Constructs a sub element base on the given language map.- Parameters:
parentElt
- the parent elementeltName
- the name of the sub elementlangMap
- the language map- Since:
- jcms-5.7.0
-
updateLangElement
public static void updateLangElement(org.jdom.Element parentElt, java.lang.String eltName, java.util.Map<java.lang.String,java.lang.String> langMap)
Constructs a sub element base on the given language map. Try to update existing elements. Remove elements not declared in the map.- Parameters:
parentElt
- the parent elementeltName
- the name of the sub elementlangMap
- the language map- Since:
- jcms-5.7.0
-
updateAttribute
public static void updateAttribute(org.jdom.Element elt, java.lang.String attr, java.lang.String value)
Updates a given attribute value on a given element. If value is null then remove attribute.- Parameters:
elt
- the XML Elementattr
- String represents tha attribute namevalue
- the attribute value to set
-
updateElements
public static void updateElements(org.jdom.Element parent, java.lang.String childName, java.lang.String attName, java.util.Set<java.lang.String> attrSet)
Update all children attribute value, with the given name, under a given parent Element. If values are not set in the given attrSet then remove child element. If there is no child element for values in the attrSet then create elements.- Parameters:
parent
- the parent ElementchildName
- the children name elementattName
- the children attribute name to work withattrSet
- the set of new values.
-
removeEmptyElement
public static void removeEmptyElement(org.jdom.Element element)
Remove element if it is no longer a node.- Parameters:
element
- the element to work with- Since:
- jcms-5.7.0
-
getFirstChild
public static org.jdom.Element getFirstChild(org.jdom.Element parent, java.lang.String childName)
Returns the first child of the given parent with the given name. It it doesn't exists then create it.- Parameters:
parent
- the Parent ElementchildName
- the name of the child to look for or create.- Returns:
- Element the child
-
getTextValue
public static java.lang.String getTextValue(org.w3c.dom.Element parentElement, java.lang.String tagName)
Returns the text value of the given tag of the given DOM element- Parameters:
parentElement
- the parenttagName
- the name of the tag to get the text value- Returns:
- the text value
-
getTextValue
public static java.lang.String getTextValue(org.w3c.dom.Element parentElement, java.lang.String tagName, java.lang.String defaultValue)
Returns the text value of the given tag of the given DOM element or the default value.- Parameters:
parentElement
- the parenttagName
- the name of the tag to get the text valuedefaultValue
- the defaultValue- Returns:
- the text value or defaultValue if null.
-
extractText
public static java.lang.String extractText(java.lang.String str)
Extract the text of the given XML/HTML string.- Parameters:
str
- the XML/HTML string .- Returns:
- the XML/HTML string without tags and .
- Since:
- jcms-5.6.0
-
extractText
public static java.lang.String extractText(java.lang.String str, boolean collapseBlank)
Extract the text of the given XML/HTML string.- Parameters:
str
- the XML/HTML string .collapseBlank
- if true, series of blanks chars are collapsed in one blank.- Returns:
- the XML/HTML string without tags and .
- Since:
- jcms-5.6.0
-
getNodeList
public static java.util.List<?> getNodeList(java.lang.String xmlString, java.lang.String xpathExpression) throws org.jdom.JDOMException
Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.- Parameters:
xmlString
- the full XML in a single StringxpathExpression
- the XPath expression- Returns:
- the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
- Throws:
org.jdom.JDOMException
- if the XPath expression is invalid or its evaluation on the specified context failed.
-
getNodeList
public static java.util.List<?> getNodeList(org.jdom.Document doc, java.lang.String xpathExpression) throws org.jdom.JDOMException
Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.- Parameters:
doc
- the XML documentxpathExpression
- the XPath expression- Returns:
- the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
- Throws:
org.jdom.JDOMException
- if the XPath expression is invalid or its evaluation on the specified context failed.
-
getNodeList
public static java.util.List<?> getNodeList(org.jdom.Element element, java.lang.String xpathExpression) throws org.jdom.JDOMException
Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.- Parameters:
element
- an XML nodexpathExpression
- the XPath expression- Returns:
- the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
- Throws:
org.jdom.JDOMException
- if the XPath expression is invalid or its evaluation on the specified context failed.
-
getSingleNode
public static java.lang.Object getSingleNode(java.lang.String xmlString, java.lang.String xpathExpression) throws org.jdom.JDOMException
Provide XPath feature : returns the first of the Nodes in the xml in first argument, with the xPathExpression in second argument.- Parameters:
xmlString
- the XML documentxpathExpression
- the XPath expression- Returns:
- the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
- Throws:
org.jdom.JDOMException
- if the XPath expression is invalid or its evaluation on the specified context failed.
-
getSingleNode
public static java.lang.Object getSingleNode(java.lang.Object doc, java.lang.String xpathExpression) throws org.jdom.JDOMException
Provide XPath feature : returns the first of the Nodes in the xml in first argument, with the xPathExpression in second argument.- Parameters:
doc
- the XML documentxpathExpression
- the XPath expression- Returns:
- the list of selected item, which may be of types: Element, Attribute, Text, CDATA, Comment, ProcessingInstruction, Boolean, Double, String, or null if no item was selected.
- Throws:
org.jdom.JDOMException
- if the XPath expression is invalid or its evaluation on the specified context failed.
-
getString
public static java.lang.String getString(java.lang.Object node, java.lang.String xpathExpression)
Return the most pertinent String for the result of the XPath query in the body content of the given resource.- Parameters:
node
- the Element node used to execute xpath expressionxpathExpression
- an xpath expression to dig into the node- Returns:
- the String found or null
-
getStringNode
public static java.lang.String getStringNode(java.lang.Object node)
Return the most pertinent String representation for this node.- Parameters:
node
- the given resource get by a rest request- Returns:
- the String found or null
-
getBoolean
public static boolean getBoolean(java.lang.Object node, java.lang.String xpathExpression, boolean def)
Gives a boolean representation of the first node wich if the result of the XPath expression into the given node in parameter. If the xpath expression is not valid, or nothing is found, or it is not corresponding to a boolean, the def value is returned.- Parameters:
node
- the node we dig intoxpathExpression
- the XPath expressiondef
- the default value- Returns:
- the computed value
-
getDate
public static java.util.Date getDate(java.lang.Object node, java.lang.String xpathExpression, java.util.Date def)
Gives a Date representation of the first node wich if the result of the XPath expression into the given node in parameter. If the xpath expression is not valid, or nothing is found, or it is not corresponding to a Date (as a W3Cdate), the def value is returned.- Parameters:
node
- the node we dig intoxpathExpression
- the XPath expressiondef
- the default value- Returns:
- the computed value
-
getLong
public static long getLong(java.lang.Object node, java.lang.String xpathExpression, long def)
Gives a long representation of the first node wich if the result of the XPath expression into the given node in parameter. If the xpath expression is not valid, or nothing is found, or it is not corresponding to a long, the def value is returned.- Parameters:
node
- the node we dig intoxpathExpression
- the XPath expressiondef
- the default value- Returns:
- the computed value
-
-