View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.release.plugin.velocity;
18  
19  import java.io.Writer;
20  
21  import org.apache.velocity.Template;
22  import org.apache.velocity.VelocityContext;
23  import org.apache.velocity.app.VelocityEngine;
24  import org.apache.velocity.runtime.RuntimeConstants;
25  import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
26  
27  /**
28   * This class' purpose is to generate the <code>HEADER.html</code> that moves along with the
29   * release for the sake of downloading the release from the distribution area.
30   *
31   * @since 1.3
32   */
33  public class HeaderHtmlVelocityDelegate {
34  
35      /**
36       * A builder class for instantiation of the {@link HeaderHtmlVelocityDelegate}.
37       */
38      public static class HeaderHtmlVelocityDelegateBuilder {
39  
40          /**
41           * Private constructor so that we can have a proper builder pattern.
42           */
43          private HeaderHtmlVelocityDelegateBuilder() {
44          }
45  
46          /**
47           * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
48           * @return a new {@link ReadmeHtmlVelocityDelegate}.
49           */
50          public HeaderHtmlVelocityDelegate build() {
51              return new HeaderHtmlVelocityDelegate();
52          }
53      }
54  
55      /** The location of the velocity template for this class. */
56      private static final String TEMPLATE = "resources/org/apache/commons/release/plugin/velocity/HEADER.vm";
57  
58      /**
59       * For instantiating our {@link HeaderHtmlVelocityDelegate} using the {@link HeaderHtmlVelocityDelegateBuilder}.
60       *
61       * @return a {@link HeaderHtmlVelocityDelegateBuilder}.
62       */
63      public static HeaderHtmlVelocityDelegateBuilder builder() {
64          return new HeaderHtmlVelocityDelegateBuilder();
65      }
66  
67      /** The private constructor to be used by the {@link HeaderHtmlVelocityDelegateBuilder}. */
68      private HeaderHtmlVelocityDelegate() {
69      }
70  
71      /**
72       * Builds the HEADER.vm velocity template to the writer passed in.
73       *
74       * @param writer any {@link Writer} that we wish to have the filled velocity template written to.
75       * @return the {@link Writer} that we've filled out the template into.
76       */
77      public Writer render(final Writer writer) {
78          final VelocityEngine ve = new VelocityEngine();
79          ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
80          ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
81          ve.init();
82          final Template template = ve.getTemplate(TEMPLATE);
83          final VelocityContext context = new VelocityContext();
84          template.merge(context, writer);
85          return writer;
86      }
87  }