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    *      https://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 final class HeaderHtmlVelocityDelegate {
34  
35      /**
36       * A builder class for instantiation of the {@link HeaderHtmlVelocityDelegate}.
37       */
38      public static final 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           *
49           * @return a new {@link ReadmeHtmlVelocityDelegate}.
50           */
51          public HeaderHtmlVelocityDelegate build() {
52              return new HeaderHtmlVelocityDelegate();
53          }
54      }
55  
56      /** The location of the velocity template for this class. */
57      private static final String TEMPLATE = "resources/org/apache/commons/release/plugin/velocity/HEADER.vm";
58  
59      /**
60       * For instantiating our {@link HeaderHtmlVelocityDelegate} using the {@link HeaderHtmlVelocityDelegateBuilder}.
61       *
62       * @return a {@link HeaderHtmlVelocityDelegateBuilder}.
63       */
64      public static HeaderHtmlVelocityDelegateBuilder builder() {
65          return new HeaderHtmlVelocityDelegateBuilder();
66      }
67  
68      /** The private constructor to be used by the {@link HeaderHtmlVelocityDelegateBuilder}. */
69      private HeaderHtmlVelocityDelegate() {
70      }
71  
72      /**
73       * Builds the HEADER.vm velocity template to the writer passed in.
74       *
75       * @param writer any {@link Writer} that we wish to have the filled velocity template written to.
76       * @return the {@link Writer} that we've filled out the template into.
77       */
78      public Writer render(final Writer writer) {
79          final VelocityEngine ve = new VelocityEngine();
80          ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
81          ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
82          ve.init();
83          final Template template = ve.getTemplate(TEMPLATE);
84          final VelocityContext context = new VelocityContext();
85          template.merge(context, writer);
86          return writer;
87      }
88  }