001    /*
002     * #%L
003     * License Maven Plugin
004     * 
005     * $Id: License.java 14664 2011-09-09 07:47:49Z tchemit $
006     * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/model/License.java $
007     * %%
008     * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009     * %%
010     * This program is free software: you can redistribute it and/or modify
011     * it under the terms of the GNU Lesser General Public License as 
012     * published by the Free Software Foundation, either version 3 of the 
013     * License, or (at your option) any later version.
014     * 
015     * This program is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Lesser Public License for more details.
019     * 
020     * You should have received a copy of the GNU General Lesser Public 
021     * License along with this program.  If not, see
022     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023     * #L%
024     */
025    
026    package org.codehaus.mojo.license.model;
027    
028    import org.apache.commons.lang.builder.ToStringBuilder;
029    import org.apache.commons.lang.builder.ToStringStyle;
030    import org.codehaus.mojo.license.MojoHelper;
031    import org.codehaus.plexus.util.IOUtil;
032    
033    import java.io.BufferedReader;
034    import java.io.IOException;
035    import java.io.InputStreamReader;
036    import java.io.Reader;
037    import java.net.URL;
038    
039    /**
040     * The model of a license.
041     *
042     * @author tchemit <chemit@codelutin.com>
043     * @since 1.0
044     */
045    public class License
046    {
047    
048        public static final String LICENSE_HEADER_FILE = "header.txt";
049    
050        public static final String LICENSE_CONTENT_FILE = "license.txt";
051    
052        /**
053         * base url of license (directory where to find license files)
054         */
055        protected URL baseURL;
056    
057        /**
058         * the name of the licenses (ex lgpl-3.0)
059         */
060        protected String name;
061    
062        /**
063         * the description of the license
064         */
065        protected String description;
066    
067        /**
068         * url of the license's content
069         */
070        protected URL licenseURL;
071    
072        /**
073         * url of the license header's content
074         */
075        protected URL headerURL;
076    
077        public License()
078        {
079        }
080    
081        public String getName()
082        {
083            return name;
084        }
085    
086        public URL getLicenseURL()
087        {
088            if ( licenseURL == null )
089            {
090                licenseURL = MojoHelper.getUrl( getBaseURL(), LICENSE_CONTENT_FILE );
091            }
092            return licenseURL;
093        }
094    
095        public URL getHeaderURL()
096        {
097            if ( headerURL == null )
098            {
099                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    }