From 2706c666d0e04c2caf76ea715180b96a2f1c1e68 Mon Sep 17 00:00:00 2001 From: Martin Scharm Date: Wed, 5 Feb 2014 10:11:55 +0100 Subject: [PATCH] created NodeUsageExample [fixes #15] --- pom.xml | 2 +- .../sems/xmlutils/eg/NodeUsageExample.java | 106 +++++++++++++++++- .../sems/xmlutils/eg/TreeUsageExample.java | 61 ++++++---- test/simple.xml | 2 +- 4 files changed, 145 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index 0acbdd0..ada6743 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ de.unirostock.sems xmlutils jar - 0.3.1 + 0.3.2 xmlutils A toolkit useful for working with XML documents diff --git a/src/main/java/de/unirostock/sems/xmlutils/eg/NodeUsageExample.java b/src/main/java/de/unirostock/sems/xmlutils/eg/NodeUsageExample.java index 82684fc..e7eb828 100644 --- a/src/main/java/de/unirostock/sems/xmlutils/eg/NodeUsageExample.java +++ b/src/main/java/de/unirostock/sems/xmlutils/eg/NodeUsageExample.java @@ -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 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 ()); } } diff --git a/src/main/java/de/unirostock/sems/xmlutils/eg/TreeUsageExample.java b/src/main/java/de/unirostock/sems/xmlutils/eg/TreeUsageExample.java index 687b296..e1a0f86 100644 --- a/src/main/java/de/unirostock/sems/xmlutils/eg/TreeUsageExample.java +++ b/src/main/java/de/unirostock/sems/xmlutils/eg/TreeUsageExample.java @@ -19,38 +19,47 @@ 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) @@ -58,40 +67,46 @@ public static void main (String[] args) throws XmlDocumentParseException, FileNo 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 node: " + node.getParent ().getLevel ()); + System.out.println ("level of parent of first node: " + + node.getParent ().getLevel ()); System.out.println ("The whole subtree below this 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< ---"); } diff --git a/test/simple.xml b/test/simple.xml index 02e3e50..484d8e7 100644 --- a/test/simple.xml +++ b/test/simple.xml @@ -1,5 +1,5 @@ - + dagmar martin keep workin'!