View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    * 
5    * $Id: License.java 14664 2011-09-09 07:47:49Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/model/License.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  
26  package org.codehaus.mojo.license.model;
27  
28  import org.apache.commons.lang.builder.ToStringBuilder;
29  import org.apache.commons.lang.builder.ToStringStyle;
30  import org.codehaus.mojo.license.MojoHelper;
31  import org.codehaus.plexus.util.IOUtil;
32  
33  import java.io.BufferedReader;
34  import java.io.IOException;
35  import java.io.InputStreamReader;
36  import java.io.Reader;
37  import java.net.URL;
38  
39  /**
40   * The model of a license.
41   *
42   * @author tchemit <chemit@codelutin.com>
43   * @since 1.0
44   */
45  public class License
46  {
47  
48      public static final String LICENSE_HEADER_FILE = "header.txt";
49  
50      public static final String LICENSE_CONTENT_FILE = "license.txt";
51  
52      /**
53       * base url of license (directory where to find license files)
54       */
55      protected URL baseURL;
56  
57      /**
58       * the name of the licenses (ex lgpl-3.0)
59       */
60      protected String name;
61  
62      /**
63       * the description of the license
64       */
65      protected String description;
66  
67      /**
68       * url of the license's content
69       */
70      protected URL licenseURL;
71  
72      /**
73       * url of the license header's content
74       */
75      protected URL headerURL;
76  
77      public License()
78      {
79      }
80  
81      public String getName()
82      {
83          return name;
84      }
85  
86      public URL getLicenseURL()
87      {
88          if ( licenseURL == null )
89          {
90              licenseURL = MojoHelper.getUrl( getBaseURL(), LICENSE_CONTENT_FILE );
91          }
92          return licenseURL;
93      }
94  
95      public URL getHeaderURL()
96      {
97          if ( headerURL == null )
98          {
99              headerURL = MojoHelper.getUrl( getBaseURL(), LICENSE_HEADER_FILE );
100         }
101         return headerURL;
102     }
103 
104     public String getDescription()
105     {
106         return description;
107     }
108 
109     public URL getBaseURL()
110     {
111         return baseURL;
112     }
113 
114     public String getLicenseContent( String encoding )
115         throws IOException
116     {
117         if ( baseURL == null )
118         {
119             throw new IllegalStateException( "no baseURL defined, can not obtain license content in " + this );
120         }
121 
122         Reader r = new BufferedReader( new InputStreamReader( getLicenseURL().openStream(), encoding ) );
123         try
124         {
125             return IOUtil.toString( r );
126         }
127         finally
128         {
129             r.close();
130         }
131     }
132 
133     public String getHeaderContent( String encoding )
134         throws IOException
135     {
136         if ( baseURL == null )
137         {
138             throw new IllegalStateException( "no baseURL defined, can not obtain header content in " + this );
139         }
140         Reader r = new BufferedReader( new InputStreamReader( getHeaderURL().openStream(), encoding ) );
141         try
142         {
143             return IOUtil.toString( r );
144         }
145         finally
146         {
147             r.close();
148         }
149     }
150 
151     public void setName( String name )
152     {
153         this.name = name;
154     }
155 
156     public void setDescription( String description )
157     {
158         this.description = description;
159     }
160 
161     public void setBaseURL( URL baseURL )
162     {
163         this.baseURL = baseURL;
164     }
165 
166     public void setLicenseURL( URL licenseURL )
167     {
168         this.licenseURL = licenseURL;
169     }
170 
171     public void setHeaderURL( URL headerURL )
172     {
173         this.headerURL = headerURL;
174     }
175 
176     @Override
177     public String toString()
178     {
179         ToStringBuilder builder = new ToStringBuilder( this, ToStringStyle.MULTI_LINE_STYLE );
180         builder.append( "name", name );
181         builder.append( "description", description );
182         builder.append( "licenseURL", licenseURL );
183         builder.append( "headerURL", headerURL );
184         return builder.toString();
185     }
186 }