1 package org.apache.torque.generator.configuration.controller;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.torque.generator.configuration.controller.ControlConfigurationTags.CONTROL_LOGLEVEL_ATTRIBUTE;
23 import static org.apache.torque.generator.configuration.controller.ControlConfigurationTags.CONTROL_TAG;
24 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.OUTPUT_TAG;
25 import static org.apache.torque.generator.configuration.option.OptionTags.OPTIONS_TAG;
26 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.ENTITY_REFERENCE;
27
28 import java.io.IOException;
29
30 import org.apache.torque.generator.configuration.ConfigurationEntityResolver;
31 import org.apache.torque.generator.configuration.ConfigurationException;
32 import org.apache.torque.generator.configuration.ConfigurationHandlers;
33 import org.apache.torque.generator.configuration.ConfigurationProvider;
34 import org.apache.torque.generator.configuration.SaxHelper;
35 import org.apache.torque.generator.configuration.XMLConstants;
36 import org.apache.torque.generator.configuration.mergepoint.OptionsSaxHandlerFactories;
37 import org.apache.torque.generator.configuration.option.OptionsConfiguration;
38 import org.apache.torque.generator.configuration.option.OptionsSaxHandler;
39 import org.apache.torque.generator.configuration.option.OptionsSaxHandlerFactory;
40 import org.apache.torque.generator.configuration.paths.ProjectPaths;
41 import org.apache.torque.generator.configuration.source.EntityReferenceSaxHandler;
42 import org.xml.sax.Attributes;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.SAXParseException;
46 import org.xml.sax.helpers.DefaultHandler;
47
48
49
50
51 public class ControlConfigurationSaxHandler extends DefaultHandler
52 {
53
54 private ControlConfiguration controllerConfiguration;
55
56
57 private ConfigurationHandlers configurationHandlers;
58
59
60
61
62
63 private OutputSaxHandler outputSaxHandler;
64
65
66
67
68
69 private EntityReferenceSaxHandler entityReferenceSaxHandler;
70
71
72
73
74
75 private OptionsSaxHandler optionsSaxHandler;
76
77
78
79
80 private ConfigurationProvider configurationProvider;
81
82
83
84
85 private ProjectPaths projectPaths;
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public ControlConfigurationSaxHandler(
100 ControlConfiguration controllerConfiguration,
101 ConfigurationProvider configurationProvider,
102 ProjectPaths projectPaths,
103 ConfigurationHandlers configurationHandlers)
104 {
105 if (controllerConfiguration == null)
106 {
107 throw new NullPointerException(
108 "controllerConfiguration must not be null");
109 }
110 if (configurationProvider == null)
111 {
112 throw new NullPointerException(
113 "configurationProvider must not be null");
114 }
115 if (projectPaths == null)
116 {
117 throw new NullPointerException(
118 "projectPaths must not be null");
119 }
120 if (configurationHandlers == null)
121 {
122 throw new NullPointerException(
123 "configurationHandlers must not be null");
124 }
125 this.configurationProvider = configurationProvider;
126 this.controllerConfiguration = controllerConfiguration;
127 this.configurationHandlers = configurationHandlers;
128 this.projectPaths = projectPaths;
129 }
130
131
132
133
134 @Override
135 public void startElement(String uri, String localName, String qName,
136 Attributes attributes)
137 throws SAXException
138 {
139 String unqualifiedName = SaxHelper.getUnqualifiedName(localName, qName);
140 if (outputSaxHandler != null)
141 {
142 outputSaxHandler.startElement(
143 uri,
144 localName,
145 qName,
146 attributes);
147 }
148 else if (optionsSaxHandler != null)
149 {
150 optionsSaxHandler.startElement(
151 uri,
152 localName,
153 qName,
154 attributes);
155 }
156 else if (entityReferenceSaxHandler != null)
157 {
158 entityReferenceSaxHandler.startElement(
159 uri,
160 localName,
161 qName,
162 attributes);
163 }
164 else if (CONTROL_TAG.equals(unqualifiedName))
165 {
166 String loglevel = attributes.getValue(CONTROL_LOGLEVEL_ATTRIBUTE);
167 if (loglevel != null)
168 {
169 Loglevel level = Loglevel.getByKey(loglevel);
170 controllerConfiguration.setLoglevel(level);
171 }
172 }
173 else if (OPTIONS_TAG.equals(unqualifiedName))
174 {
175 String type = attributes.getValue(
176 XMLConstants.XSI_NAMESPACE,
177 XMLConstants.XSI_TYPE_ATTRBUTE_NAME);
178 if (type == null)
179 {
180 throw new SAXException("The tag " + OPTIONS_TAG
181 + " requires the attribute "
182 + XMLConstants.XSI_NAMESPACE
183 + ":"
184 + XMLConstants.XSI_TYPE_ATTRBUTE_NAME);
185 }
186
187 OptionsSaxHandlerFactories optionsSaxHandlerFactories
188 = configurationHandlers.getOptionsSaxHandlerFactories();
189 OptionsSaxHandlerFactory optionsSaxHandlerFactory
190 = optionsSaxHandlerFactories.getOptionsSaxHandlerFactory(
191 type);
192 if (optionsSaxHandlerFactory == null)
193 {
194 throw new SAXException("No handler found for the action "
195 + "of type "
196 + type);
197 }
198 optionsSaxHandler = optionsSaxHandlerFactory.getOptionsSaxHandler();
199 optionsSaxHandler.startElement(uri, localName, qName, attributes);
200 }
201 else if (OUTPUT_TAG.equals(unqualifiedName))
202 {
203 outputSaxHandler = new OutputSaxHandler(
204 configurationProvider,
205 projectPaths,
206 configurationHandlers);
207 outputSaxHandler.startElement(
208 uri,
209 localName,
210 qName,
211 attributes);
212 }
213 else if (ENTITY_REFERENCE.equals(unqualifiedName))
214 {
215 entityReferenceSaxHandler = new EntityReferenceSaxHandler(
216 configurationProvider,
217 projectPaths);
218 entityReferenceSaxHandler.startElement(
219 uri,
220 localName,
221 qName,
222 attributes);
223 }
224 else
225 {
226 throw new SAXException("Unknown element " + unqualifiedName);
227 }
228 }
229
230
231
232
233 @Override
234 public void endElement(String uri, String localName, String rawName)
235 throws SAXException
236 {
237 if (optionsSaxHandler != null)
238 {
239 optionsSaxHandler.endElement(uri, localName, rawName);
240 if (optionsSaxHandler.isFinished())
241 {
242 OptionsConfiguration optionsConfiguration
243 = optionsSaxHandler.getOptionsConfiguration();
244 controllerConfiguration.addOptionsConfiguration(
245 optionsConfiguration);
246 optionsSaxHandler = null;
247 }
248 }
249 else if (OUTPUT_TAG.equals(rawName))
250 {
251 outputSaxHandler.endElement(uri, localName, rawName);
252 Output outputFile = outputSaxHandler.getOutputFile();
253 try
254 {
255 controllerConfiguration.addOutput(outputFile);
256 }
257 catch (ConfigurationException e)
258 {
259 throw new SAXException("Could not add output "
260 + outputFile.getName(), e);
261 }
262 outputSaxHandler = null;
263 }
264 else if (outputSaxHandler != null)
265 {
266 outputSaxHandler.endElement(uri, localName, rawName);
267 }
268 else if (entityReferenceSaxHandler != null)
269 {
270 entityReferenceSaxHandler.endElement(uri, localName, rawName);
271 if (entityReferenceSaxHandler.isFinished())
272 {
273 try
274 {
275 controllerConfiguration.getEntityReferences()
276 .addEntityReference(
277 entityReferenceSaxHandler.getSystemId(),
278 entityReferenceSaxHandler.readResource());
279 }
280 catch (ConfigurationException e)
281 {
282 throw new SAXException(
283 "Could not read entity reference "
284 + entityReferenceSaxHandler.getSystemId(),
285 e);
286 }
287 entityReferenceSaxHandler = null;
288 }
289 }
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303 @Override
304 public void characters(char[] ch, int start, int length)
305 throws SAXException
306 {
307 if (outputSaxHandler != null)
308 {
309 outputSaxHandler.characters(ch, start, length);
310 }
311 }
312
313
314
315
316
317
318
319
320
321
322
323 public InputSource resolveEntity(String publicId, String systemId)
324 throws SAXException, IOException
325 {
326 return new ConfigurationEntityResolver().resolveEntity(
327 publicId, systemId);
328 }
329
330 @Override
331 public void error(SAXParseException exception) throws SAXParseException
332 {
333 throw exception;
334 }
335
336 @Override
337 public void fatalError(SAXParseException exception)
338 throws SAXParseException
339 {
340 throw exception;
341 }
342
343 @Override
344 public void warning(SAXParseException exception) throws SAXParseException
345 {
346 throw exception;
347 }
348 }