pl.mbdev.xml
Class XmlTree

java.lang.Object
  extended by pl.mbdev.xml.XmlTree

public class XmlTree
extends java.lang.Object

XML data represented by a tree.

 Copyright 2011 Mateusz Bysiek,
     mb@mbdev.pl, http://mbdev.pl/
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 

Author:
Mateusz Bysiek

Constructor Summary
XmlTree()
          Creates an empty XML tree.
XmlTree(java.lang.String xmlStr)
          Creates a tree representing a String with XML contents.
XmlTree(Xml o)
          Creates a tree representing an object that implements Xml class.
XmlTree(XmlObject o)
          Creates a tree that represents an object that is convertable to XML.
XmlTree(XmlTree tree)
          Creates a copy of an another XML tree.
 
Method Summary
 void addAttribute(java.lang.String name, java.lang.String value)
          Adds new attribute to this node.
 void addSubNode(XmlTree node)
          Adds new sub node at the first free index.
 void addSubNode(XmlTree node, int subscript)
          Adds new subnode with the specific subscript.
static boolean checkIfDtdCorrect(java.lang.String dtdUrl)
          Checks if a given DTD URL is semantically correct.
static boolean checkIfNameCorrect(java.lang.String name)
          Checks if a given node name is semantically correct.
static java.lang.String createDtdUrl(java.lang.String domainAndUri, java.lang.String name)
          Creates DTD URL out of name of the node and web address with the file.
static XmlTree createFromFile(java.lang.String filePath)
          Creates new XmlTree from an external file.
static java.lang.String createHeader(java.lang.String dtdUrl, java.lang.String name)
          Creates XML header with XML document version information, encoding, and link to DTD file.
static java.lang.String createName(java.lang.Class<? extends XmlObject> c)
          Creates XML node name for a given object.
static java.lang.String createName(Xml o)
          Creates XML node name for a given object.
 java.lang.String getAttribute(java.lang.String attrName)
          Gets the attribute value for attribute with the given name.
 java.lang.String getDtdUrl()
          Gets DTD URL of this XML tree.
 java.lang.String getName()
          Returns name of this node.
 XmlTree getSubNode(java.lang.String nodeName, int subscript)
          Gets a sub node with a given subscript.
 java.lang.String getValue()
          Returns value of this node.
 boolean hasCorrectName(Xml o)
          Checks if this XML node has a name that corresponds to a given class.
 void removeAttribute(java.lang.String name)
          Removes attribute that has given name.
 void saveToFile(java.lang.String filePath)
          Saves this tree to some external file.
 void setDtdUrl(java.lang.String dtdUrl)
          Sets DTD URL to a given value
 void setName(java.lang.String name)
          Sets name of this node.
 void setValue(java.lang.String value)
          Sets the value of this node.
 java.lang.String toFullString()
          returns the contents of this node and sub-nodes, recursively.
 java.lang.String toString()
           
 java.lang.String toXmlString()
          Converts this tree to its XML string representation
 boolean validate()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XmlTree

public XmlTree()
Creates an empty XML tree.


XmlTree

public XmlTree(XmlTree tree)
Creates a copy of an another XML tree.

Parameters:
tree - source to be copied

XmlTree

public XmlTree(java.lang.String xmlStr)
Creates a tree representing a String with XML contents.

Parameters:
xmlStr - string containing the whole XML file

XmlTree

public XmlTree(XmlObject o)
Creates a tree that represents an object that is convertable to XML.

Parameters:
o - object for conversion

XmlTree

public XmlTree(Xml o)
Creates a tree representing an object that implements Xml class.

Parameters:
o - the object
Method Detail

createHeader

public static java.lang.String createHeader(java.lang.String dtdUrl,
                                            java.lang.String name)
Creates XML header with XML document version information, encoding, and link to DTD file.

Parameters:
dtdUrl - URL to DTD file
name - name of the root node
Returns:
reference to String incorporating the full XML header

createDtdUrl

public static java.lang.String createDtdUrl(java.lang.String domainAndUri,
                                            java.lang.String name)
Creates DTD URL out of name of the node and web address with the file.

Parameters:
domainAndUri - URI should not end with slash, and should begin with http://
name - name of the file should not have any extension
Returns:
reference to string with full path to DTD file

createName

public static java.lang.String createName(Xml o)
Creates XML node name for a given object.

Parameters:
o - object for which the name will be created
Returns:
name of the node, in lower-case

createName

public static java.lang.String createName(java.lang.Class<? extends XmlObject> c)
Creates XML node name for a given object.

Parameters:
c - class for which the name will be created
Returns:
name of the node, in lower-case

checkIfDtdCorrect

public static boolean checkIfDtdCorrect(java.lang.String dtdUrl)
Checks if a given DTD URL is semantically correct.

Parameters:
dtdUrl - URL to be checked
Returns:
true if given string is a correct URL to dtd file

checkIfNameCorrect

public static boolean checkIfNameCorrect(java.lang.String name)
Checks if a given node name is semantically correct.

Parameters:
name - node name to be checked
Returns:
true if given string is a correct node name

createFromFile

public static XmlTree createFromFile(java.lang.String filePath)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Creates new XmlTree from an external file.

Parameters:
filePath - path to the file
Returns:
tree that was encoded in the file
Throws:
java.io.FileNotFoundException - if the file was not found
java.io.IOException - if there was an exception while reading the file

setDtdUrl

public void setDtdUrl(java.lang.String dtdUrl)
Sets DTD URL to a given value

Parameters:
dtdUrl - a DTD URL

getDtdUrl

public java.lang.String getDtdUrl()
Gets DTD URL of this XML tree.

Returns:
URL

setName

public void setName(java.lang.String name)
Sets name of this node.

Parameters:
name -

getName

public java.lang.String getName()
Returns name of this node.

Returns:
name of this node

addAttribute

public void addAttribute(java.lang.String name,
                         java.lang.String value)
Adds new attribute to this node.

Parameters:
name - name of the attribute
value - value of the attribute

removeAttribute

public void removeAttribute(java.lang.String name)
Removes attribute that has given name.

Parameters:
name -

getAttribute

public java.lang.String getAttribute(java.lang.String attrName)
Gets the attribute value for attribute with the given name.

Parameters:
attrName - name of the attribute
Returns:
value of the attribute

setValue

public void setValue(java.lang.String value)
Sets the value of this node.

Parameters:
value -

getValue

public java.lang.String getValue()
Returns value of this node.

Returns:
value of this node, empty string if there is no value

addSubNode

public void addSubNode(XmlTree node,
                       int subscript)
Adds new subnode with the specific subscript.

Parameters:
node -
subscript -

addSubNode

public void addSubNode(XmlTree node)
Adds new sub node at the first free index.

Parameters:
node - sub-node

getSubNode

public XmlTree getSubNode(java.lang.String nodeName,
                          int subscript)
Gets a sub node with a given subscript.

Parameters:
nodeName -
subscript -
Returns:
reference to the specified childnode

hasCorrectName

public boolean hasCorrectName(Xml o)
Checks if this XML node has a name that corresponds to a given class.

Parameters:
o - object convertible to XML
Returns:
true if the name created for a given class matches the name of the node

validate

public boolean validate()
Returns:
true if this XML tree has correct contents, according to the dtd it declares

saveToFile

public void saveToFile(java.lang.String filePath)
                throws java.io.IOException
Saves this tree to some external file.

Parameters:
filePath -
Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toXmlString

public java.lang.String toXmlString()
Converts this tree to its XML string representation

Returns:
string that encodes this tree

toFullString

public java.lang.String toFullString()
returns the contents of this node and sub-nodes, recursively.

Returns:
multi-line string with indentation