A typical use :
VelocityContext context = new VelocityContext();
VelocityComponent velocityComponent = (DefaultVelocityComponent) lookup( VelocityComponent.ROLE );
Template template = velocity.getEngine().getTemplate("path to your template" );
StringWriter writer = new StringWriter();
template.merge( context, writer );
A typical component configuration :
org.codehaus.plexus.velocity.VelocityComponentorg.codehaus.plexus.velocity.DefaultVelocityComponentruntime.log.logsystem.classorg.codehaus.plexus.velocity.Log4JLoggingSystemresource.loaderclasspathclasspath.resource.loader.classorg.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
]]>
plexus-velocity-1.1.8/src/test/ 0000775 0001750 0001750 00000000000 12200126217 015766 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/log4j.properties 0000664 0001750 0001750 00000000631 10064076330 021131 0 ustar ebourg ebourg log4j.rootCategory=DEBUG,testlog
# testlog is set to be a File appender using a PatternLayout.
log4j.appender.testlog = org.apache.log4j.FileAppender
log4j.appender.testlog.File = test.log
log4j.appender.testlog.Append = false
log4j.appender.testlog.Threshold = DEBUG
log4j.appender.testlog.layout = org.apache.log4j.PatternLayout
log4j.appender.testlog.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
plexus-velocity-1.1.8/src/test/resources/ 0000775 0001750 0001750 00000000000 12200126217 020000 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/resources/org/ 0000775 0001750 0001750 00000000000 12200126217 020567 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/resources/org/codehaus/ 0000775 0001750 0001750 00000000000 12200126217 022362 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/resources/org/codehaus/plexus/ 0000775 0001750 0001750 00000000000 12200126217 023702 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/resources/org/codehaus/plexus/velocity/ 0000775 0001750 0001750 00000000000 12200126217 025540 5 ustar ebourg ebourg ././@LongLink 0000000 0000000 0000000 00000000147 00000000000 011567 L ustar root root plexus-velocity-1.1.8/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.xml plexus-velocity-1.1.8/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.x0000664 0001750 0001750 00000001420 10576542103 033566 0 ustar ebourg ebourg org.codehaus.plexus.velocity.VelocityComponentdefaultorg.codehaus.plexus.velocity.DefaultVelocityComponentresource.loaderclasspathclasspath.resource.loader.classorg.apache.velocity.runtime.resource.loader.ClasspathResourceLoaderhelloworld
././@LongLink 0000000 0000000 0000000 00000000146 00000000000 011566 L ustar root root plexus-velocity-1.1.8/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.vm plexus-velocity-1.1.8/src/test/resources/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.v0000664 0001750 0001750 00000000054 10064076330 033562 0 ustar ebourg ebourg Static text -- $variable -- More static text plexus-velocity-1.1.8/src/test/java/ 0000775 0001750 0001750 00000000000 12200126217 016707 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/java/org/ 0000775 0001750 0001750 00000000000 12200126217 017476 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/java/org/codehaus/ 0000775 0001750 0001750 00000000000 12200126217 021271 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/java/org/codehaus/plexus/ 0000775 0001750 0001750 00000000000 12200126217 022611 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/java/org/codehaus/plexus/velocity/ 0000775 0001750 0001750 00000000000 12200126217 024447 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/test/java/org/codehaus/plexus/velocity/DefaultVelocityComponentTest.java 0000664 0001750 0001750 00000003332 10645265720 033157 0 ustar ebourg ebourg package org.codehaus.plexus.velocity;
/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed 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.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.codehaus.plexus.PlexusTestCase;
public class DefaultVelocityComponentTest
extends PlexusTestCase
{
public void testBasic()
throws Exception
{
DefaultVelocityComponent velocity;
VelocityContext context;
String value;
velocity = (DefaultVelocityComponent) lookup( VelocityComponent.ROLE );
// test the properties
value = (String) velocity.getEngine().getProperty( "hello" );
assertNotNull( value );
assertEquals( "world", value );
// test the rendering
context = new VelocityContext();
context.put( "variable", "Value from context" );
Template template = velocity.getEngine().getTemplate("org/codehaus/plexus/velocity/DefaultVelocityComponentTest.vm" );
StringWriter writer = new StringWriter();
template.merge( context, writer );
assertEquals( "Static text -- Value from context -- More static text", writer.toString() );
}
}
plexus-velocity-1.1.8/src/main/ 0000775 0001750 0001750 00000000000 12200126220 015725 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/resources/ 0000775 0001750 0001750 00000000000 12200126220 017737 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/resources/META-INF/ 0000775 0001750 0001750 00000000000 12200126220 021077 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/resources/META-INF/plexus/ 0000775 0001750 0001750 00000000000 12200126220 022417 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/resources/META-INF/plexus/components.xml 0000664 0001750 0001750 00000002346 10576651441 025361 0 ustar ebourg ebourg org.codehaus.plexus.velocity.VelocityComponentdefaultorg.codehaus.plexus.velocity.DefaultVelocityComponentresource.loaderclasspath,siteclasspath.resource.loader.classorg.codehaus.plexus.velocity.ContextClassLoaderResourceLoadersite.resource.loader.classorg.codehaus.plexus.velocity.SiteResourceLoaderruntime.log.invalid.referencesfalsevelocimacro.messages.onfalseresource.manager.logwhenfoundfalse
plexus-velocity-1.1.8/src/main/java/ 0000775 0001750 0001750 00000000000 12200126217 016654 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/java/org/ 0000775 0001750 0001750 00000000000 12200126217 017443 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/java/org/codehaus/ 0000775 0001750 0001750 00000000000 12200126217 021236 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/ 0000775 0001750 0001750 00000000000 12200126217 022556 5 ustar ebourg ebourg plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/velocity/ 0000775 0001750 0001750 00000000000 12200126220 024406 5 ustar ebourg ebourg ././@LongLink 0000000 0000000 0000000 00000000147 00000000000 011567 L ustar root root plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/velocity/ContextClassLoaderResourceLoader.java plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/velocity/ContextClassLoaderResourceLoader.ja0000664 0001750 0001750 00000003744 11236360171 033347 0 ustar ebourg ebourg package org.codehaus.plexus.velocity;
/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed 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.InputStream;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.commons.collections.ExtendedProperties;
public class ContextClassLoaderResourceLoader
extends ResourceLoader
{
public void init( ExtendedProperties configuration)
{
}
public synchronized InputStream getResourceStream( String name )
throws ResourceNotFoundException
{
InputStream result = null;
if (name == null || name.length() == 0)
{
throw new ResourceNotFoundException ("No template name provided");
}
try
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if ( name.startsWith( "/" ) )
{
name = name.substring( 1 );
}
result = classLoader.getResourceAsStream( name );
}
catch( Exception fnfe )
{
throw new ResourceNotFoundException( fnfe.getMessage() );
}
return result;
}
public boolean isSourceModified(Resource resource)
{
return false;
}
public long getLastModified(Resource resource)
{
return 0;
}
}
plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/velocity/VelocityComponent.java 0000664 0001750 0001750 00000001513 10645265720 030756 0 ustar ebourg ebourg package org.codehaus.plexus.velocity;
/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed 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.velocity.app.VelocityEngine;
public interface VelocityComponent
{
public final static String ROLE = VelocityComponent.class.getName();
VelocityEngine getEngine();
}
plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/velocity/DefaultVelocityComponent.java 0000664 0001750 0001750 00000011155 10666571622 032272 0 ustar ebourg ebourg package org.codehaus.plexus.velocity;
/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed 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.util.Enumeration;
import java.util.Properties;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.LogSystem;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/**
* A simple velocity component implementation.
*
*/
public class DefaultVelocityComponent
extends AbstractLogEnabled
implements VelocityComponent, Initializable, LogSystem
{
private VelocityEngine engine;
private Properties properties;
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
public void initialize()
throws InitializationException
{
engine = new VelocityEngine();
// avoid "unable to find resource 'VM_global_library.vm' in any resource loader."
engine.setProperty( "velocimacro.library", "" );
engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this );
if ( properties != null )
{
for ( Enumeration e = properties.propertyNames(); e.hasMoreElements(); )
{
String key = e.nextElement().toString();
String value = properties.getProperty( key );
engine.setProperty( key, value );
getLogger().debug( "Setting property: " + key + " => '" + value + "'." );
}
}
try
{
engine.init();
}
catch ( Exception e )
{
throw new InitializationException( "Cannot start the velocity engine: ", e );
}
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
public VelocityEngine getEngine()
{
return engine;
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
private RuntimeServices runtimeServices;
public void init( RuntimeServices runtimeServices )
{
this.runtimeServices = runtimeServices;
}
public void logVelocityMessage( int level, String message )
{
switch ( level )
{
case LogSystem.WARN_ID:
getLogger().warn( message );
break;
case LogSystem.INFO_ID:
// velocity info messages are too verbose, just consider them as debug messages...
getLogger().debug( message );
break;
case LogSystem.DEBUG_ID:
getLogger().debug( message );
break;
case LogSystem.ERROR_ID:
getLogger().error( message );
break;
default:
getLogger().debug( message );
break;
}
}
}
plexus-velocity-1.1.8/src/main/java/org/codehaus/plexus/velocity/SiteResourceLoader.java 0000664 0001750 0001750 00000005113 11277150011 031024 0 ustar ebourg ebourg package org.codehaus.plexus.velocity;
/*
* Copyright 2001-2006 Codehaus Foundation.
*
* Licensed 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.collections.ExtendedProperties;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.exception.ResourceNotFoundException;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* Alloww you to dynamically add resources that you want to be processed by Velocity. For
* example if you want ot generate a site and pull in some random files to be interpolated
* by Velocity you can use this resource loader.
*
* @author Jason van Zyl
*/
public class SiteResourceLoader
extends ResourceLoader
{
private static String resource;
private static File resourceDir;
public static void setResource( String staticResource )
{
resource = staticResource;
resourceDir = new File( resource ).getParentFile();
}
public void init( ExtendedProperties p )
{
}
public InputStream getResourceStream( String name )
throws ResourceNotFoundException
{
if ( resource != null )
{
try
{
File f = new File( resourceDir, name );
if ( f.exists() )
{
return new FileInputStream( f );
}
else
{
return new FileInputStream( resource );
}
}
catch ( FileNotFoundException e )
{
throw new ResourceNotFoundException( "Cannot find resource, make sure you set the right resource." );
}
}
return null;
}
public boolean isSourceModified( org.apache.velocity.runtime.resource.Resource resource )
{
return false; //To change body of implemented methods use File | Settings | File Templates.
}
public long getLastModified( org.apache.velocity.runtime.resource.Resource resource )
{
return 0;
}
}