001 /** 002 * Copyright 2010-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.codehaus.mojo.license.model; 017 018 import org.apache.commons.lang.builder.ToStringBuilder; 019 import org.apache.commons.lang.builder.ToStringStyle; 020 import org.codehaus.mojo.license.MojoHelper; 021 import org.codehaus.plexus.util.IOUtil; 022 023 import java.io.BufferedReader; 024 import java.io.IOException; 025 import java.io.InputStreamReader; 026 import java.io.Reader; 027 import java.net.URL; 028 029 /** 030 * The model of a license. 031 * 032 * @author tchemit <chemit@codelutin.com> 033 * @since 1.0 034 */ 035 public class License 036 { 037 038 public static final String LICENSE_HEADER_FILE = "header.txt"; 039 040 public static final String LICENSE_CONTENT_FILE = "license.txt"; 041 042 /** 043 * base url of license (directory where to find license files) 044 */ 045 protected URL baseURL; 046 047 /** 048 * the name of the licenses (ex lgpl-3.0) 049 */ 050 protected String name; 051 052 /** 053 * the description of the license 054 */ 055 protected String description; 056 057 /** 058 * url of the license's content 059 */ 060 protected URL licenseURL; 061 062 /** 063 * url of the license header's content 064 */ 065 protected URL headerURL; 066 067 public License() 068 { 069 } 070 071 public String getName() 072 { 073 return name; 074 } 075 076 public URL getLicenseURL() 077 { 078 if ( licenseURL == null ) 079 { 080 licenseURL = MojoHelper.getUrl( getBaseURL(), LICENSE_CONTENT_FILE ); 081 } 082 return licenseURL; 083 } 084 085 public URL getHeaderURL() 086 { 087 if ( headerURL == null ) 088 { 089 headerURL = MojoHelper.getUrl( getBaseURL(), LICENSE_HEADER_FILE ); 090 } 091 return headerURL; 092 } 093 094 public String getDescription() 095 { 096 return description; 097 } 098 099 public URL getBaseURL() 100 { 101 return baseURL; 102 } 103 104 public String getLicenseContent( String encoding ) 105 throws IOException 106 { 107 if ( baseURL == null ) 108 { 109 throw new IllegalStateException( "no baseURL defined, can not obtain license content in " + this ); 110 } 111 112 Reader r = new BufferedReader( new InputStreamReader( getLicenseURL().openStream(), encoding ) ); 113 try 114 { 115 return IOUtil.toString( r ); 116 } 117 finally 118 { 119 r.close(); 120 } 121 } 122 123 public String getHeaderContent( String encoding ) 124 throws IOException 125 { 126 if ( baseURL == null ) 127 { 128 throw new IllegalStateException( "no baseURL defined, can not obtain header content in " + this ); 129 } 130 Reader r = new BufferedReader( new InputStreamReader( getHeaderURL().openStream(), encoding ) ); 131 try 132 { 133 return IOUtil.toString( r ); 134 } 135 finally 136 { 137 r.close(); 138 } 139 } 140 141 public void setName( String name ) 142 { 143 this.name = name; 144 } 145 146 public void setDescription( String description ) 147 { 148 this.description = description; 149 } 150 151 public void setBaseURL( URL baseURL ) 152 { 153 this.baseURL = baseURL; 154 } 155 156 public void setLicenseURL( URL licenseURL ) 157 { 158 this.licenseURL = licenseURL; 159 } 160 161 public void setHeaderURL( URL headerURL ) 162 { 163 this.headerURL = headerURL; 164 } 165 166 @Override 167 public String toString() 168 { 169 ToStringBuilder builder = new ToStringBuilder( this, ToStringStyle.MULTI_LINE_STYLE ); 170 builder.append( "name", name ); 171 builder.append( "description", description ); 172 builder.append( "licenseURL", licenseURL ); 173 builder.append( "headerURL", headerURL ); 174 return builder.toString(); 175 } 176 }