Source code: com/robrohan/treebeard/JTreeContentHandler.java
1 /*
2 * Treebeard: an xml xslt transfomer
3 * Copyright (C) 2002 Rob Rohan
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Email: me@robrohan.com
19 *
20 * JTreeContentHandler.java
21 *
22 * Created on January 9, 2003, 4:33 PM
23 */
24
25 package com.robrohan.treebeard;
26
27 import org.xml.sax.SAXException;
28 import javax.swing.tree.DefaultTreeModel;
29 import javax.swing.tree.DefaultMutableTreeNode;
30
31 /**
32 * this is the normal tree view. shows all paths in the treeview
33 * @author rob
34 */
35 public class JTreeContentHandler implements org.xml.sax.ContentHandler {
36
37 //the model to add nodes to
38 private DefaultTreeModel treeModel;
39 //current node
40 private DefaultMutableTreeNode current;
41 //keep track of the location
42 private org.xml.sax.Locator locator;
43 private java.util.Map namespaceMappings;
44
45 /** creates a new Content Handler
46 * @param tm the tree model this is using
47 * @param dmtn the begining tree node to use
48 */
49 public JTreeContentHandler(DefaultTreeModel tm, DefaultMutableTreeNode dmtn){
50 treeModel = tm;
51 current = dmtn;
52 namespaceMappings = new java.util.HashMap();
53 }
54
55 /** sets the documnet locator
56 * @param locator the document locator
57 */
58 public void setDocumentLocator(org.xml.sax.Locator locator) {
59 this.locator = locator;
60 }
61
62 /** process a start element */
63 public void startElement(String namespace, String localName, String str2, org.xml.sax.Attributes attributes)
64 throws SAXException {
65
66 String prefix=new String("");
67 if(namespace.length() > 0){
68 prefix = (String)namespaceMappings.get(namespace);
69 if(prefix != null && !prefix.equals("")){
70 prefix += ":";
71 }else{
72 prefix="";
73 }
74 }
75
76 DefaultMutableTreeNode element = new DefaultMutableTreeNode(prefix + localName);
77 current.add(element);
78 current = element;
79
80 for(int x=0; x<attributes.getLength(); x++){
81 current.add(new DefaultMutableTreeNode('@' + attributes.getQName(x)));
82 }
83 }
84
85 /** process an end element */
86 public void endElement(String str, String str1, String str2) throws SAXException {
87 current = (DefaultMutableTreeNode)current.getParent();
88 }
89 /** process the start prefix */
90 public void startPrefixMapping(String str, String str1) throws SAXException {
91 //save the mappings for later use
92 namespaceMappings.put(str1,str);
93
94 //if there is a prefix add a : to the begining
95 if(str != null && str.length() > 0){
96 str=":"+str;
97 }
98 current.add(new DefaultMutableTreeNode("xmlns" + str + "=" + "\"" + str1 + "\"" ));
99
100 }
101 /** process the end prefix */
102 public void endPrefixMapping(String str) throws SAXException {
103 for(java.util.Iterator i = namespaceMappings.keySet().iterator(); i.hasNext();) {
104 String uri = (String)i.next();
105 String thisPrefix = (String)namespaceMappings.get(uri);
106 if(str.equals(thisPrefix)){
107 namespaceMappings.remove(uri);
108 break;
109 }
110 }
111 }
112
113 /** process characters */
114 public void characters(char[] values, int param, int param2) throws SAXException {;}
115 /** process the end of the document */
116 public void endDocument() throws SAXException {;}
117 public void ignorableWhitespace(char[] values, int param, int param2) throws SAXException {;}
118 /** process the processing instructions */
119 public void processingInstruction(String str, String str1) throws SAXException {;}
120 public void skippedEntity(String str) throws SAXException {;}
121 /** process the start of the documnet */
122 public void startDocument() throws SAXException {;}
123 }