View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.license.model;
17  
18  import org.apache.commons.lang.builder.ToStringBuilder;
19  import org.apache.commons.lang.builder.ToStringStyle;
20  import org.codehaus.mojo.license.MojoHelper;
21  import org.codehaus.plexus.util.IOUtil;
22  
23  import java.io.BufferedReader;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.net.URL;
28  
29  /**
30   * The model of a license.
31   *
32   * @author tchemit <chemit@codelutin.com>
33   * @since 1.0
34   */
35  public class License
36  {
37  
38      public static final String LICENSE_HEADER_FILE = "header.txt";
39  
40      public static final String LICENSE_CONTENT_FILE = "license.txt";
41  
42      /**
43       * base url of license (directory where to find license files)
44       */
45      protected URL baseURL;
46  
47      /**
48       * the name of the licenses (ex lgpl-3.0)
49       */
50      protected String name;
51  
52      /**
53       * the description of the license
54       */
55      protected String description;
56  
57      /**
58       * url of the license's content
59       */
60      protected URL licenseURL;
61  
62      /**
63       * url of the license header's content
64       */
65      protected URL headerURL;
66  
67      public License()
68      {
69      }
70  
71      public String getName()
72      {
73          return name;
74      }
75  
76      public URL getLicenseURL()
77      {
78          if ( licenseURL == null )
79          {
80              licenseURL = MojoHelper.getUrl( getBaseURL(), LICENSE_CONTENT_FILE );
81          }
82          return licenseURL;
83      }
84  
85      public URL getHeaderURL()
86      {
87          if ( headerURL == null )
88          {
89              headerURL = MojoHelper.getUrl( getBaseURL(), LICENSE_HEADER_FILE );
90          }
91          return headerURL;
92      }
93  
94      public String getDescription()
95      {
96          return description;
97      }
98  
99      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 }