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.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   * Reads an output declaration from the controller configuration file.
49   */
50  public class OutputSaxHandler extends DefaultHandler
51  {
52      /** The qualified name for the filename outlet. */
53      private static final QualifiedName FILENAME_OUTLET_NAME
54              = new QualifiedName(
55                  "org.apache.torque.generator.configuration.filenameOutlet");
56  
57      /** The access object for the configuration files, not null. */
58      private ConfigurationProvider configurationProvider;
59  
60      /** The paths of the surrounding project, not null. */
61      private ProjectPaths projectPaths;
62  
63      /** All known configuration handlers. */
64      private ConfigurationHandlers configurationHandlers;
65  
66      /** The output declaration which is currently parsed. */
67      private Output output;
68  
69      /**
70       * The SAX handler which handles the reference to the content outlet,
71       * or null if no content outlet is currently processed.
72       */
73      private OutletReferenceSaxHandler contentOutletSaxHandler;
74  
75      /**
76       * The SAX handler which handles source tags, or null if source tags
77       * need not be handled in the current context.
78       */
79      private SourceSaxHandler sourceSaxHandler;
80  
81      /**
82       * The SAX handler which handles the filename outlet configuration,
83       * or null if filename outlet tags need not be handled
84       * in the current context.
85       */
86      private OutletSaxHandler filenameOutletSaxHandler;
87  
88      /**
89       * Constructor.
90       *
91       * @param configurationProvider The access object for the configuration
92       *        files, not null.
93       * @param projectPaths The paths of the surrounding project, not null.
94       * @param configurationHandlers handlers for reading the configuration.
95       *
96       * @throws NullPointerException if an argument is null.
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      * Returns the Configuration filled with the contents of the parsed snippet.
125      *
126      * @return the configuration representing the parsed snippet.
127      *         Not null if the mathcing xml snippet was parsed.
128      */
129     public Output getOutputFile()
130     {
131         return output;
132     }
133 
134     /**
135      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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 }