001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.release.plugin.velocity; 018 019import java.io.Writer; 020import org.apache.commons.lang3.StringUtils; 021import org.apache.velocity.Template; 022import org.apache.velocity.VelocityContext; 023import org.apache.velocity.app.VelocityEngine; 024import org.apache.velocity.runtime.RuntimeConstants; 025import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 026 027/** 028 * This class' purpose is to generate the <code>README.html</code> that moves along with the 029 * release for the sake of downloading the release from the distribution area. 030 * 031 * @since 1.3 032 */ 033public class ReadmeHtmlVelocityDelegate { 034 /** The location of the velocity template for this class. */ 035 private static final String TEMPLATE = "resources/org/apache/commons/release/plugin" 036 + "/velocity/README.vm"; 037 /** This is supposed to represent the maven artifactId. */ 038 private final String artifactId; 039 /** This is supposed to represent the maven version of the release. */ 040 private final String version; 041 /** The url of the site that gets set into the <code>README.html</code>. */ 042 private final String siteUrl; 043 044 /** 045 * The private constructor to be used by the {@link ReadmeHtmlVelocityDelegateBuilder}. 046 * 047 * @param artifactId sets the {@link ReadmeHtmlVelocityDelegate#artifactId}. 048 * @param version sets the {@link ReadmeHtmlVelocityDelegate#version}. 049 * @param siteUrl sets the {@link ReadmeHtmlVelocityDelegate#siteUrl}. 050 */ 051 private ReadmeHtmlVelocityDelegate(final String artifactId, final String version, final String siteUrl) { 052 this.artifactId = artifactId; 053 this.version = version; 054 this.siteUrl = siteUrl; 055 } 056 057 /** 058 * Gets the {@link ReadmeHtmlVelocityDelegateBuilder} for constructing the {@link ReadmeHtmlVelocityDelegate}. 059 * 060 * @return the {@link ReadmeHtmlVelocityDelegateBuilder}. 061 */ 062 public static ReadmeHtmlVelocityDelegateBuilder builder() { 063 return new ReadmeHtmlVelocityDelegateBuilder(); 064 } 065 066 /** 067 * Renders the <code>README.vm</code> velocity template with the variables constructed with the 068 * {@link ReadmeHtmlVelocityDelegateBuilder}. 069 * 070 * @param writer is the {@link Writer} to which we wish to render the <code>README.vm</code> template. 071 * @return a reference to the {@link Writer} passed in. 072 */ 073 public Writer render(final Writer writer) { 074 final VelocityEngine ve = new VelocityEngine(); 075 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); 076 ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); 077 ve.init(); 078 final Template template = ve.getTemplate(TEMPLATE); 079 final String[] splitArtifactId = artifactId.split("-"); 080 final String wordCommons = "commons"; 081 String artifactShortName = ""; 082 if (splitArtifactId.length > 1) { 083 artifactShortName = splitArtifactId[1]; 084 } else if (splitArtifactId.length == 1) { 085 artifactShortName = splitArtifactId[0]; 086 } 087 // ".+\\d$" matches a non-empty string that terminates in a digit {0-9}. 088 if (artifactShortName.matches(".+\\d$")) { 089 artifactShortName = artifactShortName.substring(0, artifactShortName.length() - 1); 090 } 091 final String artifactIdWithFirstLetterscapitalized = 092 StringUtils.capitalize(wordCommons) 093 + "-" 094 + artifactShortName.toUpperCase(); 095 final VelocityContext context = new VelocityContext(); 096 context.internalPut("artifactIdWithFirstLetterscapitalized", artifactIdWithFirstLetterscapitalized); 097 context.internalPut("artifactShortName", artifactShortName.toUpperCase()); 098 context.internalPut("artifactId", artifactId); 099 context.internalPut("version", version); 100 context.internalPut("siteUrl", siteUrl); 101 template.merge(context, writer); 102 return writer; 103 } 104 105 /** 106 * A builder class for instantiation of the {@link ReadmeHtmlVelocityDelegate}. 107 */ 108 public static class ReadmeHtmlVelocityDelegateBuilder { 109 /** The maven artifactId to use in the <code>README.vm</code> template. */ 110 private String artifactId; 111 /** The maven version to use in the <code>README.vm</code> template. */ 112 private String version; 113 /** The site url to use in the <code>README.vm</code> template. */ 114 private String siteUrl; 115 116 /** 117 * Private constructor for using the builder through the {@link ReadmeHtmlVelocityDelegate#builder()} 118 * method. 119 */ 120 private ReadmeHtmlVelocityDelegateBuilder() { 121 } 122 123 /** 124 * Adds the artifactId to the {@link ReadmeHtmlVelocityDelegate}. 125 * @param artifactId the {@link String} representing the maven artifactId. 126 * @return the builder to continue building. 127 */ 128 public ReadmeHtmlVelocityDelegateBuilder withArtifactId(final String artifactId) { 129 this.artifactId = artifactId; 130 return this; 131 } 132 133 /** 134 * Adds the version to the {@link ReadmeHtmlVelocityDelegate}. 135 * @param version the maven version. 136 * @return the builder to continue building. 137 */ 138 public ReadmeHtmlVelocityDelegateBuilder withVersion(final String version) { 139 this.version = version; 140 return this; 141 } 142 143 /** 144 * Adds the siteUrl to the {@link ReadmeHtmlVelocityDelegate}. 145 * @param siteUrl the site url to be used in the <code>README.html</code> 146 * @return the builder to continue building. 147 */ 148 public ReadmeHtmlVelocityDelegateBuilder withSiteUrl(final String siteUrl) { 149 this.siteUrl = siteUrl; 150 return this; 151 } 152 153 /** 154 * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters. 155 * @return a new {@link ReadmeHtmlVelocityDelegate}. 156 */ 157 public ReadmeHtmlVelocityDelegate build() { 158 return new ReadmeHtmlVelocityDelegate(this.artifactId, this.version, this.siteUrl); 159 } 160 } 161}