Statistics
| Revision:

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

History | View | Annotate | Download (3.77 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 11123 2007-04-11 08:54:24Z jorpiell $
50
 * $Log$
51
 * Revision 1.2  2007-04-11 08:54:24  jorpiell
52
 * A?adidos algunos comentarios
53
 *
54
 * Revision 1.1  2007/04/11 08:52:55  jorpiell
55
 * Se puede registrar una clase por nombre
56
 *
57
 * Revision 1.1  2007/04/11 08:22:41  jorpiell
58
 * GPE clase para registrar los drivers
59
 *
60
 *
61
 */
62
/**
63
 * This class is used to register the GPE parsers. All the 
64
 * parsers must be registered in this class before to be
65
 * used for the consumer application
66
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
67
 */
68
public class GPERegister {
69
        private static ArrayList parsers = new ArrayList();
70
        
71
        /**
72
         * Adds a new GPE parser
73
         * @param driver
74
         */
75
        private static void addGpeDriver(GPEParser parser){
76
                parsers.add(parser);
77
        }
78
        
79
        /**
80
         * Adds a new GPE parser
81
         * @param calssName
82
         * Class name for the GPE parser
83
         * @param contentHandler
84
         * Consummer content handler
85
         * @param errorsHanlder
86
         * Consumer errorsHandler
87
         * @throws ClassNotFoundException 
88
         * @throws NoSuchMethodException 
89
         * @throws InvocationTargetException 
90
         * @throws IllegalAccessException 
91
         * @throws InstantiationException 
92
         * @throws SecurityException 
93
         * @throws IllegalArgumentException 
94
         */
95
        private static void addGpeDriver(String className, 
96
                        GPEContentHandler contentHandler,
97
                        GPEErrorHandler errorsHanlder) throws ClassNotFoundException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
98
                Class [] args = {GPEContentHandler.class,GPEErrorHandler.class};
99
                Object [] params = {contentHandler,errorsHanlder};
100
                
101
                Class clazz = Class.forName(className);
102
                if (clazz != null){
103
                        GPEParser parser = (GPEParser)clazz.getConstructor(args).newInstance(params);
104
                        parsers.add(parser);
105
                }                
106
        }
107
        
108
        
109

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