com.massimo.utils
Class XMLHelper

java.lang.Object
  extended by com.massimo.utils.XMLHelper

public class XMLHelper
extends Object

Helper to do things with XML and nu.xom.Elements


Constructor Summary
XMLHelper()
           
 
Method Summary
static void copyAttributes(Element source, Element target)
          Copy all source Attributes to target
static void extractAttributes(Map<String,String> map, Element element)
          Add all element's attributes to map
static void extractAttributes(Properties properties, Element element)
          Add all element's attributes to properties
static void extractProperties(Map<String,String> map, Element element, String propertyElementName)
          Add all child properties to map
static void extractProperties(Properties properties, Element element, String propertyElementName)
          Add all child properties to properties
static Builder getNonValidatingBuilder()
           
static Element getRootElement(Element element)
          Utility method to find the root element for a given element
static String getText(Node node)
          The XOM Node.getValue() method returns the XPath 1.0 string-value of Node.
static Builder getValidatingBuilder()
          Delegates to getValidatingBuilder(String, String) without setting noNamespaceSchemaLocation or externalLocation.
static Builder getValidatingBuilder(String externalLocation)
          Delegates to getValidatingBuilder(String, String)
protected static Builder getValidatingBuilder(String noNamespaceSchemaLocation, String externalLocation)
          Unfortunately, the Xerces parser does not honour both noNamespaceSchemaLocation and externalLocation.
static Builder getValidatingBuilderNoNamespace(String noNamespaceSchemaLocation)
          Delegates to getValidatingBuilder(String, String)
static void populateAttributes(Map<String,String> attributes, Element element)
          Set attributes for an element
static boolean readBooleanAttribute(Element elm, String attrName)
          Helper method to read named attribute from specified element and convert it to boolean.
static boolean readBooleanAttribute(Element elm, String attrName, boolean defaultValue)
          Helper method to read named attribute from specified element and convert it to boolean.
static int readIntAttribute(Element elm, String attrName, int defaultValue)
           
static Element readXML(String xmlFile)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLHelper

public XMLHelper()
Method Detail

copyAttributes

public static void copyAttributes(Element source,
                                  Element target)
Copy all source Attributes to target

Parameters:
source -
target -

extractAttributes

public static void extractAttributes(Map<String,String> map,
                                     Element element)
Add all element's attributes to map

Parameters:
map - Map to add attribute (name, value) ;
element - Element

extractAttributes

public static void extractAttributes(Properties properties,
                                     Element element)
Add all element's attributes to properties

Parameters:
properties - to add attribute (name, value) ;
element - Element

extractProperties

public static void extractProperties(Map<String,String> map,
                                     Element element,
                                     String propertyElementName)
Add all child properties to map

Parameters:
map - Map to add attribute (name, value) ;
element - A properties element
propertyElementName -

extractProperties

public static void extractProperties(Properties properties,
                                     Element element,
                                     String propertyElementName)
Add all child properties to properties

Parameters:
properties - to add attribute (name, value) ;
element - A properties element
propertyElementName -

getNonValidatingBuilder

public static Builder getNonValidatingBuilder()
Returns:
A non-validating Builder

getRootElement

public static Element getRootElement(Element element)
Utility method to find the root element for a given element

Parameters:
element - the element to find the root of
Returns:
The root element for element

getText

public static String getText(Node node)
The XOM Node.getValue() method returns the XPath 1.0 string-value of Node. This includes all text inside child elements, which is not quite what is usually needed. The text specific to this element is in one or more Text nodes.

Parameters:
node -
Returns:
Concatenated XML Text nodes for the task

getValidatingBuilder

public static Builder getValidatingBuilder()
                                    throws SAXException
Delegates to getValidatingBuilder(String, String) without setting noNamespaceSchemaLocation or externalLocation.

Returns:
A validating Builder, with schema validation enabled.
Throws:
SAXException

getValidatingBuilder

public static Builder getValidatingBuilder(String externalLocation)
                                    throws SAXException
Delegates to getValidatingBuilder(String, String)

Parameters:
externalLocation - A schema location list. Defaults to XInclude schema location.
Returns:
A validating Builder, with schema vaidation enabled
Throws:
SAXException

getValidatingBuilder

protected static Builder getValidatingBuilder(String noNamespaceSchemaLocation,
                                              String externalLocation)
                                       throws SAXException
Unfortunately, the Xerces parser does not honour both noNamespaceSchemaLocation and externalLocation. It seems to assume that the first namespace listed in externalLocation is the target namespace.

Parameters:
noNamespaceSchemaLocation - If specified, externalLocation will be ignored.
externalLocation - If empty, defaults to XInclude schema location
Returns:
A validating Builder, with schema vaidation enabled. Uses Xerces.
Throws:
SAXException

getValidatingBuilderNoNamespace

public static Builder getValidatingBuilderNoNamespace(String noNamespaceSchemaLocation)
                                               throws SAXException
Delegates to getValidatingBuilder(String, String)

Parameters:
noNamespaceSchemaLocation - The location of a schema for the nameless namespace
Returns:
A validating Builder, with schema vaidation enabled
Throws:
SAXException

populateAttributes

public static void populateAttributes(Map<String,String> attributes,
                                      Element element)
Set attributes for an element

Parameters:
attributes - the attrbutes to set
element - the element the attributes belong to

readBooleanAttribute

public static boolean readBooleanAttribute(Element elm,
                                           String attrName)
Helper method to read named attribute from specified element and convert it to boolean.

Parameters:
elm - Source element
attrName - Name of the attribute
Returns:
boolean value of specified attribue, or false if not found

readBooleanAttribute

public static boolean readBooleanAttribute(Element elm,
                                           String attrName,
                                           boolean defaultValue)
Helper method to read named attribute from specified element and convert it to boolean.

Parameters:
elm - Source element
attrName - Name of the attribute
defaultValue -
Returns:
boolean value of specified attribue, or false if not found

readIntAttribute

public static int readIntAttribute(Element elm,
                                   String attrName,
                                   int defaultValue)
Parameters:
elm -
attrName -
defaultValue -
Returns:
int

readXML

public static Element readXML(String xmlFile)
                       throws ValidityException,
                              ParsingException,
                              IOException,
                              BadParseAttributeException,
                              InclusionLoopException,
                              NoIncludeLocationException,
                              XIncludeException
Parameters:
xmlFile -
Returns:
Root Element of the loaded Document
Throws:
ValidityException
ParsingException
IOException
BadParseAttributeException
InclusionLoopException
NoIncludeLocationException
XIncludeException