root / trunk / libraries / libGPE / src / org / gvsig / gpe / GPERegister.java @ 27713
History | View | Annotate | Download (12.8 KB)
1 |
package org.gvsig.gpe; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileNotFoundException; |
6 |
import java.io.IOException; |
7 |
import java.lang.reflect.InvocationTargetException; |
8 |
import java.net.URI; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Enumeration; |
11 |
import java.util.Hashtable; |
12 |
import java.util.Iterator; |
13 |
import java.util.Properties; |
14 |
|
15 |
import org.gvsig.gpe.exceptions.ParserCreationException; |
16 |
import org.gvsig.gpe.exceptions.ParserNotRegisteredException; |
17 |
import org.gvsig.gpe.exceptions.WriterHandlerCreationException; |
18 |
import org.gvsig.gpe.exceptions.WriterHandlerNotRegisteredException; |
19 |
import org.gvsig.gpe.parser.GPEParser; |
20 |
import org.gvsig.gpe.writer.GPEWriterHandler; |
21 |
import org.gvsig.gpe.writer.GPEWriterHandlerImplementor; |
22 |
import org.gvsig.gpe.writer.IGPEWriterHandlerImplementor; |
23 |
|
24 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
25 |
*
|
26 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
27 |
*
|
28 |
* This program is free software; you can redistribute it and/or
|
29 |
* modify it under the terms of the GNU General Public License
|
30 |
* as published by the Free Software Foundation; either version 2
|
31 |
* of the License, or (at your option) any later version.
|
32 |
*
|
33 |
* This program is distributed in the hope that it will be useful,
|
34 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
35 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
36 |
* GNU General Public License for more details.
|
37 |
*
|
38 |
* You should have received a copy of the GNU General Public License
|
39 |
* along with this program; if not, write to the Free Software
|
40 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
41 |
*
|
42 |
* For more information, contact:
|
43 |
*
|
44 |
* Generalitat Valenciana
|
45 |
* Conselleria d'Infraestructures i Transport
|
46 |
* Av. Blasco Ib??ez, 50
|
47 |
* 46010 VALENCIA
|
48 |
* SPAIN
|
49 |
*
|
50 |
* +34 963862235
|
51 |
* gvsig@gva.es
|
52 |
* www.gvsig.gva.es
|
53 |
*
|
54 |
* or
|
55 |
*
|
56 |
* IVER T.I. S.A
|
57 |
* Salamanca 50
|
58 |
* 46005 Valencia
|
59 |
* Spain
|
60 |
*
|
61 |
* +34 963163400
|
62 |
* dac@iver.es
|
63 |
*/
|
64 |
/* CVS MESSAGES:
|
65 |
*
|
66 |
* $Id: GPERegister.java 282 2007-12-20 11:55:56Z jpiera $
|
67 |
* $Log$
|
68 |
* Revision 1.16 2007/06/28 13:04:33 jorpiell
|
69 |
* The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
|
70 |
*
|
71 |
* Revision 1.15 2007/06/20 09:35:37 jorpiell
|
72 |
* Add the javadoc comments
|
73 |
*
|
74 |
* Revision 1.14 2007/05/16 12:34:55 csanchez
|
75 |
* GPEParser Prototipo final de lectura
|
76 |
*
|
77 |
* Revision 1.13 2007/05/09 06:54:07 jorpiell
|
78 |
* Change the File by URI
|
79 |
*
|
80 |
* Revision 1.12 2007/05/08 12:57:14 jorpiell
|
81 |
* Add the register exceptions
|
82 |
*
|
83 |
* Revision 1.11 2007/05/07 07:06:26 jorpiell
|
84 |
* Add a constructor with the name and the description fields
|
85 |
*
|
86 |
* Revision 1.10 2007/04/19 11:50:20 csanchez
|
87 |
* Actualizacion protoripo libGPE
|
88 |
*
|
89 |
* Revision 1.9 2007/04/19 07:23:20 jorpiell
|
90 |
* Add the add methods to teh contenhandler and change the register mode
|
91 |
*
|
92 |
* Revision 1.8 2007/04/18 12:54:45 csanchez
|
93 |
* Actualizacion protoripo libGPE
|
94 |
*
|
95 |
* Revision 1.7 2007/04/17 07:53:55 jorpiell
|
96 |
* Before to start a new parsing process, the initialize method of the content handlers is throwed
|
97 |
*
|
98 |
* Revision 1.6 2007/04/17 06:26:54 jorpiell
|
99 |
* Fixed one problem with a index
|
100 |
*
|
101 |
* Revision 1.5 2007/04/14 16:06:13 jorpiell
|
102 |
* The writer handler has been updated
|
103 |
*
|
104 |
* Revision 1.4 2007/04/12 17:06:42 jorpiell
|
105 |
* First GML writing tests
|
106 |
*
|
107 |
* Revision 1.3 2007/04/11 11:10:27 jorpiell
|
108 |
* Cambiado el nombre de getDriver a GetParser
|
109 |
*
|
110 |
* Revision 1.2 2007/04/11 08:54:24 jorpiell
|
111 |
* A?adidos algunos comentarios
|
112 |
*
|
113 |
* Revision 1.1 2007/04/11 08:52:55 jorpiell
|
114 |
* Se puede registrar una clase por nombre
|
115 |
*
|
116 |
* Revision 1.1 2007/04/11 08:22:41 jorpiell
|
117 |
* GPE clase para registrar los drivers
|
118 |
*
|
119 |
*
|
120 |
*/
|
121 |
/**
|
122 |
* This class is used to register the GPE parsers. All the
|
123 |
* parsers must be registered in this class before to be
|
124 |
* used for the consumer application
|
125 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
126 |
* @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
|
127 |
*/
|
128 |
public class GPERegister { |
129 |
private static Hashtable parsers = new Hashtable(); |
130 |
private static Hashtable writers = new Hashtable(); |
131 |
private static GPEParser gpeParser=null; |
132 |
|
133 |
/**
|
134 |
* Adds a new GPE parser
|
135 |
* @param name
|
136 |
* Driver name. It must be written like FORMAT VERSION
|
137 |
* @param description
|
138 |
* Driver description. Just a descriptive text
|
139 |
* @param clazz
|
140 |
* The parser class
|
141 |
* @throws ParserNotRegisteredException
|
142 |
* @throws GPEParserRegisterException
|
143 |
*/
|
144 |
public static void addGpeParser(String name, String description,Class clazz) throws ParserNotRegisteredException { |
145 |
try{
|
146 |
if (clazz != null){ |
147 |
GPEParser parser = (GPEParser)clazz.getConstructor(null).newInstance(null); |
148 |
parsers.put(name, parser); |
149 |
} |
150 |
}catch (Exception e){ |
151 |
throw new ParserNotRegisteredException(clazz.getName()); |
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Adds a new GPE parser
|
157 |
* @param clazz
|
158 |
* The parser class
|
159 |
* @throws ParserNotRegisteredException
|
160 |
* @throws GPEParserRegisterException
|
161 |
*/
|
162 |
public static void addGpeParser(Class clazz) throws ParserNotRegisteredException { |
163 |
try{
|
164 |
if (clazz != null){ |
165 |
GPEParser parser = (GPEParser)clazz.getConstructor(null).newInstance(null); |
166 |
parsers.put(parser.getName(), parser); |
167 |
} |
168 |
}catch (Exception e){ |
169 |
throw new ParserNotRegisteredException(clazz.getName()); |
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* It loads the parsers of a parsers file. The file is
|
175 |
* a properties file. Every line has the structure:
|
176 |
* Parser=Parser class
|
177 |
* @param file
|
178 |
* File that contains the parsers list
|
179 |
* @throws IOException
|
180 |
* @throws FileNotFoundException
|
181 |
* @throws ParserNotRegisteredException
|
182 |
*/
|
183 |
public static void addParsersFile(File file) throws FileNotFoundException, IOException{ |
184 |
if (!file.exists()){
|
185 |
return;
|
186 |
} |
187 |
Properties properties = new Properties(); |
188 |
properties.load(new FileInputStream(file)); |
189 |
for (Enumeration e = properties.keys(); e.hasMoreElements() ; ) { |
190 |
String key = e.nextElement().toString();
|
191 |
Class clazz;
|
192 |
try {
|
193 |
clazz = Class.forName(properties.getProperty(key).toString());
|
194 |
addGpeParser(clazz); |
195 |
} catch (ClassNotFoundException ex) { |
196 |
//Next class
|
197 |
} catch (ParserNotRegisteredException ex) {
|
198 |
//Next class
|
199 |
} |
200 |
} |
201 |
} |
202 |
|
203 |
/**
|
204 |
* It loads the writers of a writers file. The file is
|
205 |
* a properties file. Every line has the structure:
|
206 |
* Writer=Parser class
|
207 |
* @param file
|
208 |
* File that contains the writers list
|
209 |
* @throws IOException
|
210 |
* @throws FileNotFoundException
|
211 |
*/
|
212 |
public static void addWritersFile(File file) throws FileNotFoundException, IOException{ |
213 |
if (!file.exists()){
|
214 |
return;
|
215 |
} |
216 |
Properties properties = new Properties(); |
217 |
properties.load(new FileInputStream(file)); |
218 |
for (Enumeration e = properties.keys(); e.hasMoreElements() ; ) { |
219 |
String key = e.nextElement().toString();
|
220 |
Class clazz;
|
221 |
try {
|
222 |
clazz = Class.forName(properties.getProperty(key).toString());
|
223 |
addGpeWriterHandler(clazz); |
224 |
} catch (ClassNotFoundException ex){ |
225 |
//Next class
|
226 |
} catch (WriterHandlerNotRegisteredException ex){
|
227 |
//Next class
|
228 |
} |
229 |
} |
230 |
} |
231 |
|
232 |
/**
|
233 |
* @return all the registered parsers
|
234 |
*/
|
235 |
public static GPEParser[] getAllParsers(){ |
236 |
GPEParser[] auxParsers = new GPEParser[parsers.size()]; |
237 |
Iterator it = parsers.keySet().iterator();
|
238 |
int i=0; |
239 |
while (it.hasNext()){
|
240 |
String key = (String)it.next(); |
241 |
auxParsers[i] = (GPEParser)parsers.get(key); |
242 |
i++; |
243 |
} |
244 |
return auxParsers;
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* Adds a new GPEWriterHandlerImplementor
|
249 |
* @param name
|
250 |
* Driver name. It must be written like FORMAT VERSION
|
251 |
* @param description
|
252 |
* Driver description. Just a descriptive text
|
253 |
* @param clazz
|
254 |
* The parser class
|
255 |
* @throws WriterHandlerNotRegisteredException
|
256 |
* @throws GPEWriterHandlerRegisterException
|
257 |
*/
|
258 |
public static void addGpeWriterHandler(String name, String description,Class clazz) throws WriterHandlerNotRegisteredException { |
259 |
try{
|
260 |
if (clazz != null){ |
261 |
GPEWriterHandlerImplementor writerImplementor = (GPEWriterHandlerImplementor)clazz.getConstructor(null).newInstance(null); |
262 |
writers.put(name, writerImplementor); |
263 |
} |
264 |
}catch (Exception e){ |
265 |
throw new WriterHandlerNotRegisteredException(clazz.getName()); |
266 |
} |
267 |
} |
268 |
|
269 |
/**
|
270 |
* Adds a new GPEWriterHandlerImplementor
|
271 |
* @param clazz
|
272 |
* The parser class
|
273 |
* @throws WriterHandlerNotRegisteredException
|
274 |
* @throws GPEWriterHandlerRegisterException
|
275 |
*/
|
276 |
public static void addGpeWriterHandler(Class clazz) throws WriterHandlerNotRegisteredException { |
277 |
try{
|
278 |
if (clazz != null){ |
279 |
GPEWriterHandlerImplementor writerImplementor = (GPEWriterHandlerImplementor)clazz.getConstructor(null).newInstance(null); |
280 |
writers.put(writerImplementor.getName(), writerImplementor); |
281 |
} |
282 |
}catch (Exception e){ |
283 |
throw new WriterHandlerNotRegisteredException(clazz.getName()); |
284 |
} |
285 |
} |
286 |
|
287 |
/**
|
288 |
* Create a new parser from a name
|
289 |
* @param name
|
290 |
* GPEParser name
|
291 |
* @param contenHandler
|
292 |
* Application contenHandler usett to throw the parsing events
|
293 |
* @param errorHandler
|
294 |
* Application errror handler used to put errors and warnings
|
295 |
* @throws ParserCreationException
|
296 |
* @throws GPEParserCreationException
|
297 |
*/
|
298 |
public static GPEParser createParser(String name) throws ParserCreationException { |
299 |
if((gpeParser!=null)&&(gpeParser.getName()==name)) |
300 |
return gpeParser;
|
301 |
Object parser = parsers.get(name);
|
302 |
try{
|
303 |
if (parser != null){ |
304 |
return (GPEParser)parser.getClass().getConstructor(null).newInstance(null); |
305 |
}else{
|
306 |
Exception e = new ParserNotRegisteredException(name); |
307 |
throw new ParserCreationException(e); |
308 |
} |
309 |
}catch (Exception e) { |
310 |
throw new ParserCreationException(e); |
311 |
} |
312 |
|
313 |
} |
314 |
/**
|
315 |
* Create a new parser from a name
|
316 |
* @param name
|
317 |
* GPEParser name
|
318 |
* @param contenHandler
|
319 |
* Application contenHandler usett to throw the parsing events
|
320 |
* @param errorHandler
|
321 |
* Application errror handler used to put errors and warnings
|
322 |
* @throws ParserCreationException
|
323 |
* @throws GPEParserCreationException
|
324 |
*/
|
325 |
public static GPEParser createParser() throws ParserCreationException { |
326 |
try {
|
327 |
if(gpeParser!=null){ |
328 |
return (GPEParser) gpeParser.getClass().getConstructor(null).newInstance(null); |
329 |
}else{
|
330 |
Throwable e = new Exception("Fail Registering Parser"); |
331 |
throw new ParserCreationException(e); |
332 |
} |
333 |
}catch (Exception e){ |
334 |
throw new ParserCreationException(e); |
335 |
} |
336 |
} |
337 |
|
338 |
/**
|
339 |
* Gets the parser that can open the file (if it exists)
|
340 |
* @param uri
|
341 |
* File to open
|
342 |
* @return
|
343 |
* Null if the driver doesn't exist
|
344 |
* @throws GPEParserCreationException
|
345 |
* @throws NoSuchMethodException
|
346 |
* @throws InvocationTargetException
|
347 |
* @throws IllegalAccessException
|
348 |
* @throws InstantiationException
|
349 |
* @throws SecurityException
|
350 |
* @throws IllegalArgumentException
|
351 |
*/
|
352 |
public static GPEParser createParser(URI uri) throws ParserCreationException { |
353 |
Iterator keys = parsers.keySet().iterator();
|
354 |
while (keys.hasNext()){
|
355 |
String key = (String)keys.next(); |
356 |
GPEParser parser = (GPEParser)parsers.get(key); |
357 |
if (parser.accept(uri)){
|
358 |
return createParser(key);
|
359 |
} |
360 |
} |
361 |
return null; |
362 |
} |
363 |
|
364 |
/**
|
365 |
* Create a new content writer from a name
|
366 |
* @param name
|
367 |
* GPEWriterHandler name
|
368 |
* GPEParser name
|
369 |
* @param contenHandler
|
370 |
* Application contenHandler usett to throw the parsing events
|
371 |
* @param errorHandler
|
372 |
* Application errror handler used to put errors and warnings
|
373 |
* @throws GPEWriterHandlerCreationException
|
374 |
*/
|
375 |
public static GPEWriterHandler createWriter(String name) throws WriterHandlerCreationException{ |
376 |
Object writer = writers.get(name);
|
377 |
try{
|
378 |
if (writer != null){ |
379 |
GPEWriterHandlerImplementor writerImplementor = (GPEWriterHandlerImplementor)writer.getClass().getConstructor(null).newInstance(null); |
380 |
return new GPEWriterHandler(writerImplementor); |
381 |
}else{
|
382 |
Exception e = new WriterHandlerNotRegisteredException(name); |
383 |
throw new WriterHandlerCreationException(e); |
384 |
} |
385 |
}catch (Exception e) { |
386 |
throw new WriterHandlerCreationException(e); |
387 |
} |
388 |
} |
389 |
|
390 |
/**
|
391 |
* Gets all the writers that can
|
392 |
* @param format
|
393 |
* @return
|
394 |
*/
|
395 |
public static ArrayList getWriterHandlerByFormat(String format){ |
396 |
Iterator it = writers.keySet().iterator();
|
397 |
ArrayList possibleWriters = new ArrayList(); |
398 |
while (it.hasNext()){
|
399 |
String key = (String)it.next(); |
400 |
IGPEWriterHandlerImplementor implementor = (IGPEWriterHandlerImplementor)writers.get(key); |
401 |
String formats = implementor.getFormat();
|
402 |
if (formats.toLowerCase().compareTo(format.toLowerCase()) == 0){ |
403 |
possibleWriters.add(new GPEWriterHandler(implementor));
|
404 |
} |
405 |
} |
406 |
return possibleWriters;
|
407 |
} |
408 |
|
409 |
|
410 |
/**
|
411 |
* Return true if exists a driver that can open the file
|
412 |
* @param uri
|
413 |
* File to open
|
414 |
* @return
|
415 |
* true if the driver exists
|
416 |
*/
|
417 |
public static boolean accept(URI uri){ |
418 |
Iterator keys = parsers.keySet().iterator();
|
419 |
while (keys.hasNext()){
|
420 |
GPEParser parser = (GPEParser)parsers.get(keys.next()); |
421 |
if (parser.accept(uri)){
|
422 |
gpeParser=parser; |
423 |
return true; |
424 |
} |
425 |
} |
426 |
return false; |
427 |
} |
428 |
} |