1 package org.apache.torque.generator.configuration.outlet;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Handles a outlet declaration within the outlet configuration.
41 * Base class for more specific handlers.
42 */
43 public abstract class OutletSaxHandler extends DefaultHandler
44 {
45 /** the logger for the class. */
46 private static Log log = LogFactory.getLog(OutletSaxHandler.class);
47
48 /**
49 * the name for the outlet which configuration will be read in
50 * by the generated SaxHandlerFactory, or null if the name
51 * of the outlet should be determined from the parsed XML.
52 */
53 private QualifiedName outletName;
54
55 /** the outlet to be configured. */
56 private Outlet outlet;
57
58 /** The access object for the configuration files, not null. */
59 private ConfigurationProvider configurationProvider;
60
61 /** The paths of the surrounding project, not null. */
62 private ProjectPaths projectPaths;
63
64 /** The available configuration handlers. */
65 private ConfigurationHandlers configurationHandlers;
66
67 /**
68 * The SAX handler for processing mergepoint declarations.
69 * Not null only if a mergepoint declaration is currently processed.
70 */
71 private MergepointSaxHandler mergepointSaxHandler;
72
73 /** whether we are past the end of the outlet declaration. */
74 private boolean finished = false;
75
76 /**
77 * The raw name of the XML element which started the outlet definition.
78 */
79 private String startTagRawName = null;
80
81 /**
82 * Creates a OutletSaxHandler.
83 *
84 * @param outletName the name for the outlet which configuration
85 * will be read in by the generated SaxHandlerFactory,
86 * or null if the name of the outlet should be determined from
87 * the parsed XML.
88 * @param configurationProvider The access object for the configuration
89 * files, not null.
90 * @param projectPaths The paths of the surrounding project, not null.
91 * @param configurationHandlers the available configuration handlers,
92 * not null.
93 *
94 * @throws NullPointerException if an argument is null.
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 * {@inheritDoc}
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 * {@inheritDoc}
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 * @param outletName the name for the outlet which configuration
212 * will be read in by the generated SaxHandlerFactory,
213 * or null if the name of the outlet should be determined from
214 * the parsed xml.
215 * @param uri - The Namespace URI, or the empty string if the
216 * element has no Namespace URI or if Namespace processing is not
217 * being performed.
218 * @param localName - The local name (without prefix), or
219 * the empty string if Namespace processing is not being performed.
220 * @param rawName - The qualified name (with prefix), or the empty string if
221 * qualified names are not available.
222 * @param attributes - The attributes attached to the element.
223 * If there are no attributes, it shall be an empty Attributes
224 * object.
225 *
226 * @return the outlet, not null.
227 *
228 * @throws SAXException if the outlet cannot be created.
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 * Returns the outlet being configured.
240 *
241 * @return the outlet, not null.
242 */
243 public Outlet getOutlet()
244 {
245 return outlet;
246 }
247
248 /**
249 * Returns whether we are currently processing a mergepoint tag.
250 *
251 * @return true if we are currently processing a mergepoint tag,
252 * false otherwise.
253 */
254 protected boolean isProcessingMergepointTag()
255 {
256 return (mergepointSaxHandler != null);
257 }
258
259 /**
260 * Returns whether we are past the end of the outlet configuration XML
261 * snippet which we are parsing.
262 *
263 * @return true if the whole snippet has been processed, false otherwise.
264 */
265 public boolean isFinished()
266 {
267 return finished;
268 }
269
270 /**
271 * Returns the ConfigurationProvider.
272 *
273 * @return the ConfigurationProvider, not null.
274 */
275 public ConfigurationProvider getConfigurationProvider()
276 {
277 return configurationProvider;
278 }
279
280 /**
281 * Returns the paths of the surrounding project.
282 *
283 * @return the paths of the surrounding project, not null.
284 */
285 public ProjectPaths getProjectPaths()
286 {
287 return projectPaths;
288 }
289
290 /**
291 * Returns the configuration handlers.
292 *
293 * @return the configuration handlers, not null.
294 */
295 public ConfigurationHandlers getConfigurationHandlers()
296 {
297 return configurationHandlers;
298 }
299 }