package org.apache.maven.plugin;
/* ====================================================================
* 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 java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.jelly.Script;
import org.apache.maven.MavenException;
import org.apache.maven.MavenUtils;
import org.apache.maven.jelly.MavenJellyContext;
import org.apache.maven.project.Project;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
*
* @version $Id: JellyScriptHousing.java 530196 2007-04-18 23:04:51Z aheritier $
*/
public class JellyScriptHousing
{
/** Plug-in default properties name. */
private static final String PLUGIN_PROPERTIES_NAME = "plugin.properties";
/** Plugin properties. */
private Map pluginProperties = new HashMap();
/** Plugin Project. */
private Project project;
/** Jelly Script. */
private Script script;
/** the source of the jelly script. */
private File source;
private final String name;
private final File pluginDirectory;
private final MavenJellyContext parentContext;
public JellyScriptHousing()
{
this.name = null;
this.pluginDirectory = null;
this.parentContext = null;
}
public JellyScriptHousing( File pluginDir, MavenJellyContext parentContext ) throws IOException
{
this.pluginDirectory = pluginDir;
this.name = pluginDir.getName();
this.source = new File( pluginDir, "plugin.jelly" );
this.parentContext = parentContext;
this.pluginProperties = loadPluginProperties();
}
// ----------------------------------------------------------------------
// Accessors
// ----------------------------------------------------------------------
public File getSource()
{
return source;
}
public void setSource( File source )
{
this.source = source;
}
/**
*
* @return Project
*/
public Project getProject() throws MavenException
{
if ( project == null )
{
project = MavenUtils.getProject( new File( pluginDirectory, "project.xml" ), parentContext, false );
}
return project;
}
/**
*
* @param project
*/
public void setProject( Project project )
{
this.project = project;
}
public void setScript( Script script )
{
this.script = script;
}
/**
*
* @return Script
*/
Script getScript()
{
return script;
}
public String toString()
{
return "\n source = " + getSource() + "\n project = " + project + "\n script = " + script;
}
/**
* @return String
*/
public String getName()
{
return name;
}
void parse( PluginDefinitionHandler handler, String systemId, InputStream inStream ) throws MavenException
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware( true );
SAXParser parser = factory.newSAXParser();
InputSource is = new InputSource( inStream );
is.setSystemId( systemId );
parser.parse( is, new PluginScriptParser( handler, this ) );
}
catch ( ParserConfigurationException e )
{
throw new MavenException( systemId + " : Error parsing plugin script", e );
}
catch ( SAXException e )
{
throw new MavenException( systemId + " : Error parsing plugin script", e );
}
catch ( IOException e )
{
throw new MavenException( systemId + " : Error reading plugin script", e );
}
}
void parse( PluginDefinitionHandler handler ) throws MavenException
{
FileInputStream fis = null;
try
{
fis = new FileInputStream( source );
parse( handler, source.getAbsolutePath(), fis );
}
catch ( FileNotFoundException e )
{
throw new MavenException( source + " : Error reading plugin script", e );
}
finally
{
if ( fis != null )
{
try
{
fis.close();
}
catch ( IOException e )
{
// Nothing to do
}
fis = null;
}
}
}
/**
* @return Map
*/
public Map getPluginProperties()
{
return pluginProperties;
}
/**
* @return File
*/
public File getPluginDirectory()
{
return pluginDirectory;
}
/**
* Retrieve the plugin's default properties.
*
* @param unpackedPluginDir
* The location of the unpacked plugin.
* @return The default properties file for the plugin, or <code>null</code> if no such plugin.
* @throws IOException
* If an IO error occurs while attempting to read the plugin's properties.
*/
private Map loadPluginProperties() throws IOException
{
Map map = new HashMap();
File propsFile = new File( pluginDirectory, PLUGIN_PROPERTIES_NAME );
if ( propsFile.exists() )
{
Properties props = new Properties();
FileInputStream in = null;
try
{
in = new FileInputStream( propsFile );
props.load( in );
map.putAll( props );
}
finally
{
if ( in != null )
{
try
{
in.close();
}
catch ( IOException e )
{
// Nothing to do
}
in = null;
}
}
}
return map;
}
}