View Javadoc

1   package org.apache.torque.generator.configuration.outlet;
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.outlet.OutletConfigurationTags.OUTLET_ENCODING_ATTRIBUTE;
23  import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_NAME_ATTRIBUTE;
24  import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_OPTIONS_IN_CONTEXT_ATTRIBUTE;
25  import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_PATH_ATTRIBUTE;
26  import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_SOURCE_ATTRIBUTES_IN_CONTEXT_ATTRIBUTE;
27  import static org.apache.torque.generator.configuration.outlet.OutletConfigurationTags.OUTLET_VARIABLES_IN_CONTEXT_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.SaxHelper;
33  import org.apache.torque.generator.configuration.paths.ProjectPaths;
34  import org.apache.torque.generator.qname.QualifiedName;
35  import org.apache.torque.generator.template.velocity.VelocityOutlet;
36  import org.xml.sax.Attributes;
37  import org.xml.sax.SAXException;
38  
39  /**
40   * Handles a declaration of a velocity outlet within a outlet
41   * configuration file.
42   */
43  class VelocityOutletSaxHandler extends OutletSaxHandler
44  {
45      /**
46       * Constructor.
47       *
48       * @param outletName the name for the outlet which configuration
49       *        will be read in by the generated SaxHandlerFactory,
50       *        or null if the name of the outlet should be determined from
51       *        the parsed XML.
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 the available configuration handlers,
56       *        not null.
57       *
58       * @throws SAXException if an error occurs during creation of the outlet.
59       */
60      public VelocityOutletSaxHandler(
61              QualifiedName outletName,
62              ConfigurationProvider configurationProvider,
63              ProjectPaths projectPaths,
64              ConfigurationHandlers configurationHandlers)
65         throws SAXException
66      {
67          super(outletName,
68                 configurationProvider,
69                 projectPaths,
70                 configurationHandlers);
71      }
72  
73      /**
74       * Instantiates and configures a velocity outlet.
75       *
76       * @param outletName the name for the outlet which configuration
77       *        will be read in by the generated SaxHandlerFactory,
78       *        or null if the name of the outlet should be determined from
79       *        the parsed xml.
80       * @param uri - The Namespace URI, or the empty string if the
81       *        element has no Namespace URI or if Namespace processing is not
82       *        being performed.
83       * @param localName - The local name (without prefix), or
84       *        the empty string if Namespace processing is not being performed.
85       * @param rawName - The qualified name (with prefix), or the empty string if
86       *        qualified names are not available.
87       * @param attributes - The attributes attached to the element.
88       *          If there are no attributes, it shall be an empty Attributes
89       *          object.
90       *
91       * @return the created outlet, not null.
92       *
93       * @throws SAXException if an error occurs during creation.
94       */
95      protected VelocityOutlet createOutlet(
96              QualifiedName outletName,
97              String uri,
98              String localName,
99              String rawName,
100             Attributes attributes)
101         throws SAXException
102     {
103         if (outletName == null)
104         {
105             String nameAttribute
106                     = attributes.getValue(OUTLET_NAME_ATTRIBUTE);
107             if (nameAttribute == null)
108             {
109                 throw new SAXException("The attribute "
110                         + OUTLET_NAME_ATTRIBUTE
111                         + " must be set on the element "
112                         + rawName
113                         + " for Velocity Outlets");
114             }
115             outletName = new QualifiedName(nameAttribute);
116         }
117 
118         String encoding = attributes.getValue(OUTLET_ENCODING_ATTRIBUTE);
119         String path = attributes.getValue(OUTLET_PATH_ATTRIBUTE);
120 
121         try
122         {
123             VelocityOutlet result
124                     = new VelocityOutlet(
125                         outletName,
126                         getConfigurationProvider(),
127                         path,
128                         encoding);
129             Boolean optionsInContext = SaxHelper.getBooleanAttribute(
130                     OUTLET_OPTIONS_IN_CONTEXT_ATTRIBUTE,
131                     attributes,
132                     "the velocityOutlet" + outletName);
133             if (optionsInContext != null)
134             {
135                 result.setOptionsInContext(optionsInContext);
136             }
137             Boolean sourceElementAttributesInContext
138                     = SaxHelper.getBooleanAttribute(
139                             OUTLET_SOURCE_ATTRIBUTES_IN_CONTEXT_ATTRIBUTE,
140                             attributes,
141                             "the velocityOutlet" + outletName);
142             if (sourceElementAttributesInContext != null)
143             {
144                 result.setSourceAttributesInContext(
145                         sourceElementAttributesInContext);
146             }
147             Boolean variablesInContext = SaxHelper.getBooleanAttribute(
148                     OUTLET_VARIABLES_IN_CONTEXT_ATTRIBUTE,
149                     attributes,
150                     "the velocityOutlet" + outletName);
151             if (variablesInContext != null)
152             {
153                 result.setVariablesInContext(variablesInContext);
154             }
155             return result;
156         }
157         catch (ConfigurationException e)
158         {
159             throw new SAXException(e);
160         }
161     }
162 }