1 package org.apache.torque.generator.configuration.outlet;
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.mergepoint.MergepointConfigurationTags.MERGEPOINT_TAG;
23 import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.INPUT_ELEMENT_NAME_ATTRIBUTE;
24 import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.INPUT_TAG;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.torque.generator.configuration.ConfigurationException;
29 import org.apache.torque.generator.configuration.ConfigurationHandlers;
30 import org.apache.torque.generator.configuration.ConfigurationProvider;
31 import org.apache.torque.generator.configuration.mergepoint.MergepointSaxHandler;
32 import org.apache.torque.generator.configuration.paths.ProjectPaths;
33 import org.apache.torque.generator.outlet.Outlet;
34 import org.apache.torque.generator.qname.QualifiedName;
35 import org.xml.sax.Attributes;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.helpers.DefaultHandler;
38
39
40
41
42
43 public abstract class OutletSaxHandler extends DefaultHandler
44 {
45
46 private static Log log = LogFactory.getLog(OutletSaxHandler.class);
47
48
49
50
51
52
53 private QualifiedName outletName;
54
55
56 private Outlet outlet;
57
58
59 private ConfigurationProvider configurationProvider;
60
61
62 private ProjectPaths projectPaths;
63
64
65 private ConfigurationHandlers configurationHandlers;
66
67
68
69
70
71 private MergepointSaxHandler mergepointSaxHandler;
72
73
74 private boolean finished = false;
75
76
77
78
79 private String startTagRawName = null;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public OutletSaxHandler(
97 QualifiedName outletName,
98 ConfigurationProvider configurationProvider,
99 ProjectPaths projectPaths,
100 ConfigurationHandlers configurationHandlers)
101 {
102 if (configurationProvider == null)
103 {
104 log.error("OutletSaxHandler: "
105 + " configurationProvider is null");
106 throw new NullPointerException("configurationProvider is null");
107 }
108 if (projectPaths == null)
109 {
110 log.error("OutletSaxHandler: "
111 + " projectPaths is null");
112 throw new NullPointerException("projectPaths is null");
113 }
114 if (configurationHandlers == null)
115 {
116 log.error("OutletSaxHandler: "
117 + " configurationHandlers is null");
118 throw new NullPointerException("configurationHandlers is null");
119 }
120 this.outletName = outletName;
121 this.configurationProvider = configurationProvider;
122 this.projectPaths = projectPaths;
123 this.configurationHandlers = configurationHandlers;
124 }
125
126
127
128
129 @Override
130 public void startElement(
131 String uri,
132 String localName,
133 String rawName,
134 Attributes attributes)
135 throws SAXException
136 {
137 if (startTagRawName == null)
138 {
139 startTagRawName = rawName;
140 outlet = createOutlet(
141 outletName,
142 uri,
143 localName,
144 rawName,
145 attributes);
146 }
147 else if (INPUT_TAG.equals(rawName))
148 {
149 String element = attributes.getValue(INPUT_ELEMENT_NAME_ATTRIBUTE);
150 if (element == null)
151 {
152 throw new SAXException("The attribute "
153 + INPUT_ELEMENT_NAME_ATTRIBUTE
154 + " must be set for the tag "
155 + INPUT_TAG);
156 }
157 outlet.setInputElementName(element);
158 }
159 else if (MERGEPOINT_TAG.equals(rawName))
160 {
161 mergepointSaxHandler = new MergepointSaxHandler(
162 configurationProvider,
163 projectPaths,
164 configurationHandlers);
165 mergepointSaxHandler.startElement(uri, localName, rawName, attributes);
166 }
167 else if (mergepointSaxHandler != null)
168 {
169 mergepointSaxHandler.startElement(uri, localName, rawName, attributes);
170 }
171 else
172 {
173 throw new SAXException("unknown Element " + rawName);
174 }
175 }
176
177
178
179
180 @Override
181 public void endElement(String uri, String localName, String rawName)
182 throws SAXException
183 {
184 if (mergepointSaxHandler != null)
185 {
186 mergepointSaxHandler.endElement(uri, localName, rawName);
187 if (mergepointSaxHandler.isFinished())
188 {
189 try
190 {
191 outlet.addMergepointMapping(
192 mergepointSaxHandler.getMergepointMapping());
193 }
194 catch (ConfigurationException e)
195 {
196 throw new SAXException(
197 "Could not get mergepoint mapping from the "
198 + "mergepoint Sax handler",
199 e);
200 }
201 mergepointSaxHandler = null;
202 }
203 }
204 else if (startTagRawName.equals(rawName))
205 {
206 finished = true;
207 }
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 protected abstract Outlet createOutlet(
231 QualifiedName outletName,
232 String uri,
233 String localName,
234 String rawName,
235 Attributes attributes)
236 throws SAXException;
237
238
239
240
241
242
243 public Outlet getOutlet()
244 {
245 return outlet;
246 }
247
248
249
250
251
252
253
254 protected boolean isProcessingMergepointTag()
255 {
256 return (mergepointSaxHandler != null);
257 }
258
259
260
261
262
263
264
265 public boolean isFinished()
266 {
267 return finished;
268 }
269
270
271
272
273
274
275 public ConfigurationProvider getConfigurationProvider()
276 {
277 return configurationProvider;
278 }
279
280
281
282
283
284
285 public ProjectPaths getProjectPaths()
286 {
287 return projectPaths;
288 }
289
290
291
292
293
294
295 public ConfigurationHandlers getConfigurationHandlers()
296 {
297 return configurationHandlers;
298 }
299 }