View Javadoc

1   package org.apache.torque.generator.configuration.controller;
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.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   * Reads the controller configuration from the controller configuration file.
50   */
51  public class ControlConfigurationSaxHandler extends DefaultHandler
52  {
53      /** The Controller configuration which is configured by this Handler. */
54      private ControlConfiguration controllerConfiguration;
55  
56      /** The known configuration handlers. */
57      private ConfigurationHandlers configurationHandlers;
58  
59      /**
60       * The SAX handler which handles the output declarations, or null
61       * if output declarations need not currently be handled.
62       */
63      private OutputSaxHandler outputSaxHandler;
64  
65      /**
66       * The SAX handler which handles the entity reference declarations, or null
67       * if entity reference declarations need not currently be handled.
68       */
69      private EntityReferenceSaxHandler entityReferenceSaxHandler;
70  
71      /**
72       * The SAX handler which handles the options declarations, or null
73       * if options declarations need not currently be handled.
74       */
75      private OptionsSaxHandler optionsSaxHandler;
76  
77      /**
78       * Object for accessing the configuration.
79       */
80      private ConfigurationProvider configurationProvider;
81  
82      /**
83       * The paths in the configuration.
84       */
85      private ProjectPaths projectPaths;
86  
87      /**
88       * Constructor.
89       *
90       * @param controllerConfiguration the configuration object to fill, no null.
91       * @param configurationProvider the Object for accessing the configuration,
92       *        not null.
93       * @param projectPaths the paths in the configuration, not null.
94       * @param configurationHandlers the available configuration handlers,
95       *        not null.
96       *
97       * @throws NullPointerException if an argument is null.
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * Receive notification of character data inside an element.
294      *
295      * @param ch The characters.
296      * @param start The start position in the character array.
297      * @param length The number of characters to use from the
298      *               character array.
299      * @exception org.xml.sax.SAXException Any SAX exception, possibly
300      *            wrapping another exception.
301      * @see org.xml.sax.ContentHandler#characters
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      * EntityResolver implementation. Called by the XML parser
315      *
316      * @param publicId The public identifier of the external entity.
317      * @param systemId The system identifier of the external entity.
318      *
319      * @return an InputSource for the entity, or null if the URI is not known.
320      *
321      * @see ConfigurationEntityResolver#resolveEntity(String, String)
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 }