svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / xmlViewer / MultipleXMLContent.java @ 40561
History | View | Annotate | Download (6.86 KB)
1 | 40561 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40561 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40561 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40561 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40561 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.utils.xmlViewer; |
||
25 | |||
26 | import java.io.ByteArrayInputStream; |
||
27 | import java.io.IOException; |
||
28 | import java.util.ArrayList; |
||
29 | |||
30 | import org.xml.sax.Attributes; |
||
31 | import org.xml.sax.ContentHandler; |
||
32 | import org.xml.sax.InputSource; |
||
33 | import org.xml.sax.Locator; |
||
34 | import org.xml.sax.SAXException; |
||
35 | import org.xml.sax.XMLReader; |
||
36 | import org.xml.sax.helpers.XMLReaderFactory; |
||
37 | |||
38 | |||
39 | /**
|
||
40 | * XMLContent al que se le pueden a?adir varios ficheros XML en forma de String
|
||
41 | *
|
||
42 | * @author Fernando Gonz?lez Cort?s
|
||
43 | */
|
||
44 | public class MultipleXMLContent implements XMLContent { |
||
45 | private ContentHandler handler; |
||
46 | private ArrayList xmls = new ArrayList(); |
||
47 | |||
48 | /**
|
||
49 | * A?ade un fichero XML al modelo
|
||
50 | *
|
||
51 | * @param xmlContent String con el contenido xml
|
||
52 | */
|
||
53 | public void addXML(String xmlContent) { |
||
54 | xmls.add(xmlContent); |
||
55 | } |
||
56 | |||
57 | /**
|
||
58 | * @see org.gvsig.utils.xmlViewer.XMLContent#setContentHandler(org.xml.sax.ContentHandler)
|
||
59 | */
|
||
60 | public void setContentHandler(ContentHandler handler) { |
||
61 | this.handler = handler;
|
||
62 | } |
||
63 | |||
64 | /**
|
||
65 | * Genera un evento de startDocument, luego genera todos los eventos de los
|
||
66 | * XML que han sido a?adidos a la clase filtrando los startDocument y
|
||
67 | * endDocument de cada uno de ellos y luego genera un endDocument
|
||
68 | *
|
||
69 | * @see org.gvsig.utils.xmlViewer.XMLContent#parse()
|
||
70 | */
|
||
71 | public void parse() throws SAXException { |
||
72 | XMLReader reader; |
||
73 | |||
74 | //Empieza el documento
|
||
75 | handler.startDocument(); |
||
76 | |||
77 | for (int i = 0; i < xmls.size(); i++) { |
||
78 | String text = (String) xmls.get(i); |
||
79 | |||
80 | //Handler que filtra los eventos de startDocument y end Document
|
||
81 | FilterHandler filter = new FilterHandler();
|
||
82 | filter.setHandler(handler); |
||
83 | |||
84 | //Generamos los eventos del XML i-?simo
|
||
85 | reader = XMLReaderFactory.createXMLReader(); |
||
86 | reader.setFeature("http://xml.org/sax/features/namespaces", false); |
||
87 | reader.setContentHandler(filter); |
||
88 | |||
89 | if (text == null) { |
||
90 | continue;
|
||
91 | } |
||
92 | |||
93 | try {
|
||
94 | reader.parse(new InputSource(
|
||
95 | new ByteArrayInputStream(text.getBytes()))); |
||
96 | } catch (IOException e) { |
||
97 | //Una IO exception en un array de bytes???
|
||
98 | } |
||
99 | } |
||
100 | |||
101 | //Finalizamos el documento
|
||
102 | handler.endDocument(); |
||
103 | } |
||
104 | |||
105 | /**
|
||
106 | * Manejador que delega en otro manejador todos los eventos excepto el
|
||
107 | * startDocument y el endDocument. "Filtra" los eventos de startDocument y
|
||
108 | * endDocument
|
||
109 | *
|
||
110 | * @author Fernando Gonz?lez Cort?s
|
||
111 | */
|
||
112 | public class FilterHandler implements ContentHandler { |
||
113 | private ContentHandler handler; |
||
114 | |||
115 | /**
|
||
116 | * Establece el handler al que se le van a filtrar los eventos de
|
||
117 | * comienzo y fin de documento
|
||
118 | *
|
||
119 | * @param handler The handler to set.
|
||
120 | */
|
||
121 | public void setHandler(ContentHandler handler) { |
||
122 | this.handler = handler;
|
||
123 | } |
||
124 | |||
125 | /**
|
||
126 | * DOCUMENT ME!
|
||
127 | *
|
||
128 | * @param arg0
|
||
129 | * @param arg1
|
||
130 | * @param arg2
|
||
131 | *
|
||
132 | * @throws SAXException
|
||
133 | */
|
||
134 | public void characters(char[] arg0, int arg1, int arg2) |
||
135 | throws SAXException {
|
||
136 | handler.characters(arg0, arg1, arg2); |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * DOCUMENT ME!
|
||
141 | *
|
||
142 | * @throws SAXException
|
||
143 | */
|
||
144 | public void endDocument() throws SAXException { |
||
145 | } |
||
146 | |||
147 | /**
|
||
148 | * DOCUMENT ME!
|
||
149 | *
|
||
150 | * @param arg0
|
||
151 | * @param arg1
|
||
152 | * @param arg2
|
||
153 | *
|
||
154 | * @throws SAXException
|
||
155 | */
|
||
156 | public void endElement(String arg0, String arg1, String arg2) |
||
157 | throws SAXException {
|
||
158 | handler.endElement(arg0, arg1, arg2); |
||
159 | } |
||
160 | |||
161 | /**
|
||
162 | * DOCUMENT ME!
|
||
163 | *
|
||
164 | * @param arg0
|
||
165 | *
|
||
166 | * @throws SAXException
|
||
167 | */
|
||
168 | public void endPrefixMapping(String arg0) throws SAXException { |
||
169 | handler.endPrefixMapping(arg0); |
||
170 | } |
||
171 | |||
172 | /**
|
||
173 | * DOCUMENT ME!
|
||
174 | *
|
||
175 | * @param arg0
|
||
176 | * @param arg1
|
||
177 | * @param arg2
|
||
178 | *
|
||
179 | * @throws SAXException
|
||
180 | */
|
||
181 | public void ignorableWhitespace(char[] arg0, int arg1, int arg2) |
||
182 | throws SAXException {
|
||
183 | handler.ignorableWhitespace(arg0, arg1, arg2); |
||
184 | } |
||
185 | |||
186 | /**
|
||
187 | * DOCUMENT ME!
|
||
188 | *
|
||
189 | * @param arg0
|
||
190 | * @param arg1
|
||
191 | *
|
||
192 | * @throws SAXException
|
||
193 | */
|
||
194 | public void processingInstruction(String arg0, String arg1) |
||
195 | throws SAXException {
|
||
196 | handler.processingInstruction(arg0, arg1); |
||
197 | } |
||
198 | |||
199 | /**
|
||
200 | * DOCUMENT ME!
|
||
201 | *
|
||
202 | * @param arg0
|
||
203 | */
|
||
204 | public void setDocumentLocator(Locator arg0) { |
||
205 | handler.setDocumentLocator(arg0); |
||
206 | } |
||
207 | |||
208 | /**
|
||
209 | * DOCUMENT ME!
|
||
210 | *
|
||
211 | * @param arg0
|
||
212 | *
|
||
213 | * @throws SAXException
|
||
214 | */
|
||
215 | public void skippedEntity(String arg0) throws SAXException { |
||
216 | handler.skippedEntity(arg0); |
||
217 | } |
||
218 | |||
219 | /**
|
||
220 | * DOCUMENT ME!
|
||
221 | *
|
||
222 | * @throws SAXException
|
||
223 | */
|
||
224 | public void startDocument() throws SAXException { |
||
225 | } |
||
226 | |||
227 | /**
|
||
228 | * DOCUMENT ME!
|
||
229 | *
|
||
230 | * @param arg0
|
||
231 | * @param arg1
|
||
232 | * @param arg2
|
||
233 | * @param arg3
|
||
234 | *
|
||
235 | * @throws SAXException
|
||
236 | */
|
||
237 | public void startElement(String arg0, String arg1, String arg2, |
||
238 | Attributes arg3) throws SAXException { |
||
239 | handler.startElement(arg0, arg1, arg2, arg3); |
||
240 | } |
||
241 | |||
242 | /**
|
||
243 | * DOCUMENT ME!
|
||
244 | *
|
||
245 | * @param arg0
|
||
246 | * @param arg1
|
||
247 | *
|
||
248 | * @throws SAXException
|
||
249 | */
|
||
250 | public void startPrefixMapping(String arg0, String arg1) |
||
251 | throws SAXException {
|
||
252 | handler.startPrefixMapping(arg0, arg1); |
||
253 | } |
||
254 | } |
||
255 | } |