svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / com / iver / utiles / xmlViewer / XMLViewer.java @ 5202
History | View | Annotate | Download (13.5 KB)
1 |
/*
|
---|---|
2 |
* Created on 13-sep-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package com.iver.utiles.xmlViewer; |
48 |
|
49 |
import java.util.HashMap; |
50 |
import java.util.Iterator; |
51 |
import java.util.Stack; |
52 |
|
53 |
import javax.swing.JPanel; |
54 |
import javax.swing.table.DefaultTableModel; |
55 |
import javax.swing.tree.DefaultMutableTreeNode; |
56 |
import javax.swing.tree.DefaultTreeModel; |
57 |
|
58 |
import org.xml.sax.Attributes; |
59 |
import org.xml.sax.ContentHandler; |
60 |
import org.xml.sax.Locator; |
61 |
import org.xml.sax.SAXException; |
62 |
|
63 |
|
64 |
/**
|
65 |
* Control que visualiza los contenidos de un XML jerarquicamente en un arbol,
|
66 |
* los attributos de cada nodo en una tabla y el texto contenido entre los
|
67 |
* tags en un area de texto
|
68 |
*
|
69 |
* @author Fernando Gonz?lez Cort?s
|
70 |
*/
|
71 |
public class XMLViewer extends JPanel { |
72 |
private javax.swing.JScrollPane jScrollPane = null; |
73 |
private javax.swing.JTree xmlTree = null; |
74 |
private javax.swing.JScrollPane jScrollPane1 = null; |
75 |
private javax.swing.JTable attributeTable = null; |
76 |
private javax.swing.JSplitPane jSplitPane = null; |
77 |
private javax.swing.JScrollPane jScrollPane2 = null; |
78 |
private javax.swing.JTextArea txtContent = null; |
79 |
private javax.swing.JSplitPane jSplitPane1 = null; |
80 |
private DefaultTreeModel treeModel = null; |
81 |
private XMLContent content;
|
82 |
private javax.swing.JPanel jPanel = null; |
83 |
private javax.swing.JPanel jPanel1 = null; |
84 |
private String[] namesColumn; |
85 |
|
86 |
/**
|
87 |
* This is the default constructor
|
88 |
*/
|
89 |
public XMLViewer() {
|
90 |
super();
|
91 |
initialize(); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Establece el origen XML del control
|
96 |
*
|
97 |
* @param content Objeto que lanzar? los eventos del XML a representar
|
98 |
*
|
99 |
* @throws SAXException Si se produce alg?n error en el parseado del
|
100 |
* contenido
|
101 |
*/
|
102 |
public void setModel(XMLContent content) throws SAXException { |
103 |
this.content = content;
|
104 |
parse(); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* Parsea el modelo rellenando los controles con la informaci?n
|
109 |
*
|
110 |
* @throws SAXException Si se produce un error de SAX parseando el
|
111 |
* XMLContent
|
112 |
*/
|
113 |
private void parse() throws SAXException { |
114 |
MyContentHandler contentHandler = new MyContentHandler();
|
115 |
content.setContentHandler(contentHandler); |
116 |
content.parse(); |
117 |
treeModel = new DefaultTreeModel(contentHandler.getRoot()); |
118 |
xmlTree.setModel(treeModel); |
119 |
xmlTree.clearSelection(); |
120 |
// getJSplitPane().setDividerLocation(1.0); // Por defecto
|
121 |
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* This method initializes this
|
126 |
*/
|
127 |
private void initialize() { |
128 |
this.setLayout(new java.awt.BorderLayout()); |
129 |
this.add(getJSplitPane1(), java.awt.BorderLayout.CENTER);
|
130 |
this.setSize(579, 379); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* This method initializes jScrollPane
|
135 |
*
|
136 |
* @return javax.swing.JScrollPane
|
137 |
*/
|
138 |
private javax.swing.JScrollPane getJScrollPane() {
|
139 |
if (jScrollPane == null) { |
140 |
jScrollPane = new javax.swing.JScrollPane();
|
141 |
jScrollPane.setViewportView(getJPanel()); |
142 |
} |
143 |
|
144 |
return jScrollPane;
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* This method initializes xmlTree
|
149 |
*
|
150 |
* @return javax.swing.JTree
|
151 |
*/
|
152 |
public javax.swing.JTree getXmlTree() {
|
153 |
if (xmlTree == null) { |
154 |
xmlTree = new javax.swing.JTree();
|
155 |
xmlTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
156 |
|
157 |
public void valueChanged( |
158 |
javax.swing.event.TreeSelectionEvent e) { |
159 |
DefaultTableModel tableModel = new DefaultTableModel(); |
160 |
for (int i=0;i<namesColumn.length;i++){ |
161 |
tableModel.addColumn(namesColumn[i]); |
162 |
} |
163 |
|
164 |
Node selected = (Node) e.getPath().getLastPathComponent(); |
165 |
xmlTree.expandPath(e.getPath()); |
166 |
HashMap map = selected.attributes;
|
167 |
boolean hasAtts = false; |
168 |
Iterator i = map.keySet().iterator();
|
169 |
|
170 |
while (i.hasNext()) {
|
171 |
hasAtts = true;
|
172 |
|
173 |
Object key = i.next();
|
174 |
tableModel.addRow(new Object[] { key, map.get(key) }); |
175 |
} |
176 |
|
177 |
attributeTable.setModel(tableModel); |
178 |
attributeTable.getTableHeader().setVisible(true);
|
179 |
|
180 |
if (selected.content == null) { |
181 |
getJSplitPane().setDividerLocation(1.0);
|
182 |
} else {
|
183 |
getJSplitPane().setDividerLocation(0.0);
|
184 |
} |
185 |
txtContent.setText(selected.content); |
186 |
} |
187 |
}); |
188 |
} |
189 |
xmlTree.setShowsRootHandles(true);
|
190 |
xmlTree.setExpandsSelectedPaths(true);
|
191 |
// xmlTree.setToggleClickCount(1);
|
192 |
return xmlTree;
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* This method initializes attributeTable
|
197 |
*
|
198 |
* @return javax.swing.JTable
|
199 |
*/
|
200 |
private javax.swing.JTable getAttributeTable() {
|
201 |
if (attributeTable == null) { |
202 |
attributeTable = new javax.swing.JTable();
|
203 |
attributeTable.getTableHeader().setVisible(true);
|
204 |
} |
205 |
|
206 |
return attributeTable;
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* This method initializes jScrollPane1
|
211 |
*
|
212 |
* @return javax.swing.JScrollPane
|
213 |
*/
|
214 |
private javax.swing.JScrollPane getJScrollPane1() {
|
215 |
if (jScrollPane1 == null) { |
216 |
jScrollPane1 = new javax.swing.JScrollPane();
|
217 |
jScrollPane1.setViewportView(getAttributeTable()); |
218 |
} |
219 |
|
220 |
return jScrollPane1;
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* This method initializes jSplitPane
|
225 |
*
|
226 |
* @return javax.swing.JSplitPane
|
227 |
*/
|
228 |
private javax.swing.JSplitPane getJSplitPane() {
|
229 |
if (jSplitPane == null) { |
230 |
jSplitPane = new javax.swing.JSplitPane();
|
231 |
jSplitPane.setTopComponent(getJScrollPane1()); |
232 |
jSplitPane.setBottomComponent(getJScrollPane2()); |
233 |
jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
234 |
jSplitPane.setDividerLocation(100);
|
235 |
jSplitPane.setDividerSize(5);
|
236 |
} |
237 |
|
238 |
return jSplitPane;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* This method initializes jScrollPane2
|
243 |
*
|
244 |
* @return javax.swing.JScrollPane
|
245 |
*/
|
246 |
private javax.swing.JScrollPane getJScrollPane2() {
|
247 |
if (jScrollPane2 == null) { |
248 |
jScrollPane2 = new javax.swing.JScrollPane();
|
249 |
jScrollPane2.setViewportView(getTxtContent()); |
250 |
} |
251 |
|
252 |
return jScrollPane2;
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* This method initializes txtContent
|
257 |
*
|
258 |
* @return javax.swing.JTextArea
|
259 |
*/
|
260 |
private javax.swing.JTextArea getTxtContent() {
|
261 |
if (txtContent == null) { |
262 |
txtContent = new javax.swing.JTextArea();
|
263 |
} |
264 |
|
265 |
return txtContent;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* This method initializes jSplitPane1
|
270 |
*
|
271 |
* @return javax.swing.JSplitPane
|
272 |
*/
|
273 |
private javax.swing.JSplitPane getJSplitPane1() {
|
274 |
if (jSplitPane1 == null) { |
275 |
jSplitPane1 = new javax.swing.JSplitPane();
|
276 |
jSplitPane1.setLeftComponent(getJScrollPane()); |
277 |
jSplitPane1.setRightComponent(getJSplitPane()); |
278 |
jSplitPane1.setDividerSize(4);
|
279 |
jSplitPane1.setDividerLocation(200);
|
280 |
} |
281 |
|
282 |
return jSplitPane1;
|
283 |
} |
284 |
|
285 |
/**
|
286 |
* This method initializes jPanel
|
287 |
*
|
288 |
* @return javax.swing.JPanel
|
289 |
*/
|
290 |
private javax.swing.JPanel getJPanel() {
|
291 |
if (jPanel == null) { |
292 |
jPanel = new javax.swing.JPanel();
|
293 |
jPanel.setLayout(new java.awt.BorderLayout());
|
294 |
jPanel.add(getXmlTree(), java.awt.BorderLayout.CENTER); |
295 |
} |
296 |
|
297 |
return jPanel;
|
298 |
} |
299 |
|
300 |
/**
|
301 |
* This method initializes jPanel1
|
302 |
*
|
303 |
* @return javax.swing.JPanel
|
304 |
*/
|
305 |
/* private javax.swing.JPanel getJPanel1() {
|
306 |
if (jPanel1 == null) {
|
307 |
jPanel1 = new javax.swing.JPanel();
|
308 |
jPanel1.setLayout(new java.awt.BorderLayout());
|
309 |
jPanel1.add(getAttributeTable(), java.awt.BorderLayout.CENTER);
|
310 |
}
|
311 |
|
312 |
return jPanel1;
|
313 |
} */
|
314 |
|
315 |
/**
|
316 |
* Clase nodo que almacena los datos de cada nodo de un XML
|
317 |
*
|
318 |
* @author Fernando Gonz?lez Cort?s
|
319 |
*/
|
320 |
public class Node extends DefaultMutableTreeNode { |
321 |
public HashMap attributes = new HashMap(); |
322 |
public String content; |
323 |
public String name; |
324 |
|
325 |
/**
|
326 |
* Representaci?n de texto de este objeto
|
327 |
*
|
328 |
* @return String
|
329 |
*/
|
330 |
public String toString() { |
331 |
return name;
|
332 |
} |
333 |
} |
334 |
|
335 |
/**
|
336 |
* Handler que recibe los SAXEvents del XMLContent y se guarda la
|
337 |
* informaci?n de forma jer?rquica en objetos Node
|
338 |
*
|
339 |
* @author Fernando Gonz?lez Cort?s
|
340 |
*/
|
341 |
public class MyContentHandler implements ContentHandler { |
342 |
private Stack node = new Stack(); |
343 |
private boolean betweenTags = false; |
344 |
private Node root = null; |
345 |
|
346 |
/**
|
347 |
* @see org.xml.sax.ContentHandler#endDocument()
|
348 |
*/
|
349 |
public void endDocument() throws SAXException { |
350 |
} |
351 |
|
352 |
/**
|
353 |
* @see org.xml.sax.ContentHandler#startDocument()
|
354 |
*/
|
355 |
public void startDocument() throws SAXException { |
356 |
root = new Node();
|
357 |
node.push(root); |
358 |
} |
359 |
|
360 |
/**
|
361 |
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
|
362 |
*/
|
363 |
public void characters(char[] ch, int start, int length) |
364 |
throws SAXException {
|
365 |
if (!betweenTags) {
|
366 |
return;
|
367 |
} |
368 |
|
369 |
Node actual = (Node) node.peek(); |
370 |
actual.content = new String(ch, start, length).trim(); |
371 |
} |
372 |
|
373 |
/**
|
374 |
* @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int,
|
375 |
* int)
|
376 |
*/
|
377 |
public void ignorableWhitespace(char[] ch, int start, int length) |
378 |
throws SAXException {
|
379 |
} |
380 |
|
381 |
/**
|
382 |
* @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
|
383 |
*/
|
384 |
public void endPrefixMapping(String prefix) throws SAXException { |
385 |
} |
386 |
|
387 |
/**
|
388 |
* @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
|
389 |
*/
|
390 |
public void skippedEntity(String name) throws SAXException { |
391 |
} |
392 |
|
393 |
/**
|
394 |
* @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
395 |
*/
|
396 |
public void setDocumentLocator(Locator locator) { |
397 |
} |
398 |
|
399 |
/**
|
400 |
* @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
|
401 |
* java.lang.String)
|
402 |
*/
|
403 |
public void processingInstruction(String target, String data) |
404 |
throws SAXException {
|
405 |
} |
406 |
|
407 |
/**
|
408 |
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
|
409 |
* java.lang.String)
|
410 |
*/
|
411 |
public void startPrefixMapping(String prefix, String uri) |
412 |
throws SAXException {
|
413 |
} |
414 |
|
415 |
/**
|
416 |
* @see org.xml.sax.ContentHandler#endElement(java.lang.String,
|
417 |
* java.lang.String, java.lang.String)
|
418 |
*/
|
419 |
public void endElement(String namespaceURI, String localName, |
420 |
String qName) throws SAXException { |
421 |
Node n = (Node) node.pop(); |
422 |
((Node) node.peek()).add(n); |
423 |
betweenTags = false;
|
424 |
} |
425 |
|
426 |
/**
|
427 |
* @see org.xml.sax.ContentHandler#startElement(java.lang.String,
|
428 |
* java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
429 |
*/
|
430 |
public void startElement(String namespaceURI, String localName, |
431 |
String qName, Attributes atts) throws SAXException { |
432 |
Node nuevo = new Node();
|
433 |
nuevo.name = qName; |
434 |
|
435 |
for (int i = 0; i < atts.getLength(); i++) { |
436 |
nuevo.attributes.put(atts.getLocalName(i), atts.getValue(i)); |
437 |
} |
438 |
|
439 |
node.push(nuevo); |
440 |
betweenTags = true;
|
441 |
} |
442 |
|
443 |
/**
|
444 |
* Obtiene la raiz del arbol generado o null si no se ha realizado
|
445 |
* ning?n parseado
|
446 |
*
|
447 |
* @return
|
448 |
*/
|
449 |
public Node getRoot() {
|
450 |
return root;
|
451 |
} |
452 |
} |
453 |
public void setNamesColumn(String[] names){ |
454 |
namesColumn=names; |
455 |
} |
456 |
} |
457 |
|
458 |
// @jve:visual-info decl-index=0 visual-constraint="10,10"
|