|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.myjavatools.xml.BasicXmlData
| 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.util.Map attributes,
java.util.Map byType)
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 Map, type->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 (use clone() to get a real 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. |
XmlData |
cast(java.util.Map typemap,
BasicXmlData.Policy policy)
Casts XmlData and its kids, recursively, to specified classes, according to the typemap. |
XmlData |
cast(java.lang.String packageName,
BasicXmlData.Policy policy)
Casts XmlData and its kids, recursively, to classes in specified package. |
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). |
java.lang.Object |
clone()
Clones of XmlData, same thing as deepCopy. |
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(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. |
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.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). |
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. |
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. |
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 |
public static final java.lang.Class XmlDataClass
| Constructor Detail |
public BasicXmlData()
Creates an empty instance.
public BasicXmlData(java.lang.String type)
type - the type of data, an arbitrary identifier string.
public BasicXmlData(java.lang.String type,
java.lang.String value)
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)
<?xml version="1.0" encoding="UTF-8"?> <example>This is an example</example>
public BasicXmlData(java.lang.String type,
java.lang.String value,
java.util.Collection kids)
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)
<?xml version="1.0" encoding="UTF-8"?> <example> <subexample>This is a kid of example</subexample>This is an example</example>
public BasicXmlData(java.lang.String type,
java.lang.String value,
BasicXmlData[] kids)
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)
<?xml version="1.0" encoding="UTF-8"?> <example> <subexample>This is a kid of example</subexample>This is an example</example>
public BasicXmlData(java.lang.String type,
java.lang.String value,
java.lang.String[] attributes)
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)
<?xml version="1.0" encoding="UTF-8"?> <example id="0123dx" name="Whittman">This is an example</example>
public BasicXmlData(java.lang.String type,
java.lang.String value,
java.lang.String[] attributes,
java.util.Collection kids)
type - the type of data, an arbitrary identifier string.value - the value of data, any string.attributes - an String array of name-value pairskids - 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)
<?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>
public BasicXmlData(java.lang.String type,
java.lang.String value,
java.lang.String[] attributes,
BasicXmlData[] kids)
type - the type of data, an arbitrary identifier string.value - the value of data, any string.attributes - an String array of name-value pairskids - 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)
<?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>
public BasicXmlData(java.lang.String type,
java.lang.String value,
org.xml.sax.AttributeList attributes)
type - the type of data, an arbitrary identifier string.value - the value of data, any string.attributes - an AttributeList (@see org.xml.sax.AttributeList)
public BasicXmlData(java.lang.String type,
java.lang.String value,
org.xml.sax.AttributeList attributes,
java.util.Collection kids)
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
public BasicXmlData(java.lang.String type,
java.lang.String value,
org.xml.sax.AttributeList attributes,
BasicXmlData[] kids)
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
public BasicXmlData(java.lang.String type,
java.lang.String value,
java.util.Map attributes,
java.util.Collection kids)
type - the type of data, an arbitrary identifier string.value - the value of data, any string.attributes - a Map that maps attributes names to valueskids - a Collection of XmlData that attach themselves as kids to the instance being created
public BasicXmlData(java.lang.String type,
java.lang.String value,
java.util.Map attributes,
java.util.Map byType)
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.byType - a Map that maps types to collections of XmlData kids.
Example:
Map attr = new HashMap();
Map byType = new HashMap();
byType.put("subexample", Arrays.asList(new Object[] {new BasicXmlData("subexample", "This is a kid of example")}));
byType.put("feature", Arrays.asList(new Object[] {new BasicXmlData("feature", null, new String[] {"name", "arity", "value", "3"})}));
attr.put("id", "008");
attr.put("name", "Dumb");
new BasicXmlData("example", "This is an example", attr, byType).save(filename)
will result in the following output:
<?xml version="1.0" encoding="UTF-8"?> <example id="008" name="Dumb"> <subexample>This is a kid of example</subexample> <feature name="arity" value="3">This is an example</example>
public BasicXmlData(XmlData org)
org - original XmlData
public BasicXmlData(java.io.InputStream in)
throws java.io.IOException,
java.lang.InstantiationException
in - input stream
java.io.IOException
java.lang.InstantiationiException
java.lang.InstantiationException
public BasicXmlData(java.io.File in)
throws java.io.IOException,
java.lang.InstantiationException
in - file containing XML data
java.io.IOException
java.lang.InstantiationiException
java.lang.InstantiationException
public BasicXmlData(java.net.URL url)
throws java.io.IOException,
java.lang.InstantiationException
url - url pointing to XML data
java.io.IOException
java.lang.InstantiationiException
java.lang.InstantiationException| Method Detail |
public void setXmlContent(XmlData org)
setXmlContent in interface XmlDataorg - original XmlDatapublic XmlData getXmlContent()
getXmlContent in interface XmlDatapublic XmlData deepCopy()
public java.lang.Object clone()
clone in interface XmlDatapublic boolean equals(java.lang.Object o)
o - object to be compared for equality with 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.
public void save(java.lang.String filename)
throws java.io.IOException
filename - name of the file to write to
java.io.IOException
public void save(java.io.File file)
throws java.io.IOException
file - file to write to
java.io.IOException
public void save(java.io.OutputStream os)
throws java.io.IOException
os - output stream to write to
java.io.IOException
public void setAttribute(java.lang.String name,
java.lang.String value)
setAttribute in interface XmlDataname - attribute namevalue - attribute valuepublic void setAttributes(java.lang.String[] attributes)
setAttributes in interface XmlDataattributes - a String array consisting of name-value pairs.public void setAttributes(java.util.Map attributes)
setAttributes in interface XmlDataattributes - a Map, name -> value.public void setAttributes(org.xml.sax.AttributeList attributes)
setAttributes in interface XmlDataattributes - AttributeListpublic java.util.Map getAttributes()
getAttributes in interface XmlDatapublic java.lang.String getType()
getType in interface XmlDatapublic java.lang.String getValue()
getValue in interface XmlDatapublic XmlData setValue(java.lang.String v)
setValue in interface XmlDatav - new value for XmlData
public java.lang.String getAttribute(java.lang.String name)
getAttribute in interface XmlDataname - attribute name
public java.lang.String getAttribute(java.lang.String name,
java.lang.String defaultValue)
getAttribute in interface XmlDataname - attribute namedefaultValue -
public java.lang.String getName()
Same thing as getAttribute("name").
getName in interface XmlDatapublic java.lang.String getId()
Same thing as getAttribute("id").
getId in interface XmlDatapublic java.util.Collection getAllKids()
getAllKids in interface XmlDatapublic java.util.Collection getKids(java.lang.String type)
getKids in interface XmlDatatype - type of the kids to choose
public int getKidCount(java.lang.String type)
getKidCount in interface XmlDatatype -
public XmlData getKid(java.lang.String type)
getKid in interface XmlDatatype - type of the kid to chose
public java.lang.String getKidValue(java.lang.String type)
getKidValue in interface XmlDatatype -
public XmlData getKid(java.lang.String type,
java.lang.String attribute,
java.lang.String value)
getKid in interface XmlDatatype - type of the kid to chooseattribute - attribute namevalue - attribute value
public XmlData getKid(java.lang.String type,
java.lang.String id)
getKid in interface XmlDatatype - kid typeid - kid id
public XmlData addKid(XmlData kid)
addKid in interface XmlDatakid - kid to add
public void removeKid(XmlData kid)
removeKid in interface XmlDatakid - kid to removepublic void addKids(java.util.Collection kids)
addKids in interface XmlDatakids - public java.util.Collection removeKids(java.lang.String type)
removeKids in interface XmlDatatype - type of kids to remove
public boolean castKids(java.lang.String type,
java.lang.Class clazz)
type - type of the kids to castclazz - class to cast to
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
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).
java.lang.InstantiationException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException - Exception are thrown only if policy is Policy.THROW_ON_ERROR
public XmlData cast(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
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).
java.lang.InstantiationException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException - Exception are thrown only if policy is Policy.THROW_ON_ERRORpublic void trim()
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.
public boolean satisfies(XmlData.Condition condition)
condition -
public boolean satisfies(java.lang.String expression)
the string is parsed first, and then the node is checked against the parsed expression.
expression -
public XmlData selectTree(XmlData.Condition condition)
The node is included into the resulting tree iff it satisfies the condition; this filtering operation is applied recursively to its kids.
selectTree in interface XmlDatacondition -
public XmlData selectTree(java.lang.String expression)
The node is included into the resulting tree iff it satisfies the condition; this filtering operation is applied recursively to its kids.
expression - the string that contains a filtering expression
Example:
<?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\"")
<?xml version="1.0" encoding="UTF-8"?> <example a1="v1" a2="v2"> <sub2 aa="va" ab="vb"/> </example>
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||