Package overit.geocall.xml
Class XMLTree
java.lang.Object
overit.geocall.xml.XMLTree
- All Implemented Interfaces:
Serializable,Getter
The class implements a tree that defines an XML object
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassDefines an iterator over the parent elements of the XMLTree -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefines that the element is a CDATA element, used to insert HTML code into XMLstatic final intDefines that the element is a node elementstatic final intDefines that the element is a text element; a text element can exists only as child of a node element -
Constructor Summary
ConstructorsConstructorDescriptionXMLTree(InputStream is) Creates an XMLTree starting from anInputStream.XMLTree(InputStream is, XMLMode xm) Creates an XMLTree starting from anInputStream.Creates an XMLTree starting from aReader.Creates an XMLTree starting from aReader.Creates an XMLTree with the only root element, with namenameCreates an XMLTree of the desired type.Creates an XMLTree starting from an XML resource and a Class objectCreates an XMLTree which will have a root element with namenameand a child XMLTree ofTYPE_TEXTtype.Creates an XMLTree starting from an XML tree parsed in anElement, with the org.w3c.dom implementationCreates a new XMLTree by cloning an existing one. -
Method Summary
Modifier and TypeMethodDescriptionAdds an XMLTree as a child in the i-th position.Adds an XMLTree as a childReturns an Iterator over the attributes entry keys of the root elementReturns an Iterator over the childs of the XMLTreechildrenIterator(String tagName) Returns aFilteredIteratorover the childs of the XMLTreebooleancontainsKey(String key) Checks if thePropertyGetterof attributes of the root of the XMLTree contains an entry with the key passed as parametercopyNode()Copies the single current node and its attributes.protected voidReturns theDocumentthat represents the entire XMLprotected NodecreateElement(XMLTree xmlt, Document doc) Deletes the child of the XMLTree with the desired name passed as parameterExtracts from thePropertyGetterof attributes of the root of the XMLTree the object that corresponds to the key passed as parameters.Extracts from thePropertyGetterof attributes of the root of the XMLTree the object that corresponds to the key passed as parameters.getAbsoluteChild(String path) Returns a child (or grandson) of the XMLTree, searching it by the pathgetAttribute(String key) Returns the current node attribute given its key.Returns the list of attributes of the root element of the XMLTreebooleangetboolean(String key, boolean def) Extracts a boolean attribute from the XMLTreegetBoolean(String key, Boolean def) Extracts a Boolean attribute from the XMLTreegetChild(int i) Returns the child of the XMLTree at the desired i-th positionReturns the child of the XMLTree with the desired name passed as parametergetChildNodeByAttribute(String attr, Object value) Gets the specific node related to the attribute and the value passed by paramter.
if there is no nodes that has that attribute or that value for the attribute, null will be returned.Returns the children of the XMLTreeintReturns the number of the childs of the XMLTreegetDate(String key, Date def, SimpleDateFormat sdf) Extracts a Date attribute from the XMLTree.getDeepChild(String path) Returns a child (or grandson) of the XMLTree, searching it by the pathdoubleExtracts a double attribute from the XMLTreeExtracts a Double attribute from the XMLTreefloatExtracts a float attribute from the XMLTreeExtracts a Float attribute from the XMLTreeintExtracts an int attribute from the XMLTreegetInteger(String key, Integer def) Extracts an Integer attribute from the XMLTreelongExtracts a long attribute from the XMLTreeExtracts a Long attribute from the XMLTreegetName()Returns the root nameReturns the parent node of the XMLTreeintReturns the position in the parent children listgetRoot()Returns a root element of a given tree by traversing upward the parent nodes.Extracts a String attribute from the XMLTreeReturns the text of aTYPE_TEXTnode.intgetType()Returns the type of the XMLTreegetValue()Returns the value of theTYPE_TEXTorCDATA_SECTIONnodebooleanhasAttribute(String key) Returns true if the current node has the specified attribute.booleanReturns true if the XMLTree has children nodes.booleanhasName()Return true if the current node has a valid name, that means not null and not blank.booleanReturns true if the XMLTree has a parent node.booleanReturns true if the current node is equal to the given one without considering parents or children nodes.booleanisRoot()Returns true if the XMLTree is a root node.Extracts an Iterator used to iterate over all the attributes of the XMLTreeprotected voidload(InputSource is, XMLMode xm) voidoutput(OutputStream os, int indent, String encoding) Writes the XMLTree to an OutputStreamvoidoutput(OutputStream os, int indent, String encoding, boolean preserveSpace) Deprecated.voidoutput(OutputStream os, int indent, Charset charset) Writes the XMLTree to an OutputStreamvoidWrites the XMLTree to a WritervoidDeprecated.voidWrites the XMLTree to an OutputStreamReturns aXMLTree.ParentIteratorover the parents of the XMLTreevoidputAttribute(String key, Object value) Adds a new attribute for the current node.voidremoveAttribute(String key) Removes an attribute from the attributes list of the root of the XMLTreesearchDeepChild(String name) Returns a child (or grandson) of the XMLTree, searching it by the namevoidsetAttribute(String key, String value) Adds an attribute to the attributes list of the root of the XMLTreevoidsetAttributes(Map m) Sets the attributes of the root element of the XMLTree starting from the Map passed as parametervoidsetChildren(ArrayList<XMLTree> children) Sets the children of the XMLTreevoidSets the name of the root element of the XMLTreevoidSets the value text of the XMLTreetoString()Returns the string representation of the XMLTreevoidtranslateLabel(Teacher t, String attribute) Translates all the string values in the XMLTree that corresponds to the XMLTree attribute name passed as parameter
-
Field Details
-
TYPE_NODE
public static final int TYPE_NODEDefines that the element is a node element- See Also:
-
TYPE_TEXT
public static final int TYPE_TEXTDefines that the element is a text element; a text element can exists only as child of a node element- See Also:
-
CDATA_SECTION
public static final int CDATA_SECTIONDefines that the element is a CDATA element, used to insert HTML code into XML- See Also:
-
-
Constructor Details
-
XMLTree
Creates an XMLTree starting from an XML resource and a Class object- Parameters:
resource- Name of the XML resourcec- The Class object. Usually the method that calls this constructor usesObject.getClass(). In this way the constructor can correctly use the ClassLoader and retrieve the resource.- Throws:
XMLException- Exception that can be raised reading the XML resource (it groupsIOException,SAXException,ParserConfigurationException)
-
XMLTree
Creates an XMLTree with the only root element, with namename- Parameters:
name- The name of the root element
-
XMLTree
Creates an XMLTree of the desired type.- Parameters:
value- The name of the root if the type isTYPE_NODEor the text value if the type isTYPE_TEXTorCDATA_SECTIONtype- One ofTYPE_NODE,TYPE_TEXT,CDATA_SECTION
-
XMLTree
Creates an XMLTree which will have a root element with namenameand a child XMLTree ofTYPE_TEXTtype.- Parameters:
name- The name of the root elementtext- The value of theTYPE_TEXTelement
-
XMLTree
Creates an XMLTree starting from aReader. This constructor is typically used when we start from a String (for example in this way:XMLTree xml = new XMLTree(new StringReader())). It must be avoided if we start from a file or a source with undefined charset; in those cases we must useXMLTree(InputStream, XMLMode)- Parameters:
r- The reader that point to the XML filexm- TheXMLModethat defines the reading type- Throws:
XMLException- Exception that can be raised reading the XML resource (it groupsIOException,SAXException,ParserConfigurationException)
-
XMLTree
Creates an XMLTree starting from aReader. This constructor is typically used when we start from a String (for example in this way:XMLTree xml = new XMLTree(new StringReader())). It must be avoided if we start from a file or a source with undefined charset; in those cases we must useXMLTree(InputStream, XMLMode)- Parameters:
r- The reader that point to the XML file- Throws:
XMLException- Exception that can be raised reading the XML resource (it groupsIOException,SAXException,ParserConfigurationException)
-
XMLTree
Creates an XMLTree starting from anInputStream. This constructor must be used when the source has an undefined charset, for example for the Files- Parameters:
is- The InputStream from which reading the XML filexm- TheXMLModethat defines the reading type- Throws:
XMLException- Exception that can be raised reading the XML resource (it groupsIOException,SAXException,ParserConfigurationException)
-
XMLTree
Creates an XMLTree starting from anInputStream. This constructor must be used when the source has an undefined charset, for example for the Files- Parameters:
is- The InputStream from which reading the XML file- Throws:
XMLException- Exception that can be raised reading the XML resource (it groupsIOException,SAXException,ParserConfigurationException)
-
XMLTree
Creates a new XMLTree by cloning an existing one.- Parameters:
other- the input tree to be cloned
-
XMLTree
Creates an XMLTree starting from an XML tree parsed in anElement, with the org.w3c.dom implementation- Parameters:
el- The root node of the org.w3c.dom implementation
-
-
Method Details
-
load
- Throws:
XMLException
-
create
-
addChild
Adds an XMLTree as a child- Parameters:
xmlt- The new XMLTree child to add- Returns:
- The XMLTree with the new child
-
addChild
Adds an XMLTree as a child in the i-th position.- Parameters:
i- The positionxmlt- The new XMLTree child to add- Returns:
- The XMLTree with the new child in the i-th position
-
getRoot
Returns a root element of a given tree by traversing upward the parent nodes.- Returns:
- the root node of the current tree
-
hasName
public boolean hasName()Return true if the current node has a valid name, that means not null and not blank.- Returns:
- true if the node has a not blank name specified
-
copyNode
Copies the single current node and its attributes. It doesn't copy the children nodes.- Returns:
- the single node copy
-
isEqualTo
Returns true if the current node is equal to the given one without considering parents or children nodes. It considers only the name and the attributes of the current node with the given one.- Parameters:
node- the node object of comparison- Returns:
- true if the nodes are equals
-
setAttribute
Adds an attribute to the attributes list of the root of the XMLTree- Parameters:
key- The name of the attributevalue- The value of the attribute
-
removeAttribute
Removes an attribute from the attributes list of the root of the XMLTree- Parameters:
key- The name of the attribute to remove- Returns:
- the previous value associated with key, or null if there was no mapping for key
-
hasAttribute
Returns true if the current node has the specified attribute.- Parameters:
key- the key of the attribute- Returns:
- true if the current node has the specified attribute
-
getParent
Returns the parent node of the XMLTree- Returns:
- The parent node or
null
-
hasParent
public boolean hasParent()Returns true if the XMLTree has a parent node.- Returns:
- true if the XMLTree has a parent node
-
isRoot
public boolean isRoot()Returns true if the XMLTree is a root node.- Returns:
- true if the XMLTree is a root node
-
getChildren
Returns the children of the XMLTree- Returns:
- The ArrayList of the childs of the XMLTree
-
setChildren
Sets the children of the XMLTree- Parameters:
children- The ArrayList of the childs to set
-
getChildrenSize
public int getChildrenSize()Returns the number of the childs of the XMLTree- Returns:
- The size of the ArrayList of childs of the XMLTree
-
hasChildren
public boolean hasChildren()Returns true if the XMLTree has children nodes.- Returns:
- true if the XMLTree has children nodes
-
getChild
Returns the child of the XMLTree with the desired name passed as parameter- Parameters:
name- The name of the desired child- Returns:
- The child with the name
nameornullif there is no such child
-
delChild
Deletes the child of the XMLTree with the desired name passed as parameter- Parameters:
name- The name of the desired child to delete- Returns:
- The XMLTree without the child, or
nullif there is no such child
-
getChild
Returns the child of the XMLTree at the desired i-th position- Parameters:
i- The position- Returns:
- The child in the i-th poisition, or
nullif there is no child at the desired position
-
getAttributes
Returns the list of attributes of the root element of the XMLTree- Returns:
- The
PropertyGetterof the attributes of the root element of the XMLTree
-
setAttributes
Sets the attributes of the root element of the XMLTree starting from the Map passed as parameter- Parameters:
m- The map that contains the attributes and values
-
getAttribute
Returns the current node attribute given its key.- Parameters:
key- the key for the attribute- Returns:
- the attribute value for the current node
-
putAttribute
Adds a new attribute for the current node.- Parameters:
key- the key for the attributevalue- the value of the attribute
-
getName
Returns the root name- Returns:
- The name of the root element of the XMLTree
-
getValue
Returns the value of theTYPE_TEXTorCDATA_SECTIONnode- Returns:
- The string text value
-
getText
Returns the text of aTYPE_TEXTnode.- Parameters:
def- The default string returned if the XMLTree has no childs or if the value text isnull- Returns:
- The value text of the text node of the XMLTree; if the tree has no childs, or if the value text is
nullit will return the default string passed as parameter. If the tree has more than one child, or if the node is not a text node, the method will raise a newXMLException
-
setName
Sets the name of the root element of the XMLTree- Parameters:
name- The string to set as name
-
setValue
Sets the value text of the XMLTree- Parameters:
s- The string to set as value
-
getDeepChild
Returns a child (or grandson) of the XMLTree, searching it by the path- Parameters:
path- The path used to retrieve the child / grandson node. The string must be composed by the name of the nodes (separated by empty spaces) that must be traveled to reach the desired node. It must contains the name of the root and the name of the desired element.- Returns:
- The XMLTree if exists,
nullotherwise
-
getAbsoluteChild
Returns a child (or grandson) of the XMLTree, searching it by the path- Parameters:
path- The path used to retrieve the child / grandson node. The string must be composed by the position of the nodes (separated by dots) that must be traveled to reach the desired node.- Returns:
- The XMLTree if exists,
nullotherwise
-
searchDeepChild
Returns a child (or grandson) of the XMLTree, searching it by the name- Parameters:
name- The name used to retrieve the child / grandson node.- Returns:
- The XMLTree if exists,
nullotherwise
-
getPosition
public int getPosition()Returns the position in the parent children list- Returns:
- the index of the current gtree inside the parent children list
-
qualify
-
attributesIterator
Returns an Iterator over the attributes entry keys of the root element- Returns:
- The iterator over the attribute's keys
-
childrenIterator
Returns an Iterator over the childs of the XMLTree- Returns:
- The iterator over the childs
-
childrenIterator
Returns aFilteredIteratorover the childs of the XMLTree- Parameters:
tagName- The name used to determine if the element must be iterated or not- Returns:
- An iterator over the childs of the XMLTree which name is the one passed as parameter
-
parentIterator
Returns aXMLTree.ParentIteratorover the parents of the XMLTree- Returns:
- The iterator over the parents
-
getBoolean
Extracts a Boolean attribute from the XMLTree- Specified by:
getBooleanin interfaceGetter- Parameters:
key- Name of the attribute object to extractdef- Default value returned when there isn't a correct object that corresponds to thekey- Returns:
- The object of the specified type, or the default one
-
getDate
Extracts a Date attribute from the XMLTree. The Date must meet the characteristics of the specified SimpleDateFormat- Specified by:
getDatein interfaceGetter- Parameters:
key- Name of the attribute object to extractdef- Default value returned when there isn't a correct object that corresponds to thekeysdf- The SimpleDateFormat that defines the format of the date to extract- Returns:
- The object of the specified type, or the default one
-
getDouble
Extracts a Double attribute from the XMLTree -
getFloat
Extracts a Float attribute from the XMLTree -
getInteger
Extracts an Integer attribute from the XMLTree- Specified by:
getIntegerin interfaceGetter- Parameters:
key- Name of the attribute object to extractdef- Default value returned when there isn't a correct object that corresponds to thekey- Returns:
- The object of the specified type, or the default one
-
getlong
Extracts a long attribute from the XMLTree -
getLong
Extracts a Long attribute from the XMLTree -
getString
Extracts a String attribute from the XMLTree -
getboolean
Extracts a boolean attribute from the XMLTree- Specified by:
getbooleanin interfaceGetter- Parameters:
key- Name of the attribute object to extractdef- Default value returned when there isn't a correct object that corresponds to thekey- Returns:
- The object of the specified type, or the default one
-
getdouble
Extracts a double attribute from the XMLTree -
getfloat
Extracts a float attribute from the XMLTree -
getint
Extracts an int attribute from the XMLTree -
keyIterator
Extracts an Iterator used to iterate over all the attributes of the XMLTree- Specified by:
keyIteratorin interfaceGetter- Returns:
- The iterator on the elements of the list of attributes
-
get
Extracts from thePropertyGetterof attributes of the root of the XMLTree the object that corresponds to the key passed as parameters. In this case there isn't a default value. -
get
Extracts from thePropertyGetterof attributes of the root of the XMLTree the object that corresponds to the key passed as parameters. If the object isnull, or if the list of attributes contains no mapping for the key, the default object is returned -
containsKey
Checks if thePropertyGetterof attributes of the root of the XMLTree contains an entry with the key passed as parameter- Specified by:
containsKeyin interfaceGetter- Parameters:
key- The name of the key to search- Returns:
trueif there is an entry (attribute) with the key passed as parameter,falseotherwise
-
getType
public int getType()Returns the type of the XMLTree- Returns:
- One of
TYPE_NODE,TYPE_TEXT,CDATA_SECTION
-
output
@Deprecated public void output(OutputStream os, int indent, String encoding, boolean preserveSpace) throws XMLException Deprecated.- Parameters:
os- The OutputStream used to write the character streamindent- Indent amount. Use the value -1 for non indenting.encoding- The encodingpreserveSpace- does nothing- Throws:
XMLException- The method raise an XMLException if there are errors during the output operation
-
output
@Deprecated public void output(Writer w, int indent, String encoding, boolean preserveSpace) throws XMLException Deprecated.- Parameters:
w- The Writer used to write the character streamindent- Indent amount. Use the value -1 for non indenting.encoding- The encodingpreserveSpace- does nothing- Throws:
XMLException- The method raise an XMLException if there are errors during the output operation
-
output
Writes the XMLTree to an OutputStream- Parameters:
os- The OutputStream used to write the character streamindent- Indent amount. Use the value -1 for non indenting.encoding- The encoding- Throws:
XMLException- The method raise an XMLException if there are errors during the output operation
-
output
Writes the XMLTree to an OutputStream- Parameters:
os- The OutputStream used to write the character streamindent- Indent amount. Use the value -1 for non indenting.charset- the charset used to set the output encoding- Throws:
XMLException- The method raise an XMLException if there are errors during the output operation
-
output
Writes the XMLTree to a Writer- Parameters:
w- The Writer used to write the character streamindent- Indent amount. Use the value -1 for non indenting.encoding- The encoding- Throws:
XMLException- The method raise an XMLException if there are errors during the output operation
-
output
Writes the XMLTree to an OutputStream- Parameters:
w- The Writer used to write the character streamindent- Indent amount. Use the value -1 for non indenting.charset- the charset used to set the output encoding- Throws:
XMLException- The method raise an XMLException if there are errors during the output operation
-
toString
-
toString
Returns the string representation of the XMLTree- Parameters:
indent- Indent amount. Use the value -1 for non indenting.encoding- The encoding- Returns:
- The string that represents the XMLTree
-
createDom
Returns theDocumentthat represents the entire XML- Returns:
- The
Documentthat represents the entire XML - Throws:
ParserConfigurationException- The method raise a ParserConfigurationException if there are errors during the operation
-
createElement
-
translateLabel
Translates all the string values in the XMLTree that corresponds to the XMLTree attribute name passed as parameter- Parameters:
t- TheTeacherthat will manage the translationattribute- The name of the attribute which values will be translated
-
getChildNodeByAttribute
Gets the specific node related to the attribute and the value passed by paramter.
if there is no nodes that has that attribute or that value for the attribute, null will be returned.- Parameters:
attr- The attribute for which research the node inside the tree.value- The attribute's value for which research the node inside the tree.- Returns:
- The specific node that has the attirbute and value passed by parameter, null otherwise.
-
output(OutputStream, int, String)