Source code: com/robrohan/treebeard/JTreeContentTemplateHandler.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 * Created on January 9, 2003, 4:32 PM
21 */
22
23 package com.robrohan.treebeard;
24
25 import org.xml.sax.SAXException;
26 import javax.swing.tree.DefaultTreeModel;
27 import javax.swing.tree.DefaultMutableTreeNode;
28
29 /**
30 * this shows only template nodes and their parameters
31 * @author rob
32 */
33 public class JTreeContentTemplateHandler implements org.xml.sax.ContentHandler {
34
35 //the model to add nodes to
36 private DefaultTreeModel treeModel;
37 //current node
38 private DefaultMutableTreeNode current;
39 //keep track of the location
40 private org.xml.sax.Locator locator;
41 private java.util.Map namespaceMappings;
42
43 /** creates a new Content Handler
44 * @param tm the tree model this is using
45 * @param dmtn the begining tree node to use
46 */
47 public JTreeContentTemplateHandler(DefaultTreeModel tm, DefaultMutableTreeNode dmtn){
48 treeModel = tm;
49 current = dmtn;
50 namespaceMappings = new java.util.HashMap();
51 }
52 /** sets the documnet locator
53 * @param locator the document locator
54 */
55 public void setDocumentLocator(org.xml.sax.Locator locator) {
56 this.locator = locator;
57 }
58
59 /** process a start element */
60 public void startElement(String namespace, String localName, String str2, org.xml.sax.Attributes attributes)
61 throws SAXException {
62
63 if(localName.toString().equals("template")
64 || localName.toString().equals("param")
65 || localName.toString().equals("element")
66 || localName.toString().equals("function")){
67
68 String prefix=new String("");
69 if(namespace.length() > 0){
70 prefix = (String)namespaceMappings.get(namespace);
71 if(prefix != null && !prefix.equals("")){
72 prefix += ":";
73 }else{
74 prefix="";
75 }
76 }
77
78 for(int x=0; x<attributes.getLength(); x++){
79 localName += " " + attributes.getQName(x) + "=\"" + attributes.getValue(x) + "\"";
80 }
81
82 DefaultMutableTreeNode element = new DefaultMutableTreeNode(prefix + localName);
83 current.add(element);
84 current = element;
85
86 /* for(int x=0; x<attributes.getLength(); x++){
87 current.add(new DefaultMutableTreeNode(attributes.getQName(x) + "=\"" + attributes.getValue(x) + "\""));
88 } */
89 }
90
91 }
92
93 /** process an end element */
94 public void endElement(String str, String str1, String str2) throws SAXException {
95 if(str1.toString().equals("template")
96 || str1.toString().equals("param")
97 || str1.toString().equals("element")
98 || str1.toString().equals("function")
99 ){
100 current = (DefaultMutableTreeNode)current.getParent();
101 }
102 }
103 /** process the start prefix */
104 public void startPrefixMapping(String str, String str1) throws SAXException {
105 //save the mappings for later use
106 namespaceMappings.put(str1,str);
107
108 //if there is a prefix add a : to the begining
109 if(str != null && str.length() > 0){
110 str=":"+str;
111 }
112 current.add(new DefaultMutableTreeNode("xmlns" + str + "=" + "\"" + str1 + "\"" ));
113
114 }
115
116 /** process the end prefix */
117 public void endPrefixMapping(String str) throws SAXException {
118 for(java.util.Iterator i = namespaceMappings.keySet().iterator(); i.hasNext();) {
119 String uri = (String)i.next();
120 String thisPrefix = (String)namespaceMappings.get(uri);
121 if(str.equals(thisPrefix)){
122 namespaceMappings.remove(uri);
123 break;
124 }
125 }
126 }
127
128 /** process characters */
129 public void characters(char[] values, int param, int param2) throws SAXException {;}
130 public void endDocument() throws SAXException {;}
131 public void ignorableWhitespace(char[] values, int param, int param2) throws SAXException {;}
132 public void processingInstruction(String str, String str1) throws SAXException {;}
133 public void skippedEntity(String str) throws SAXException {;}
134 public void startDocument() throws SAXException {;}
135 }