Statistics
| Revision:

root / trunk / libraries / libGPE / src / org / gvsig / gpe / GPERegister.java @ 11126

History | View | Annotate | Download (3.87 KB)

1
package org.gvsig.gpe;
2

    
3
import java.io.File;
4
import java.lang.reflect.InvocationTargetException;
5
import java.util.ArrayList;
6

    
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
/* CVS MESSAGES:
48
 *
49
 * $Id: GPERegister.java 11126 2007-04-11 11:10:27Z jorpiell $
50
 * $Log$
51
 * Revision 1.3  2007-04-11 11:10:27  jorpiell
52
 * Cambiado el nombre de getDriver a GetParser
53
 *
54
 * Revision 1.2  2007/04/11 08:54:24  jorpiell
55
 * A?adidos algunos comentarios
56
 *
57
 * Revision 1.1  2007/04/11 08:52:55  jorpiell
58
 * Se puede registrar una clase por nombre
59
 *
60
 * Revision 1.1  2007/04/11 08:22:41  jorpiell
61
 * GPE clase para registrar los drivers
62
 *
63
 *
64
 */
65
/**
66
 * This class is used to register the GPE parsers. All the 
67
 * parsers must be registered in this class before to be
68
 * used for the consumer application
69
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
70
 */
71
public class GPERegister {
72
        private static ArrayList parsers = new ArrayList();
73
        
74
        /**
75
         * Adds a new GPE parser
76
         * @param driver
77
         */
78
        private static void addGpeDriver(GPEParser parser){
79
                parsers.add(parser);
80
        }
81
        
82
        /**
83
         * Adds a new GPE parser
84
         * @param calssName
85
         * Class name for the GPE parser
86
         * @param contentHandler
87
         * Consummer content handler
88
         * @param errorsHanlder
89
         * Consumer errorsHandler
90
         * @throws ClassNotFoundException 
91
         * @throws NoSuchMethodException 
92
         * @throws InvocationTargetException 
93
         * @throws IllegalAccessException 
94
         * @throws InstantiationException 
95
         * @throws SecurityException 
96
         * @throws IllegalArgumentException 
97
         */
98
        private static void addGpeDriver(String className, 
99
                        GPEContentHandler contentHandler,
100
                        GPEErrorHandler errorsHanlder) throws ClassNotFoundException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
101
                Class [] args = {GPEContentHandler.class,GPEErrorHandler.class};
102
                Object [] params = {contentHandler,errorsHanlder};
103
                
104
                Class clazz = Class.forName(className);
105
                if (clazz != null){
106
                        GPEParser parser = (GPEParser)clazz.getConstructor(args).newInstance(params);
107
                        parsers.add(parser);
108
                }                
109
        }
110
        
111
        
112

    
113
        /**
114
         * Return true if exists a driver that can open the file
115
         * @param file
116
         * File to open
117
         * @return
118
         * true if the driver exists
119
         */
120
        private static boolean accept(File file){
121
                for (int i=0 ; i<parsers.size() ; i++){
122
                        GPEParser parser = (GPEParser)parsers.get(i);
123
                        if (parser.accept(file)){
124
                                return true;
125
                        }
126
                }
127
                return false;
128
        }
129
        
130
        /**
131
         * Gets the parser that can open the file (if it exists)
132
         * @param file
133
         * File to open
134
         * @return
135
         * Null if the driver doesn't exist
136
         */
137
        private static GPEParser getParser(File file){
138
                for (int i=0 ; i<parsers.size() ; i++){
139
                        GPEParser parser = (GPEParser)parsers.get(i);
140
                        if (parser.accept(file)){
141
                                return parser;
142
                        }
143
                }
144
                return null;
145
        }
146
        
147
}