org.codehaus.mojo.license.model.LicenseRepository Maven / Gradle / Ivy
package org.codehaus.mojo.license.model;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.mojo.license.utils.MojoHelper;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author tchemit [email protected]
* @since 1.0
*/
public class LicenseRepository
implements Iterable
{
/**
* Logger.
*/
private static final Log LOG = LogFactory.getLog( LicenseRepository.class );
public static final String REPOSITORY_DEFINITION_FILE = "licenses.properties";
public static final Pattern LICENSE_DESCRIPTION_PATTERN =
Pattern.compile( "(.*)\\s*~~\\s*license\\s*:\\s*(.*)\\s*~~\\s*header\\s*:\\s*(.*)\\s*" );
/**
* the base url of the licenses repository.
*/
protected URL baseURL;
/**
* licenses of this repository.
*/
protected List licenses;
/**
* flag to known if repository was init (pass to {@code true} when invoking
* the method {@link #load()}).
*/
protected boolean init;
public URL getBaseURL()
{
return baseURL;
}
public void setBaseURL( URL baseURL )
{
checkNotInit( "setBaseURL" );
this.baseURL = baseURL;
}
protected URL getDefinitionURL()
{
if ( baseURL == null || StringUtils.isEmpty( baseURL.toString() ) )
{
throw new IllegalStateException( "no baseURL defined in " + this );
}
URL definitionURL = MojoHelper.getUrl( getBaseURL(), REPOSITORY_DEFINITION_FILE );
return definitionURL;
}
protected URL getLicenseBaseURL(String licenseName)
{
URL licenseBaseURL = MojoHelper.getUrl( baseURL, licenseName );
return licenseBaseURL;
}
public void load()
throws IOException
{
checkNotInit( "load" );
try
{
URL definitionURL = getDefinitionURL();
if ( licenses != null )
{
licenses.clear();
}
else
{
licenses = new ArrayList();
}
if ( !checkExists( definitionURL ) )
{
throw new IllegalArgumentException(
"no licenses.properties found with url [" + definitionURL + "] for resolver " + this );
}
Properties p = new Properties();
p.load( definitionURL.openStream() );
for ( Entry