com.myjavatools.xml
Class BasicXmlData

java.lang.Object
  extended bycom.myjavatools.xml.BasicXmlData
All Implemented Interfaces:
XmlData
Direct Known Subclasses:
Rss, com.myjavatools.xml.Rss.Member

public class BasicXmlData
extends java.lang.Object
implements XmlData


Nested Class Summary
static class BasicXmlData.Expression
          An abstract class that stores an expression used in XmlData search/filtering.
static class BasicXmlData.Policy
          Class Policy defines three different casting policies.
 
Nested classes inherited from class com.myjavatools.xml.XmlData
XmlData.Condition
 
Field Summary
static java.lang.Class XmlDataClass
           
 
Constructor Summary
BasicXmlData()
          Default constructor.
BasicXmlData(java.io.File in)
          Creates an instance from the data read from a file.
BasicXmlData(java.io.InputStream in)
          Creates an instance from the data read from input stream.
BasicXmlData(java.lang.String type)
          Creates an instance of BasicXmlData of specified type.
BasicXmlData(java.lang.String type, java.lang.String value)
          Creates an instance of specified type and with the specified value.
BasicXmlData(java.lang.String type, java.lang.String value, org.xml.sax.AttributeList attributes)
          Creates an instance of specified type, with the specified value, attributes that are listed in the provided AttributeList.
BasicXmlData(java.lang.String type, java.lang.String value, org.xml.sax.AttributeList attributes, BasicXmlData[] kids)
          Creates an instance of specified type, with the specified value, attributes that are listed in the provided AttributeList, and the kids that are listed in the provided array of XmlData.
BasicXmlData(java.lang.String type, java.lang.String value, org.xml.sax.AttributeList attributes, java.util.Collection kids)
          Creates an instance of specified type, with the specified value, attributes that are listed in the provided AttributeList, and the kids that are listed in the provided collection.
BasicXmlData(java.lang.String type, java.lang.String value, BasicXmlData[] kids)
          Creates an instance of specified type, with the specified value and the kids that are listed in the provided array.
BasicXmlData(java.lang.String type, java.lang.String value, java.util.Collection kids)
          Creates an instance of specified type, with the specified value and the kids that are listed in the provided collection.
BasicXmlData(java.lang.String type, java.lang.String value, java.util.Map attributes, java.util.Collection kids)
          Creates an instance of specified type, with the specified value, attributes that are listed in the provided map (name -> value), and the kids that are listed in the provided collection.
BasicXmlData(java.lang.String type, java.lang.String value, java.lang.String[] attributes)
          Creates an instance of specified type, with the specified value and the attributes that are listed in the provided array as name-value pairs.
BasicXmlData(java.lang.String type, java.lang.String value, java.lang.String[] attributes, BasicXmlData[] kids)
          Creates an instance of BasicXmlData of specified type, with the specified value, attributes that are listed in the provided array as name-value pairs, and the kids that are listed in the provided array of XmlData.
BasicXmlData(java.lang.String type, java.lang.String value, java.lang.String[] attributes, java.util.Collection kids)
          Creates an instance of specified type, with the specified value, attributes that are listed in the provided array as name-value pairs, and the kids that are listed in the provided collection.
BasicXmlData(java.net.URL url)
          Creates an instance from the data read from URL.
BasicXmlData(XmlData org)
          Creates an instance from another XmlData object (deep copy).
 
Method Summary
 XmlData addKid(XmlData kid)
          Adds a kid to the set of kids.
 void addKids(java.util.Collection kids)
          Adds all XmlData elements from given Collection to the set of kids, skipping elements that are not XmlData.
 void addKids(java.util.Iterator kids)
          Adds all XmlData elements from given Iterator to the collection of kids, skipping elements that are not BasicXmlData.
 XmlData cast(java.util.Map typemap, BasicXmlData.Policy policy)
          Casts XmlData and its kids, recursively, to specified classes, according to the typemap.
static XmlData cast(XmlData object, java.util.Map typemap, BasicXmlData.Policy policy)
          Casts XmlData and its kids, recursively, to specified classes, according to the typemap.
 boolean castKids(java.lang.String type, java.lang.Class clazz)
          Casts kids of specified type to a specified class (actually replacing them with the new instances).
 XmlData castToPackage(java.lang.String packageName, BasicXmlData.Policy policy)
          Casts XmlData and its kids, recursively, to classes in specified package.
static XmlData castToPackage(XmlData object, java.lang.String packageName, BasicXmlData.Policy policy)
          Casts XmlData and its kids, recursively, to classes in specified package.
 void cleanAttributes()
          cleans attributes map Just removes all the attributes except xmlns (because)
 java.lang.Object clone()
          Clones of XmlData, same thing as deepCopy.
static void debug(boolean doit)
           
 XmlData deepCopy()
          Deep copy of XmlData.
 boolean equals(java.lang.Object o)
          Compares the specified object with this XmlData for equality.
 java.util.Collection getAllKids()
          Gets a collection of all kids of XmlData.
 java.lang.String getAttribute(java.lang.String name)
          Gets the value of a specified attribute.
 java.lang.String getAttribute(java.lang.String name, java.lang.String defaultValue)
          Gets the value of a specified attribute; if there is none, returns default value.
 java.util.Map getAttributes()
          Gets a Map of attributes.
 java.lang.String getId()
          Gets the id of XmlData, which is the value of attribute "id".
 XmlData getKid(int position)
          Gets a kid at specified position.
 XmlData getKid(java.lang.String type)
          Gets a kid of specified type, if any.
 XmlData getKid(java.lang.String type, java.lang.String id)
          Gets a kid having specified type and specified id.
 XmlData getKid(java.lang.String type, java.lang.String attribute, java.lang.String value)
          Gets a kid of specified type that has an attribute with a specified value.
 java.lang.String getKidAttribute(java.lang.String type, java.lang.String attribute)
          Gets the value of specified attribute of a kid of specified type, if any
 int getKidCount(java.lang.String type)
          Gets the number of kids of specified type.
 java.util.Collection getKids(java.lang.String type)
          Gets a Collection of kids of specified type.
 java.util.Collection getKidTypes()
          Gets a collection of kid types
 java.lang.String getKidValue(java.lang.String type)
          Gets the value of a kid of specified type, if any.
 java.lang.String getName()
          Gets the name of XmlData, which is the value of attribute "name".
 java.lang.String getType()
          Gets XmlData type, which is just a String.
 java.lang.String getValue()
          Gets a value of XmlData, which is just a String.
 XmlData getXmlContent()
          Gets the contents of this XmlData (that is, itself).
 int indexOf(XmlData kid)
          returns the position of kid in the list of kids
 XmlData insertKid(int position, XmlData kid)
          Insertes a kid into the list of kids.
 XmlData insertKid(XmlData before, XmlData kid)
          Insertes a kid before a specified kid.
 void removeKid(XmlData kid)
          Removes a kid from the set of kids.
 java.util.Collection removeKids(java.lang.String type)
          Removes all kids of given type.
 void replaceKid(XmlData oldKid, XmlData newKid)
          Replaces a kid with another.
 boolean satisfies(java.lang.String expression)
          Checks whether this XmlData node satisfies given expression (presented as string).
 boolean satisfies(XmlData.Condition condition)
          Checks whether this XmlData node satisfies given condition.
 void save(java.io.File file)
          Saves XmlData to a file.
 void save(java.io.OutputStream os)
          Saves XmlData to an output stream.
 void save(java.lang.String filename)
          Saves XmlData to a file.
 XmlData selectTree(java.lang.String expression)
          Selects a subtree from XmlData, that is, the tree of those nodes that satisfy a condition.
 XmlData selectTree(XmlData.Condition condition)
          Selects a subtree from XmlData, that is, the tree of those nodes that satisfy a condition.
 void setAttribute(java.lang.String name, java.lang.String value)
          Sets an attribute value.
 void setAttributes(org.xml.sax.AttributeList attributes)
          Sets attributes from an AttributeList.
 void setAttributes(java.util.Map attributes)
          Sets attributes from a Map.
 void setAttributes(java.lang.String[] attributes)
          Sets attributes from an array of name-value pairs.
 void setKidValue(java.lang.String type, java.lang.String value)
          Sets the value of a kid of specified type, creating it if necessary
 void setType(java.lang.String type)
           
 XmlData setValue(java.lang.String v)
          Sets the value of XmlData.
 void setXmlContent(XmlData org)
          Sets the contents from another XmlData.
 void trim()
          Tries to trim the set of kids, so that there are no spare slots left.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XmlDataClass

public static final java.lang.Class XmlDataClass
Constructor Detail

BasicXmlData

public BasicXmlData()
Default constructor.

Creates an empty instance.


BasicXmlData

public BasicXmlData(java.lang.String type)
Creates an instance of BasicXmlData of specified type.

Parameters:
type - the type of data, an arbitrary identifier string.

BasicXmlData

public BasicXmlData(java.lang.String type,
                    java.lang.String value)
Creates an instance of specified type and with the specified value.

Parameters:
type - the type of data, an arbitrary identifier string.
value - the value of data, any string.

Example:

  • new BasicXmlData("example", "This is an example").save(filename)
    will result in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <example>This is an example</example>
     

  • BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        java.util.Collection kids)
    Creates an instance of specified type, with the specified value and the kids that are listed in the provided collection.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    kids - a Collection of XmlData that attach themselves as kids to the instance being created

    Example:

  • new BasicXmlData("example", "This is an example", Arrays.asList(new BasicXmlData[] {new BasicXmlData("subexample", "This is a kid of example")}).save(filename)
    will result in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <example>
      <subexample>This is a kid of example</subexample>This is an example</example>
     

  • BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        BasicXmlData[] kids)
    Creates an instance of specified type, with the specified value and the kids that are listed in the provided array.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    kids - an array of XmlData that attach themselves as kids to the instance being created

    Example:

  • new BasicXmlData("example", "This is an example", new BasicXmlData[] {new BasicXmlData("subexample", "This is a kid of example")}).save(filename)
    will result in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <example>
      <subexample>This is a kid of example</subexample>This is an example</example>
     

  • BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        java.lang.String[] attributes)
    Creates an instance of specified type, with the specified value and the attributes that are listed in the provided array as name-value pairs.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - an String array of name-value pairs

    Example:

  • new BasicXmlData("example", "This is an example", new String[] {"id", "0123dx", "name", "Whittman"}).save(filename)
    will result in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <example id="0123dx" name="Whittman">This is an example</example>
     

  • BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        java.lang.String[] attributes,
                        java.util.Collection kids)
    Creates an instance of specified type, with the specified value, attributes that are listed in the provided array as name-value pairs, and the kids that are listed in the provided collection.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - an String array of name-value pairs
    kids - a Collection of XmlData that attach themselves as kids to the instance being created

    Example:

  • new BasicXmlData("example", "This is an example", new String[] {"id", "007", "name", "Bond"}, Arrays.asList(new BasicXmlData[] {new BasicXmlData("subexample", "This is a kid of example")}).save(filename)
    will result in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <example id="007" name="Bond">
      <subexample>This is a kid of example</subexample>This is an example</example>
     

  • BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        java.lang.String[] attributes,
                        BasicXmlData[] kids)
    Creates an instance of BasicXmlData of specified type, with the specified value, attributes that are listed in the provided array as name-value pairs, and the kids that are listed in the provided array of XmlData.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - an String array of name-value pairs
    kids - an array of XmlData that attach themselves as kids to the instance being created

    Example:

  • new BasicXmlData("example", "This is an example", new String[] {"id", "007", "name", "Bond"}, new BasicXmlData[] {new BasicXmlData("subexample", "This is a kid of example")}).save(filename)
    will result in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
    <example id="007" name="Bond">
      <subexample>This is a kid of example</subexample>This is an example</example>
     

  • BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        org.xml.sax.AttributeList attributes)
    Creates an instance of specified type, with the specified value, attributes that are listed in the provided AttributeList.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - an AttributeList (@see org.xml.sax.AttributeList)

    BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        org.xml.sax.AttributeList attributes,
                        java.util.Collection kids)
    Creates an instance of specified type, with the specified value, attributes that are listed in the provided AttributeList, and the kids that are listed in the provided collection.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - an AttributeList (@see org.xml.sax.AttributeList)
    kids - a Collection of XmlData that attach themselves as kids to the instance being created

    BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        org.xml.sax.AttributeList attributes,
                        BasicXmlData[] kids)
    Creates an instance of specified type, with the specified value, attributes that are listed in the provided AttributeList, and the kids that are listed in the provided array of XmlData.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - an AttributeList (@see org.xml.sax.AttributeList)
    kids - an array of XmlData that attach themselves as kids to the instance being created

    BasicXmlData

    public BasicXmlData(java.lang.String type,
                        java.lang.String value,
                        java.util.Map attributes,
                        java.util.Collection kids)
    Creates an instance of specified type, with the specified value, attributes that are listed in the provided map (name -> value), and the kids that are listed in the provided collection.

    Parameters:
    type - the type of data, an arbitrary identifier string.
    value - the value of data, any string.
    attributes - a Map that maps attributes names to values
    kids - a Collection of XmlData that attach themselves as kids to the instance being created

    BasicXmlData

    public BasicXmlData(XmlData org)
    Creates an instance from another XmlData object (deep copy).

    Parameters:
    org - original XmlData

    BasicXmlData

    public BasicXmlData(java.io.InputStream in)
                 throws java.io.IOException,
                        java.lang.InstantiationException
    Creates an instance from the data read from input stream.

    Parameters:
    in - input stream
    Throws:
    java.io.IOException
    java.lang.InstantiationiException
    java.lang.InstantiationException

    BasicXmlData

    public BasicXmlData(java.io.File in)
                 throws java.io.IOException,
                        java.lang.InstantiationException
    Creates an instance from the data read from a file.

    Parameters:
    in - file containing XML data
    Throws:
    java.io.IOException
    java.lang.InstantiationiException
    java.lang.InstantiationException

    BasicXmlData

    public BasicXmlData(java.net.URL url)
                 throws java.io.IOException,
                        java.lang.InstantiationException
    Creates an instance from the data read from URL.

    Parameters:
    url - url pointing to XML data
    Throws:
    java.io.IOException
    java.lang.InstantiationiException
    java.lang.InstantiationException
    Method Detail

    debug

    public static void debug(boolean doit)

    setXmlContent

    public void setXmlContent(XmlData org)
    Sets the contents from another XmlData.

    Specified by:
    setXmlContent in interface XmlData
    Parameters:
    org - original XmlData

    getXmlContent

    public XmlData getXmlContent()
    Gets the contents of this XmlData (that is, itself).

    Specified by:
    getXmlContent in interface XmlData
    Returns:
    itself

    deepCopy

    public XmlData deepCopy()
    Deep copy of XmlData.

    Returns:
    deep copy of itself

    clone

    public java.lang.Object clone()
    Clones of XmlData, same thing as deepCopy.

    Specified by:
    clone in interface XmlData
    Returns:
    deep copy of itself

    equals

    public boolean equals(java.lang.Object o)
    Compares the specified object with this XmlData for equality. Returns true if the given object is also a XmlData, and the two XmlData represent the same data. More formally, two XmlDatas t1 and t2 represent the same data if t1.type.equals(t2.type) and t1.value.equals(t2.value) and t1.getAttributes.equals(t2.attributes()) and t1.getAllKids().equals(t2.getAllKids()).

    Specified by:
    equals in interface XmlData
    Parameters:
    o - object to be compared for equality with this XmlData.
    Returns:
    true if the specified object is equal to this XmlData.

    Example:

  • <?xml version="1.0" encoding="UTF-8"?>
     <example a1="v1" a2="v2">
       <sub1 ax="vx" ay="vy"/>
       <sub2 aa="va" ab="vb"/>
     </example>
     
    and
    <?xml version="1.0" encoding="UTF-8"?>
     <example a2="v2" a1="v1">
       <sub2 ab="vb" aa="va"/>
       <sub1 ay="vy" ax="vx"/>
     </example>
     
    are equal.

  • save

    public void save(java.lang.String filename)
              throws java.io.IOException
    Saves XmlData to a file.

    Parameters:
    filename - name of the file to write to
    Throws:
    java.io.IOException

    save

    public void save(java.io.File file)
              throws java.io.IOException
    Saves XmlData to a file.

    Parameters:
    file - file to write to
    Throws:
    java.io.IOException

    save

    public void save(java.io.OutputStream os)
              throws java.io.IOException
    Saves XmlData to an output stream.

    Parameters:
    os - output stream to write to
    Throws:
    java.io.IOException

    cleanAttributes

    public void cleanAttributes()
    cleans attributes map Just removes all the attributes except xmlns (because)

    Specified by:
    cleanAttributes in interface XmlData

    setAttribute

    public void setAttribute(java.lang.String name,
                             java.lang.String value)
    Sets an attribute value.

    Specified by:
    setAttribute in interface XmlData
    Parameters:
    name - attribute name
    value - attribute value

    setAttributes

    public void setAttributes(java.lang.String[] attributes)
    Sets attributes from an array of name-value pairs.

    Specified by:
    setAttributes in interface XmlData
    Parameters:
    attributes - a String array consisting of name-value pairs.

    setAttributes

    public void setAttributes(java.util.Map attributes)
    Sets attributes from a Map.

    Specified by:
    setAttributes in interface XmlData
    Parameters:
    attributes - a Map, name -> value.

    setAttributes

    public void setAttributes(org.xml.sax.AttributeList attributes)
    Sets attributes from an AttributeList.

    Specified by:
    setAttributes in interface XmlData
    Parameters:
    attributes - AttributeList

    getAttributes

    public java.util.Map getAttributes()
    Gets a Map of attributes.

    Specified by:
    getAttributes in interface XmlData
    Returns:
    a Map that maps attribute names to attribute values, all Strings

    getType

    public java.lang.String getType()
    Gets XmlData type, which is just a String.

    Specified by:
    getType in interface XmlData
    Returns:
    the type string

    setType

    public void setType(java.lang.String type)

    getValue

    public java.lang.String getValue()
    Gets a value of XmlData, which is just a String.

    Specified by:
    getValue in interface XmlData
    Returns:
    the value

    setValue

    public XmlData setValue(java.lang.String v)
    Sets the value of XmlData.

    Specified by:
    setValue in interface XmlData
    Parameters:
    v - new value for XmlData
    Returns:
    itself

    getAttribute

    public java.lang.String getAttribute(java.lang.String name)
    Gets the value of a specified attribute.

    Specified by:
    getAttribute in interface XmlData
    Parameters:
    name - attribute name
    Returns:
    attribute value

    getAttribute

    public java.lang.String getAttribute(java.lang.String name,
                                         java.lang.String defaultValue)
    Gets the value of a specified attribute; if there is none, returns default value.

    Specified by:
    getAttribute in interface XmlData
    Parameters:
    name - attribute name
    defaultValue -
    Returns:
    attribute value or defaultValue

    getName

    public java.lang.String getName()
    Gets the name of XmlData, which is the value of attribute "name".

    Same thing as getAttribute("name").

    Specified by:
    getName in interface XmlData
    Returns:
    the name string

    getId

    public java.lang.String getId()
    Gets the id of XmlData, which is the value of attribute "id".

    Same thing as getAttribute("id").

    Specified by:
    getId in interface XmlData
    Returns:
    the id string

    getAllKids

    public java.util.Collection getAllKids()
    Gets a collection of all kids of XmlData. Normally you would not need it; retrieving kids by type is what one normally does, but you never know...

    Specified by:
    getAllKids in interface XmlData
    Returns:
    a Collection of XmlData that contains all kids

    getKids

    public java.util.Collection getKids(java.lang.String type)
    Gets a Collection of kids of specified type.

    Specified by:
    getKids in interface XmlData
    Parameters:
    type - type of the kids to choose
    Returns:
    all kids that have specified type

    getKidCount

    public int getKidCount(java.lang.String type)
    Gets the number of kids of specified type.

    Specified by:
    getKidCount in interface XmlData
    Parameters:
    type -
    Returns:
    the number of such kids

    getKid

    public XmlData getKid(java.lang.String type)
    Gets a kid of specified type, if any.

    Specified by:
    getKid in interface XmlData
    Parameters:
    type - type of the kid to chose
    Returns:
    any kid that has this type or null if there is no such kid

    getKid

    public XmlData getKid(int position)
    Gets a kid at specified position.

    Specified by:
    getKid in interface XmlData
    Parameters:
    position - position of the kid to retrieve, starting with 0;
    Returns:
    any kid that has this type or null if there is no such kid

    getKidValue

    public java.lang.String getKidValue(java.lang.String type)
    Gets the value of a kid of specified type, if any.

    Specified by:
    getKidValue in interface XmlData
    Parameters:
    type -
    Returns:
    the value of a kid of specified type or null if there's none

    setKidValue

    public void setKidValue(java.lang.String type,
                            java.lang.String value)
    Sets the value of a kid of specified type, creating it if necessary

    Specified by:
    setKidValue in interface XmlData
    Parameters:
    type -
    value -

    getKidAttribute

    public java.lang.String getKidAttribute(java.lang.String type,
                                            java.lang.String attribute)
    Gets the value of specified attribute of a kid of specified type, if any

    Specified by:
    getKidAttribute in interface XmlData
    Parameters:
    type - String
    attribute - String
    Returns:
    String the value of the attribute of the kid of specified type, or null if ther's none

    getKid

    public XmlData getKid(java.lang.String type,
                          java.lang.String attribute,
                          java.lang.String value)
    Gets a kid of specified type that has an attribute with a specified value.

    Specified by:
    getKid in interface XmlData
    Parameters:
    type - type of the kid to choose
    attribute - attribute name
    value - attribute value
    Returns:
    such a kid, if found, or null otherwise

    getKid

    public XmlData getKid(java.lang.String type,
                          java.lang.String id)
    Gets a kid having specified type and specified id.

    Specified by:
    getKid in interface XmlData
    Parameters:
    type - kid type
    id - kid id
    Returns:
    a kid of specified type that has the value named "id" equal to id

    addKid

    public XmlData addKid(XmlData kid)
    Adds a kid to the set of kids.

    Specified by:
    addKid in interface XmlData
    Parameters:
    kid - kid to add
    Returns:
    the kid

    indexOf

    public int indexOf(XmlData kid)
    returns the position of kid in the list of kids

    Parameters:
    kid - XmlData
    Returns:
    int kid position, starting at 0; -1 if not found

    insertKid

    public XmlData insertKid(int position,
                             XmlData kid)
    Insertes a kid into the list of kids.

    Parameters:
    position - int the position where to insert the kid
    kid - kid to add
    Returns:
    the kid

    insertKid

    public XmlData insertKid(XmlData before,
                             XmlData kid)
    Insertes a kid before a specified kid.

    Parameters:
    before - the kid before which this one should be inserted
    kid - kid to add
    Returns:
    the inserted kid, or null if not inserted Does not do anything if the kid before does not exist

    removeKid

    public void removeKid(XmlData kid)
    Removes a kid from the set of kids.

    Specified by:
    removeKid in interface XmlData
    Parameters:
    kid - kid to remove

    replaceKid

    public void replaceKid(XmlData oldKid,
                           XmlData newKid)
    Replaces a kid with another.

    Specified by:
    replaceKid in interface XmlData
    Parameters:
    oldKid - kid to remove
    newKid - kid to insert in its place

    addKids

    public void addKids(java.util.Iterator kids)
    Adds all XmlData elements from given Iterator to the collection of kids, skipping elements that are not BasicXmlData.

    Parameters:
    kids - kids to add; can be null

    addKids

    public void addKids(java.util.Collection kids)
    Adds all XmlData elements from given Collection to the set of kids, skipping elements that are not XmlData.

    Specified by:
    addKids in interface XmlData
    Parameters:
    kids - kids to add; can be null

    removeKids

    public java.util.Collection removeKids(java.lang.String type)
    Removes all kids of given type.

    Specified by:
    removeKids in interface XmlData
    Parameters:
    type - type of kids to remove
    Returns:
    Collection of removed kids

    castKids

    public boolean castKids(java.lang.String type,
                            java.lang.Class clazz)
    Casts kids of specified type to a specified class (actually replacing them with the new instances).

    Specified by:
    castKids in interface XmlData
    Parameters:
    type - type of the kids to cast
    clazz - class to cast to
    Returns:
    true in case of success, false if casting failed somehow To cast, the class has to have a constructor that takes XmlData as the only argument

    cast

    public static XmlData cast(XmlData object,
                               java.util.Map typemap,
                               BasicXmlData.Policy policy)
                        throws java.lang.InstantiationException,
                               java.lang.NoSuchMethodException,
                               java.lang.reflect.InvocationTargetException,
                               java.lang.IllegalArgumentException,
                               java.lang.IllegalAccessException,
                               java.lang.ClassNotFoundException
    Casts XmlData and its kids, recursively, to specified classes, according to the typemap.

    Parameters:
    object - the objec to cast
    typemap - maps types to classes. Type is what XmlData considers a type: a string value of type property.
    policy - one of the following: Policy.THROW_ON_ERROR (causes an exception in case of any error), Policy.KEEP_ON_ERROR (keeps nodes that cannot be cast), Policy.SKIP_ON_ERROR (ignores nodes that cannot be cast).
    Returns:
    a new instance of the class corresponding, via typemap, to the type of data.
    Throws:
    InstantiationException
    NoSuchMethodException
    InvocationTargetException
    IllegalArgumentException
    IllegalAccessException - Exception are thrown only if policy is Policy.THROW_ON_ERROR
    java.lang.ClassNotFoundException

    castToPackage

    public static XmlData castToPackage(XmlData object,
                                        java.lang.String packageName,
                                        BasicXmlData.Policy policy)
                                 throws java.lang.InstantiationException,
                                        java.lang.NoSuchMethodException,
                                        java.lang.reflect.InvocationTargetException,
                                        java.lang.IllegalArgumentException,
                                        java.lang.IllegalAccessException,
                                        java.lang.ClassNotFoundException
    Casts XmlData and its kids, recursively, to classes in specified package.

    Parameters:
    object - the objec to cast
    packageName - the name of the package for casting. XmlData types are mapped to classes with the same name inside specified package. packageName can be null: then it is ignored.
    policy - one of the following: Policy.THROW_ON_ERROR (causes an exception in case of any error), Policy.KEEP_ON_ERROR (keeps nodes that cannot be cast), Policy.SKIP_ON_ERROR (ignores nodes that cannot be cast).
    Returns:
    a new instance of the class corresponding, via typemap, to the type of data.
    Throws:
    InstantiationException
    NoSuchMethodException
    InvocationTargetException
    IllegalArgumentException
    IllegalAccessException - Exception are thrown only if policy is Policy.THROW_ON_ERROR
    java.lang.ClassNotFoundException

    cast

    public XmlData cast(java.util.Map typemap,
                        BasicXmlData.Policy policy)
                 throws java.lang.InstantiationException,
                        java.lang.NoSuchMethodException,
                        java.lang.reflect.InvocationTargetException,
                        java.lang.IllegalArgumentException,
                        java.lang.IllegalAccessException,
                        java.lang.ClassNotFoundException
    Casts XmlData and its kids, recursively, to specified classes, according to the typemap.

    Parameters:
    typemap - maps types to classes. Type is what XmlData considers a type: a string value of type property.
    policy - one of the following: Policy.THROW_ON_ERROR (causes an exception in case of any error), Policy.KEEP_ON_ERROR (keeps nodes that cannot be cast), Policy.SKIP_ON_ERROR (ignores nodes that cannot be cast).
    Returns:
    a new instance of the class corresponding, via typemap, to the type of data.
    Throws:
    InstantiationException
    NoSuchMethodException
    InvocationTargetException
    IllegalArgumentException
    IllegalAccessException - Exception are thrown only if policy is Policy.THROW_ON_ERROR
    java.lang.ClassNotFoundException

    castToPackage

    public XmlData castToPackage(java.lang.String packageName,
                                 BasicXmlData.Policy policy)
                          throws java.lang.InstantiationException,
                                 java.lang.NoSuchMethodException,
                                 java.lang.reflect.InvocationTargetException,
                                 java.lang.IllegalArgumentException,
                                 java.lang.IllegalAccessException,
                                 java.lang.ClassNotFoundException
    Casts XmlData and its kids, recursively, to classes in specified package.

    Parameters:
    packageName - the name of the package for casting. XmlData types are mapped to classes with the same name inside specified package. packageName can be null: then it is ignored.
    policy - one of the following: Policy.THROW_ON_ERROR (causes an exception in case of any error), Policy.KEEP_ON_ERROR (keeps nodes that cannot be cast), Policy.SKIP_ON_ERROR (ignores nodes that cannot be cast).
    Returns:
    a new instance of the class which name is the type of the data
    Throws:
    InstantiationException
    NoSuchMethodException
    InvocationTargetException
    IllegalArgumentException
    IllegalAccessException
    ClassNotFoundException - Exception are thrown only if policy is Policy.THROW_ON_ERROR

    trim

    public void trim()
    Tries to trim the set of kids, so that there are no spare slots left.

    This operation does not go deep, since it is assumed that you apply it in the process of building XmlData, and the kids are already trimmed.


    satisfies

    public boolean satisfies(XmlData.Condition condition)
    Checks whether this XmlData node satisfies given condition.

    Parameters:
    condition -
    Returns:
    true if it does satisfy the condition, false otherwise. Any node satisfies an empty condition. This can look arguable, but you have to believe it, it comes from logic.

    satisfies

    public boolean satisfies(java.lang.String expression)
    Checks whether this XmlData node satisfies given expression (presented as string).

    the string is parsed first, and then the node is checked against the parsed expression.

    Parameters:
    expression -
    Returns:
    true if it does satisfy the expression, false otherwise. Note that if the expression is void or invalid, any node satisfies this expression.

    selectTree

    public XmlData selectTree(XmlData.Condition condition)
    Selects a subtree from XmlData, that is, the tree of those nodes that satisfy a condition.

    The node is included into the resulting tree iff it satisfies the condition; this filtering operation is applied recursively to its kids.

    Specified by:
    selectTree in interface XmlData
    Parameters:
    condition -
    Returns:
    XmlData containing only nodes that satisfy the filter

    selectTree

    public XmlData selectTree(java.lang.String expression)
    Selects a subtree from XmlData, that is, the tree of those nodes that satisfy a condition.

    The node is included into the resulting tree iff it satisfies the condition; this filtering operation is applied recursively to its kids.

    Parameters:
    expression - the string that contains a filtering expression
    Returns:
    XmlData containing only nodes that satisfy the expression

    Example:

  • for the following data:
    <?xml version="1.0" encoding="UTF-8"?>
     <example a1="v1" a2="v2">
       <sub1 ax="vx" ay="vy"/>
       <sub2 aa="va" ab="vb"/>
     </example>
     
    selectTree("ax != \"vx\"") and
    selectTree("a1 == \"v1\" || ab == \"vb\"")
    will select the same subtree,
    <?xml version="1.0" encoding="UTF-8"?>
     <example a1="v1" a2="v2">
       <sub2 aa="va" ab="vb"/>
     </example>
     

  • getKidTypes

    public java.util.Collection getKidTypes()
    Gets a collection of kid types

    Returns:
    Collection all present kid types (as strings)