View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.creadur.whisker.out.velocity;
20  
21  import java.util.Collection;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.creadur.whisker.app.AbstractEngine;
25  import org.apache.creadur.whisker.app.Configuration;
26  import org.apache.creadur.whisker.app.ResultWriterFactory;
27  import org.apache.creadur.whisker.app.analysis.LicenseAnalyst;
28  import org.apache.creadur.whisker.model.Descriptor;
29  import org.apache.creadur.whisker.scan.Directory;
30  
31  /**
32   * Uses Apache Velocity to implement {@link AbstractEngine}.
33   *
34   * @see <a href='http://velocity.apache.org'>Apache Velocity</a>
35   */
36  public class VelocityEngine extends AbstractEngine {
37      /** Not null. */
38      private final Log log;
39  
40      /**
41       * Constructs an engine running on Apache Velocity.
42       * @param log not null
43       */
44      public VelocityEngine(final Log log) {
45          super();
46          this.log = log;
47      }
48  
49      /**
50       * Generates a template, and writes result using given factory.
51       * @param withBase not null
52       * @param writerFactory not null
53       * @param configuration not null
54       * @return this engine, not null
55       * @throws Exception when generation fails
56       * @see AbstractEngine#skeleton(Collection, ResultWriterFactory, Configuration)
57       */
58      @Override
59      public final AbstractEngine skeleton(
60              final Collection<Directory> withBase,
61              final ResultWriterFactory writerFactory,
62              final Configuration configuration)
63                  throws Exception {
64          reporter(writerFactory).generateTemplate(withBase);
65          return this;
66      }
67  
68      /**
69       * Creates a reporter for the given factory.
70       * @param writerFactory not null
71       * @return a reporter, not null
72       */
73      private VelocityReports reporter(final ResultWriterFactory writerFactory) {
74          return new VelocityReports(writerFactory, log);
75      }
76  
77      /**
78       * Generates a validation report, and writes result using given factory.
79       * @param analyst not null
80       * @param writerFactory not null
81       * @param configuration not null
82       * @return this, not null
83       * @throws Exception when validation fails
84       * @see AbstractEngine#validate(LicenseAnalyst, ResultWriterFactory, Configuration)
85       */
86      @Override
87      public final AbstractEngine validate(
88              final LicenseAnalyst analyst,
89              final ResultWriterFactory writerFactory,
90              final Configuration configuration) throws Exception {
91          reporter(writerFactory).validate(analyst);
92          return this;
93      }
94  
95      /**
96       * Generates a directories report, and writes result using given factory.
97       * @param directories not null
98       * @param writerFactory not null
99       * @param configuration not null
100      * @return this, not null
101      * @throws Exception when reporting fails
102      * @see AbstractEngine#report(java.util.Collection, ResultWriterFactory, Configuration)
103      */
104     @Override
105     public final AbstractEngine report(
106             final Collection<Directory> directories,
107             final ResultWriterFactory writerFactory,
108             final Configuration configuration) throws Exception {
109         reporter(writerFactory).report(directories);
110         return this;
111     }
112 
113     /**
114      * Generates documents, and writes results using given factory.
115      * @param work not null
116      * @param writerFactory not null
117      * @param configuration not null
118      * @return this, not null
119      * @throws Exception when generation fails.
120      * @see AbstractEngine#generate(Descriptor, ResultWriterFactory, Configuration)
121      */
122     @Override
123     public final AbstractEngine generate(
124             final Descriptor work,
125             final ResultWriterFactory writerFactory,
126             final Configuration configuration) throws Exception {
127         reporter(writerFactory).generate(work, configuration);
128         return this;
129     }
130 }