Skip to content

Commit

Permalink
created NodeUsageExample [fixes #15]
Browse files Browse the repository at this point in the history
  • Loading branch information
binfalse committed Feb 5, 2014
1 parent 8ff48e8 commit 2706c66
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<groupId>de.unirostock.sems</groupId>
<artifactId>xmlutils</artifactId>
<packaging>jar</packaging>
<version>0.3.1</version>
<version>0.3.2</version>
<name>xmlutils</name>
<description>
A toolkit useful for working with XML documents
Expand Down
106 changes: 105 additions & 1 deletion src/main/java/de/unirostock/sems/xmlutils/eg/NodeUsageExample.java
Expand Up @@ -3,19 +3,123 @@
*/
package de.unirostock.sems.xmlutils.eg;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.unirostock.sems.xmlutils.ds.DocumentNode;
import de.unirostock.sems.xmlutils.ds.TreeDocument;
import de.unirostock.sems.xmlutils.ds.TreeNode;
import de.unirostock.sems.xmlutils.exception.XmlDocumentConsistencyException;
import de.unirostock.sems.xmlutils.exception.XmlDocumentParseException;
import de.unirostock.sems.xmlutils.tools.XmlTools;



/**
* @author Martin Scharm
*
*
*/
public class NodeUsageExample
{

/**
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws FileNotFoundException
* @throws XmlDocumentParseException
* @throws XmlDocumentConsistencyException
*/
public static void main (String[] args)
throws XmlDocumentParseException,
FileNotFoundException,
ParserConfigurationException,
SAXException,
IOException,
XmlDocumentConsistencyException
{
File file = new File ("test/simple.xml");
TreeDocument document = new TreeDocument (XmlTools.readDocument (file),
file.toURI ());

// get root node
DocumentNode root = document.getRoot ();
Vector<TreeNode> firstLevel = root.getChildren ();
System.out.println ("There are " + firstLevel.size () + " children in "
+ root.getXPath () + " :");
for (TreeNode kid : firstLevel)
System.out.println ("\t" + kid.getXPath () + " having "
+ ((DocumentNode) kid).getNumLeaves () + " leaves and a weight of "
+ kid.getWeight ());
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// get first message node
DocumentNode message = document.getNodeById ("messageone");
// you can also get access to the same node using it's path:
TreeNode sameNode = document.getNodeByPath (message.getXPath ());
// let's test if it's really the same:
System.out.println ("found same node by id and by XPath? "
+ (sameNode == message));
// you can also get this node by it's signature (here i know it's the first
// node having this hash value)
sameNode = document.getNodesByHash (message.getSubTreeHash ()).get (0);
// test:
System.out.println ("found same node by id and by hash? "
+ (sameNode == message));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// let's print some information about this node
System.out.println ("Path to the message node: " + message.getXPath ());
System.out.println ("Path to its parent: "
+ message.getParent ().getXPath ());
System.out.println ("Weight of the message node: " + message.getWeight ());
System.out.println ("Signature of the message node: "
+ message.getOwnHash ());
System.out.println ("Signature of the subtree rooted in the message node: "
+ message.getSubTreeHash ());
System.out.println ("#number nodes in its subtree: "
+ (message.getSizeSubtree () + 1));
System.out.println ("number of direct children: "
+ message.getNumChildren ());
System.out.println ("id of the node: " + message.getId ());
System.out.println ("tag name: " + message.getTagName ());
System.out.println ("attributes in this node:");
for (String attr : message.getAttributes ())
System.out.println ("\t" + attr + " => " + message.getAttribute (attr));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// remove the node from the tree
System.out.println ("# nodes in document before remove: "
+ document.getNumNodes ());
DocumentNode parent = message.getParent ();
parent.rmChild (message);
System.out.println ("# nodes in document after remove: "
+ document.getNumNodes ());

// and reinsert it
parent.addChild (message);
System.out.println ("# nodes in document after reinsert: "
+ document.getNumNodes ());

// note how the path ot the node has changed
System.out.println ("New path to the message node: " + message.getXPath ());
// but everything else is still the same
System.out.println ("Weight of the message node: " + message.getWeight ());
System.out.println ("Signature of the subtree rooted in the message node: "
+ message.getSubTreeHash ());
}

}
61 changes: 38 additions & 23 deletions src/main/java/de/unirostock/sems/xmlutils/eg/TreeUsageExample.java
Expand Up @@ -19,79 +19,94 @@
import de.unirostock.sems.xmlutils.tools.XmlTools;



/**
* @author Martin Scharm
*
*
*/
public class TreeUsageExample
{

/**
* @param args
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws FileNotFoundException
* @throws XmlDocumentParseException
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws FileNotFoundException
* @throws XmlDocumentParseException
*/
public static void main (String[] args) throws XmlDocumentParseException, FileNotFoundException, ParserConfigurationException, SAXException, IOException
public static void main (String[] args)
throws XmlDocumentParseException,
FileNotFoundException,
ParserConfigurationException,
SAXException,
IOException
{
File file = new File ("test/simple.xml");
TreeDocument document = new TreeDocument (XmlTools.readDocument (file), file.toURI ());
TreeDocument document = new TreeDocument (XmlTools.readDocument (file),
file.toURI ());
System.out.println ("this is the document:");
System.out.println ();
System.out.println (DocumentTools.printPrettySubDoc (document.getRoot ()));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

System.out.println ("There are " + document.getNumNodes () + " nodes in this tree");

System.out.println ("There are " + document.getNumNodes ()
+ " nodes in this tree");
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// get all subtrees (i.e. nodes rooting these trees) ordered by size, biggest first:

// get all subtrees (i.e. nodes rooting these trees) ordered by size,
// biggest first:
TreeNode[] subTrees = document.getSubtreesBySize ();
System.out.println ("path to subtrees ordered by size:");
for (TreeNode node : subTrees)
System.out.println ("\t" + node.getXPath ());
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// get the root of the tree
DocumentNode root = document.getRoot ();
System.out.println ("tag name of root is: " + root.getTagName ());
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// get the node with id="messagetwo"
// and print the number of nodes below this node
DocumentNode semsNode = document.getNodeById ("messagetwo");
System.out.println ("#nodes below messagetwo-node: " + semsNode.getSizeSubtree ());
System.out.println ("#nodes below messagetwo-node: "
+ semsNode.getSizeSubtree ());
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// get the first node having a tag name of "example"
// and print the level of its parent
DocumentNode node = document.getNodesByTag ("content").get (0);
System.out.println ("level of parent of first <content> node: " + node.getParent ().getLevel ());
System.out.println ("level of parent of first <content> node: "
+ node.getParent ().getLevel ());
System.out.println ("The whole subtree below this <content> node:");
System.out.println (DocumentTools.printPrettySubDoc (node));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
System.out.println ();

// compare two trees
TreeDocument document2 = new TreeDocument (XmlTools.readDocument (new File ("test/simple.xml")), null);
TreeDocument document2 = new TreeDocument (XmlTools.readDocument (new File (
"test/simple.xml")), null);
// same document -> should be true
System.out.println ("document2.equals (document) ? " + (document2.equals (document)));

TreeDocument document3 = new TreeDocument (XmlTools.readDocument (new File ("test/mathml.xml")), null);
System.out.println ("document2.equals (document) ? "
+ (document2.equals (document)));

TreeDocument document3 = new TreeDocument (XmlTools.readDocument (new File (
"test/mathml.xml")), null);
// different document -> most likely false
System.out.println ("document3.equals (document) ? " + (document3.equals (document)));
System.out.println ("document3.equals (document) ? "
+ (document3.equals (document)));
System.out.println ();
System.out.println ("--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---");
}
Expand Down
2 changes: 1 addition & 1 deletion test/simple.xml
@@ -1,5 +1,5 @@
<conversations>
<message id="messageone">
<message id="messageone" priority="high">
<from>dagmar</from>
<to>martin</to>
<content>keep workin'!</content>
Expand Down

0 comments on commit 2706c66

Please sign in to comment.