Class XMLTree

java.lang.Object
overit.geocall.xml.XMLTree
All Implemented Interfaces:
Serializable, Getter

public class XMLTree extends Object implements Getter, Serializable
The class implements a tree that defines an XML object
See Also:
  • Field Details

    • TYPE_NODE

      public static final int TYPE_NODE
      Defines that the element is a node element
      See Also:
    • TYPE_TEXT

      public static final int TYPE_TEXT
      Defines 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_SECTION
      Defines that the element is a CDATA element, used to insert HTML code into XML
      See Also:
  • Constructor Details

    • XMLTree

      public XMLTree(String resource, Class c) throws XMLException
      Creates an XMLTree starting from an XML resource and a Class object
      Parameters:
      resource - Name of the XML resource
      c - The Class object. Usually the method that calls this constructor uses Object.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 groups IOException, SAXException, ParserConfigurationException)
    • XMLTree

      public XMLTree(String name)
      Creates an XMLTree with the only root element, with name name
      Parameters:
      name - The name of the root element
    • XMLTree

      public XMLTree(String value, int type)
      Creates an XMLTree of the desired type.
      Parameters:
      value - The name of the root if the type is TYPE_NODE or the text value if the type is TYPE_TEXT or CDATA_SECTION
      type - One of TYPE_NODE, TYPE_TEXT, CDATA_SECTION
    • XMLTree

      public XMLTree(String name, String text)
      Creates an XMLTree which will have a root element with name name and a child XMLTree of TYPE_TEXT type.
      Parameters:
      name - The name of the root element
      text - The value of the TYPE_TEXT element
    • XMLTree

      public XMLTree(Reader r, XMLMode xm) throws XMLException
      Creates an XMLTree starting from a Reader. 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 use XMLTree(InputStream, XMLMode)
      Parameters:
      r - The reader that point to the XML file
      xm - The XMLMode that defines the reading type
      Throws:
      XMLException - Exception that can be raised reading the XML resource (it groups IOException, SAXException, ParserConfigurationException)
    • XMLTree

      public XMLTree(Reader r) throws XMLException
      Creates an XMLTree starting from a Reader. 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 use XMLTree(InputStream, XMLMode)
      Parameters:
      r - The reader that point to the XML file
      Throws:
      XMLException - Exception that can be raised reading the XML resource (it groups IOException, SAXException, ParserConfigurationException)
    • XMLTree

      public XMLTree(InputStream is, XMLMode xm) throws XMLException
      Creates an XMLTree starting from an InputStream. 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
      xm - The XMLMode that defines the reading type
      Throws:
      XMLException - Exception that can be raised reading the XML resource (it groups IOException, SAXException, ParserConfigurationException)
    • XMLTree

      public XMLTree(InputStream is) throws XMLException
      Creates an XMLTree starting from an InputStream. 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 groups IOException, SAXException, ParserConfigurationException)
    • XMLTree

      public XMLTree(XMLTree other)
      Creates a new XMLTree by cloning an existing one.
      Parameters:
      other - the input tree to be cloned
    • XMLTree

      public XMLTree(Element el)
      Creates an XMLTree starting from an XML tree parsed in an Element, with the org.w3c.dom implementation
      Parameters:
      el - The root node of the org.w3c.dom implementation
  • Method Details

    • load

      protected void load(InputSource is, XMLMode xm) throws XMLException
      Throws:
      XMLException
    • create

      protected void create(Element el)
    • addChild

      public XMLTree addChild(XMLTree xmlt)
      Adds an XMLTree as a child
      Parameters:
      xmlt - The new XMLTree child to add
      Returns:
      The XMLTree with the new child
    • addChild

      public XMLTree addChild(Integer i, XMLTree xmlt)
      Adds an XMLTree as a child in the i-th position.
      Parameters:
      i - The position
      xmlt - The new XMLTree child to add
      Returns:
      The XMLTree with the new child in the i-th position
    • getRoot

      public XMLTree 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

      public XMLTree copyNode()
      Copies the single current node and its attributes. It doesn't copy the children nodes.
      Returns:
      the single node copy
    • isEqualTo

      public boolean isEqualTo(XMLTree node)
      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

      public void setAttribute(String key, String value)
      Adds an attribute to the attributes list of the root of the XMLTree
      Parameters:
      key - The name of the attribute
      value - The value of the attribute
    • removeAttribute

      public Object removeAttribute(String key)
      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

      public boolean hasAttribute(String key)
      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

      public XMLTree 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

      public ArrayList<XMLTree> getChildren()
      Returns the children of the XMLTree
      Returns:
      The ArrayList of the childs of the XMLTree
    • setChildren

      public void setChildren(ArrayList<XMLTree> children)
      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

      public XMLTree getChild(String name)
      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 name or null if there is no such child
    • delChild

      public XMLTree delChild(String name)
      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 null if there is no such child
    • getChild

      public XMLTree getChild(int i)
      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 null if there is no child at the desired position
    • getAttributes

      public Map getAttributes()
      Returns the list of attributes of the root element of the XMLTree
      Returns:
      The PropertyGetter of the attributes of the root element of the XMLTree
    • setAttributes

      public void setAttributes(Map m)
      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

      public Object getAttribute(String key)
      Returns the current node attribute given its key.
      Parameters:
      key - the key for the attribute
      Returns:
      the attribute value for the current node
    • putAttribute

      public void putAttribute(String key, Object value)
      Adds a new attribute for the current node.
      Parameters:
      key - the key for the attribute
      value - the value of the attribute
    • getName

      public String getName()
      Returns the root name
      Returns:
      The name of the root element of the XMLTree
    • getValue

      public String getValue()
      Returns the value of the TYPE_TEXT or CDATA_SECTION node
      Returns:
      The string text value
    • getText

      public String getText(String def)
      Returns the text of a TYPE_TEXT node.
      Parameters:
      def - The default string returned if the XMLTree has no childs or if the value text is null
      Returns:
      The value text of the text node of the XMLTree; if the tree has no childs, or if the value text is null it 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 new XMLException
    • setName

      public void setName(String name)
      Sets the name of the root element of the XMLTree
      Parameters:
      name - The string to set as name
    • setValue

      public void setValue(String s)
      Sets the value text of the XMLTree
      Parameters:
      s - The string to set as value
    • getDeepChild

      public XMLTree getDeepChild(String path)
      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, null otherwise
    • getAbsoluteChild

      public XMLTree getAbsoluteChild(String path)
      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, null otherwise
    • searchDeepChild

      public XMLTree searchDeepChild(String name)
      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, null otherwise
    • 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

      public void qualify(String qualify)
    • attributesIterator

      public Iterator attributesIterator()
      Returns an Iterator over the attributes entry keys of the root element
      Returns:
      The iterator over the attribute's keys
    • childrenIterator

      public Iterator<XMLTree> childrenIterator()
      Returns an Iterator over the childs of the XMLTree
      Returns:
      The iterator over the childs
    • childrenIterator

      public Iterator<XMLTree> childrenIterator(String tagName)
      Returns a FilteredIterator over 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

      public Iterator parentIterator()
      Returns a XMLTree.ParentIterator over the parents of the XMLTree
      Returns:
      The iterator over the parents
    • getBoolean

      public Boolean getBoolean(String key, Boolean def)
      Extracts a Boolean attribute from the XMLTree
      Specified by:
      getBoolean in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getDate

      public Date getDate(String key, Date def, SimpleDateFormat sdf)
      Extracts a Date attribute from the XMLTree. The Date must meet the characteristics of the specified SimpleDateFormat
      Specified by:
      getDate in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      sdf - The SimpleDateFormat that defines the format of the date to extract
      Returns:
      The object of the specified type, or the default one
    • getDouble

      public Double getDouble(String key, Double def)
      Extracts a Double attribute from the XMLTree
      Specified by:
      getDouble in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getFloat

      public Float getFloat(String key, Float def)
      Extracts a Float attribute from the XMLTree
      Specified by:
      getFloat in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getInteger

      public Integer getInteger(String key, Integer def)
      Extracts an Integer attribute from the XMLTree
      Specified by:
      getInteger in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getlong

      public long getlong(String key, long def)
      Extracts a long attribute from the XMLTree
      Specified by:
      getlong in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getLong

      public Long getLong(String key, Long def)
      Extracts a Long attribute from the XMLTree
      Specified by:
      getLong in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getString

      public String getString(String key, String def)
      Extracts a String attribute from the XMLTree
      Specified by:
      getString in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getboolean

      public boolean getboolean(String key, boolean def)
      Extracts a boolean attribute from the XMLTree
      Specified by:
      getboolean in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getdouble

      public double getdouble(String key, double def)
      Extracts a double attribute from the XMLTree
      Specified by:
      getdouble in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getfloat

      public float getfloat(String key, float def)
      Extracts a float attribute from the XMLTree
      Specified by:
      getfloat in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • getint

      public int getint(String key, int def)
      Extracts an int attribute from the XMLTree
      Specified by:
      getint in interface Getter
      Parameters:
      key - Name of the attribute object to extract
      def - Default value returned when there isn't a correct object that corresponds to the key
      Returns:
      The object of the specified type, or the default one
    • keyIterator

      public Iterator keyIterator()
      Extracts an Iterator used to iterate over all the attributes of the XMLTree
      Specified by:
      keyIterator in interface Getter
      Returns:
      The iterator on the elements of the list of attributes
    • get

      public Object get(String key)
      Extracts from the PropertyGetter of 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.
      Specified by:
      get in interface Getter
      Parameters:
      key - The name of the key
      Returns:
      The object associated to the key passed as parameter, or null if the list of attribute doesen't contains a mapping for the key
    • get

      public Object get(String key, Object def)
      Extracts from the PropertyGetter of attributes of the root of the XMLTree the object that corresponds to the key passed as parameters. If the object is null, or if the list of attributes contains no mapping for the key, the default object is returned
      Specified by:
      get in interface Getter
      Parameters:
      key - The name of the key
      def - The default object
      Returns:
      The object associated to the key passed as parameter, or the default one
    • containsKey

      public boolean containsKey(String key)
      Checks if the PropertyGetter of attributes of the root of the XMLTree contains an entry with the key passed as parameter
      Specified by:
      containsKey in interface Getter
      Parameters:
      key - The name of the key to search
      Returns:
      true if there is an entry (attribute) with the key passed as parameter, false otherwise
    • 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
      Parameters:
      os - The OutputStream used to write the character stream
      indent - Indent amount. Use the value -1 for non indenting.
      encoding - The encoding
      preserveSpace - 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
      Parameters:
      w - The Writer used to write the character stream
      indent - Indent amount. Use the value -1 for non indenting.
      encoding - The encoding
      preserveSpace - does nothing
      Throws:
      XMLException - The method raise an XMLException if there are errors during the output operation
    • output

      public void output(OutputStream os, int indent, String encoding) throws XMLException
      Writes the XMLTree to an OutputStream
      Parameters:
      os - The OutputStream used to write the character stream
      indent - 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

      public void output(OutputStream os, int indent, Charset charset) throws XMLException
      Writes the XMLTree to an OutputStream
      Parameters:
      os - The OutputStream used to write the character stream
      indent - 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

      public void output(Writer w, int indent, String encoding) throws XMLException
      Writes the XMLTree to a Writer
      Parameters:
      w - The Writer used to write the character stream
      indent - 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

      public void output(Writer w, int indent, Charset charset) throws XMLException
      Writes the XMLTree to an OutputStream
      Parameters:
      w - The Writer used to write the character stream
      indent - 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

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(int indent, String encoding)
      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

      public Document createDom() throws ParserConfigurationException
      Returns the Document that represents the entire XML
      Returns:
      The Document that represents the entire XML
      Throws:
      ParserConfigurationException - The method raise a ParserConfigurationException if there are errors during the operation
    • createElement

      protected Node createElement(XMLTree xmlt, Document doc)
    • translateLabel

      public void translateLabel(Teacher t, String attribute)
      Translates all the string values in the XMLTree that corresponds to the XMLTree attribute name passed as parameter
      Parameters:
      t - The Teacher that will manage the translation
      attribute - The name of the attribute which values will be translated
    • getChildNodeByAttribute

      public XMLTree getChildNodeByAttribute(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.
      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.