svn-gvsig-desktop / trunk / libraries / libGPE / src / org / gvsig / gpe / GPEFactory.java @ 19801
History | View | Annotate | Download (7.81 KB)
1 |
package org.gvsig.gpe; |
---|---|
2 |
|
3 |
import java.net.URI; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Iterator; |
6 |
|
7 |
import org.gvsig.gpe.exceptions.GPEParserCreationException; |
8 |
import org.gvsig.gpe.exceptions.GPEWriterHandlerCreationException; |
9 |
import org.gvsig.gpe.parser.GPEParser; |
10 |
import org.gvsig.gpe.writer.GPEWriterHandler; |
11 |
import org.gvsig.gpe.writer.IGPEWriterHandlerImplementor; |
12 |
|
13 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
14 |
*
|
15 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
16 |
*
|
17 |
* This program is free software; you can redistribute it and/or
|
18 |
* modify it under the terms of the GNU General Public License
|
19 |
* as published by the Free Software Foundation; either version 2
|
20 |
* of the License, or (at your option) any later version.
|
21 |
*
|
22 |
* This program is distributed in the hope that it will be useful,
|
23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25 |
* GNU General Public License for more details.
|
26 |
*
|
27 |
* You should have received a copy of the GNU General Public License
|
28 |
* along with this program; if not, write to the Free Software
|
29 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
30 |
*
|
31 |
* For more information, contact:
|
32 |
*
|
33 |
* Generalitat Valenciana
|
34 |
* Conselleria d'Infraestructures i Transport
|
35 |
* Av. Blasco Ib??ez, 50
|
36 |
* 46010 VALENCIA
|
37 |
* SPAIN
|
38 |
*
|
39 |
* +34 963862235
|
40 |
* gvsig@gva.es
|
41 |
* www.gvsig.gva.es
|
42 |
*
|
43 |
* or
|
44 |
*
|
45 |
* IVER T.I. S.A
|
46 |
* Salamanca 50
|
47 |
* 46005 Valencia
|
48 |
* Spain
|
49 |
*
|
50 |
* +34 963163400
|
51 |
* dac@iver.es
|
52 |
*/
|
53 |
/* CVS MESSAGES:
|
54 |
*
|
55 |
* $Id: GPERegister.java 282 2007-12-20 11:55:56Z jpiera $
|
56 |
* $Log$
|
57 |
* Revision 1.16 2007/06/28 13:04:33 jorpiell
|
58 |
* The Qname has been updated to the 1.5 JVM machine. The schema validation is made in the GPEWriterHandlerImplementor class
|
59 |
*
|
60 |
* Revision 1.15 2007/06/20 09:35:37 jorpiell
|
61 |
* Add the javadoc comments
|
62 |
*
|
63 |
* Revision 1.14 2007/05/16 12:34:55 csanchez
|
64 |
* GPEParser Prototipo final de lectura
|
65 |
*
|
66 |
* Revision 1.13 2007/05/09 06:54:07 jorpiell
|
67 |
* Change the File by URI
|
68 |
*
|
69 |
* Revision 1.12 2007/05/08 12:57:14 jorpiell
|
70 |
* Add the register exceptions
|
71 |
*
|
72 |
* Revision 1.11 2007/05/07 07:06:26 jorpiell
|
73 |
* Add a constructor with the name and the description fields
|
74 |
*
|
75 |
* Revision 1.10 2007/04/19 11:50:20 csanchez
|
76 |
* Actualizacion protoripo libGPE
|
77 |
*
|
78 |
* Revision 1.9 2007/04/19 07:23:20 jorpiell
|
79 |
* Add the add methods to teh contenhandler and change the register mode
|
80 |
*
|
81 |
* Revision 1.8 2007/04/18 12:54:45 csanchez
|
82 |
* Actualizacion protoripo libGPE
|
83 |
*
|
84 |
* Revision 1.7 2007/04/17 07:53:55 jorpiell
|
85 |
* Before to start a new parsing process, the initialize method of the content handlers is throwed
|
86 |
*
|
87 |
* Revision 1.6 2007/04/17 06:26:54 jorpiell
|
88 |
* Fixed one problem with a index
|
89 |
*
|
90 |
* Revision 1.5 2007/04/14 16:06:13 jorpiell
|
91 |
* The writer handler has been updated
|
92 |
*
|
93 |
* Revision 1.4 2007/04/12 17:06:42 jorpiell
|
94 |
* First GML writing tests
|
95 |
*
|
96 |
* Revision 1.3 2007/04/11 11:10:27 jorpiell
|
97 |
* Cambiado el nombre de getDriver a GetParser
|
98 |
*
|
99 |
* Revision 1.2 2007/04/11 08:54:24 jorpiell
|
100 |
* A?adidos algunos comentarios
|
101 |
*
|
102 |
* Revision 1.1 2007/04/11 08:52:55 jorpiell
|
103 |
* Se puede registrar una clase por nombre
|
104 |
*
|
105 |
* Revision 1.1 2007/04/11 08:22:41 jorpiell
|
106 |
* GPE clase para registrar los drivers
|
107 |
*
|
108 |
*
|
109 |
*/
|
110 |
/**
|
111 |
* This class is used to register the GPE parsers. All the
|
112 |
* parsers must be registered in this class before to be
|
113 |
* used for the consumer application
|
114 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
115 |
* @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
|
116 |
*/
|
117 |
public class GPEFactory { |
118 |
|
119 |
/**
|
120 |
* @return all the registered parsers
|
121 |
*/
|
122 |
public static Iterator availableParsers(){ |
123 |
Iterator providers = sun.misc.Service.providers(GPEParser.class);
|
124 |
return providers;
|
125 |
} |
126 |
|
127 |
public static GPEParser createParserByClass(String prefferredImplClassName) { |
128 |
Class prefferredImplClass = null; |
129 |
try {
|
130 |
prefferredImplClass = Class.forName(prefferredImplClassName);
|
131 |
} catch (ClassNotFoundException e) { |
132 |
return null; |
133 |
} |
134 |
|
135 |
Iterator providers = availableParsers();
|
136 |
while (providers.hasNext()) {
|
137 |
GPEParser next = (GPEParser)providers.next(); |
138 |
if (prefferredImplClass.isInstance(next)) {
|
139 |
return next;
|
140 |
} |
141 |
} |
142 |
return null; |
143 |
} |
144 |
|
145 |
/**
|
146 |
* Create a new parser from a name
|
147 |
* @param name
|
148 |
* GPEParser name
|
149 |
* @param contenHandler
|
150 |
* Application contenHandler usett to throw the parsing events
|
151 |
* @param errorHandler
|
152 |
* Application errror handler used to put errors and warnings
|
153 |
* @throws GPEParserCreationException
|
154 |
*/
|
155 |
public static GPEParser createParser(String name) throws GPEParserCreationException { |
156 |
Iterator providers = availableParsers();
|
157 |
if (providers.hasNext()) {
|
158 |
GPEParser parser = (GPEParser)providers.next(); |
159 |
if (parser.getName().compareTo(name) == 0){ |
160 |
return parser;
|
161 |
} |
162 |
} |
163 |
return null; |
164 |
} |
165 |
|
166 |
/**
|
167 |
* Gets the parser that can open the file (if it exists)
|
168 |
* @param uri
|
169 |
* File to open
|
170 |
* @return
|
171 |
* Null if the driver doesn't exist
|
172 |
* @throws GPEParserCreationException
|
173 |
* @throws SecurityException
|
174 |
* @throws IllegalArgumentException
|
175 |
*/
|
176 |
public static GPEParser createParser(URI uri) throws GPEParserCreationException { |
177 |
Iterator providers = availableParsers();
|
178 |
if (providers.hasNext()) {
|
179 |
GPEParser parser = (GPEParser)providers.next(); |
180 |
if (parser.accept(uri)){
|
181 |
return parser;
|
182 |
} |
183 |
} |
184 |
return null; |
185 |
} |
186 |
|
187 |
public static Iterator availableWriters() { |
188 |
Iterator providers = sun.misc.Service.providers(IGPEWriterHandlerImplementor.class);
|
189 |
return providers;
|
190 |
} |
191 |
|
192 |
/**
|
193 |
* Create a new content writer from a name
|
194 |
* @param name
|
195 |
* GPEWriterHandler name
|
196 |
* GPEParser name
|
197 |
* @param contenHandler
|
198 |
* Application contenHandler usett to throw the parsing events
|
199 |
* @param errorHandler
|
200 |
* Application errror handler used to put errors and warnings
|
201 |
* @throws GPEWriterHandlerCreationException
|
202 |
*/
|
203 |
public static GPEWriterHandler createWriter(String name) throws GPEWriterHandlerCreationException{ |
204 |
Iterator providers = availableWriters();
|
205 |
if (providers.hasNext()) {
|
206 |
IGPEWriterHandlerImplementor implementor = (IGPEWriterHandlerImplementor)providers.next(); |
207 |
if (implementor.getName().compareTo(name) == 0){ |
208 |
new GPEWriterHandler(implementor);
|
209 |
} |
210 |
} |
211 |
return null; |
212 |
} |
213 |
|
214 |
public static GPEWriterHandler createWriterByClass(String prefferredImplClassName) { |
215 |
Class prefferredImplClass = null; |
216 |
try {
|
217 |
prefferredImplClass = Class.forName(prefferredImplClassName);
|
218 |
} catch (ClassNotFoundException e) { |
219 |
return null; |
220 |
} |
221 |
|
222 |
Iterator providers = availableWriters();
|
223 |
while (providers.hasNext()) {
|
224 |
IGPEWriterHandlerImplementor next = (IGPEWriterHandlerImplementor)providers.next(); |
225 |
if (prefferredImplClass.isInstance(next)) {
|
226 |
return new GPEWriterHandler(next); |
227 |
} |
228 |
} |
229 |
return null; |
230 |
} |
231 |
|
232 |
/**
|
233 |
* Gets all the writers that can
|
234 |
* @param format
|
235 |
* @return
|
236 |
*/
|
237 |
public static ArrayList getWriterHandlerByFormat(String format){ |
238 |
Iterator providers = availableWriters();
|
239 |
ArrayList possibleWriters = new ArrayList(); |
240 |
if (providers.hasNext()) {
|
241 |
IGPEWriterHandlerImplementor implementor = (IGPEWriterHandlerImplementor)providers.next(); |
242 |
String[] formats = implementor.getFormats(); |
243 |
for (int i=0 ; i<formats.length ; i++){ |
244 |
if (formats[i].toLowerCase().compareTo(format.toLowerCase()) == 0){ |
245 |
possibleWriters.add(new GPEWriterHandler(implementor));
|
246 |
} |
247 |
} |
248 |
} |
249 |
return possibleWriters;
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Return true if exists a driver that can open the file
|
254 |
* @param uri
|
255 |
* File to open
|
256 |
* @return
|
257 |
* true if the driver exists
|
258 |
*/
|
259 |
public static boolean accept(URI uri){ |
260 |
Iterator providers = availableParsers();
|
261 |
if (providers.hasNext()) {
|
262 |
GPEParser parser = (GPEParser)providers.next(); |
263 |
if (parser.accept(uri)){
|
264 |
return true; |
265 |
} |
266 |
} |
267 |
return false; |
268 |
} |
269 |
} |