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.OutputConfigurationTags.ENCODING_ATTRIBUTE;
23 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.EXISTING_TARGET_STRATEGY_ATTRIBUTE;
24 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.FILENAME_GENERATOR_TAG;
25 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.NAME_ATTRIBUTE;
26 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.OUTLET_TAG;
27 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.OUTPUT_TAG;
28 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.FILE_ATTRIBUTE;
29 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.OUTPUT_DIR_KEY_ATTRIBUTE;
30 import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.INPUT_TAG;
31 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SOURCE_TAG;
32
33 import org.apache.torque.generator.configuration.ConfigurationHandlers;
34 import org.apache.torque.generator.configuration.ConfigurationProvider;
35 import org.apache.torque.generator.configuration.XMLConstants;
36 import org.apache.torque.generator.configuration.outlet.OutletConfigurationSaxHandler;
37 import org.apache.torque.generator.configuration.outlet.OutletSaxHandler;
38 import org.apache.torque.generator.configuration.paths.ProjectPaths;
39 import org.apache.torque.generator.configuration.source.SourceSaxHandler;
40 import org.apache.torque.generator.configuration.source.SourceSaxHandlerFactories;
41 import org.apache.torque.generator.configuration.source.SourceSaxHandlerFactory;
42 import org.apache.torque.generator.qname.QualifiedName;
43 import org.xml.sax.Attributes;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.helpers.DefaultHandler;
46
47
48
49
50 public class OutputSaxHandler extends DefaultHandler
51 {
52
53 private static final QualifiedName FILENAME_OUTLET_NAME
54 = new QualifiedName(
55 "org.apache.torque.generator.configuration.filenameOutlet");
56
57
58 private ConfigurationProvider configurationProvider;
59
60
61 private ProjectPaths projectPaths;
62
63
64 private ConfigurationHandlers configurationHandlers;
65
66
67 private Output output;
68
69
70
71
72
73 private OutletReferenceSaxHandler contentOutletSaxHandler;
74
75
76
77
78
79 private SourceSaxHandler sourceSaxHandler;
80
81
82
83
84
85
86 private OutletSaxHandler filenameOutletSaxHandler;
87
88
89
90
91
92
93
94
95
96
97
98 public OutputSaxHandler(
99 ConfigurationProvider configurationProvider,
100 ProjectPaths projectPaths,
101 ConfigurationHandlers configurationHandlers)
102 {
103 if (configurationProvider == null)
104 {
105 throw new NullPointerException(
106 "configurationProvider must not be null");
107 }
108 if (projectPaths == null)
109 {
110 throw new NullPointerException(
111 "projectPaths must not be null");
112 }
113 if (configurationHandlers == null)
114 {
115 throw new NullPointerException(
116 "configurationHandlers must not be null");
117 }
118 this.configurationProvider = configurationProvider;
119 this.configurationHandlers = configurationHandlers;
120 this.projectPaths = projectPaths;
121 }
122
123
124
125
126
127
128
129 public Output getOutputFile()
130 {
131 return output;
132 }
133
134
135
136
137 @Override
138 public void startElement(
139 String uri,
140 String localName,
141 String rawName,
142 Attributes attributes)
143 throws SAXException
144 {
145 if (contentOutletSaxHandler != null)
146 {
147 contentOutletSaxHandler.startElement(
148 uri,
149 localName,
150 rawName,
151 attributes);
152 }
153 else if (filenameOutletSaxHandler != null)
154 {
155 filenameOutletSaxHandler.startElement(
156 uri,
157 localName,
158 rawName,
159 attributes);
160 }
161 else if (sourceSaxHandler != null)
162 {
163 sourceSaxHandler.startElement(
164 uri,
165 localName,
166 rawName,
167 attributes);
168 }
169 else if (OUTPUT_TAG.equals(rawName))
170 {
171 String name = attributes.getValue(NAME_ATTRIBUTE);
172 if (name == null)
173 {
174 throw new SAXException("The attribute "
175 + NAME_ATTRIBUTE
176 + " must be set for the tag "
177 + INPUT_TAG);
178 }
179 this.output = new Output(new QualifiedName(name));
180
181 if (attributes.getValue(FILE_ATTRIBUTE) != null)
182 {
183 output.setFilename(attributes.getValue(FILE_ATTRIBUTE));
184 }
185 if (attributes.getValue(EXISTING_TARGET_STRATEGY_ATTRIBUTE) != null)
186 {
187 output.setExistingTargetStrategy(
188 attributes.getValue(EXISTING_TARGET_STRATEGY_ATTRIBUTE));
189 }
190 if (attributes.getValue(OUTPUT_DIR_KEY_ATTRIBUTE) != null)
191 {
192 output.setOutputDirKey(
193 attributes.getValue(OUTPUT_DIR_KEY_ATTRIBUTE));
194 }
195 if (attributes.getValue(ENCODING_ATTRIBUTE) != null)
196 {
197 output.setEncoding(attributes.getValue(ENCODING_ATTRIBUTE));
198 }
199 }
200 else if (SOURCE_TAG.equals(rawName))
201 {
202 String type = attributes.getValue(
203 XMLConstants.XSI_NAMESPACE,
204 XMLConstants.XSI_TYPE_ATTRBUTE_NAME);
205 SourceSaxHandlerFactories sourceSaxHandlerFactories
206 = configurationHandlers.getSourceSaxHandlerFactories();
207 SourceSaxHandlerFactory sourceSaxHandlerFactory
208 = sourceSaxHandlerFactories.getSourceSaxHandlerFactory(type);
209 if (sourceSaxHandlerFactory == null)
210 {
211 throw new SAXException("Unknown source type "
212 + type
213 + ". Known source types are "
214 + sourceSaxHandlerFactories.getSourceTypes());
215 }
216 sourceSaxHandler = sourceSaxHandlerFactory.getSourceSaxHandler(
217 configurationProvider,
218 projectPaths,
219 configurationHandlers);
220 sourceSaxHandler.startElement(uri, localName, rawName, attributes);
221 }
222 else if (OUTLET_TAG.equals(rawName))
223 {
224 contentOutletSaxHandler
225 = new OutletReferenceSaxHandler();
226 contentOutletSaxHandler.startElement(
227 uri,
228 localName,
229 rawName,
230 attributes);
231 }
232 else if (FILENAME_GENERATOR_TAG.equals(rawName))
233 {
234 OutletConfigurationSaxHandler outletConfigurationSaxHandler
235 = new OutletConfigurationSaxHandler(
236 configurationProvider,
237 projectPaths,
238 configurationHandlers);
239 String outletType
240 = OutletConfigurationSaxHandler.getOutletType(
241 attributes);
242 filenameOutletSaxHandler
243 = outletConfigurationSaxHandler.getOutletHandler(
244 FILENAME_OUTLET_NAME,
245 outletType);
246 filenameOutletSaxHandler.startElement(
247 uri,
248 localName,
249 rawName,
250 attributes);
251 }
252 else
253 {
254 throw new SAXException("Unknown element " + rawName);
255 }
256 }
257
258
259
260
261 @Override
262 public void endElement(String uri, String localName, String rawName)
263 throws SAXException
264 {
265 if (contentOutletSaxHandler != null)
266 {
267 contentOutletSaxHandler.endElement(
268 uri,
269 localName,
270 rawName);
271 if (contentOutletSaxHandler.isFinished())
272 {
273 output.setContentOutlet(
274 contentOutletSaxHandler
275 .getOutletReference());
276 contentOutletSaxHandler = null;
277 }
278 }
279 else if (sourceSaxHandler != null)
280 {
281 sourceSaxHandler.endElement(uri, localName, rawName);
282 if (sourceSaxHandler.isFinished())
283 {
284 output.setSourceProvider(
285 sourceSaxHandler.getSourceProvider());
286 output.setSourceProcessConfiguration(
287 sourceSaxHandler.getSourceProcessConfiguration());
288 sourceSaxHandler = null;
289 }
290 }
291 else if (filenameOutletSaxHandler != null)
292 {
293 filenameOutletSaxHandler.endElement(uri, localName, rawName);
294 if (filenameOutletSaxHandler.isFinished())
295 {
296 output.setFilenameOutlet(
297 filenameOutletSaxHandler
298 .getOutlet());
299 filenameOutletSaxHandler = null;
300 }
301 }
302 }
303
304
305
306
307 @Override
308 public void characters(char[] ch, int start, int length)
309 throws SAXException
310 {
311 if (contentOutletSaxHandler != null)
312 {
313 contentOutletSaxHandler.characters(
314 ch, start, length);
315 }
316 else if (sourceSaxHandler != null)
317 {
318 sourceSaxHandler.characters(ch, start, length);
319 }
320 else if (filenameOutletSaxHandler != null)
321 {
322 filenameOutletSaxHandler.characters(ch, start, length);
323 }
324 }
325 }