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.COMBINE_FILES_ATTRIBUTE;
23  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.EXCLUDE_TAG;
24  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.FORMAT_ATTRIBUTE;
25  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.INCLUDE_TAG;
26  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SOURCE_TAG;
27  
28  import java.util.HashSet;
29  import java.util.Set;
30  
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.paths.ProjectPaths;
36  import org.apache.torque.generator.file.Fileset;
37  import org.apache.torque.generator.source.SourceProvider;
38  import org.apache.torque.generator.source.stream.FileSourceProvider;
39  import org.apache.torque.generator.source.stream.StreamSourceFormat;
40  import org.xml.sax.Attributes;
41  import org.xml.sax.SAXException;
42  
43  /**
44   * Reads file source definitions from the controller configuration file.
45   */
46  public class FileSourceSaxHandler extends SourceSaxHandler
47  {
48      /** The file format of the source element which is currently parsed. */
49      private String format;
50  
51      /** The source file names which should be included in generation. */
52      private Set<String> includes = new HashSet<String>();
53  
54      /** The source file names which should be excluded from generation. */
55      private Set<String> excludes = new HashSet<String>();
56  
57      /** Whether to combine all source files. */
58      private Boolean combineFiles;
59  
60      /**
61       * The string builder for an include definition.
62       * Not null if an include tag is currently parsed.
63       */
64      private StringBuilder currentInclude = null;
65  
66      /**
67       * The string builder for an exclude definition.
68       * Not null if an exclude tag is currently parsed.
69       */
70      private StringBuilder currentExclude = null;
71  
72      /**
73       * The source provider which is configured by this SAX handler.
74       */
75      private SourceProvider sourceProvider;
76  
77      /**
78       * Constructor.
79       *
80       * @param configurationProvider The access object for the configuration
81       *        files, not null.
82       * @param projectPaths The paths of the surrounding project, not null.
83       * @param configurationHandlers All known configuration handlers, not null.
84       *
85       * @throws NullPointerException if an argument is null.
86       */
87      public FileSourceSaxHandler(
88              ConfigurationProvider configurationProvider,
89              ProjectPaths projectPaths,
90              ConfigurationHandlers configurationHandlers)
91      {
92          super(configurationProvider, projectPaths, configurationHandlers);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public void startElement(String uri, String localName, String rawName,
100                              Attributes attributes)
101             throws SAXException
102     {
103         if (rawName.equals(INCLUDE_TAG))
104         {
105             currentInclude = new StringBuilder();
106         }
107         else if (rawName.equals(EXCLUDE_TAG))
108         {
109             currentExclude = new StringBuilder();
110         }
111         else if (rawName.equals(SOURCE_TAG))
112         {
113             super.startElement(uri, localName, rawName, attributes);
114             format = attributes.getValue(FORMAT_ATTRIBUTE);
115             if (attributes.getValue(COMBINE_FILES_ATTRIBUTE) != null)
116             {
117                 combineFiles = SaxHelper.getBooleanAttribute(
118                         COMBINE_FILES_ATTRIBUTE,
119                         attributes,
120                         "the element " + SOURCE_TAG);
121             }
122         }
123         else
124         {
125             super.startElement(uri, localName, rawName, attributes);
126         }
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public void endElement(String uri, String localName, String rawName)
134         throws SAXException
135     {
136         if (rawName.equals(INCLUDE_TAG))
137         {
138             includes.add(currentInclude.toString());
139             currentInclude = null;
140         }
141         else if (rawName.equals(EXCLUDE_TAG))
142         {
143             excludes.add(currentExclude.toString());
144             currentExclude = null;
145         }
146         else if (rawName.equals(SOURCE_TAG))
147         {
148             try
149             {
150                 StreamSourceFormat sourceFormat = null;
151                 if (format != null)
152                 {
153                     Set<StreamSourceFormat> sourceFormats
154                         = getConfigurationHandlers().getStreamSourceFormats();
155                     for (StreamSourceFormat candidate : sourceFormats)
156                     {
157                         if (format.equals(candidate.getKey()))
158                         {
159                             sourceFormat = candidate;
160                             break;
161                         }
162                     }
163                     if (sourceFormat == null)
164                     {
165                         throw new SAXException("Unknown source format : "
166                                 + format
167                                 + " Known types are: " + sourceFormats);
168                     }
169                 }
170                 Fileset sourceFileset = new Fileset(
171                         getProjectPaths().getDefaultSourcePath(),
172                         includes,
173                         excludes);
174 
175                 sourceProvider = new FileSourceProvider(
176                         sourceFormat,
177                         sourceFileset,
178                         combineFiles);
179             }
180             catch (ConfigurationException e)
181             {
182                 throw new SAXException("Could not create source: "
183                             + e.getMessage(),
184                         e);
185             }
186             super.endElement(uri, localName, rawName);
187             finished();
188         }
189         else
190         {
191             super.endElement(uri, localName, rawName);
192         }
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public void characters(char[] ch, int start, int length)
200             throws SAXException
201     {
202         if (currentInclude != null)
203         {
204             for (int i = start; i < start + length; ++i)
205             {
206                 currentInclude.append(ch[i]);
207             }
208         }
209         else if (currentExclude != null)
210         {
211             for (int i = start; i < start + length; ++i)
212             {
213                 currentExclude.append(ch[i]);
214             }
215         }
216         else
217         {
218             super.characters(ch, start, length);
219         }
220     }
221 
222     /**
223      * Returns the information how to read the sources.
224      *
225      * @return the source Provider, not null if the
226      *         source snippet was processed.
227      */
228     public SourceProvider getSourceProvider()
229     {
230         return sourceProvider;
231     }
232 }