1 package net.logAnalyzer.gui.config;
2
3 import java.util.Vector;
4
5 import net.logAnalyzer.utils.gui.treetable.AbstractTreeTableModel;
6 import net.logAnalyzer.utils.gui.treetable.TreeTableModel;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.NamedNodeMap;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14 /***
15 * FileSystemModel is a TreeTableModel representing a hierarchical file system. Nodes in the
16 * FileSystemModel are FileNodes which, when they are directory nodes, cache their children to avoid
17 * repeatedly querying the real file system.
18 * @version %I% %G%
19 * @author Philip Milne
20 * @author Scott Violet
21 */
22
23 public class XMLDocumentModel extends AbstractTreeTableModel implements TreeTableModel {
24
25
26 static protected String[] cNames = {"Name", "Value"};
27
28
29 static protected Class[] cTypes = {TreeTableModel.class, String.class};
30
31
32 public static final Integer ZERO = new Integer(0);
33
34 private Document doc;
35
36 public XMLDocumentModel(Document doc) {
37 super(new XMLNode(doc.getDocumentElement()));
38 Element node = doc.getDocumentElement();
39 this.doc = doc;
40 }
41
42
43
44
45
46 protected XMLNode getXMLNode(Object node) {
47 XMLNode currentnode = ((XMLNode) node);
48 return currentnode;
49 }
50
51 protected Object[] getChildren(Object node) {
52 XMLNode currentnode = ((XMLNode) node);
53 return currentnode.getChildren();
54 }
55
56
57
58
59
60 public int getChildCount(Object node) {
61 Object[] children = getChildren(node);
62 return (children == null) ? 0 : children.length;
63 }
64
65 public Object getChild(Object node, int i) {
66 return getChildren(node)[i];
67 }
68
69
70 public boolean isLeaf(Object node) {
71 return !getXMLNode(node).hasChildNodes();
72 }
73
74
75
76
77
78 public int getColumnCount() {
79 return cNames.length;
80 }
81
82 public String getColumnName(int column) {
83
84 return null;
85 }
86
87 public Class getColumnClass(int column) {
88 return cTypes[column];
89 }
90
91 public Object getValueAt(Object node, int column) {
92 XMLNode currentnode = ((XMLNode) node);
93 try {
94 switch (column) {
95 case 0:
96 return currentnode.toString();
97 case 1:
98 return currentnode.getValue();
99 default:
100 return new String("");
101 }
102 } catch (SecurityException se) {
103 }
104
105 return null;
106 }
107 }
108
109
110 class XMLNode {
111 Node element;
112
113 Object[] children;
114 String value;
115
116 public XMLNode(Node element) {
117 this.element = element;
118 }
119
120 /***
121 * Returns the the string to be used to display this leaf in the JTree.
122 */
123 public String toString() {
124 return element.getNodeName();
125 }
126
127 public Node getElement() {
128 return element;
129 }
130
131 public int getNodeType() {
132 return element.getNodeType();
133 }
134
135 public String getValue() {
136 String returnValue=element.getNodeValue();
137 if (element.getNodeType() == Node.ELEMENT_NODE){
138 returnValue=this.value;
139 }
140 return returnValue;
141 }
142
143 /***
144 * Loads the children, caching the results in the children ivar.
145 */
146 protected Object[] getChildren() {
147 if (children != null) {
148 return children;
149 }
150 try {
151 Vector childs = new Vector();
152 NamedNodeMap attribs = element.getAttributes();
153 if (attribs != null && attribs.getLength() > 0) {
154 for (int i = 0; i < attribs.getLength(); i++) {
155 Node node = attribs.item(i);
156 if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
157 XMLNode childNode = new XMLNode(node);
158 childs.add(childNode);
159 }
160
161 }
162 }
163 if (element.hasChildNodes()) {
164 NodeList nodelist = element.getChildNodes();
165 if (nodelist != null) {
166
167 for (int i = 0; i < nodelist.getLength(); i++) {
168 Node node = nodelist.item(i);
169 if (node.getNodeType() != Node.TEXT_NODE) {
170 XMLNode childNode = new XMLNode(node);
171 childs.add(childNode);
172 }else{
173 this.value = node.getNodeValue();
174 }
175
176 }
177 }
178 }
179 children = childs.toArray();
180 } catch (SecurityException se) {
181 }
182
183 return children;
184 }
185
186 protected boolean hasChildNodes() {
187 boolean hasChild = false;
188 if (getChildren().length > 0) {
189 hasChild = true;
190 } else {
191 hasChild = false;
192 }
193 return hasChild;
194 }
195 }