1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
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
44
45 protected URL baseURL;
46
47
48
49
50 protected String name;
51
52
53
54
55 protected String description;
56
57
58
59
60 protected URL licenseURL;
61
62
63
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 }