1 package org.apache.torque.generator.configuration.source;
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.source.SourceConfigurationTags.ELEMENTS_ATTRIBUTE;
23 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SKIP_DECIDER_ARRTIBUTE;
24 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SOURCE_TAG;
25 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.TRANSFORMER_TAG;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.torque.generator.configuration.ConfigurationException;
31 import org.apache.torque.generator.configuration.ConfigurationHandlers;
32 import org.apache.torque.generator.configuration.ConfigurationProvider;
33 import org.apache.torque.generator.configuration.paths.ProjectPaths;
34 import org.apache.torque.generator.source.SourceProcessConfiguration;
35 import org.apache.torque.generator.source.SourceProvider;
36 import org.apache.torque.generator.source.SourceTransformerDefinition;
37 import org.xml.sax.Attributes;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.helpers.DefaultHandler;
40
41
42
43
44
45 public abstract class SourceSaxHandler extends DefaultHandler
46 {
47
48 private ConfigurationProvider configurationProvider;
49
50
51 private ProjectPaths projectPaths;
52
53
54 private ConfigurationHandlers configurationHandlers;
55
56
57 private SourceProcessConfiguration sourceProcessConfiguration;
58
59
60
61
62 private List<SourceTransformerDefinition> transformerDefinitions
63 = new ArrayList<SourceTransformerDefinition>();
64
65
66 private SourceTransformerSaxHandler transformerSaxHandler;
67
68
69 private boolean finished = false;
70
71
72
73
74
75
76
77
78
79
80
81 public SourceSaxHandler(
82 ConfigurationProvider configurationProvider,
83 ProjectPaths projectPaths,
84 ConfigurationHandlers configurationHandlers)
85 {
86 if (configurationProvider == null)
87 {
88 throw new NullPointerException(
89 "configurationProvider must not be null");
90 }
91 if (projectPaths == null)
92 {
93 throw new NullPointerException(
94 "projectPaths must not be null");
95 }
96 if (configurationHandlers == null)
97 {
98 throw new NullPointerException(
99 "configurationHandlers must not be null");
100 }
101 this.configurationProvider = configurationProvider;
102 this.projectPaths = projectPaths;
103 this.configurationHandlers = configurationHandlers;
104 }
105
106
107
108
109 @Override
110 public void startElement(String uri, String localName, String rawName,
111 Attributes attributes)
112 throws SAXException
113 {
114 if (transformerSaxHandler != null)
115 {
116 transformerSaxHandler.startElement(
117 uri, localName, rawName, attributes);
118 }
119 else if (TRANSFORMER_TAG.equals(rawName))
120 {
121 transformerSaxHandler
122 = new SourceTransformerSaxHandler(
123 configurationProvider, projectPaths);
124 transformerSaxHandler.startElement(
125 uri, localName, rawName, attributes);
126 }
127 else if (rawName.equals(SOURCE_TAG))
128 {
129 sourceProcessConfiguration = new SourceProcessConfiguration();
130 sourceProcessConfiguration.setStartElementsPath(
131 attributes.getValue(ELEMENTS_ATTRIBUTE));
132 try
133 {
134 sourceProcessConfiguration.setSkipDecider(
135 attributes.getValue(SKIP_DECIDER_ARRTIBUTE));
136 }
137 catch (ConfigurationException e)
138 {
139 throw new SAXException("Could not create source: "
140 + e.getMessage(),
141 e);
142 }
143 }
144 else
145 {
146 throw new SAXException("Unknown element " + rawName);
147 }
148 }
149
150
151
152
153 @Override
154 public void endElement(String uri, String localName, String rawName)
155 throws SAXException
156 {
157 if (transformerSaxHandler != null)
158 {
159 transformerSaxHandler.endElement(
160 uri,
161 localName,
162 rawName);
163 if (transformerSaxHandler.isFinished())
164 {
165 transformerDefinitions.add(new SourceTransformerDefinition(
166 transformerSaxHandler.getSourceTransformer(),
167 transformerSaxHandler.getElements()));
168 transformerSaxHandler = null;
169 }
170 }
171 else if (rawName.equals(SOURCE_TAG))
172 {
173 sourceProcessConfiguration.setSourceTransformerDefinitions(
174 transformerDefinitions);
175 transformerDefinitions
176 = new ArrayList<SourceTransformerDefinition>();
177 }
178 }
179
180
181
182
183 @Override
184 public void characters(char[] ch, int start, int length)
185 throws SAXException
186 {
187 if (transformerSaxHandler != null)
188 {
189 transformerSaxHandler.characters(ch, start, length);
190 }
191 }
192
193
194
195
196
197
198 public ConfigurationProvider getConfigurationProvider()
199 {
200 return configurationProvider;
201 }
202
203
204
205
206
207
208 public ConfigurationHandlers getConfigurationHandlers()
209 {
210 return configurationHandlers;
211 }
212
213
214
215
216
217
218 public ProjectPaths getProjectPaths()
219 {
220 return projectPaths;
221 }
222
223
224
225
226
227
228
229 public boolean isFinished()
230 {
231 return finished;
232 }
233
234
235
236
237 protected void finished()
238 {
239 finished = true;
240 }
241
242
243
244
245
246
247
248 public abstract SourceProvider getSourceProvider();
249
250
251
252
253
254
255
256 public SourceProcessConfiguration getSourceProcessConfiguration()
257 {
258 return sourceProcessConfiguration;
259 }
260 }