1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
41
42
43
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
54
55 protected URL baseURL;
56
57
58
59
60 protected String name;
61
62
63
64
65 protected String description;
66
67
68
69
70 protected URL licenseURL;
71
72
73
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 }