Revision 3073 trunk/applications/appCatalogYNomenclatorClient/src/es/gva/cit/catalogClient/metadataXML/XMLTree.java
XMLTree.java | ||
---|---|---|
1 |
|
|
2 | 1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
3 | 2 |
* |
4 | 3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
... | ... | |
45 | 44 |
import org.apache.xml.serialize.XMLSerializer; |
46 | 45 |
|
47 | 46 |
import org.w3c.dom.Document; |
48 |
import org.w3c.dom.Node; |
|
49 |
import org.w3c.dom.NodeList; |
|
50 | 47 |
|
51 |
import org.xml.sax.SAXException; |
|
52 |
|
|
53 | 48 |
/* |
54 | 49 |
* Created on 15-abr-2005 |
55 | 50 |
* |
... | ... | |
65 | 60 |
import java.util.StringTokenizer; |
66 | 61 |
import java.util.Vector; |
67 | 62 |
|
68 |
import javax.xml.parsers.DocumentBuilder; |
|
69 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
70 |
import javax.xml.parsers.ParserConfigurationException; |
|
71 | 63 |
|
72 | 64 |
|
73 | 65 |
/** |
... | ... | |
78 | 70 |
public class XMLTree { |
79 | 71 |
/** |
80 | 72 |
* Create a XML node from a File |
81 |
* |
|
82 | 73 |
* @param file |
83 |
* File name
|
|
74 |
* File name |
|
84 | 75 |
* @return |
85 |
* DOM node
|
|
76 |
* XML node
|
|
86 | 77 |
*/ |
87 |
public static Node XMLToTree(File file) { |
|
78 |
public static XMLNode XMLToTree(File file) {
|
|
88 | 79 |
try { |
89 |
// Step 1: create a DocumentBuilderFactory |
|
90 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
91 |
|
|
92 |
// Step 2: create a DocumentBuilder |
|
93 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
|
94 |
|
|
95 |
// Step 3: parse the input file to geta Document object |
|
96 |
Document doc = db.parse(file); |
|
97 |
|
|
98 |
return doc.getDocumentElement(); |
|
99 |
} catch (ParserConfigurationException e) { |
|
80 |
return new XMLNode(file); |
|
81 |
} catch (Exception e) { |
|
100 | 82 |
// TODO Auto-generated catch block |
101 |
System.err.println(e.toString()); |
|
102 |
|
|
83 |
e.printStackTrace(); |
|
103 | 84 |
return null; |
104 |
} catch (SAXException e1) { |
|
105 |
// TODO Auto-generated catch block |
|
106 |
System.err.println(e1.toString()); |
|
107 |
|
|
108 |
return null; |
|
109 |
} catch (IOException e1) { |
|
110 |
// TODO Auto-generated catch block |
|
111 |
System.err.println(e1.toString()); |
|
112 |
|
|
113 |
return null; |
|
114 | 85 |
} |
115 | 86 |
} |
116 | 87 |
|
117 | 88 |
/** |
118 | 89 |
* Create a XML node from a InputStream |
119 |
* |
|
120 | 90 |
* @param stream |
121 |
* InputStream
|
|
91 |
* InputStream |
|
122 | 92 |
* @return |
123 |
* DOM node
|
|
93 |
* XML node
|
|
124 | 94 |
*/ |
125 |
public static Node XMLToTree(InputStream stream) { |
|
95 |
public static XMLNode XMLToTree(InputStream stream) {
|
|
126 | 96 |
try { |
127 |
if (stream == null) { |
|
128 |
return null; |
|
129 |
} |
|
130 |
|
|
131 |
// Step 1: create a DocumentBuilderFactory |
|
132 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
133 |
|
|
134 |
//Step 2: create a DocumentBuilder |
|
135 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
|
136 |
|
|
137 |
//Step 3: parse the input file to geta Document object |
|
138 |
Document doc = db.parse(stream); |
|
139 |
|
|
140 |
return doc.getDocumentElement(); |
|
141 |
} catch (ParserConfigurationException e) { |
|
97 |
return new XMLNode(stream); |
|
98 |
} catch (Exception e) { |
|
142 | 99 |
// TODO Auto-generated catch block |
143 | 100 |
e.printStackTrace(); |
144 |
} catch (SAXException e1) { |
|
145 |
// TODO Auto-generated catch block |
|
146 |
e1.printStackTrace(); |
|
147 |
} catch (IOException e1) { |
|
148 |
// TODO Auto-generated catch block |
|
149 |
e1.printStackTrace(); |
|
101 |
return null; |
|
150 | 102 |
} |
151 |
|
|
152 |
return null; |
|
153 | 103 |
} |
154 | 104 |
|
155 | 105 |
/** |
156 | 106 |
* Devuelve un fichero que crea a partir de un arbol XML |
157 |
* |
|
158 | 107 |
* @param dom |
159 |
* Documento en XML
|
|
108 |
* Documento en XML |
|
160 | 109 |
* @param nombreFichero |
161 |
* Nombre del fichero.
|
|
110 |
* Nombre del fichero. |
|
162 | 111 |
* @return |
163 |
* Devuelve el fichero escrito
|
|
112 |
* Devuelve el fichero escrito |
|
164 | 113 |
*/ |
165 | 114 |
public static File TreeToXML(Document dom, String nombreFichero) { |
166 | 115 |
OutputFormat format = null; |
... | ... | |
198 | 147 |
|
199 | 148 |
/** |
200 | 149 |
* Busca un Nodo dado una ruta de nodo del tipo "nodoRaiz:nodoPrimerNivel:...:nodoNivelN": |
201 |
* |
|
202 | 150 |
* @param nodoRaiz |
203 |
* Nodo a partir del cual se quiere hacer la b?squeda
|
|
151 |
* Nodo a partir del cual se quiere hacer la b?squeda |
|
204 | 152 |
* @param etiqueta |
205 |
* Ruta del campo que queremos buscar, separando los niveles por ':'
|
|
153 |
* Ruta del campo que queremos buscar, separando los niveles por ':' |
|
206 | 154 |
* @return |
207 |
* Devuelve el Nodo que corresponde a la ruta correcta o 'null' si no
|
|
208 |
* lo encuentra
|
|
155 |
* Devuelve el Nodo que corresponde a la ruta correcta o 'null' si no |
|
156 |
* lo encuentra |
|
209 | 157 |
*/ |
210 |
public static Node searchNode(Node nodoRaiz, String etiqueta) {
|
|
211 |
Node[] nodes = searchMultipleNode(nodoRaiz, etiqueta); |
|
158 |
public static XMLNode searchNode(XMLNode nodoRaiz, String etiqueta) {
|
|
159 |
XMLNode[] nodes = searchMultipleNode(nodoRaiz, etiqueta);
|
|
212 | 160 |
|
213 | 161 |
if ((nodes != null) && (nodes.length > 0)) { |
214 | 162 |
return nodes[0]; |
... | ... | |
219 | 167 |
|
220 | 168 |
/** |
221 | 169 |
* Busca el padre de un Nodo dado una ruta de nodo del tipo "nodoRaiz:nodoPrimerNivel:...:nodoNivelN": |
222 |
* |
|
223 | 170 |
* @param nodoRaiz |
224 |
* Nodo a partir del cual se quiere hacer la b?squeda
|
|
171 |
* Nodo a partir del cual se quiere hacer la b?squeda |
|
225 | 172 |
* @param etiqueta |
226 |
* Ruta del campo que queremos buscar, separando los niveles por ':'
|
|
173 |
* Ruta del campo que queremos buscar, separando los niveles por ':' |
|
227 | 174 |
* @return |
228 |
* Devuelve el Nodo padre que corresponde a la ruta correcta o 'null' si no
|
|
229 |
* lo encuentra
|
|
175 |
* Devuelve el Nodo padre que corresponde a la ruta correcta o 'null' si no |
|
176 |
* lo encuentra |
|
230 | 177 |
*/ |
231 |
public static Node searchParentNode(Node nodoRaiz, String etiqueta) {
|
|
232 |
StringTokenizer sti = new StringTokenizer(etiqueta, "->");
|
|
178 |
public static XMLNode searchParentNode(XMLNode rootNode, String label) {
|
|
179 |
StringTokenizer sti = new StringTokenizer(label, "->");
|
|
233 | 180 |
|
234 |
if (nodoRaiz == null) {
|
|
181 |
if (rootNode == null) {
|
|
235 | 182 |
return null; |
236 | 183 |
} |
237 |
|
|
238 |
Node n = nodoRaiz.getFirstChild(); |
|
239 |
|
|
184 |
|
|
185 |
XMLNode currentNode = rootNode.getSubNode(0); |
|
186 |
XMLNode parentNode = rootNode; |
|
187 |
|
|
240 | 188 |
//A cuantos niveles est? el TOKEN |
241 | 189 |
int niveles = sti.countTokens(); |
242 | 190 |
|
243 | 191 |
String nombreNodo = sti.nextToken(); |
244 | 192 |
|
245 | 193 |
int nivelActual = 1; |
194 |
int i = 0; |
|
246 | 195 |
|
247 |
while (n != null) {
|
|
248 |
if (nombreNodo.equals(n.getNodeName())) {
|
|
196 |
while (i < parentNode.getNumSubNodes()) {
|
|
197 |
if (nombreNodo.equals(currentNode.getName())) {
|
|
249 | 198 |
if (niveles == nivelActual) { |
250 |
return n.getParentNode();
|
|
199 |
return parentNode;
|
|
251 | 200 |
} |
252 |
|
|
253 |
n = n.getFirstChild();
|
|
201 |
parentNode = currentNode; |
|
202 |
currentNode = currentNode.getSubNode(0);
|
|
254 | 203 |
nombreNodo = sti.nextToken(); |
255 | 204 |
nivelActual++; |
205 |
i = 0; |
|
256 | 206 |
} else { |
257 |
// Sugiente nodo del mismo nivel
|
|
258 |
n = n.getNextSibling();
|
|
207 |
currentNode = currentNode.getSubNode(i);
|
|
208 |
i ++;
|
|
259 | 209 |
} |
260 | 210 |
} |
261 | 211 |
|
... | ... | |
272 | 222 |
* @return |
273 | 223 |
* Valor del atributo, o null si no lo ha encontrado |
274 | 224 |
*/ |
275 |
public static String searchAtribute(Node nodo, String nombreAtributo) { |
|
276 |
for (int i = 0; i < nodo.getAttributes().getLength(); i++) |
|
277 |
if (nodo.getAttributes().item(i).getNodeName().equals(nombreAtributo)) { |
|
278 |
return nodo.getAttributes().item(i).getNodeValue(); |
|
279 |
} |
|
280 |
|
|
281 |
return null; |
|
225 |
public static String searchAtribute(XMLNode node, String attributeName) { |
|
226 |
return node.getAttribute(attributeName); |
|
282 | 227 |
} |
283 | 228 |
|
284 | 229 |
/** |
285 | 230 |
* Hace una busqueda de una etiqueta en un nodo y devuelve |
286 | 231 |
* su valor |
287 |
* |
|
288 | 232 |
* @param nodo |
289 |
* Nodo del que se quiere buscar el atributo
|
|
233 |
* Nodo del que se quiere buscar el atributo |
|
290 | 234 |
* @param etiqueta |
291 |
* Nombre de la etiqueta
|
|
235 |
* Nombre de la etiqueta |
|
292 | 236 |
* @return |
293 |
* Valor de la etiqueta
|
|
237 |
* Valor de la etiqueta |
|
294 | 238 |
*/ |
295 |
public static String searchNodeValue(Node nodo, String etiqueta) { |
|
296 |
Node nodoB = searchNode(nodo, etiqueta); |
|
297 |
|
|
298 |
if (nodoB == null) { |
|
239 |
public static String searchNodeValue(XMLNode node, String etiqueta) { |
|
240 |
XMLNode nodoB = searchNode(node, etiqueta); |
|
241 |
if (nodoB == null) |
|
299 | 242 |
return null; |
300 |
} |
|
301 |
|
|
302 |
if (nodoB.getFirstChild() == null) { |
|
303 |
return null; |
|
304 |
} |
|
305 |
|
|
306 |
return nodoB.getFirstChild().getNodeValue(); |
|
243 |
return nodoB.getText(); |
|
244 |
|
|
307 | 245 |
} |
308 | 246 |
|
309 | 247 |
/** |
... | ... | |
318 | 256 |
* @return |
319 | 257 |
* Valor del atributo de la etiqueta o null |
320 | 258 |
*/ |
321 |
public static String searchNodeAtribute(Node nodo, String etiqueta,
|
|
259 |
public static String searchNodeAtribute(XMLNode node, String etiqueta,
|
|
322 | 260 |
String atributo) { |
323 |
Node nodoB = searchNode(nodo, etiqueta);
|
|
261 |
XMLNode nodoB = searchNode(node, etiqueta);
|
|
324 | 262 |
|
325 | 263 |
if (nodoB == null) { |
326 | 264 |
return null; |
... | ... | |
333 | 271 |
* Hace una busqueda de nodos que se llaman igual y devuleve el valor |
334 | 272 |
* |
335 | 273 |
* @param rootNode |
336 |
* Nodo a partir del cual se quiere hacer la b?squeda
|
|
274 |
* Nodo a partir del cual se quiere hacer la b?squeda |
|
337 | 275 |
* @param parentLabel |
338 |
* Ruta del campo que queremos buscar, separando los niveles por '->'
|
|
276 |
* Ruta del campo que queremos buscar, separando los niveles por '->' |
|
339 | 277 |
* @param label |
340 |
* Node label
|
|
278 |
* Node label |
|
341 | 279 |
* @return |
342 |
* Un vector con valores de las etiquetas
|
|
280 |
* Un vector con valores de las etiquetas |
|
343 | 281 |
*/ |
344 |
public static String[] searchMultipleNodeValue(Node rootNode, String label) { |
|
345 |
Node[] nodes = searchMultipleNode(rootNode, label); |
|
282 |
public static String[] searchMultipleNodeValue(XMLNode rootNode, String label) {
|
|
283 |
XMLNode[] nodes = searchMultipleNode(rootNode, label);
|
|
346 | 284 |
|
347 | 285 |
if ((nodes == null) || (nodes.length == 0)) { |
348 | 286 |
return null; |
... | ... | |
351 | 289 |
String[] values = new String[nodes.length]; |
352 | 290 |
|
353 | 291 |
for (int i = 0; i < nodes.length; i++) |
354 |
if (nodes[i].getFirstChild() != null) { |
|
355 |
values[i] = nodes[i].getFirstChild().getNodeValue();
|
|
356 |
} |
|
292 |
//if (nodes[i].getFirstChild() != null) {
|
|
293 |
values[i] = nodes[i].getText();
|
|
294 |
//}
|
|
357 | 295 |
|
358 | 296 |
return values; |
359 | 297 |
} |
... | ... | |
361 | 299 |
|
362 | 300 |
/** |
363 | 301 |
* Hace una busqueda de nodos que se llaman igual desde uno dado(sin recursividad) |
364 |
* |
|
365 | 302 |
* @param nodoRaiz |
366 |
* Nodo a partir del cual se quiere hacer la b?squeda
|
|
303 |
* Nodo a partir del cual se quiere hacer la b?squeda |
|
367 | 304 |
* @param etiqueta |
368 |
* Ruta del campo que queremos buscar, separando los niveles por ':'
|
|
305 |
* Ruta del campo que queremos buscar, separando los niveles por ':' |
|
369 | 306 |
* @return |
370 |
* Un vector con los nodos que ha encontrado
|
|
307 |
* Un vector con los nodos que ha encontrado |
|
371 | 308 |
*/ |
372 |
public static Node[] searchMultipleNode(Node nodoRaiz, String label) {
|
|
309 |
public static XMLNode[] searchMultipleNode(XMLNode nodoRaiz, String label) {
|
|
373 | 310 |
Vector rootNodes = new Vector(); |
374 | 311 |
Vector leafNodes = new Vector(); |
375 | 312 |
|
... | ... | |
387 | 324 |
leafNodes.clear(); |
388 | 325 |
|
389 | 326 |
for (int i = 0; i < rootNodes.size(); i++) { |
390 |
Node root = (Node) rootNodes.get(i);
|
|
327 |
XMLNode root = (XMLNode) rootNodes.get(i);
|
|
391 | 328 |
|
392 | 329 |
if (root != null) { |
393 |
NodeList nodes = root.getChildNodes();
|
|
330 |
XMLNode[] nodes = root.getSubnodes();
|
|
394 | 331 |
|
395 |
for (int j = 0; j < nodes.getLength(); j++) {
|
|
396 |
if (nodes.item(j).getNodeName().equals(firstLabel)) {
|
|
397 |
leafNodes.add(nodes.item(j));
|
|
332 |
for (int j = 0; j < nodes.length; j++) {
|
|
333 |
if (nodes[j].getName().equals(firstLabel)) {
|
|
334 |
leafNodes.add(nodes[j]);
|
|
398 | 335 |
} |
399 | 336 |
} |
400 | 337 |
} |
... | ... | |
403 | 340 |
k++; |
404 | 341 |
} |
405 | 342 |
|
406 |
Node[] nodes = new Node[leafNodes.size()];
|
|
343 |
XMLNode[] nodes = new XMLNode[leafNodes.size()];
|
|
407 | 344 |
|
408 | 345 |
for (int i = 0; i < leafNodes.size(); i++) |
409 |
nodes[i] = (Node) leafNodes.get(i); |
|
346 |
nodes[i] = (XMLNode) leafNodes.get(i);
|
|
410 | 347 |
|
411 | 348 |
return nodes; |
412 | 349 |
} |
... | ... | |
479 | 416 |
* @param node |
480 | 417 |
* @param value |
481 | 418 |
*/ |
482 |
public static void setNodeValue(Node node, String value) { |
|
483 |
if (node.getFirstChild() != null) { |
|
484 |
node.getFirstChild().setNodeValue(value); |
|
485 |
} |
|
419 |
public static void setNodeValue(XMLNode node, String value) { |
|
420 |
node.setText(value); |
|
486 | 421 |
} |
487 | 422 |
|
488 | 423 |
/** |
Also available in: Unified diff