diff java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/XMLHandler.java @ 3:9e67171477bc

PHANTOM Omni Heptic device release
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Wed, 25 Apr 2012 17:09:09 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/XMLHandler.java	Wed Apr 25 17:09:09 2012 +0100
@@ -0,0 +1,106 @@
+/*  
+ CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
+  
+ Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+package uk.ac.qmul.eecs.ccmi.checkboxtree;
+
+import java.util.Stack;
+
+import javax.swing.tree.DefaultTreeModel;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/* Build a tree out of an XML file. The hierarchical structure of the XML file reflects 
+ * the structure of the tree. The XMl file must have the form specified in CheckBoxTree
+ * class comment.  
+ *
+ */
+class XMLHandler extends DefaultHandler {
+	
+	@SuppressWarnings("serial")
+	public XMLHandler(DefaultTreeModel treeModel, SetProperties properties){
+		this.properties = properties;
+		this.treeModel = treeModel;
+		/* path is used to keep track of the current node's path. If the path is present as a          *      
+		 * properties entry, then the current node, that has jus been created, will be set as selected */
+		path = new Stack<String>() {
+			@Override
+			public String toString(){
+				StringBuilder builder = new StringBuilder();
+				for(int i=0; i<size();i++){
+					builder.append(get(i));
+					if(i != size()-1)
+						builder.append(CheckBoxTreeNode.STRING_PATH_SEPARATOR);
+				}
+				return builder.toString();
+			}
+		};
+	}
+
+	/*
+	 * Create a CheckBoxTreeNode out of an xml tag. The tree node name is given by the value 
+	 * attribute. Whether the tree node is selectable or not, depends on the tag name, which can be 
+	 * selectable/unselectable. If the node is selectable, then it will be set as selected if its path 
+	 * is present in the properties  
+	 */
+	@Override
+	public void startElement(String uri,
+            String localName,
+            String qName,
+            Attributes attributes)
+     throws SAXException {
+		String nodeName = attributes.getValue(VALUE_ATTR);
+		if(nodeName == null)
+			throw new SAXException("Value attribute missing");
+		boolean isSelectable = SELECTABLE_NODE.equals(qName);
+		
+		CheckBoxTreeNode newNode = new CheckBoxTreeNode(nodeName,isSelectable);
+		if(currentNode == null){
+			currentNode = newNode;
+			treeModel.setRoot(newNode);
+		}else{
+			currentNode.add(newNode);
+		}
+		currentNode = newNode;
+		path.push(nodeName);
+		if(properties.contains(path.toString()))
+			newNode.setSelected(true);
+	}
+	
+	/* when an end tag is encountered, we carry on building the tree with the father as current node */
+	@Override
+	public void endElement(String uri,
+            String localName,
+            String qName)
+     throws SAXException {
+		path.pop();
+		currentNode = (CheckBoxTreeNode)currentNode.getParent();
+	}
+
+	private CheckBoxTreeNode currentNode;
+	private DefaultTreeModel treeModel;
+	private SetProperties properties;
+	private Stack<String> path;
+	/* attributes used in the XML file */
+	public static final String VALUE_ATTR = "value";
+	public static final String SELECTABLE_NODE = "selectable";
+	public static final String UNSELECTABLE_NODE = "selectable";
+	
+	
+}