View Javadoc

1   package org.apache.torque.generator.configuration.source;
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.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   * A base class for reading source definitions from the controller
43   * configuration file.
44   */
45  public abstract class SourceSaxHandler extends DefaultHandler
46  {
47      /** The access object for the configuration files, not null. */
48      private ConfigurationProvider configurationProvider;
49  
50      /** The paths of the surrounding project, not null. */
51      private ProjectPaths projectPaths;
52  
53      /** The known configuration handlers. */
54      private ConfigurationHandlers configurationHandlers;
55  
56      /** The source process configuration. */
57      private SourceProcessConfiguration sourceProcessConfiguration;
58  
59      /**
60       * The transformer definitions in the source element.
61       */
62      private List<SourceTransformerDefinition> transformerDefinitions
63              = new ArrayList<SourceTransformerDefinition>();
64  
65      /** The handler which handles transformer elements. */
66      private SourceTransformerSaxHandler transformerSaxHandler;
67  
68      /** Whether this handler has completed its task. */
69      private boolean finished = false;
70  
71      /**
72       * Constructor.
73       *
74       * @param configurationProvider The access object for the configuration
75       *        files, not null.
76       * @param projectPaths The paths of the surrounding project, not null.
77       * @param configurationHandlers All known configuration handlers, not null.
78       *
79       * @throws NullPointerException if an argument is null.
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * Returns the configurationProvider to access the configuration.
195      *
196      * @return the configurationProvider to access the configuration, not null.
197      */
198     public ConfigurationProvider getConfigurationProvider()
199     {
200         return configurationProvider;
201     }
202 
203     /**
204      * Returns the known configuration handlers.
205      *
206      * @return the configuration handlers, not null.
207      */
208     public ConfigurationHandlers getConfigurationHandlers()
209     {
210         return configurationHandlers;
211     }
212 
213     /**
214      * Returns the paths of the surrounding project.
215      *
216      * @return the paths of the surrounding project, not null.
217      */
218     public ProjectPaths getProjectPaths()
219     {
220         return projectPaths;
221     }
222 
223     /**
224      * Returns whether the matching snippet was completely parsed.
225      *
226      * @return true if the matching snippet was completely parsed,
227      *         false otherwise.
228      */
229     public boolean isFinished()
230     {
231         return finished;
232     }
233 
234     /**
235      * Marks that the matching snippet was completely parsed.
236      */
237     protected void finished()
238     {
239         finished = true;
240     }
241 
242     /**
243      * Returns the information how to read the sources.
244      *
245      * @return the source Provider, not null if the
246      *         source snippet was processed.
247      */
248     public abstract SourceProvider getSourceProvider();
249 
250     /**
251      * Returns the information how to post-process the sources after loading.
252      *
253      * @return the sourceProcessConfiguration, not null if the
254      *         source snippet was processed.
255      */
256     public SourceProcessConfiguration getSourceProcessConfiguration()
257     {
258         return sourceProcessConfiguration;
259     }
260 }