You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by ka...@apache.org on 2002/12/16 02:01:00 UTC
cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity JellyContextAdapter.java MergeTag.java VelocityTagLibrary.java VelocityTagSupport.java
kaz 2002/12/15 17:01:00
Modified: . project.xml
Added: src/java/org/apache/maven/jelly/tags/velocity
JellyContextAdapter.java MergeTag.java
VelocityTagLibrary.java VelocityTagSupport.java
Log:
Adding a Velocity taglib for use in our Jelly scripts. The taglib is
very basic. It contains only one tag at the moment with only a few
options. This was just a first pass at getting something working.
Basically, the tag takes a Velocity template, merges it with the current
JellyContext, and dumps the output to a file or stuffs it into a
variable in the JellyContext (similar to the <j:file> tag).
For example (taken from uberjar plugin):
<velocity:merge
name="${dest}/classworlds.conf"
template="${plugin.dir}/src/conf/classworlds.conf"/>
This uses the template specified by the 'template' attribute, and merges
it with the current JellyContext (containing all of the maven goodies),
and dumps the output to classworlds.conf. Alternatively, the output
could have been dumped to a variable. In addition, 'inputEncoding' and
'outputEncoding' attributes can be specified to set the encoding for the
template and output file respectively.
Thanks to James for the idea and slacking off on his day off so I could
write it before he got to it :-)
Revision Changes Path
1.225 +6 -0 jakarta-turbine-maven/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-turbine-maven/project.xml,v
retrieving revision 1.224
retrieving revision 1.225
diff -u -r1.224 -r1.225
--- project.xml 10 Dec 2002 15:49:13 -0000 1.224
+++ project.xml 16 Dec 2002 01:01:00 -0000 1.225
@@ -491,6 +491,12 @@
</dependency>
<dependency>
+ <id>velocity</id>
+ <version>1.3</version>
+ <url>http://jakarta.apache.org/velocity/</url>
+ </dependency>
+
+ <dependency>
<id>which</id>
<version>1.0</version>
<url>http://cvs.apache.org/viewcvs.cgi/xml-commons/java/src/org/apache/env/</url>
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity/JellyContextAdapter.java
Index: JellyContextAdapter.java
===================================================================
package org.apache.maven.jelly.tags.velocity;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import org.apache.commons.jelly.JellyContext;
import org.apache.velocity.context.Context;
/**
* Adapts a JellyContext for use as a Velocity Context.
*
* @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
* @version $Id: JellyContextAdapter.java,v 1.1 2002/12/16 01:01:00 kaz Exp $
*/
class JellyContextAdapter implements Context
{
private JellyContext jellyContext;
/**
* Constructor.
*
* @param jellyContext The JellyContext to adapt
*/
public JellyContextAdapter( JellyContext jellyContext )
{
this.jellyContext = jellyContext;
}
public boolean containsKey( Object key )
{
if ( key == null )
{
return false;
}
return jellyContext.getVariable( key.toString() ) != null ? true : false;
}
public Object get( String key )
{
if ( key == null )
{
return null;
}
return jellyContext.getVariable( key );
}
public Object[] getKeys()
{
return jellyContext.getVariables().keySet().toArray();
}
public Object put( String key, Object value )
{
if ( key == null || value == null )
{
return null;
}
Object oldValue = jellyContext.getVariable( key );
jellyContext.setVariable( key, value );
return oldValue;
}
public Object remove( Object key )
{
if ( key == null )
{
return null;
}
Object oldValue = jellyContext.getVariable( key.toString() );
jellyContext.removeVariable( key.toString() );
return oldValue;
}
}
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity/MergeTag.java
Index: MergeTag.java
===================================================================
package org.apache.maven.jelly.tags.velocity;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import org.apache.velocity.context.Context;
import org.apache.velocity.app.VelocityEngine;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
/**
* A tag that uses Velocity to render a specified template with the
* JellyContext storing the results in either a variable in the
* JellyContext or in a specified file.
*
* @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
* @version $Id: MergeTag.java,v 1.1 2002/12/16 01:01:00 kaz Exp $
*/
public class MergeTag extends VelocityTagSupport
{
private String var;
private String name;
private String template;
private String inputEncoding;
private String outputEncoding;
// -- Tag interface -----------------------------------------------------
public void doTag( final XMLOutput output ) throws Exception
{
if ( template == null )
{
throw new JellyException( "This tag must define 'template'" );
}
if ( name != null )
{
Writer writer = new OutputStreamWriter(
new FileOutputStream( name ),
outputEncoding == null ? "ISO-8859-1" : outputEncoding );
mergeTemplate( writer );
writer.close();
}
else if ( var != null )
{
StringWriter writer = new StringWriter();
mergeTemplate( writer );
context.setVariable( var, writer.toString() );
}
else
{
throw new JellyException(
"This tag must define either 'name' or 'var'" );
}
}
// -- Properties --------------------------------------------------------
/**
* Sets the var used to store the results of the merge.
*
* @param var The var to set in the JellyContext with the results of
* the merge.
*/
public void setVar( String var )
{
this.var = var;
}
/**
* Sets the file name for the merged output.
*
* @param name The name of the output file that is used to store the
* results of the merge.
*/
public void setName( String name )
{
this.name = name;
}
/**
* Sets the filename of the template used to merge with the
* JellyContext.
*
* @param template The filename of the template to be merged.
*/
public void setTemplate( String template )
{
this.template = template;
}
/**
* Sets the output encoding mode which defaults to ISO-8859-1 used
* when storing the results of a merge in a file.
*
* @param encoding The file encoding to use when writing the
* output.
*/
public void setOutputEncoding( String encoding )
{
this.outputEncoding = encoding;
}
/**
* Sets the input encoding used in the specified template which
* defaults to ISO-8859-1.
*
* @param encoding The encoding used in the template.
*/
public void setInputEncoding( String encoding )
{
this.inputEncoding = encoding;
}
// -- Implementation ----------------------------------------------------
/**
* Merges the Velocity template with the Jelly context.
*
* @param writer The output writer used to write the merged results.
* @throws Exception If an exception occurs during the merge.
*/
private void mergeTemplate( Writer writer ) throws Exception
{
Context velocityContext = new JellyContextAdapter( getContext() );
getVelocityEngine().mergeTemplate(
template,
inputEncoding == null ? "ISO-8859-1" : inputEncoding,
velocityContext,
writer );
}
}
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity/VelocityTagLibrary.java
Index: VelocityTagLibrary.java
===================================================================
package org.apache.maven.jelly.tags.velocity;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import org.apache.commons.jelly.TagLibrary;
/**
* Describes the Taglib. This class could be generated by XDoclet
*
* @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
* @version $Id: VelocityTagLibrary.java,v 1.1 2002/12/16 01:01:00 kaz Exp $
*/
public class VelocityTagLibrary extends TagLibrary
{
public VelocityTagLibrary()
{
registerTag( "merge", MergeTag.class );
}
}
1.1 jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity/VelocityTagSupport.java
Index: VelocityTagSupport.java
===================================================================
package org.apache.maven.jelly.tags.velocity;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Maven" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Maven", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* ====================================================================
*/
import org.apache.commons.jelly.TagSupport;
import org.apache.velocity.app.VelocityEngine;
/**
* Support methods for the Velocity tag library. Currently this is only
* used to get an instance of the VelocityEngine.
*
* @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
* @version $Id: VelocityTagSupport.java,v 1.1 2002/12/16 01:01:00 kaz Exp $
*/
public abstract class VelocityTagSupport extends TagSupport
{
/** The VelocityEngine variable name in the JellyContext. */
public static final String VELOCITY_ENGINE_VAR_NAME =
"org.apache.maven.jelly.tags.velocity.VelocityEngine";
/**
* Gets or creates a VelocityEngine if one doesn't already exist.
*
* @return A VelocityEngine.
*/
public VelocityEngine getVelocityEngine() throws Exception
{
VelocityEngine ve = ( VelocityEngine ) getContext().getVariable(
VELOCITY_ENGINE_VAR_NAME );
if ( ve == null )
{
ve = new VelocityEngine();
ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
ve.setProperty( VelocityEngine.FILE_RESOURCE_LOADER_PATH, "/" );
ve.init();
getContext().setVariable( VELOCITY_ENGINE_VAR_NAME, ve );
}
return ve;
}
}
Re: cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity JellyContextAdapter.java MergeTag.java VelocityTagLibrary.java VelocityTagSupport.java
Posted by Pete Kazmier <pe...@kazmier.com>.
One more thing, I just dumped the taglib in
org.apache.maven.jelly.tags.velocity until we've had a chance to refine
it and then move it somewhere else permanently.
On Sun, Dec 15, 2002 at 08:03:38PM -0500, Pete Kazmier wrote:
> I should also mention that variables #set in the velocity template are
> then visible in the JellyContext thereafter ...
>
Re: cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity JellyContextAdapter.java MergeTag.java VelocityTagLibrary.java VelocityTagSupport.java
Posted by Pete Kazmier <pe...@kazmier.com>.
On Sun, Dec 15, 2002 at 05:19:24PM -0800, Jon Scott Stevens wrote:
> on 2002/12/15 5:03 PM, "Pete Kazmier" <pe...@kazmier.com> wrote:
>
> > I should also mention that variables #set in the velocity template are
> > then visible in the JellyContext thereafter ...
>
> I don't think that is a good idea. Scope is going to be blown apart if you
> do that.
Good point. I wasn't really thinking too much about the reverse
direction as I just wanted to parse my template; however, I will just
adjust the JellyContextAdapter to not propogate any 'puts' to the
JellyContext (make it read-only) sometime tommorrow. Gotta finish
wrapping gifts with the wife now ...
Thanks,
Pete
Re: cvs commit:
jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity
JellyContextAdapter.java MergeTag.java VelocityTagLibrary.java
VelocityTagSupport.java
Posted by Jon Scott Stevens <jo...@latchkey.com>.
on 2002/12/15 5:03 PM, "Pete Kazmier" <pe...@kazmier.com> wrote:
> I should also mention that variables #set in the velocity template are
> then visible in the JellyContext thereafter ...
I don't think that is a good idea. Scope is going to be blown apart if you
do that.
-jon
--
StudioZ.tv /\ Bar/Nightclub/Entertainment
314 11th Street @ Folsom /\ San Francisco
http://studioz.tv/
Re: cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/jelly/tags/velocity JellyContextAdapter.java MergeTag.java VelocityTagLibrary.java VelocityTagSupport.java
Posted by Pete Kazmier <pe...@kazmier.com>.
I should also mention that variables #set in the velocity template are
then visible in the JellyContext thereafter ...
On Mon, Dec 16, 2002 at 01:01:00AM -0000, kaz@apache.org wrote:
> kaz 2002/12/15 17:01:00
>
> Modified: . project.xml
> Added: src/java/org/apache/maven/jelly/tags/velocity
> JellyContextAdapter.java MergeTag.java
> VelocityTagLibrary.java VelocityTagSupport.java
> Log:
> Adding a Velocity taglib for use in our Jelly scripts. The taglib is
> very basic. It contains only one tag at the moment with only a few
> options. This was just a first pass at getting something working.
> Basically, the tag takes a Velocity template, merges it with the current
> JellyContext, and dumps the output to a file or stuffs it into a
> variable in the JellyContext (similar to the <j:file> tag).
>
> For example (taken from uberjar plugin):
>
> <velocity:merge
> name="${dest}/classworlds.conf"
> template="${plugin.dir}/src/conf/classworlds.conf"/>
>
> This uses the template specified by the 'template' attribute, and merges
> it with the current JellyContext (containing all of the maven goodies),
> and dumps the output to classworlds.conf. Alternatively, the output
> could have been dumped to a variable. In addition, 'inputEncoding' and
> 'outputEncoding' attributes can be specified to set the encoding for the
> template and output file respectively.
>
> Thanks to James for the idea and slacking off on his day off so I could
> write it before he got to it :-)