Revision 11247 trunk/libraries/libGPE/src/org/gvsig/gpe/GPERegister.java

View differences:

GPERegister.java
3 3
import java.io.File;
4 4
import java.lang.reflect.InvocationTargetException;
5 5
import java.util.ArrayList;
6
import java.util.Hashtable;
7
import java.util.Iterator;
6 8

  
7 9
import org.gvsig.gpe.writers.GPEWriterHandler;
8 10

  
......
50 52
 *
51 53
 * $Id$
52 54
 * $Log$
53
 * Revision 1.8  2007-04-18 12:54:45  csanchez
55
 * Revision 1.9  2007-04-19 07:23:20  jorpiell
56
 * Add the add methods to teh contenhandler and change the register mode
57
 *
58
 * Revision 1.8  2007/04/18 12:54:45  csanchez
54 59
 * Actualizacion protoripo libGPE
55 60
 *
56 61
 * Revision 1.7  2007/04/17 07:53:55  jorpiell
......
86 91
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
87 92
 */
88 93
public class GPERegister {
89
	private static ArrayList parsers = new ArrayList();
94
	private static Hashtable parsers = new Hashtable();
95
	private static Hashtable writers = new Hashtable();
90 96
	
91 97
	/**
92 98
	 * Adds a new GPE parser
93
	 * @param driver
99
	 * @param name
100
	 * Driver name. It must be written like FORMAT VERSION
101
	 * @param description
102
	 * Driver description. Just a descriptive text
103
	 * @param clazz
104
	 * The parser class	
105
	 * @throws NoSuchMethodException 
106
	 * @throws InvocationTargetException 
107
	 * @throws IllegalAccessException 
108
	 * @throws InstantiationException 
109
	 * @throws SecurityException 
110
	 * @throws IllegalArgumentException 
94 111
	 */
95
	public static void addGpeDriver(GPEParser parser){
96
		parsers.add(parser);
112
	public static void addGpeParser(String name, String description,Class clazz) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{ 
113
		if (clazz != null){
114
			GPEParser parser = (GPEParser)clazz.getConstructor(null).newInstance(null);
115
			parsers.put(name, parser);
116
		}		
97 117
	}
98 118
	
99 119
	/**
100
	 * Adds a new GPE parser
101
	 * @param calssName
102
	 * Class name for the GPE parser
103
	 * @param contentHandler
104
	 * Consummer content handler
105
	 * @param errorsHanlder
106
	 * Consumer errorsHandler
107
	 * @throws ClassNotFoundException 
120
	 * Adds a new GPEWriterHandler
121
	 * @param name
122
	 * Driver name. It must be written like FORMAT VERSION
123
	 * @param description
124
	 * Driver description. Just a descriptive text
125
	 * @param clazz
126
	 * The parser class	
108 127
	 * @throws NoSuchMethodException 
109 128
	 * @throws InvocationTargetException 
110 129
	 * @throws IllegalAccessException 
......
112 131
	 * @throws SecurityException 
113 132
	 * @throws IllegalArgumentException 
114 133
	 */
115
	public static void addGpeDriver(String className, 
116
			GPEContentHandler contentHandler,
117
			GPEErrorHandler errorsHanlder) throws ClassNotFoundException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
118
		Class [] args = {GPEContentHandler.class,GPEErrorHandler.class};
119
		Object [] params = {contentHandler,errorsHanlder};
120
		
121
		Class clazz = Class.forName(className);
134
	public static void addGpeWriterHandler(String name, String description,Class clazz) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
122 135
		if (clazz != null){
123
			GPEParser parser = (GPEParser)clazz.getConstructor(args).newInstance(params);
124
			parsers.add(parser);
125
		}		
136
			GPEWriterHandler parser = (GPEWriterHandler)clazz.getConstructor(null).newInstance(null);
137
			writers.put(name, parser);
138
		}	
126 139
	}
127 140
	
128
	
129

  
130 141
	/**
131
	 * Return true if exists a driver that can open the file
132
	 * @param file
133
	 * File to open
142
	 * Create a new parser from a name
143
	 * @param name
144
	 * @param contenHandler
145
	 * @param errorHandler
146
	 * @throws NoSuchMethodException 
147
	 * @throws InvocationTargetException 
148
	 * @throws IllegalAccessException 
149
	 * @throws InstantiationException 
150
	 * @throws SecurityException 
151
	 * @throws IllegalArgumentException 
134 152
	 * @return
135
	 * true if the driver exists
153
	 * Null if the parser doesn't exist
136 154
	 */
137
	public static boolean accept(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 true;
142
			}
143
		}
144
		return false;
155
	public static GPEParser createParser(String name) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
156
		Object parser =  parsers.get(name);
157
		if (parser != null){
158
			return (GPEParser)parser.getClass().getConstructor(null).newInstance(null);
159
		}	
160
		return null;
145 161
	}
146 162
	
147 163
	/**
......
150 166
	 * File to open
151 167
	 * @return
152 168
	 * Null if the driver doesn't exist
169
	 * @throws NoSuchMethodException 
170
	 * @throws InvocationTargetException 
171
	 * @throws IllegalAccessException 
172
	 * @throws InstantiationException 
173
	 * @throws SecurityException 
174
	 * @throws IllegalArgumentException 
153 175
	 */
154

  
155

  
156
	public static GPEParser getParser(File file) throws Exception{
157

  
158
		for (int i=0 ; i<parsers.size() ; i++){
159
			GPEParser parser = (GPEParser)parsers.get(i);
176
	public static GPEParser createParser(File file) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
177
		Iterator keys = parsers.keySet().iterator();
178
		while (keys.hasNext()){
179
			String key = (String)keys.next();
180
			GPEParser parser = (GPEParser)parsers.get(key);
160 181
			if (parser.accept(file)){
161
				parser.getContentHandler().initialize();
162
				parser.getErrorHandler().initialize();
163
				return parser;
182
				return createParser(key);
164 183
			}
165 184
		}
166 185
		return null;
167 186
	}
168 187
	
169 188
	/**
170
	 * Gets the writer that is used to write the file
171
	 * @param format
172
	 * File format
189
	 * Create a new content writer from a name
190
	 * @param name
191
	 * @param contenHandler
192
	 * @param errorHandler
193
	 * @throws NoSuchMethodException 
194
	 * @throws InvocationTargetException 
195
	 * @throws IllegalAccessException 
196
	 * @throws InstantiationException 
197
	 * @throws SecurityException 
198
	 * @throws IllegalArgumentException 
199
	 */
200
	public static GPEWriterHandler createWriter(String name) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
201
		Object writer =  writers.get(name);
202
		if (writer != null){
203
			return (GPEWriterHandler)writer.getClass().getConstructor(null).newInstance(null);
204
		}	
205
		return null;
206
	}
207
	
208

  
209
	/**
210
	 * Return true if exists a driver that can open the file
211
	 * @param file
212
	 * File to open
173 213
	 * @return
174
	 * Null if the writer doesn't exist
214
	 * true if the driver exists
175 215
	 */
176
	public static GPEWriterHandler getWriter(String format){
177
		for (int i=0 ; i<parsers.size() ; i++){
178
			GPEParser parser = (GPEParser)parsers.get(i);
179
			String[] formats = parser.getFormats();
180
			for (int j=0 ; j<formats.length ; j++){
181
				if (formats[j].toUpperCase().equals(format.toUpperCase())){
182
					return parser.getWriter(format);
183
				}
216
	public static boolean accept(File file){
217
		Iterator keys = parsers.keySet().iterator();
218
		while (keys.hasNext()){
219
			GPEParser parser = (GPEParser)parsers.get(keys.next());
220
			if (parser.accept(file)){
221
				return true;
184 222
			}
185 223
		}
186
		return null;
224
		return false;
187 225
	}
188

  
189 226
}

Also available in: Unified diff