View Javadoc

1   package org.apache.torque.generator.control;
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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.torque.generator.configuration.UnitConfiguration;
27  import org.apache.torque.generator.configuration.controller.OutletReference;
28  import org.apache.torque.generator.configuration.controller.Output;
29  import org.apache.torque.generator.option.Option;
30  import org.apache.torque.generator.option.OptionName;
31  import org.apache.torque.generator.option.Options;
32  import org.apache.torque.generator.outlet.Outlet;
33  import org.apache.torque.generator.qname.Namespace;
34  import org.apache.torque.generator.qname.QualifiedName;
35  import org.apache.torque.generator.source.SourceElement;
36  import org.apache.torque.generator.source.SourceProvider;
37  import org.apache.torque.generator.variable.VariableStore;
38  
39  /**
40   * The state of the controller.  Contains all stuff the controller needs to
41   * track.
42   */
43  public class ControllerState
44  {
45      /**
46       * The Source provider which is currently used.
47       */
48      private SourceProvider sourceProvider;
49  
50      /**
51       * The output which currently processed .
52       */
53      private Output output;
54  
55      /**
56       * The current stack of outlets being executed.
57       */
58      private List<Outlet> outlets = new ArrayList<Outlet>();
59  
60      /**
61       * The root element of the source graph.
62       */
63      private SourceElement rootElement;
64  
65      /**
66       * The current element within the source.
67       */
68      private SourceElement sourceElement;
69  
70      /**
71       * The generation unit which is currently processed.
72       */
73      private UnitConfiguration unitConfiguration;
74  
75      /**
76       * The reference in the controller to the root outlet.
77       * May override mergepoints in the outlets.
78       */
79      private OutletReference rootOutletReference;
80  
81      /**
82       * The variable store.
83       */
84      private VariableStore variableStore = new VariableStore();
85  
86      /**
87       * The currently generated output file. May be null if
88       * no file is currently generated (e.g. if the filename is currently
89       * generated).
90       */
91      private File outputFile;
92  
93      /**
94       * The name of the currently processed source file. May be null if
95       * no source file is used (e.g. if the input is created by other means
96       * than reading a file).
97       */
98      private File sourceFile;
99  
100     /**
101      * The name space in which the current outlet operates.
102      */
103     private Namespace outletNamespace;
104 
105     /**
106      * Returns the source provider which is currently in use.
107      *
108      * @return the current source provider.
109      */
110     public SourceProvider getSourceProvider()
111     {
112         return sourceProvider;
113     }
114 
115     /**
116      * Sets the source provider which is currently in use.
117      *
118      * @param sourceProvider the current source provider.
119      */
120     public void setSourceProvider(SourceProvider sourceProvider)
121     {
122         this.sourceProvider = sourceProvider;
123     }
124 
125     /**
126      * Returns the output declaration which is currently processed.
127      *
128      * @return the output declaration which is currently processed, may be null
129      *         only if no output is processed at the moment.
130      */
131     public Output getOutput()
132     {
133         return output;
134     }
135 
136     /**
137      * Sets the output declaration which is currently processed.
138      *
139      * @param output the output which is currently processed.
140      */
141     void setOutput(Output output)
142     {
143         this.output = output;
144     }
145 
146     /**
147      * Returns the topmost outlet in the stack of outlets.
148      *
149      * @return the topmost outlet in the stack of outlets, or null
150      *         if the stack is empty.
151      */
152     public Outlet getOutlet()
153     {
154         if (outlets.isEmpty())
155         {
156             return null;
157         }
158         return outlets.get(outlets.size() - 1);
159     }
160 
161     /**
162      * Pushes a outlet onto the stack of outlets.
163      *
164      * @param outlet the outlet to be added to the stack of outlets,
165      *        not null.
166      */
167     public void pushOutlet(Outlet outlet)
168     {
169         if (outlet == null)
170         {
171             throw new NullPointerException("outlet must not be null");
172         }
173         this.outlets.add(outlet);
174     }
175 
176     /**
177      * Pops the topmost outlets from the stack of outlets.
178      *
179      * @return the removed outlet, not null.
180      *
181      * @throws IndexOutOfBoundsException if the stack is empty.
182      */
183     public Outlet popOutlet()
184     {
185         return outlets.remove(outlets.size() - 1);
186     }
187 
188     /**
189      * Returns the current source element. Does not return null
190      * during generation.
191      *
192      * @return the current source element.
193      */
194     public SourceElement getSourceElement()
195     {
196         return sourceElement;
197     }
198 
199     /**
200      * Sets the current source element.
201      *
202      * @param sourceElement the new current source element, or null
203      *        to remove the current source element.
204      */
205     public void setSourceElement(SourceElement sourceElement)
206     {
207         this.sourceElement = sourceElement;
208     }
209 
210     /**
211      * Returns the root element of the current source.
212      *
213      * @return The the root element of the current source;
214      *         may be null only if no source is currently processed.
215      */
216     public SourceElement getRootElement()
217     {
218         return rootElement;
219     }
220 
221     /**
222      * Sets the root element of the current source.
223      *
224      * @param rootElement the the root element of the current source,
225      *        or null to remove the current root element.
226      */
227     public void setRootElement(SourceElement rootElement)
228     {
229         this.rootElement = rootElement;
230     }
231 
232     /**
233      * Returns the reference to the current outlet.
234      *
235      * @return the reference to the current outlet, or null if no
236      *         outlet is currently active.
237      */
238     public OutletReference getRootOutletReference()
239     {
240         return rootOutletReference;
241     }
242 
243     /**
244      * Sets the reference to the root outlet.
245      *
246      * @param rootOutletReference the reference to the root outlet
247      *        (i.e. the outlet which produces the whole content),
248      *        or null to remove the reference.
249      */
250     void setRootOutletReference(OutletReference rootOutletReference)
251     {
252         this.rootOutletReference = rootOutletReference;
253     }
254 
255 
256     /**
257      * Sets the name space of the outlet which is currently active.
258      *
259      * @param namespace the namespace of the outlet which is currently
260      *        active, or null to remove the name space.
261      */
262     void setOutletNamespace(Namespace namespace)
263     {
264         outletNamespace = namespace;
265     }
266 
267     /**
268      * Returns the namespace of the outlet which is currently active.
269      *
270      * @return the name space of the active outlet. May be null only
271      *         if no generation is in progress.
272      */
273     public Namespace getOutletNamespace()
274     {
275         return outletNamespace;
276     }
277 
278     /**
279      * Calculates the value of an option in the current outlet's context.
280      * The default namespace which is used when no namespace is given in
281      * <code>name</code> is the namespace of the currently used outlet.
282      *
283      * @param name the name of the option, can contain a namespace.
284      *
285      * @return The value of the option, or null if no option with that name
286      *         is visible from the given namespace.
287      */
288     public Object getOption(String name)
289     {
290         Options options = unitConfiguration.getOptions();
291         QualifiedName qualifiedName = getQualifiedName(name);
292         Option option = options.getInHierarchy(qualifiedName);
293         Object result = null;
294         if (option != null)
295         {
296             result = option.getValue();
297         }
298         return result;
299     }
300 
301     /**
302      * Calculates the value of an option in the current outlet's context.
303      * The default namespace which is used when no namespace is given in
304      * <code>name</code> is the namespace of the currently used outlet.
305      *
306      * @param name the object containing the name of the option,
307      *        which can contain a namespace, not null.
308      *
309      * @return The value of the option, or null if no option with that name
310      *         is visible from the given namespace.
311      *
312      * @throws NullPointerException if optionName is null.
313      */
314     public Object getOption(OptionName optionName)
315     {
316         return getOption(optionName.getName());
317     }
318 
319     /**
320      * Convenience method to return the value of an option as boolean.
321      * The option is evaluated in the current outlet's context, see
322      * getOption(String). <br/>
323      * Uses Boolean.paseBoolean() for String->Boolean conversion.
324      * @param name the name of the option, can contain a namespace.
325      *
326      * @return The value of the option as boolean, or false if no option
327      *         with that name is visible from the given namespace,
328      */
329     public boolean getBooleanOption(String name)
330     {
331         Object option = getOption(name);
332         if (option == null)
333         {
334             return false;
335         }
336         return Boolean.parseBoolean(option.toString());
337     }
338 
339     /**
340      * Convenience method to return the value of an option as boolean.
341      * The option is evaluated in the current outlet's context, see
342      * getOption(String). <br/>
343      * Uses Boolean.paseBoolean() for String->Boolean conversion.
344      *
345      * @param optionName the object containing the name of the option,
346      *        which can contain a namespace.
347      *
348      * @return The value of the option as boolean, or false if no option
349      *         with that name is visible from the given namespace.
350      *
351      * @throws NullPointerExeption if optionName is null.
352      */
353     public boolean getBooleanOption(OptionName optionName)
354     {
355         return getBooleanOption(optionName.getName());
356     }
357 
358     /**
359      * Convenience method to return the value of an option as String.
360      * The option is evaluated in the current outlet's context, see
361      * getOption(String). <br/>
362      *
363      * @param name the name of the option, can contain a namespace.
364      *
365      * @return The value of the option as boolean, or false if no option
366      *         with that name is visible from the given namespace,
367      */
368     public String getStringOption(String name)
369     {
370         Object option = getOption(name);
371         if (option == null)
372         {
373             return null;
374         }
375         return option.toString();
376     }
377 
378     /**
379      * Convenience method to return the value of an option as String.
380      * The option is evaluated in the current outlet's context, see
381      * getOption(String). <br/>
382      *
383      * @param optionName the object containing the name of the option,
384      *        which can contain a namespace.
385      *
386      * @return The value of the option as String, or null if no option
387      *         with that name is visible from the given namespace,
388      *
389      * @throws NullPointerExeption if optionName is null.
390      */
391     public String getStringOption(OptionName optionName)
392     {
393         return getStringOption(optionName.getName());
394     }
395 
396     /**
397      * Returns all options which are visible from the current outlet's
398      * namespace.
399      *
400      * @return all visible options, not null.
401      */
402     public Options getVisibleOptions()
403     {
404         return unitConfiguration.getOptions().getInHierarchy(
405                 outletNamespace);
406     }
407 
408     /**
409      * Returns the VariableStore where generation variables can be set.
410      *
411      * @return the variableStore, never null.
412      */
413     public VariableStore getVariableStore()
414     {
415         return variableStore;
416     }
417 
418     /**
419      * Converts a name to a QualifiedName, using the outlet namespace as
420      * default namespace is fone is given.
421      *
422      * @param name the name to convert to a qualifiedName, not null.
423      * @return the corresponding qualifiedName.
424      *
425      * @throws NullPointerException if name is null
426      * @throws IllegalArgumentException if name is no valid qualifiedName.
427      */
428     public QualifiedName getQualifiedName(String name)
429     {
430         QualifiedName qualifiedName = new QualifiedName(
431                 name,
432                 outletNamespace);
433         return qualifiedName;
434     }
435 
436     /**
437      * Returns the currently generated file.
438      *
439      * @return the current output file. May only be null if no
440      *         output file is currently generated (e.g. if the file name
441      *         is currently generated).
442      */
443     public File getOutputFile()
444     {
445         return outputFile;
446     }
447 
448     /**
449      * Sets the currently generated file.
450      *
451      * @param outputFilePath the currently generated file, or null to remove
452      *        the current output file.
453      */
454     void setOutputFile(File outputFilePath)
455     {
456         this.outputFile = outputFilePath;
457     }
458 
459 
460     /**
461      * Returns the currently used source file.
462      *
463      * @return the current source file. May be null if no
464      *         source file is currently used (e.g. if the source is created
465      *         by other means than reading a file).
466      */
467     public File getSourceFile()
468     {
469         return sourceFile;
470     }
471 
472     /**
473      * Sets the currently used source file.
474      *
475      * @param sourceFile the current source file, or null to remove the
476      *        source file.
477      */
478     public void setSourceFile(File sourceFile)
479     {
480         this.sourceFile = sourceFile;
481     }
482 
483 
484     /**
485      * Returns the configuration of the currently processed generation unit.
486      *
487      * @return the configuration of the currently processed generation unit.
488      */
489     public UnitConfiguration getUnitConfiguration()
490     {
491         return unitConfiguration;
492     }
493 
494     /**
495      * Sets the configuration of the currently processed generation unit.
496      *
497      * @param unitConfiguration the configuration of the currently processed
498      *        generation unit.
499      */
500     public void setUnitConfiguration(UnitConfiguration unitConfiguration)
501     {
502         this.unitConfiguration = unitConfiguration;
503     }
504 
505     @Override
506     public String toString()
507     {
508         StringBuffer result = new StringBuffer();
509         result.append("sourceProvider=").append(sourceProvider)
510                 .append("output=").append(output)
511                 .append("outputFilePath=").append(outputFile)
512                 .append("\noutletNamespace=").append(outletNamespace)
513                 .append("\noutlets=").append(outlets)
514                 .append("\nrootElement=").append(rootElement)
515                 .append("\nsourceElement").append(sourceElement)
516                 .append("\nrootOutletReference=")
517                 .append(rootOutletReference)
518                 .append("\nunitConfiguration=").append(unitConfiguration)
519                 .append("\nvariableStore=").append(variableStore);
520         return result.toString();
521     }
522 }