View Javadoc

1   package org.apache.torque.generator.configuration.mergepoint;
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.mergepoint.MergepointConfigurationTags.ACTION_TAG;
23  import static org.apache.torque.generator.configuration.mergepoint.MergepointConfigurationTags.MERGEPOINT_NAME_ATTRIBUTE;
24  import static org.apache.torque.generator.configuration.mergepoint.MergepointConfigurationTags.MERGEPOINT_TAG;
25  
26  import org.apache.torque.generator.configuration.ConfigurationHandlers;
27  import org.apache.torque.generator.configuration.ConfigurationProvider;
28  import org.apache.torque.generator.configuration.XMLConstants;
29  import org.apache.torque.generator.configuration.paths.ProjectPaths;
30  import org.xml.sax.Attributes;
31  import org.xml.sax.SAXException;
32  
33  /**
34   * A SAX Handler which processes a mergepoint configuration in an outlet.
35   */
36  public class MergepointSaxHandler
37  {
38      /**
39       * The mergepoint mapping which is created and filled using the
40       * information in the parsed XML element.
41       */
42      private MergepointMapping mergepointMapping;
43  
44      /**
45       * The access object for the configuration files, not null.
46       */
47      private ConfigurationProvider configurationProvider;
48  
49      /**
50       * The paths of the surrounding project, not null.
51       */
52      private ProjectPaths projectPaths;
53  
54      /**
55       * The available configuration handlers.
56       */
57      private ConfigurationHandlers configurationHandlers;
58  
59      /**
60       * A SAX handler which parses nested elements. Null if no nested element
61       * is currently parsed.
62       */
63      private ActionSaxHandler delegateHandler;
64  
65      /**
66       * Whether the mergepoint element is completely parsed.
67       */
68      private boolean finished = false;
69  
70      /**
71       * Constructor.
72       *
73       * @param configurationProvider The access object for the configuration
74       *        files, not null.
75       * @param projectPaths The paths of the surrounding project, not null.
76       * @param configurationHandlers the available configuration handlers,
77       *        not null.
78       *
79       * @throws NullPointerException if an argument is null.
80       */
81      public MergepointSaxHandler(
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("projectPaths must not be null");
94          }
95          if (configurationHandlers == null)
96          {
97              throw new NullPointerException(
98                      "configurationHandlers must not be null");
99          }
100         this.configurationProvider = configurationProvider;
101         this.projectPaths = projectPaths;
102         this.configurationHandlers = configurationHandlers;
103     }
104 
105     /**
106      * Returns the mergepointMapping configured by this SaxHandler.
107      * If this method is called before the mergepoint tag has been processed
108      * completely, it will throw an IllegalStateException.
109      *
110      * @return the complete mergepointMapping, never null.
111      *
112      * @throws IllegalStateException if the mergepoint tag has not been
113      *           processed completely.
114      */
115     public MergepointMapping getMergepointMapping()
116     {
117         if (!finished)
118         {
119             throw new IllegalStateException("not finished parsing");
120         }
121         return mergepointMapping;
122     }
123 
124     /**
125      * Callback method which is called by the SAX parser if an XML element is
126      * started.
127      * If a known element is encountered, its settings are read and applied to
128      * the mergepoint; if an unknown element is encountered, a SaxException is
129      * thrown.
130      *
131      * @param uri The namespace URI of the started element,
132      *        or the empty string if the element has no namespace URI
133      *        or if namespace processing is not being performed.
134      * @param localName The local name (without prefix), or
135      *        the empty string if Namespace processing is not being performed.
136      * @param qName The qualified name (with prefix, if present),
137      *        or the empty string if  qualified names are not available.
138      * @param attributes The attributes attached to the element.
139      *
140      * @throws SAXException if an error occurs during parsing.
141      *
142      * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
143      */
144     public void startElement(
145             String uri,
146             String localName,
147             String qName,
148             Attributes attributes)
149         throws SAXException
150     {
151         if (delegateHandler != null)
152         {
153             delegateHandler.startElement(uri, localName, qName, attributes);
154         }
155         else if (MERGEPOINT_TAG.equals(qName))
156         {
157             String name = attributes.getValue(MERGEPOINT_NAME_ATTRIBUTE);
158             if (name == null)
159             {
160                 throw new SAXException("The tag "
161                         + MERGEPOINT_TAG
162                         + " needs to have the attribute "
163                         + MERGEPOINT_NAME_ATTRIBUTE);
164             }
165             mergepointMapping = new MergepointMapping(name);
166         }
167         else if (ACTION_TAG.equals(qName))
168         {
169             String type = attributes.getValue(
170                     XMLConstants.XSI_NAMESPACE,
171                     XMLConstants.XSI_TYPE_ATTRBUTE_NAME);
172             if (type == null)
173             {
174                 throw new SAXException("The tag " + ACTION_TAG
175                         + " requires the attribute "
176                         + XMLConstants.XSI_NAMESPACE
177                         + ":"
178                         + XMLConstants.XSI_TYPE_ATTRBUTE_NAME);
179             }
180 
181             ActionSaxHandlerFactories actionSaxHandlerFactories
182                    = configurationHandlers.getActionSaxHandlerFactories();
183             ActionSaxHandlerFactory handlerFactory
184                 = actionSaxHandlerFactories.getActionSaxHandlerFactory(type);
185             if (handlerFactory == null)
186             {
187                 throw new SAXException("No handler found for the action "
188                         + "of type "
189                         + type);
190             }
191 
192             delegateHandler = handlerFactory.getActionSaxHandler(
193                     uri,
194                     localName,
195                     qName,
196                     attributes,
197                     configurationProvider,
198                     projectPaths);
199         }
200         else
201         {
202             throw new SAXException("unknown element : " + qName);
203         }
204     }
205 
206     /**
207      * Callback method which is called by the SAX parser if an XML element is
208      * ended.
209      * If an action element is ended, the action is added to the action
210      * list for the mergepoint. If the mergepoint element is ended, the parser
211      * marks itself as finished.
212      *
213      * @param uri The namespace URI of the ended element,
214      *        or the empty string if the element has no namespace URI
215      *        or if namespace processing is not being performed.
216      * @param localName The local name (without prefix), or
217      *        the empty string if Namespace processing is not being performed.
218      * @param qName The qualified name (with prefix, if present),
219      *        or the empty string if  qualified names are not available.
220      *
221      * @see org.xml.sax.ContentHandler#endElement(String, String, String)
222      */
223     public void endElement(String uri, String localName, String qName)
224     {
225         if (ACTION_TAG.equals(qName))
226         {
227             mergepointMapping.addAction(delegateHandler.getAction());
228             delegateHandler = null;
229         }
230         else if (MERGEPOINT_TAG.equals(qName))
231         {
232             finished = true;
233         }
234     }
235 
236     /**
237      * Returns whether the parser has finished parsing the mergepoint tag.
238      *
239      * @return true if the parser has finished, false otherwise.
240      */
241     public boolean isFinished()
242     {
243         return finished;
244     }
245 }