com.jalios.util
Class XmlUtil

java.lang.Object
  extended by com.jalios.util.XmlUtil

public class XmlUtil
extends Object

This class provides a set of static methods which perform various utility operations for XML.

Version:
$Revision: 27751 $
Author:
Olivier Dedieu

Field Summary
static String REVISION
           
static String XML_HEADER
           
 
Method Summary
static void addLangElement(org.jdom.Element parentElt, String eltName, Map<String,String> langMap)
          Constructs a sub element base on the given language map.
static String extractText(String str)
          Extract the text of the given XML/HTML string.
static String extractText(String str, boolean collapseBlank)
          Extract the text of the given XML/HTML string.
static HashMap<String,String> getAttributesAsMap(org.jdom.Element elt)
          Return a map of attribute (key, value) from the attributes of the JDom Element
static boolean getBoolean(Object node, 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 SAXBuilder
static Date getDate(Object node, String xpathExpression, 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(File file)
          Returns the JDOM document for the given file.
static org.jdom.Document getDocument(InputStream in)
          Returns the JDOM document for the given input stream.
static org.jdom.Document getDocument(Reader reader)
          Returns the JDOM document for the given reader.
static org.jdom.Document getDocument(String text)
          Returns a JDOM document for the given text.
static org.jdom.Element getFirstChild(org.jdom.Element parent, String childName)
          Returns the first child of the given parent with the given name.
static org.jdom.Element getLangChild(org.jdom.Element elt, String childName, String lang, String defaultLang)
          Returns the first child of elt if possible which uses lang or defaultLang as value for its xml:lang attribute.
static List<org.jdom.Element> getLangChildren(org.jdom.Element elt, String childName, String lang)
          Returns all the children of elt which use lang as value for their xml:lang attribute.
static Map<String,String> getLangChildrenMap(org.jdom.Element elt, String childName)
          Returns the map of the children of elt with their xml:lang as key.
static String getLangChildText(org.jdom.Element elt, String childName, String lang)
          Returns the text of the first child of elt which uses lang as value for its xml:lang attribute.
static String getLangChildText(org.jdom.Element elt, String childName, String lang, String defaultLang)
          Returns the text of the first child of elt which uses lang or defaultLang as value for its xml:lang attribute.
static long getLong(Object node, 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 List<?> getNodeList(org.jdom.Document doc, String xpathExpression)
          Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.
static List<?> getNodeList(org.jdom.Element element, String xpathExpression)
          Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.
static List<?> getNodeList(String xmlString, String xpathExpression)
          Provide XPath feature : returns the list of Nodes in the xml in first argument, with the xPathExpression in second argument.
static Object getSingleNode(Object doc, String xpathExpression)
          Provide XPath feature : returns the first of the Nodes in the xml in first argument, with the xPathExpression in second argument.
static Object getSingleNode(String xmlString, String xpathExpression)
          Provide XPath feature : returns the first of the Nodes in the xml in first argument, with the xPathExpression in second argument.
static String getString(Object node, String xpathExpression)
          Return the most pertinent String for the result of the XPath query in the body content of the given resource.
static String getStringNode(Object node)
          Return the most pertinent String representation for this node.
static String getTextValue(Element parentElement, String tagName)
          Returns the text value of the given tag of the given DOM element
static String getTextValue(Element parentElement, String tagName, String defaultValue)
          Returns the text value of the given tag of the given DOM element or the default value.
static String getXmlString(org.jdom.Element elt)
          Returns a String representation of the given element.
static int lastIndexOfElement(org.jdom.Element parentElt, 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 String normalize(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, String tagName, String attName, String attValue)
          Returns the first child having a given attribute value.
static List<org.jdom.Element> searchChildren(org.jdom.Element elt, String tagName, String attName, String attValue)
          Returns all the children having the given attribute value.
static org.jdom.Element searchElement(org.jdom.Document document, String tagName, String attName, String attValue)
          Returns the first element having a given attribute value
static String searchValue(org.jdom.Element element, String tagName, String attName)
          Returns the first element having a given attribute name
static void updateAttribute(org.jdom.Element elt, String attr, String value)
          Updates a given attribute value on a given element.
static void updateElements(org.jdom.Element parent, String childName, String attName, Set<String> attrSet)
          Update all children attribute value, with the given name, under a given parent Element.
static void updateLangElement(org.jdom.Element parentElt, String eltName, Map<String,String> langMap)
          Constructs a sub element base on the given language map.
static void write(org.jdom.Document doc, File file)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values

XML_HEADER

public static final String XML_HEADER
See Also:
Constant Field Values
Method Detail

normalize

public static String normalize(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,
                                           String tagName,
                                           String attName,
                                           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 List<org.jdom.Element> searchChildren(org.jdom.Element elt,
                                                    String tagName,
                                                    String attName,
                                                    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,
                                             String tagName,
                                             String attName,
                                             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 String searchValue(org.jdom.Element element,
                                 String tagName,
                                 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 HashMap<String,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(File file)
                                     throws org.jdom.JDOMException,
                                            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
IOException
Since:
jcms-5.7.0

getDocument

public static org.jdom.Document getDocument(InputStream in)
                                     throws org.jdom.JDOMException,
                                            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
IOException
Since:
jcms-5.7.0

getDocument

public static org.jdom.Document getDocument(Reader reader)
                                     throws org.jdom.JDOMException,
                                            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
IOException
Since:
jcms-5.7.0

getDocument

public static org.jdom.Document getDocument(String text)
                                     throws org.jdom.JDOMException,
                                            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
IOException
Since:
jcms-5.7.2

write

public static void write(org.jdom.Document doc,
                         File file)
                  throws IOException
Throws:
IOException

getXmlString

public static 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,
                                            String childName,
                                            String lang,
                                            String defaultLang)
Returns the first child of elt if possible which uses lang or defaultLang as value for its xml:lang attribute.

Parameters:
elt - the Element
childName - the name of the child
lang - the language
defaultLang - the default language
Returns:
the first child of elt which uses lang as value for its xml:lang attribute.
Since:
jcms-5.7.0

getLangChildText

public static String getLangChildText(org.jdom.Element elt,
                                      String childName,
                                      String lang)
Returns the text of the first child of elt which uses lang as value for its xml:lang attribute.

Parameters:
elt - the Element
childName - the name of the child
lang - the language
Returns:
the text of the first child of elt which uses lang as value for its xml:lang attribute.
Since:
jcms-5.7.0
See Also:
getLangChildText(Element, String, String, String)

getLangChildText

public static String getLangChildText(org.jdom.Element elt,
                                      String childName,
                                      String lang,
                                      String defaultLang)
Returns the text of the first child of elt which uses lang or defaultLang as value for its xml:lang attribute.

Parameters:
elt - the Element
childName - the name of the child
lang - the language
defaultLang - the default language
Returns:
the text of the first child of elt which uses lang or defaultLang as value for its xml:lang attribute.
Since:
jcms-5.7.0

getLangChildren

public static List<org.jdom.Element> getLangChildren(org.jdom.Element elt,
                                                     String childName,
                                                     String lang)
Returns all the children of elt which use lang as value for their xml:lang attribute.

Parameters:
elt - the Element
childName - the name of the child
lang - the language
Returns:
the a list of child Element of elt which uses lang as value for its xml:lang attribute.
Since:
jcms-5.7.0

getLangChildrenMap

public static Map<String,String> getLangChildrenMap(org.jdom.Element elt,
                                                    String childName)
Returns the map of the children of elt with their xml:lang as key.

Parameters:
elt - the Element
childName - 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,
                                     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 element
eltName - 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,
                                  String eltName,
                                  Map<String,String> langMap)
Constructs a sub element base on the given language map.

Parameters:
parentElt - the parent element
eltName - the name of the sub element
langMap - the language map
Since:
jcms-5.7.0

updateLangElement

public static void updateLangElement(org.jdom.Element parentElt,
                                     String eltName,
                                     Map<String,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 element
eltName - the name of the sub element
langMap - the language map
Since:
jcms-5.7.0

updateAttribute

public static void updateAttribute(org.jdom.Element elt,
                                   String attr,
                                   String value)
Updates a given attribute value on a given element. If value is null then remove attribute.

Parameters:
elt - the XML Element
attr - String represents tha attribute name
value - the attribute value to set

updateElements

public static void updateElements(org.jdom.Element parent,
                                  String childName,
                                  String attName,
                                  Set<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 Element
childName - the children name element
attName - the children attribute name to work with
attrSet - 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,
                                             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 Element
childName - the name of the child to look for or create.
Returns:
Element the child

getTextValue

public static String getTextValue(Element parentElement,
                                  String tagName)
Returns the text value of the given tag of the given DOM element

Parameters:
parentElement - the parent
tagName - the name of the tag to get the text value
Returns:
the text value

getTextValue

public static String getTextValue(Element parentElement,
                                  String tagName,
                                  String defaultValue)
Returns the text value of the given tag of the given DOM element or the default value.

Parameters:
parentElement - the parent
tagName - the name of the tag to get the text value
defaultValue - the defaultValue
Returns:
the text value or defaultValue if null.

extractText

public static String extractText(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 &nbsp;.
Since:
jcms-5.6.0

extractText

public static String extractText(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 &nbsp;.
Since:
jcms-5.6.0

getNodeList

public static List<?> getNodeList(String xmlString,
                                  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 String
xpathExpression - 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 List<?> getNodeList(org.jdom.Document doc,
                                  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 document
xpathExpression - 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 List<?> getNodeList(org.jdom.Element element,
                                  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 node
xpathExpression - 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 Object getSingleNode(String xmlString,
                                   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 document
xpathExpression - 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 Object getSingleNode(Object doc,
                                   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 document
xpathExpression - 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 String getString(Object node,
                               String xpathExpression)
Return the most pertinent String for the result of the XPath query in the body content of the given resource.

Parameters:
node -
xpathExpression - an xpath expression to dig into the node
Returns:
the String found or null

getStringNode

public static String getStringNode(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(Object node,
                                 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 into
xpathExpression - the XPath expression
def - the default value
Returns:
the computed value

getDate

public static Date getDate(Object node,
                           String xpathExpression,
                           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 into
xpathExpression - the XPath expression
def - the default value
Returns:
the computed value

getLong

public static long getLong(Object node,
                           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 into
xpathExpression - the XPath expression
def - the default value
Returns:
the computed value


Copyright © 2001-2010 Jalios SA. All Rights Reserved.