KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ui > DOMTreeFull


1 /*
2  * Copyright 1999,2000,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package ui;
18
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Hashtable JavaDoc;
22
23 import javax.swing.JTree JavaDoc;
24 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
25 import javax.swing.tree.DefaultTreeModel JavaDoc;
26 import javax.swing.tree.MutableTreeNode JavaDoc;
27 import javax.swing.tree.TreeNode JavaDoc;
28
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.Notation JavaDoc;
34
35 /**
36  * DOMTree class to enter every DOM node into a Swing JTree tree.
37  * The forward and backward mappings between Nodes to TreeNodes are kept.
38  *
39  * @version $Id: DOMTreeFull.java,v 1.6 2005/05/09 01:18:19 mrglavas Exp $
40  */

41 public class DOMTreeFull
42     extends JTree JavaDoc
43     {
44     
45     private static final long serialVersionUID = 3978144335541975344L;
46
47     //
48
// Constructors
49
//
50

51     /** Default constructor. */
52     public DOMTreeFull() {
53         this(null);
54         }
55
56     /** Constructs a tree with the specified document. */
57     public DOMTreeFull(Node JavaDoc root) {
58         super(new Model JavaDoc());
59
60         // set tree properties
61
setRootVisible(false);
62
63         // set properties
64
setRootNode(root);
65
66         } // <init>()
67

68     //
69
// Public methods
70
//
71

72     /** Sets the root. */
73     public void setRootNode(Node JavaDoc root) {
74         ((Model JavaDoc)getModel()).setRootNode(root);
75         expandRow(0);
76         }
77
78     /** Returns the root. */
79     public Node JavaDoc getRootNode() {
80         return ((Model JavaDoc)getModel()).getRootNode();
81         }
82
83     /** get the org.w3c.Node for a MutableTreeNode. */
84     public Node JavaDoc getNode(Object JavaDoc treeNode) {
85         return ((Model JavaDoc)getModel()).getNode(treeNode);
86     }
87
88     /** get the TreeNode for the org.w3c.Node */
89     public TreeNode JavaDoc getTreeNode(Object JavaDoc node) {
90         return ((Model JavaDoc)getModel()).getTreeNode(node);
91     }
92
93     //
94
// Classes
95
//
96

97     /**
98      * DOM tree model.
99      *
100      */

101     public static class Model
102         extends DefaultTreeModel JavaDoc
103         implements Serializable JavaDoc
104         {
105         
106         private static final long serialVersionUID = 3258131375181018673L;
107
108         //
109
// Data
110
//
111

112         /** Root. */
113         private Node JavaDoc root;
114         /** Node Map. */
115         private Hashtable JavaDoc nodeMap = new Hashtable JavaDoc();
116         /** Tree Node Map. */
117         private Hashtable JavaDoc treeNodeMap = new Hashtable JavaDoc();
118
119         //
120
// Constructors
121
//
122

123         /** Default constructor. */
124         public Model() {
125             this(null);
126             }
127
128         /** Constructs a model from the specified root. */
129         public Model(Node JavaDoc node) {
130             super(new DefaultMutableTreeNode JavaDoc());
131             if (node!=null)
132             setRootNode(node);
133             }
134
135         //
136
// Public methods
137
//
138

139         /** Sets the root. */
140         public synchronized void setRootNode(Node JavaDoc root) {
141             
142             // save root
143
this.root = root;
144
145             // clear tree and re-populate
146
DefaultMutableTreeNode JavaDoc where = (DefaultMutableTreeNode JavaDoc)getRoot();
147             where.removeAllChildren();
148             nodeMap.clear();
149             treeNodeMap.clear();
150
151             buildTree(root, where);
152             
153             fireTreeStructureChanged(this, new Object JavaDoc[] { getRoot() }, new int[0], new Object JavaDoc[0]);
154
155             } // setRootNode(Node)
156

157         /** Returns the root. */
158         public Node JavaDoc getRootNode() {
159             return root;
160             }
161
162         /** get the org.w3c.Node for a MutableTreeNode. */
163         public Node JavaDoc getNode(Object JavaDoc treeNode) {
164             return (Node JavaDoc)nodeMap.get(treeNode);
165         }
166         public Hashtable JavaDoc getAllNodes() {
167             return nodeMap;
168         }
169         /** get the org.w3c.Node for a MutableTreeNode. */
170         public TreeNode JavaDoc getTreeNode(Object JavaDoc node) {
171             Object JavaDoc object = treeNodeMap.get(node);
172             return (TreeNode JavaDoc)object;
173         }
174
175         //
176
// Private methods
177
//
178

179         /** Builds the tree. */
180         private void buildTree(Node JavaDoc node, MutableTreeNode JavaDoc where) {
181             
182             // is there anything to do?
183
if (node == null) { return; }
184
185             MutableTreeNode JavaDoc treeNode = insertNode(node, where);
186
187             // iterate over children of this node
188
NodeList JavaDoc nodes = node.getChildNodes();
189             int len = (nodes != null) ? nodes.getLength() : 0;
190             for (int i = 0; i < len; i++) {
191                 Node JavaDoc child = nodes.item(i);
192                 buildTree(child, treeNode);
193
194             }
195
196
197         } // buildTree()
198

199         /** Inserts a node and returns a reference to the new node. */
200         private MutableTreeNode JavaDoc insertNode(String JavaDoc what, MutableTreeNode JavaDoc where) {
201
202             MutableTreeNode JavaDoc node = new DefaultMutableTreeNode JavaDoc(what);
203             insertNodeInto(node, where, where.getChildCount());
204             return node;
205
206             } // insertNode(Node,MutableTreeNode):MutableTreeNode
207

208
209         /** Inserts a text node. */
210         public MutableTreeNode JavaDoc insertNode(Node JavaDoc what, MutableTreeNode JavaDoc where) {
211                 MutableTreeNode JavaDoc treeNode = insertNode(DOMTreeFull.toString(what), where);
212                 nodeMap.put(treeNode, what);
213                 treeNodeMap.put(what, treeNode);
214                 return treeNode;
215             }
216         } // class Model
217

218         public static String JavaDoc whatArray[] = new String JavaDoc [] {
219                 "ALL",
220                 "ELEMENT",
221                 "ATTRIBUTE",
222                 "TEXT",
223                 "CDATA_SECTION",
224                 "ENTITY_REFERENCE",
225                 "ENTITY",
226                 "PROCESSING_INSTRUCTION",
227                 "COMMENT",
228                 "DOCUMENT",
229                 "DOCUMENT_TYPE",
230                 "DOCUMENT_FRAGMENT",
231                 "NOTATION"
232                 };
233         //
234
// Public methods
235
//
236

237         public static String JavaDoc toString(Node JavaDoc node) {
238             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
239                 
240             // is there anything to do?
241
if (node == null) {
242                 return "";
243             }
244
245             int type = node.getNodeType();
246             sb.append(whatArray[type]);
247             sb.append(" : ");
248             sb.append(node.getNodeName());
249             String JavaDoc value = node.getNodeValue();
250             if (value != null) {
251                 sb.append(" Value: \"");
252                 sb.append(value);
253                 sb.append("\"");
254             }
255                     
256             switch (type) {
257                     
258                 // document
259
case Node.DOCUMENT_NODE: {
260                     break;
261                 }
262
263                 // element with attributes
264
case Node.ELEMENT_NODE: {
265                     Attr JavaDoc attrs[] = sortAttributes(node.getAttributes());
266                     if (attrs.length > 0)
267                         sb.append(" ATTRS:");
268                     for (int i = 0; i < attrs.length; i++) {
269                         Attr JavaDoc attr = attrs[i];
270                             
271                         sb.append(' ');
272                         sb.append(attr.getNodeName());
273                         sb.append("=\"");
274                         sb.append(normalize(attr.getNodeValue()));
275                         sb.append('"');
276                     }
277                     sb.append('>');
278                     break;
279                 }
280
281                 // handle entity reference nodes
282
case Node.ENTITY_REFERENCE_NODE: {
283                     break;
284                 }
285
286                 // cdata sections
287
case Node.CDATA_SECTION_NODE: {
288                     break;
289                 }
290
291                 // text
292
case Node.TEXT_NODE: {
293                     break;
294                 }
295
296                 // processing instruction
297
case Node.PROCESSING_INSTRUCTION_NODE: {
298                     break;
299                 }
300                     
301                 // comment node
302
case Node.COMMENT_NODE: {
303                     break;
304                 }
305                 // DOCTYPE node
306
case Node.DOCUMENT_TYPE_NODE: {
307                     break;
308                 }
309                 // Notation node
310
case Node.NOTATION_NODE: {
311                         sb.append("public:");
312                         String JavaDoc id = ((Notation JavaDoc)node).getPublicId();
313                         if (id == null) {
314                             sb.append("PUBLIC ");
315                             sb.append(id);
316                             sb.append(" ");
317                         }
318                         id = ((Notation JavaDoc)node).getSystemId();
319                         if (id == null) {
320                             sb.append("system: ");
321                             sb.append(id);
322                             sb.append(" ");
323                         }
324                     break;
325                 }
326             }
327             return sb.toString();
328         }
329             
330         /** Normalizes the given string. */
331         static protected String JavaDoc normalize(String JavaDoc s) {
332             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
333
334             int len = (s != null) ? s.length() : 0;
335             for (int i = 0; i < len; i++) {
336                 char ch = s.charAt(i);
337                 switch (ch) {
338                     case '<': {
339                         str.append("&lt;");
340                         break;
341                     }
342                     case '>': {
343                         str.append("&gt;");
344                         break;
345                     }
346                     case '&': {
347                         str.append("&amp;");
348                         break;
349                     }
350                     case '"': {
351                         str.append("&quot;");
352                         break;
353                     }
354                     case '\r':
355                     case '\n':
356                     default: {
357                         str.append(ch);
358                     }
359                 }
360             }
361
362             return str.toString();
363
364         } // normalize(String):String
365

366         /** Returns a sorted list of attributes. */
367         static protected Attr JavaDoc[] sortAttributes(NamedNodeMap JavaDoc attrs) {
368
369             int len = (attrs != null) ? attrs.getLength() : 0;
370             Attr JavaDoc array[] = new Attr JavaDoc[len];
371             for (int i = 0; i < len; i++) {
372                 array[i] = (Attr JavaDoc)attrs.item(i);
373             }
374             for (int i = 0; i < len - 1; i++) {
375                 String JavaDoc name = array[i].getNodeName();
376                 int index = i;
377                 for (int j = i + 1; j < len; j++) {
378                     String JavaDoc curName = array[j].getNodeName();
379                     if (curName.compareTo(name) < 0) {
380                         name = curName;
381                         index = j;
382                     }
383                 }
384                 if (index != i) {
385                     Attr JavaDoc temp = array[i];
386                     array[i] = array[index];
387                     array[index] = temp;
388                 }
389             }
390
391             return array;
392
393         } // sortAttributes(NamedNodeMap):Attr[]
394

395     } // class DOMTreeFull
396

397
Popular Tags