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.DRIVER_OPTION_ATTRIBUTE;
23  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.PASSWORD_OPTION_ATTRIBUTE;
24  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SCHEMA_OPTION_ATTRIBUTE;
25  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SOURCE_TAG;
26  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.URL_OPTION_ATTRIBUTE;
27  import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.USERNAME_OPTION_ATTRIBUTE;
28  
29  import org.apache.torque.generator.configuration.ConfigurationException;
30  import org.apache.torque.generator.configuration.ConfigurationHandlers;
31  import org.apache.torque.generator.configuration.ConfigurationProvider;
32  import org.apache.torque.generator.configuration.paths.ProjectPaths;
33  import org.apache.torque.generator.source.SourceProvider;
34  import org.apache.torque.generator.source.jdbc.JdbcMetadataSourceProvider;
35  import org.xml.sax.Attributes;
36  import org.xml.sax.SAXException;
37  
38  /**
39   * Reads jdbc metadata source definitions from the controller configuration
40   * file.
41   */
42  public class JdbcMetadataSourceSaxHandler extends SourceSaxHandler
43  {
44      /**
45       * The source provider which is configured by this SAX handler.
46       */
47      private SourceProvider sourceProvider;
48  
49      /**
50       * Constructor.
51       *
52       * @param configurationProvider The access object for the configuration
53       *        files, not null.
54       * @param projectPaths The paths of the surrounding project, not null.
55       * @param configurationHandlers All known configuration handlers, not null.
56       *
57       * @throws NullPointerException if an argument is null.
58       */
59      public JdbcMetadataSourceSaxHandler(
60              ConfigurationProvider configurationProvider,
61              ProjectPaths projectPaths,
62              ConfigurationHandlers configurationHandlers)
63      {
64          super(configurationProvider, projectPaths, configurationHandlers);
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void startElement(String uri, String localName, String rawName,
72                               Attributes attributes)
73              throws SAXException
74      {
75          if (rawName.equals(SOURCE_TAG))
76          {
77              String urlOption
78                      = attributes.getValue(URL_OPTION_ATTRIBUTE);
79              String driverOption
80                      = attributes.getValue(DRIVER_OPTION_ATTRIBUTE);
81              String usernameOption
82                      = attributes.getValue(USERNAME_OPTION_ATTRIBUTE);
83              String passwordOption
84                      = attributes.getValue(PASSWORD_OPTION_ATTRIBUTE);
85              String schemaOption
86                      = attributes.getValue(SCHEMA_OPTION_ATTRIBUTE);
87              try
88              {
89                  sourceProvider = new JdbcMetadataSourceProvider(
90                          urlOption,
91                          driverOption,
92                          usernameOption,
93                          passwordOption,
94                          schemaOption);
95              }
96              catch (ConfigurationException e)
97              {
98                  throw new SAXException(
99                          "Could not parse Source Tag: " + e.getMessage(),
100                         e);
101             }
102         }
103         super.startElement(uri, localName, rawName, attributes);
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public void endElement(String uri, String localName, String rawName)
111         throws SAXException
112     {
113         super.endElement(uri, localName, rawName);
114         if (rawName.equals(SOURCE_TAG))
115         {
116             finished();
117         }
118     }
119 
120     /**
121      * Returns the configuration filled with the contents of the parsed snippet.
122      *
123      * @return the configuration which was filled, not null if a
124      *         matching snippet was processed.
125      */
126     public SourceProvider getSourceProvider()
127     {
128         return sourceProvider;
129     }
130 }