root / trunk / extensions / extWCS / src / es / uji / lsi / wcs / XmlWcsParsing / XMLNode.java @ 1877
History | View | Annotate | Download (5.99 KB)
1 |
package es.uji.lsi.wcs.XmlWcsParsing; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileWriter; |
5 |
import java.io.InputStream; |
6 |
import java.io.StringWriter; |
7 |
import java.io.Writer; |
8 |
import java.util.Hashtable; |
9 |
import java.util.Vector; |
10 |
|
11 |
import javax.xml.parsers.DocumentBuilderFactory; |
12 |
|
13 |
import org.w3c.dom.Document; |
14 |
import org.w3c.dom.Element; |
15 |
import org.w3c.dom.NamedNodeMap; |
16 |
import org.w3c.dom.Node; |
17 |
import org.w3c.dom.NodeList; |
18 |
|
19 |
/* Esta clase representa un XMLNode simplificado
|
20 |
* Contiene una lista de subnodos y una lista de atributos.
|
21 |
* Tambi?n tiene una cadena.
|
22 |
*
|
23 |
* Soporta la lectura y escritura de y desde un fichero XML.
|
24 |
* Tambi?n soporta la lectura desde Internet
|
25 |
*
|
26 |
* Modificado por jaume
|
27 |
*/
|
28 |
|
29 |
public class XMLNode { |
30 |
|
31 |
// subnodos
|
32 |
private Vector subNodes = new Vector(); |
33 |
// atributos
|
34 |
private Hashtable attr = new Hashtable(); |
35 |
private String nodeName; |
36 |
private String text = null; |
37 |
// los nombres de los atributos
|
38 |
private Vector attrKeys = new Vector(); |
39 |
// cabecera XML
|
40 |
private String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
41 |
|
42 |
// Constructor, lee de un fichero
|
43 |
public XMLNode(File file) throws Exception { |
44 |
this(factory.newDocumentBuilder().parse(file));
|
45 |
} |
46 |
|
47 |
// Constructor, usando url.openStream() puedo leer de internet
|
48 |
public XMLNode(InputStream inputstream) throws Exception{ |
49 |
this(factory.newDocumentBuilder().parse(inputstream));
|
50 |
} |
51 |
|
52 |
// Contructor, constructor desde un documento DOM
|
53 |
public XMLNode(Document dom) throws Exception { |
54 |
this((Element)dom.getFirstChild()); |
55 |
} |
56 |
|
57 |
// Contructor, crea un nodo con su nombre
|
58 |
public XMLNode(String name) throws Exception { |
59 |
nodeName = name; |
60 |
} |
61 |
|
62 |
// Contructor, crea un nodo con su nombre y el texto
|
63 |
public XMLNode(String name, String text) throws Exception { |
64 |
nodeName = name; |
65 |
this.text = text;
|
66 |
} |
67 |
|
68 |
// Contructor, desde un elemento DOM
|
69 |
public XMLNode(Element dom) throws Exception { |
70 |
nodeName = dom.getNodeName(); |
71 |
NamedNodeMap map = dom.getAttributes(); |
72 |
for (int i = 0; i < map.getLength(); i++) { |
73 |
Node att = map.item(i); |
74 |
addAtrribute(att.getNodeName(), att.getNodeValue()); |
75 |
} |
76 |
NodeList nodeList = dom.getChildNodes(); |
77 |
for (int i = 0; i < nodeList.getLength(); i++) { |
78 |
Node sub = nodeList.item(i); |
79 |
if (sub.getNodeType() == Node.ELEMENT_NODE) {
|
80 |
addSubNode(new XMLNode((Element)sub)); |
81 |
} |
82 |
else if (sub.getNodeType() == Node.TEXT_NODE) { |
83 |
String s = sub.getNodeValue().trim();
|
84 |
if (s.length() > 0) { |
85 |
if (text != null) { |
86 |
throw new Exception("XMLNode '" + nodeName + "' has 2 Textblocks"); |
87 |
} |
88 |
text = s; |
89 |
} |
90 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
public void setText(String s) { |
95 |
text = s; |
96 |
} |
97 |
|
98 |
public void addSubNode(XMLNode s) { |
99 |
subNodes.add(s); |
100 |
} |
101 |
|
102 |
public void addAtrribute(String name, String value) throws Exception { |
103 |
if (attr.containsKey(name)) {
|
104 |
throw new Exception( |
105 |
"XMLNode '" + nodeName + "' already contains Attribute '" + name + "'"); |
106 |
} |
107 |
attr.put(name, value); |
108 |
attrKeys.add(name); |
109 |
} |
110 |
|
111 |
public int getNumSubNodes() { |
112 |
return subNodes.size();
|
113 |
} |
114 |
|
115 |
public String getName() { |
116 |
return nodeName;
|
117 |
} |
118 |
|
119 |
public String getText() { |
120 |
return text;
|
121 |
} |
122 |
|
123 |
public XMLNode getSubNode(int index) { |
124 |
return (XMLNode)subNodes.get(index);
|
125 |
} |
126 |
|
127 |
public Vector getAttributeNames() { |
128 |
return attrKeys;
|
129 |
} |
130 |
|
131 |
public void write(Writer wr) throws Exception { |
132 |
wwrite("", wr);
|
133 |
} |
134 |
|
135 |
// escribe el c?digo XML de este objeto con su identaci?n
|
136 |
private void wwrite(String pre, Writer wr) throws Exception { |
137 |
wr.write(pre + "<" + nodeName);
|
138 |
for (int i = 0; i < attrKeys.size(); i++) { |
139 |
String name = (String)attrKeys.get(i); |
140 |
String val = (String)attr.get(name); |
141 |
wr.write(" " + name + "='" + val + "'"); |
142 |
} |
143 |
if (getNumSubNodes() == 0 && text == null) { |
144 |
wr.write("/>\n");
|
145 |
} |
146 |
else {
|
147 |
wr.write(">");
|
148 |
if (text != null) { |
149 |
wr.write(text); |
150 |
} |
151 |
if (getNumSubNodes() > 0) { |
152 |
wr.write("\n");
|
153 |
for (int i = 0; i < subNodes.size(); i++) { |
154 |
if (getSubNode(i) != null) { |
155 |
getSubNode(i).wwrite(pre + " ", wr);
|
156 |
} |
157 |
} |
158 |
wr.write(pre + "</" + nodeName + ">\n"); |
159 |
} |
160 |
else {
|
161 |
wr.write("</" + nodeName + ">\n"); |
162 |
} |
163 |
} |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
public String getAttribute(String key) { |
170 |
return (String)attr.get(key); |
171 |
} |
172 |
|
173 |
public double getDoubleAttribute(String key) { |
174 |
return Double.parseDouble((String)attr.get(key)); |
175 |
} |
176 |
|
177 |
public boolean getBoolAttribute(String key) { |
178 |
if (!hasAttribute(key)) {
|
179 |
return false; |
180 |
} |
181 |
return new Boolean((String)attr.get(key)).booleanValue(); |
182 |
} |
183 |
|
184 |
public int getIntAttribute(String key) { |
185 |
return Integer.parseInt((String)attr.get(key)); |
186 |
} |
187 |
|
188 |
public boolean hasAttribute(String key) { |
189 |
return attr.containsKey(key);
|
190 |
} |
191 |
|
192 |
// escribe al fichero
|
193 |
public void write(File f) throws Exception { |
194 |
FileWriter fw = new FileWriter(f); |
195 |
fw.write(header); |
196 |
write(fw); |
197 |
fw.flush(); |
198 |
fw.close(); |
199 |
} |
200 |
|
201 |
|
202 |
public String toString() { |
203 |
StringWriter wr = new StringWriter(); |
204 |
try {
|
205 |
write(wr); |
206 |
} |
207 |
catch (Exception e) { |
208 |
System.err.println("Exception in StringWriter " + e); |
209 |
e.printStackTrace(); |
210 |
System.exit(1); |
211 |
} |
212 |
return wr.toString();
|
213 |
} |
214 |
|
215 |
static DocumentBuilderFactory factory; |
216 |
static {
|
217 |
factory = DocumentBuilderFactory.newInstance();
|
218 |
factory.setValidating(false);
|
219 |
factory.setNamespaceAware(false);
|
220 |
factory.setIgnoringComments(true);
|
221 |
} |
222 |
|
223 |
public void setHeader(String header) { |
224 |
this.header = header;
|
225 |
} |
226 |
|
227 |
public static void main(String []args) throws Exception { |
228 |
XMLNode node=new XMLNode(new File(args[0])); |
229 |
System.out.println(node);
|
230 |
} |
231 |
} |