Package javaforce.awt

Class XMLTree

java.lang.Object
javaforce.awt.XMLTree
All Implemented Interfaces:
EventListener, TreeModelListener

public class XMLTree extends Object implements TreeModelListener
XML is a TreeModel data model that encapsules a complete XML file.
Each XML tag (element) is treated as a node in the tree. Once read() it can be viewed and edited with a JTree. Then you can write() it back to a file. XML will monitor changes made and update nodes as needed. The read() functions include a callback interface so you can further tweak the layout of the XML tree.
Typical XML Tag:
<name [attributes...]> content | children </name>
Singleton XML Tag: (no children)
<name [attributes...] />
Caveats:
Only leaf nodes can contain actual data (content) (in other words @XmlMixed is not supported).
Mixed tags are read, but when written the content is lost.
There must be only one root tag.
Support for the standard XML header is provided (see header).
readClass() and writeClass() support : int, short, byte, float, double, boolean, String, Color and Custom Classes.
Arrays of any of these.
All classes and fields MUST be public. static and transient members are skipped. No special annotations are required.
  • Field Details

  • Constructor Details

    • XMLTree

      public XMLTree()
      Constructs a new XML object.
  • Method Details

    • getTreeModel

      public TreeModel getTreeModel()
      Returns the TreeModel that can be passed to JTree constructor.
    • getDefaultTreeModel

      public DefaultTreeModel getDefaultTreeModel()
    • read

      public boolean read(String filename)
      Reads the entire tree from a XML file from filename. No call handler is used.
      Parameters:
      filename - name of file to load XML data from
    • read

      public boolean read(String filename, XMLTree.XMLEvent event)
      Reads the entire tree from a XML file from filename.
      Parameters:
      filename - name of file to load XML data from
      event - callback handler to process each loaded XML tag
    • read

      public boolean read(InputStream is)
      Reads the entire tree from a XML file from the InputStream. No callback handler is used.
      Parameters:
      is - InputStream to load XML data from
    • read

      public boolean read(InputStream is, XMLTree.XMLEvent event)
      Reads the entire tree from a XML file from the InputStream.
      Parameters:
      is - InputStream to load XML data from
      event - callback handler to process each loaded XML tag
    • write

      public boolean write(String filename)
      Writes the entire tree as a XML file to the filename.
    • write

      public boolean write(OutputStream os)
      Writes the entire tree as a XML file to the OutputStream.
    • deleteAll

      public void deleteAll()
      Deletes the entire tree and resets the root and header tags.
    • deleteTag

      public boolean deleteTag(XMLTree.XMLTag tag)
      Deletes a node from the parent. Also deletes all it's children.
    • createTag

      public XMLTree.XMLTag createTag()
      Creates an empty node that can be inserted into the tree using addTag().
    • addTag

      public XMLTree.XMLTag addTag(XMLTree.XMLTag targetParent, XMLTree.XMLTag tag)
      Adds the node to the targetParent.
    • addTag

      public XMLTree.XMLTag addTag(XMLTree.XMLTag targetParent, String name, String attrs, String content)
      Adds node with the name, attrs and content specified. If another node already exists with the same name the new node's unique name will differ.
    • addSetTag

      public XMLTree.XMLTag addSetTag(XMLTree.XMLTag targetParent, String name, String attrs, String content)
      Adds (a non-existing) or sets (an existing) node with the name, attrs and content specified.
    • changedTag

      public void changedTag(XMLTree.XMLTag tag)
      Notify the treemodel that you changed a node.
    • setRoot

      public void setRoot(String name, String attrs, String content)
      Sets the name, attrs and contents of the true root node.
    • getName

      public String getName(XMLTree.XMLTag tag)
      Returns the unique name of a node.
    • setName

      public void setName(XMLTree.XMLTag tag, String newName)
      Sets the name of a node. It's unique name may differ when shown in a tree.
    • getTag

      public XMLTree.XMLTag getTag(TreePath path)
      Returns a node based on the TreePath path;
    • getTag

      public XMLTree.XMLTag getTag(Object[] objs)
      Returns a node based on the objs[] path. Relative to virtual root tag. (see setRoot())
    • setTag

      public void setTag(XMLTree.XMLTag tag, String name, String attrs, String content)
      Sets the name, attrs and content for an existing node.
    • setRoot

      public void setRoot(XMLTree.XMLTag newRoot)
      Sets the root node for the tree. This doesn't effect the true root node. This is usefull in hiding parts of a XML file from view when viewed in a JTree.
    • setUseContentForName

      public void setUseContentForName(boolean state)
      Forces getName() to return the tags content instead of the actual name.
      Parameters:
      state -
    • getUseContentForName

      public boolean getUseContentForName()
      Forces getName() to return the tags content instead of the actual name.
    • setUseAttributeNameForName

      public void setUseAttributeNameForName(boolean state)
      Forces getName() to return the tags attribute 'name' (if available)
      Parameters:
      state -
    • getUseAttributeNameForName

      public boolean getUseAttributeNameForName()
      Forces getName() to return the tags content instead of the actual name.
    • setUseUniqueNames

      public void setUseUniqueNames(boolean state)
      Forces getName() to return unique names.
      Parameters:
      state -
    • getUseUniqueNames

      public boolean getUseUniqueNames()
      Forces getName() to return the tags content instead of the actual name.
    • writeClass

      public void writeClass(XMLTree.XMLTag tag, Object obj)
      Writes all children of tag to object.
    • writeClass

      public void writeClass(Object obj)
      Writes entire XML tree to object.
    • readClass

      public void readClass(XMLTree.XMLTag tag, Object obj)
      Reads all fields from an object and places into tag.
    • readClass

      public void readClass(String rootName, Object obj)
      Reads entire XML tree from object (deletes entire tree first).
      Parameters:
      rootName - = name to assign to root tag.
    • setEventListener

      public void setEventListener(XMLTree.XMLEvent event)
    • treeNodesChanged

      public void treeNodesChanged(TreeModelEvent e)
      Specified by:
      treeNodesChanged in interface TreeModelListener
    • treeNodesInserted

      public void treeNodesInserted(TreeModelEvent e)
      Specified by:
      treeNodesInserted in interface TreeModelListener
    • treeNodesRemoved

      public void treeNodesRemoved(TreeModelEvent e)
      Specified by:
      treeNodesRemoved in interface TreeModelListener
    • treeStructureChanged

      public void treeStructureChanged(TreeModelEvent e)
      Specified by:
      treeStructureChanged in interface TreeModelListener