root / trunk / libraries / libIverUtiles / src / com / iver / utiles / xmlViewer / XMLViewer.java @ 33213
History | View | Annotate | Download (14.4 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.ArrayList; |
50 |
import java.util.Stack; |
51 |
|
52 |
import javax.swing.JPanel; |
53 |
import javax.swing.table.DefaultTableModel; |
54 |
import javax.swing.tree.DefaultMutableTreeNode; |
55 |
import javax.swing.tree.DefaultTreeModel; |
56 |
|
57 |
import org.xml.sax.Attributes; |
58 |
import org.xml.sax.ContentHandler; |
59 |
import org.xml.sax.Locator; |
60 |
import org.xml.sax.SAXException; |
61 |
|
62 |
|
63 |
/**
|
64 |
* Control que visualiza los contenidos de un XML jerarquicamente en un arbol,
|
65 |
* los attributos de cada nodo en una tabla y el texto contenido entre los
|
66 |
* tags en un area de texto
|
67 |
*
|
68 |
* @author Fernando Gonz?lez Cort?s
|
69 |
*/
|
70 |
public class XMLViewer extends JPanel { |
71 |
|
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 |
private class PairValue { |
87 |
public Object field; |
88 |
public Object value; |
89 |
public PairValue(Object field, Object value) { |
90 |
this.field = field;
|
91 |
this.value = value;
|
92 |
} |
93 |
}; |
94 |
|
95 |
|
96 |
/**
|
97 |
* This is the default constructor
|
98 |
*/
|
99 |
public XMLViewer() {
|
100 |
super();
|
101 |
initialize(); |
102 |
} |
103 |
|
104 |
/**
|
105 |
* Establece el origen XML del control
|
106 |
*
|
107 |
* @param content Objeto que lanzar? los eventos del XML a representar
|
108 |
*
|
109 |
* @throws SAXException Si se produce alg?n error en el parseado del
|
110 |
* contenido
|
111 |
*/
|
112 |
public synchronized void setModel(XMLContent content) throws SAXException { |
113 |
this.content = content;
|
114 |
parse(); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Parsea el modelo rellenando los controles con la informaci?n
|
119 |
*
|
120 |
* @throws SAXException Si se produce un error de SAX parseando el
|
121 |
* XMLContent
|
122 |
*/
|
123 |
private void parse() throws SAXException { |
124 |
MyContentHandler contentHandler = new MyContentHandler();
|
125 |
content.setContentHandler(contentHandler); |
126 |
content.parse(); |
127 |
treeModel = new DefaultTreeModel(contentHandler.getRoot()); |
128 |
xmlTree.setModel(treeModel); |
129 |
xmlTree.clearSelection(); |
130 |
// getJSplitPane().setDividerLocation(1.0); // Por defecto
|
131 |
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* This method initializes this
|
136 |
*/
|
137 |
private void initialize() { |
138 |
this.setLayout(new java.awt.BorderLayout()); |
139 |
this.add(getJSplitPane1(), java.awt.BorderLayout.CENTER);
|
140 |
//this.setPreferredSize(new Dimension(600, 600));
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* This method initializes jScrollPane
|
145 |
*
|
146 |
* @return javax.swing.JScrollPane
|
147 |
*/
|
148 |
private javax.swing.JScrollPane getJScrollPane() {
|
149 |
if (jScrollPane == null) { |
150 |
jScrollPane = new javax.swing.JScrollPane();
|
151 |
jScrollPane.setViewportView(getJPanel()); |
152 |
} |
153 |
|
154 |
return jScrollPane;
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* This method initializes xmlTree
|
159 |
*
|
160 |
* @return javax.swing.JTree
|
161 |
*/
|
162 |
public javax.swing.JTree getXmlTree() {
|
163 |
if (xmlTree == null) { |
164 |
xmlTree = new javax.swing.JTree();
|
165 |
xmlTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
166 |
|
167 |
public void valueChanged( |
168 |
javax.swing.event.TreeSelectionEvent e) { |
169 |
DefaultTableModel tableModel = new DefaultTableModel(); |
170 |
for (int i=0;i<namesColumn.length;i++){ |
171 |
tableModel.addColumn(namesColumn[i]); |
172 |
} |
173 |
|
174 |
Node selected = (Node) e.getPath().getLastPathComponent(); |
175 |
xmlTree.expandPath(e.getPath()); |
176 |
// HashMap map = selected.attributes;
|
177 |
// boolean hasAtts = false;
|
178 |
// Iterator i = map.keySet().iterator();
|
179 |
//
|
180 |
// while (i.hasNext()) {
|
181 |
// hasAtts = true;
|
182 |
//
|
183 |
// Object key = i.next();
|
184 |
// tableModel.addRow(new Object[] { key, map.get(key) });
|
185 |
// }
|
186 |
ArrayList atts = selected.attributes;
|
187 |
for (int i = 0; i < atts.size(); i++) { |
188 |
PairValue pair = (PairValue) atts.get(i); |
189 |
tableModel.addRow(new Object[] { pair.field, pair.value}); |
190 |
} |
191 |
|
192 |
getAttributeTable().setModel(tableModel); |
193 |
getAttributeTable().getTableHeader().setVisible(true);
|
194 |
|
195 |
if (selected.content == null) { |
196 |
getTxtContent().setVisible( false);
|
197 |
} else {
|
198 |
getTxtContent().setVisible(true);
|
199 |
getTxtContent().setText(selected.content); |
200 |
getTxtContent().setEditable( false);
|
201 |
getJSplitPane().setDividerLocation(0);//(jSplitPane.getHeight()); |
202 |
} |
203 |
|
204 |
} |
205 |
}); |
206 |
} |
207 |
xmlTree.setShowsRootHandles(true);
|
208 |
xmlTree.setExpandsSelectedPaths(true);
|
209 |
// xmlTree.setToggleClickCount(1);
|
210 |
return xmlTree;
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* This method initializes attributeTable
|
215 |
*
|
216 |
* @return javax.swing.JTable
|
217 |
*/
|
218 |
private javax.swing.JTable getAttributeTable() {
|
219 |
if (attributeTable == null) { |
220 |
attributeTable = new javax.swing.JTable();
|
221 |
attributeTable.getTableHeader().setVisible(true);
|
222 |
} |
223 |
|
224 |
return attributeTable;
|
225 |
} |
226 |
|
227 |
/**
|
228 |
* This method initializes jScrollPane1
|
229 |
*
|
230 |
* @return javax.swing.JScrollPane
|
231 |
*/
|
232 |
private javax.swing.JScrollPane getJScrollPane1() {
|
233 |
if (jScrollPane1 == null) { |
234 |
jScrollPane1 = new javax.swing.JScrollPane();
|
235 |
jScrollPane1.setViewportView(getAttributeTable()); |
236 |
} |
237 |
|
238 |
return jScrollPane1;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* This method initializes jSplitPane
|
243 |
*
|
244 |
* @return javax.swing.JSplitPane
|
245 |
*/
|
246 |
private javax.swing.JSplitPane getJSplitPane() {
|
247 |
if (jSplitPane == null) { |
248 |
jSplitPane = new javax.swing.JSplitPane();
|
249 |
jSplitPane.setTopComponent(getJScrollPane1()); |
250 |
jSplitPane.setBottomComponent(getJScrollPane2()); |
251 |
jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
252 |
//jSplitPane.setOrientation(javax.swing.JSplitPane.HORIZONTAL_SPLIT);
|
253 |
jSplitPane.setDividerLocation(0.9);
|
254 |
jSplitPane.setResizeWeight(0.9);
|
255 |
jSplitPane.setDividerSize(5);
|
256 |
} |
257 |
|
258 |
return jSplitPane;
|
259 |
} |
260 |
|
261 |
/**
|
262 |
* This method initializes jScrollPane2
|
263 |
*
|
264 |
* @return javax.swing.JScrollPane
|
265 |
*/
|
266 |
private javax.swing.JScrollPane getJScrollPane2() {
|
267 |
if (jScrollPane2 == null) { |
268 |
jScrollPane2 = new javax.swing.JScrollPane();
|
269 |
jScrollPane2.setViewportView(getTxtContent()); |
270 |
} |
271 |
|
272 |
return jScrollPane2;
|
273 |
} |
274 |
|
275 |
/**
|
276 |
* This method initializes txtContent
|
277 |
*
|
278 |
* @return javax.swing.JTextArea
|
279 |
*/
|
280 |
private javax.swing.JTextArea getTxtContent() {
|
281 |
if (txtContent == null) { |
282 |
txtContent = new javax.swing.JTextArea();
|
283 |
} |
284 |
|
285 |
return txtContent;
|
286 |
} |
287 |
|
288 |
/**
|
289 |
* This method initializes jSplitPane1
|
290 |
*
|
291 |
* @return javax.swing.JSplitPane
|
292 |
*/
|
293 |
private javax.swing.JSplitPane getJSplitPane1() {
|
294 |
if (jSplitPane1 == null) { |
295 |
jSplitPane1 = new javax.swing.JSplitPane();
|
296 |
jSplitPane1.setLeftComponent(getJScrollPane()); |
297 |
jSplitPane1.setRightComponent(getJSplitPane()); |
298 |
jSplitPane1.setDividerSize(5);
|
299 |
jSplitPane1.setDividerLocation(0.3);
|
300 |
jSplitPane1.setResizeWeight(0.2);
|
301 |
} |
302 |
|
303 |
return jSplitPane1;
|
304 |
} |
305 |
|
306 |
/**
|
307 |
* This method initializes jPanel
|
308 |
*
|
309 |
* @return javax.swing.JPanel
|
310 |
*/
|
311 |
private javax.swing.JPanel getJPanel() {
|
312 |
if (jPanel == null) { |
313 |
jPanel = new javax.swing.JPanel();
|
314 |
jPanel.setLayout(new java.awt.BorderLayout());
|
315 |
jPanel.add(getXmlTree(), java.awt.BorderLayout.CENTER); |
316 |
} |
317 |
|
318 |
return jPanel;
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* This method initializes jPanel1
|
323 |
*
|
324 |
* @return javax.swing.JPanel
|
325 |
*/
|
326 |
/* private javax.swing.JPanel getJPanel1() {
|
327 |
if (jPanel1 == null) {
|
328 |
jPanel1 = new javax.swing.JPanel();
|
329 |
jPanel1.setLayout(new java.awt.BorderLayout());
|
330 |
jPanel1.add(getAttributeTable(), java.awt.BorderLayout.CENTER);
|
331 |
}
|
332 |
|
333 |
return jPanel1;
|
334 |
} */
|
335 |
|
336 |
/**
|
337 |
* Clase nodo que almacena los datos de cada nodo de un XML
|
338 |
*
|
339 |
* @author Fernando Gonz?lez Cort?s
|
340 |
*/
|
341 |
public class Node extends DefaultMutableTreeNode { |
342 |
// public HashMap attributes = new HashMap();
|
343 |
public ArrayList attributes = new ArrayList(); |
344 |
public String content; |
345 |
public String name; |
346 |
|
347 |
/**
|
348 |
* Representaci?n de texto de este objeto
|
349 |
*
|
350 |
* @return String
|
351 |
*/
|
352 |
public String toString() { |
353 |
return name;
|
354 |
} |
355 |
} |
356 |
|
357 |
|
358 |
/**
|
359 |
* Handler que recibe los SAXEvents del XMLContent y se guarda la
|
360 |
* informaci?n de forma jer?rquica en objetos Node
|
361 |
*
|
362 |
* @author Fernando Gonz?lez Cort?s
|
363 |
*/
|
364 |
public class MyContentHandler implements ContentHandler { |
365 |
private Stack node = new Stack(); |
366 |
private boolean betweenTags = false; |
367 |
private Node root = null; |
368 |
|
369 |
/**
|
370 |
* @see org.xml.sax.ContentHandler#endDocument()
|
371 |
*/
|
372 |
public void endDocument() throws SAXException { |
373 |
} |
374 |
|
375 |
/**
|
376 |
* @see org.xml.sax.ContentHandler#startDocument()
|
377 |
*/
|
378 |
public void startDocument() throws SAXException { |
379 |
root = new Node();
|
380 |
node.push(root); |
381 |
} |
382 |
|
383 |
/**
|
384 |
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
|
385 |
*/
|
386 |
public void characters(char[] ch, int start, int length) |
387 |
throws SAXException {
|
388 |
if (!betweenTags) {
|
389 |
return;
|
390 |
} |
391 |
|
392 |
Node actual = (Node) node.peek(); |
393 |
actual.content = new String(ch, start, length).trim(); |
394 |
} |
395 |
|
396 |
/**
|
397 |
* @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int,
|
398 |
* int)
|
399 |
*/
|
400 |
public void ignorableWhitespace(char[] ch, int start, int length) |
401 |
throws SAXException {
|
402 |
} |
403 |
|
404 |
/**
|
405 |
* @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
|
406 |
*/
|
407 |
public void endPrefixMapping(String prefix) throws SAXException { |
408 |
} |
409 |
|
410 |
/**
|
411 |
* @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
|
412 |
*/
|
413 |
public void skippedEntity(String name) throws SAXException { |
414 |
} |
415 |
|
416 |
/**
|
417 |
* @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
418 |
*/
|
419 |
public void setDocumentLocator(Locator locator) { |
420 |
} |
421 |
|
422 |
/**
|
423 |
* @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
|
424 |
* java.lang.String)
|
425 |
*/
|
426 |
public void processingInstruction(String target, String data) |
427 |
throws SAXException {
|
428 |
} |
429 |
|
430 |
/**
|
431 |
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
|
432 |
* java.lang.String)
|
433 |
*/
|
434 |
public void startPrefixMapping(String prefix, String uri) |
435 |
throws SAXException {
|
436 |
} |
437 |
|
438 |
/**
|
439 |
* @see org.xml.sax.ContentHandler#endElement(java.lang.String,
|
440 |
* java.lang.String, java.lang.String)
|
441 |
*/
|
442 |
public void endElement(String namespaceURI, String localName, |
443 |
String qName) throws SAXException { |
444 |
Node n = (Node) node.pop(); |
445 |
((Node) node.peek()).add(n); |
446 |
betweenTags = false;
|
447 |
} |
448 |
|
449 |
/**
|
450 |
* @see org.xml.sax.ContentHandler#startElement(java.lang.String,
|
451 |
* java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
452 |
*/
|
453 |
public void startElement(String namespaceURI, String localName, |
454 |
String qName, Attributes atts) throws SAXException { |
455 |
Node nuevo = new Node();
|
456 |
nuevo.name = qName; |
457 |
|
458 |
for (int i = 0; i < atts.getLength(); i++) { |
459 |
PairValue p = new PairValue(atts.getQName(i), atts.getValue(i));
|
460 |
// nuevo.attributes.put(atts.getQName(i), atts.getValue(i));
|
461 |
nuevo.attributes.add(p); |
462 |
} |
463 |
|
464 |
node.push(nuevo); |
465 |
betweenTags = true;
|
466 |
} |
467 |
|
468 |
/**
|
469 |
* Obtiene la raiz del arbol generado o null si no se ha realizado
|
470 |
* ning?n parseado
|
471 |
*
|
472 |
* @return
|
473 |
*/
|
474 |
public Node getRoot() {
|
475 |
return root;
|
476 |
} |
477 |
} |
478 |
public void setNamesColumn(String[] names){ |
479 |
namesColumn=names; |
480 |
} |
481 |
} |
482 |
|
483 |
// @jve:visual-info decl-index=0 visual-constraint="10,10"
|