Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.apache.maven.doxia.tools;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.doxia.site.decoration.Banner;
import org.apache.maven.doxia.site.decoration.DecorationModel;
import org.apache.maven.doxia.site.decoration.Menu;
import org.apache.maven.doxia.site.decoration.MenuItem;
import org.apache.maven.doxia.site.decoration.Skin;
import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.reporting.MavenReport;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.MapBasedValueSource;
import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
/**
* Default implementation of the site tool.
*
* @author Vincent Siveton
* @version $Id: DefaultSiteTool.java 664309 2008-06-07 10:18:58Z bentmann $
*
* @plexus.component role="org.apache.maven.doxia.tools.SiteTool" role-hint="default"
*/
public class DefaultSiteTool
extends AbstractLogEnabled
implements SiteTool
{
// ----------------------------------------------------------------------
// Components
// ----------------------------------------------------------------------
/**
* The component that is used to resolve additional artifacts required.
*
* @plexus.requirement
*/
private ArtifactResolver artifactResolver;
/**
* The component used for creating artifact instances.
*
* @plexus.requirement
*/
private ArtifactFactory artifactFactory;
/**
* Internationalization.
*
* @plexus.requirement
*/
protected I18N i18n;
/**
* The component for assembling inheritance.
*
* @plexus.requirement
*/
protected DecorationModelInheritanceAssembler assembler;
/**
* Project builder.
*
* @plexus.requirement
*/
protected MavenProjectBuilder mavenProjectBuilder;
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
/** {@inheritDoc} */
public Artifact getSkinArtifactFromRepository( ArtifactRepository localRepository, List remoteArtifactRepositories,
DecorationModel decoration )
throws SiteToolException
{
if ( localRepository == null )
{
throw new IllegalArgumentException( "The parameter 'localRepository' can not be null" );
}
if ( remoteArtifactRepositories == null )
{
throw new IllegalArgumentException( "The parameter 'remoteArtifactRepositories' can not be null" );
}
if ( decoration == null )
{
throw new IllegalArgumentException( "The parameter 'decoration' can not be null" );
}
Skin skin = decoration.getSkin();
if ( skin == null )
{
skin = Skin.getDefaultSkin();
}
String version = skin.getVersion();
Artifact artifact;
try
{
if ( version == null )
{
version = Artifact.RELEASE_VERSION;
}
VersionRange versionSpec = VersionRange.createFromVersionSpec( version );
artifact = artifactFactory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec,
"jar", null, null );
artifactResolver.resolve( artifact, remoteArtifactRepositories, localRepository );
}
catch ( InvalidVersionSpecificationException e )
{
throw new SiteToolException( "InvalidVersionSpecificationException: The skin version '" + version
+ "' is not valid: " + e.getMessage() );
}
catch ( ArtifactResolutionException e )
{
throw new SiteToolException( "ArtifactResolutionException: Unable to find skin", e );
}
catch ( ArtifactNotFoundException e )
{
throw new SiteToolException( "ArtifactNotFoundException: The skin does not exist: " + e.getMessage() );
}
return artifact;
}
/** {@inheritDoc} */
public Artifact getDefaultSkinArtifact( ArtifactRepository localRepository, List remoteArtifactRepositories )
throws SiteToolException
{
return getSkinArtifactFromRepository( localRepository, remoteArtifactRepositories, new DecorationModel() );
}
/** {@inheritDoc} */
public String getRelativePath( String to, String from )
{
if ( to == null )
{
throw new IllegalArgumentException( "The parameter 'to' can not be null" );
}
if ( from == null )
{
throw new IllegalArgumentException( "The parameter 'from' can not be null" );
}
URL toUrl = null;
URL fromUrl = null;
String toPath = to;
String fromPath = from;
try
{
toUrl = new URL( to );
}
catch ( MalformedURLException e )
{
try
{
toUrl = new File( getNormalizedPath( to ) ).toURL();
}
catch ( MalformedURLException e1 )
{
getLogger().warn( "Unable to load a URL for '" + to + "': " + e.getMessage() );
}
}
try
{
fromUrl = new URL( from );
}
catch ( MalformedURLException e )
{
try
{
fromUrl = new File( getNormalizedPath( from ) ).toURL();
}
catch ( MalformedURLException e1 )
{
getLogger().warn( "Unable to load a URL for '" + from + "': " + e.getMessage() );
}
}
if ( toUrl != null && fromUrl != null )
{
// URLs, determine if they share protocol and domain info
if ( ( toUrl.getProtocol().equalsIgnoreCase( fromUrl.getProtocol() ) )
&& ( toUrl.getHost().equalsIgnoreCase( fromUrl.getHost() ) )
&& ( toUrl.getPort() == fromUrl.getPort() ) )
{
// shared URL domain details, use URI to determine relative path
toPath = toUrl.getFile();
fromPath = fromUrl.getFile();
}
else
{
// dont share basic URL infomation, no relative available
return to;
}
}
else if ( ( toUrl != null && fromUrl == null ) || ( toUrl == null && fromUrl != null ) )
{
// one is a URL and the other isnt, no relative available.
return to;
}
// either the two locations are not URLs or if they are they
// share the common protocol and domain info and we are left
// with their URI information
// normalise the path delimters
toPath = new File( toPath ).getPath();
fromPath = new File( fromPath ).getPath();
// strip any leading slashes if its a windows path
if ( toPath.matches( "^\\[a-zA-Z]:" ) )
{
toPath = toPath.substring( 1 );
}
if ( fromPath.matches( "^\\[a-zA-Z]:" ) )
{
fromPath = fromPath.substring( 1 );
}
// lowercase windows drive letters.
if ( toPath.startsWith( ":", 1 ) )
{
toPath = toPath.substring( 0, 1 ).toLowerCase() + toPath.substring( 1 );
}
if ( fromPath.startsWith( ":", 1 ) )
{
fromPath = fromPath.substring( 0, 1 ).toLowerCase() + fromPath.substring( 1 );
}
// check for the presence of windows drives. No relative way of
// traversing from one to the other.
if ( ( toPath.startsWith( ":", 1 ) && fromPath.startsWith( ":", 1 ) )
&& ( !toPath.substring( 0, 1 ).equals( fromPath.substring( 0, 1 ) ) ) )
{
// they both have drive path element but they dont match, no
// relative path
return to;
}
if ( ( toPath.startsWith( ":", 1 ) && !fromPath.startsWith( ":", 1 ) )
|| ( !toPath.startsWith( ":", 1 ) && fromPath.startsWith( ":", 1 ) ) )
{
// one has a drive path element and the other doesnt, no relative
// path.
return to;
}
// use tokeniser to traverse paths and for lazy checking
StringTokenizer toTokeniser = new StringTokenizer( toPath, File.separator );
StringTokenizer fromTokeniser = new StringTokenizer( fromPath, File.separator );
int count = 0;
// walk along the to path looking for divergence from the from path
while ( toTokeniser.hasMoreTokens() && fromTokeniser.hasMoreTokens() )
{
if ( File.separatorChar == '\\' )
{
if ( !fromTokeniser.nextToken().equalsIgnoreCase( toTokeniser.nextToken() ) )
{
break;
}
}
else
{
if ( !fromTokeniser.nextToken().equals( toTokeniser.nextToken() ) )
{
break;
}
}
count++;
}
// reinitialise the tokenisers to count positions to retrieve the
// gobbled token
toTokeniser = new StringTokenizer( toPath, File.separator );
fromTokeniser = new StringTokenizer( fromPath, File.separator );
while ( count-- > 0 )
{
fromTokeniser.nextToken();
toTokeniser.nextToken();
}
String relativePath = "";
// add back refs for the rest of from location.
while ( fromTokeniser.hasMoreTokens() )
{
fromTokeniser.nextToken();
relativePath += "..";
if ( fromTokeniser.hasMoreTokens() )
{
relativePath += File.separatorChar;
}
}
if ( relativePath.length() != 0 && toTokeniser.hasMoreTokens() )
{
relativePath += File.separatorChar;
}
// add fwd fills for whatevers left of to.
while ( toTokeniser.hasMoreTokens() )
{
relativePath += toTokeniser.nextToken();
if ( toTokeniser.hasMoreTokens() )
{
relativePath += File.separatorChar;
}
}
if ( !relativePath.equals( to ) )
{
getLogger().debug( "Mapped url: " + to + " to relative path: " + relativePath );
}
return relativePath;
}
/** {@inheritDoc} */
public File getSiteDescriptorFromBasedir( String siteDirectory, File basedir, Locale locale )
{
if ( basedir == null )
{
throw new IllegalArgumentException( "The parameter 'basedir' can not be null" );
}
if ( siteDirectory == null )
{
// TODO need to be more dynamic
siteDirectory = "src/site";
}
if ( locale == null )
{
locale = new Locale( "" );
}
File siteDir = new File( basedir, siteDirectory );
File siteDescriptor = new File( siteDir, "site_" + locale.getLanguage() + ".xml" );
if ( !siteDescriptor.isFile() )
{
siteDescriptor = new File( siteDir, "site.xml" );
}
return siteDescriptor;
}
/** {@inheritDoc} */
public File getSiteDescriptorFromRepository( MavenProject project, ArtifactRepository localRepository,
List remoteArtifactRepositories, Locale locale )
throws SiteToolException
{
if ( project == null )
{
throw new IllegalArgumentException( "The parameter 'project' can not be null" );
}
if ( localRepository == null )
{
throw new IllegalArgumentException( "The parameter 'localRepository' can not be null" );
}
if ( remoteArtifactRepositories == null )
{
throw new IllegalArgumentException( "The parameter 'remoteArtifactRepositories' can not be null" );
}
if ( locale == null )
{
locale = new Locale( "" );
}
try
{
return resolveSiteDescriptor( project, localRepository, remoteArtifactRepositories, locale );
}
catch ( ArtifactNotFoundException e )
{
getLogger().debug( "ArtifactNotFoundException: Unable to locate site descriptor: " + e );
return null;
}
catch ( ArtifactResolutionException e )
{
throw new SiteToolException( "ArtifactResolutionException: Unable to locate site descriptor: "
+ e.getMessage() );
}
catch ( IOException e )
{
throw new SiteToolException( "IOException: Unable to locate site descriptor: " + e.getMessage() );
}
}
/** {@inheritDoc} */
public DecorationModel getDecorationModel( MavenProject project, List reactorProjects,
ArtifactRepository localRepository, List repositories,
String siteDirectory, Locale locale, String inputEncoding,
String outputEncoding )
throws SiteToolException
{
if ( project == null )
{
throw new IllegalArgumentException( "The parameter 'project' can not be null" );
}
if ( reactorProjects == null )
{
throw new IllegalArgumentException( "The parameter 'reactorProjects' can not be null" );
}
if ( localRepository == null )
{
throw new IllegalArgumentException( "The parameter 'localRepository' can not be null" );
}
if ( repositories == null )
{
throw new IllegalArgumentException( "The parameter 'repositories' can not be null" );
}
if ( inputEncoding == null )
{
throw new IllegalArgumentException( "The parameter 'inputEncoding' can not be null" );
}
if ( outputEncoding == null )
{
throw new IllegalArgumentException( "The parameter 'outputEncoding' can not be null" );
}
if ( locale == null )
{
locale = Locale.getDefault();
}
Map props = new HashMap();
// This is to support the deprecated ${reports} and ${modules} tags.
props.put( "reports", "