org.backsource.utils.xml
Class XmlBeanUtil

java.lang.Object
  extended byorg.backsource.utils.xml.XmlBeanUtil

public class XmlBeanUtil
extends java.lang.Object

XPath based property setter util. Used in three situations:

It is loosely based on the apache BeansUtils, which it also uses under the hood.

Version:
$Revision: 1.1.1.1 $
Author:
Peter Antman

Constructor Summary
XmlBeanUtil()
           
 
Method Summary
static boolean containsElementChildren(org.w3c.dom.Node node)
          Ckeck if node contains any Element children.
static java.lang.String getValue(org.w3c.dom.Node node)
          Get the String value from node
static java.lang.String[] getXmlArrayProperty(org.w3c.dom.Node xml, java.lang.String xpath)
          Get the values from node the xpath points at.
static java.lang.String getXmlProperty(org.w3c.dom.Node xml, java.lang.String xpath)
          Get the value from node the xpath points at.
static boolean hasChildNodes(org.w3c.dom.Node node)
          Check if node contains any child nodes other that whitespace text and comments.
static void main(java.lang.String[] args)
           
static void populateBean(java.lang.Object bean, org.w3c.dom.Node xml, java.util.Map map)
          Populate the given bean, with values from node gotten through the property/xpath mapping given in map.
static void populateXml(org.w3c.dom.Node xml, java.util.Map map)
          Populate the Node with xpath/object mappings given in map.
static void setBeanProperty(java.lang.Object bean, java.lang.String prop, org.w3c.dom.Node xml, java.lang.String xpath)
          Set the property in the given bean, getting the value from the Node specifyed with xpath.
static void setValue(org.w3c.dom.Node node, java.lang.Object value)
          Set the value of node with object.
static void setXmlProperty(org.w3c.dom.Node xml, java.lang.String xpath, java.lang.Object value)
          Set value in node based on xpath.
static void setXmlProperty(org.w3c.dom.Node xml, java.lang.String xpath, java.lang.Object bean, java.lang.String prop)
          Set value in node base on xpath and derived from bean with property prop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlBeanUtil

public XmlBeanUtil()
Method Detail

setXmlProperty

public static void setXmlProperty(org.w3c.dom.Node xml,
                                  java.lang.String xpath,
                                  java.lang.Object value)
                           throws XmlException
Set value in node based on xpath. What will be set i dependant on both what the xpath retrurns and what type of object value is.

Parameters:
xml - the xml node to set a (Node) value on.
xpath - xpath pointing to a node where value should be inserted. if pointing to more than one node, the value will be inserted in all places.
value - the value to be inserted, if of Node type inserted as node, if of text type will be inserted as text, if the returned not only contains text, otherwise it will be converted to a DocumentFragment a inserted AFTER the hitted node.
Throws:
XmlException

setXmlProperty

public static void setXmlProperty(org.w3c.dom.Node xml,
                                  java.lang.String xpath,
                                  java.lang.Object bean,
                                  java.lang.String prop)
                           throws XmlException

Set value in node base on xpath and derived from bean with property prop.

if the xpath returns multiple nodes and prop point to an array or collection, set the value of each node by traversing the collection.

Throws:
XmlException

setBeanProperty

public static void setBeanProperty(java.lang.Object bean,
                                   java.lang.String prop,
                                   org.w3c.dom.Node xml,
                                   java.lang.String xpath)
                            throws XmlException

Set the property in the given bean, getting the value from the Node specifyed with xpath.

if the property in the bean is of the same Node type as that one returned by the xpath the result will be cloned and set as is.

This will for now only ever get one Node and set the prop with.

What xpath returns is the same as in setXmlProperty. The same type of concerion that normally occurs in apache BeanUtils will also take place.

Parameters:
bean - the JavaBean to populate
prop - the string property expression, following the style in apache BeanUtils.
xml - the node to query for a value.
xpath - the xpath expression used to query the node.
Throws:
XmlException - if nothing was found with xpath.

populateXml

public static void populateXml(org.w3c.dom.Node xml,
                               java.util.Map map)

Populate the Node with xpath/object mappings given in map.

Same as setXmlProperty except that mutiple xpath/value mapping may be given in map.


populateBean

public static void populateBean(java.lang.Object bean,
                                org.w3c.dom.Node xml,
                                java.util.Map map)

Populate the given bean, with values from node gotten through the property/xpath mapping given in map.

Same as setBeanPropert except that multiple property/xpath mappings may be given.


getXmlProperty

public static java.lang.String getXmlProperty(org.w3c.dom.Node xml,
                                              java.lang.String xpath)
                                       throws XmlException

Get the value from node the xpath points at.

if the Node returned by the xpath is an Element node with only text content, that content will be returned.

if the Node is a Text node (and CDataSection) the text will be returned.

if the Node is an Attribute the value of the attribute will be returned.

if none of the above does apply, the serialized XML form of the node will be returned.

This one will allways only select a single Node even if the xpath might hit more than one node.

Throws:
XmlException

getXmlArrayProperty

public static java.lang.String[] getXmlArrayProperty(org.w3c.dom.Node xml,
                                                     java.lang.String xpath)
                                              throws XmlException

Get the values from node the xpath points at.

The differense compared to getXmlProperty(Node, String)is that this allways selects as if the xpath may return more than one node.

Throws:
XmlException

getValue

public static java.lang.String getValue(org.w3c.dom.Node node)
                                 throws XmlException

Get the String value from node

if the Node is an Element node with only text content, that content will be returned.

if the Node is a Text node (and CDataSection) the text will be returned.

if the Node is an Attribute the value of the attribute will be returned.

if none of the above does apply, the serialized XML form of the node will be returned, without a prolog.

Parameters:
node - a node to get the string value from.
Returns:
the String form of the value, or null if node is null.
Throws:
XmlException

setValue

public static void setValue(org.w3c.dom.Node node,
                            java.lang.Object value)
                     throws XmlException

Set the value of node with object.

if value is of Node type replace node. if the node is a DocumentFragment this will insert all the children of the document fragment.

if Node is of text, set value from value, converted to String.

if Node is of element, set content from value. if node only contains text, set text -owerwriting any prevoius content, if elemnt contains children, or is empty (skipping any comments and whitespace) check if value is possible to convert to a DocumentFragmen where all children are elements and insert , otherwise insert as text.

Parameters:
value - the value to be inserted, if of Node type inserted as node, if of text type will be inserted as text, if the returned not only contains text, otherwise it will be converted to a DocumentFragment a inserted AFTER the hitted node.
Throws:
XmlException

containsElementChildren

public static boolean containsElementChildren(org.w3c.dom.Node node)
                                       throws XmlException
Ckeck if node contains any Element children.

Returns:
true if the node contains Element children.
Throws:
XmlException

hasChildNodes

public static boolean hasChildNodes(org.w3c.dom.Node node)
Check if node contains any child nodes other that whitespace text and comments.


main

public static void main(java.lang.String[] args)


Copyright © 2000-2004 Backsource. All Rights Reserved.