dion 2003/01/06 19:33:34 Added: jelly/jelly-tags/velocity/src/java/org/apache/commons/jelly/tags/velocity VelocityTagSupport.java VelocityTagLibrary.java JellyContextAdapter.java MergeTag.java jelly/jelly-tags/velocity project.xml .cvsignore project.properties maven.xml Log: Moved velocity taglib from maven into jelly Revision Changes Path 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/src/java/org/apache/commons/jelly/tags/velocity/VelocityTagSupport.java Index: VelocityTagSupport.java =================================================================== package org.apache.commons.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 [EMAIL PROTECTED] * * 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. For each unique base * directory specified, a new VelocityEngine instance is stored in the * context (as the author hasn't figured out how to change the resource * loader of an already init'd VelocityEngine). * * @author <a href="mailto:[EMAIL PROTECTED]">Pete Kazmier</a> * @version $Id: VelocityTagSupport.java,v 1.1 2003/01/07 03:33:31 dion 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 for * the specified base directory. * * @return A VelocityEngine with a file resource loader configured * for the specified base directory. */ public VelocityEngine getVelocityEngine( String basedir ) throws Exception { VelocityEngine ve = ( VelocityEngine ) getContext().getVariable( keyName( basedir ) ); if ( ve == null ) { ve = new VelocityEngine(); ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this ); ve.setProperty( VelocityEngine.FILE_RESOURCE_LOADER_PATH, basedir ); ve.init(); getContext().setVariable( keyName( basedir ), ve ); } return ve; } /** * Constructs the name of the key used to reference the * VelocityEngine for the specified base directory. * * @param basedir The base directory used by the VelocityEngine * @return The key used to reference the VelocityEngine that has * been initialized with the specified base directory. */ private String keyName( String basedir ) { return new StringBuffer() .append( VELOCITY_ENGINE_VAR_NAME ) .append( '.' ) .append( basedir ) .toString(); } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/src/java/org/apache/commons/jelly/tags/velocity/VelocityTagLibrary.java Index: VelocityTagLibrary.java =================================================================== package org.apache.commons.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 [EMAIL PROTECTED] * * 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:[EMAIL PROTECTED]">Pete Kazmier</a> * @version $Id: VelocityTagLibrary.java,v 1.1 2003/01/07 03:33:31 dion Exp $ */ public class VelocityTagLibrary extends TagLibrary { public VelocityTagLibrary() { registerTag( "merge", MergeTag.class ); } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/src/java/org/apache/commons/jelly/tags/velocity/JellyContextAdapter.java Index: JellyContextAdapter.java =================================================================== package org.apache.commons.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 [EMAIL PROTECTED] * * 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.util.Set; import java.util.HashSet; import java.util.HashMap; import org.apache.commons.jelly.JellyContext; import org.apache.velocity.context.Context; /** * Adapts a JellyContext for use as a Velocity Context. This context * can be used in either read-only or read-write mode. When used as a * read-only adapter, items <tt>put</tt> or <tt>remove</tt>ed from the * Velocity context are not permitted to propogate to the JellyContext, * which is the default behavior. The adapter can also be used in a * read-write mode. This permits changes made by Velocity to propogate * to the JellyContext. * * @author <a href="mailto:[EMAIL PROTECTED]">Pete Kazmier</a> * @version $Id: JellyContextAdapter.java,v 1.1 2003/01/07 03:33:31 dion Exp $ */ class JellyContextAdapter implements Context { /** Flag to indicate read-only or read-write mode */ private boolean readOnly = true; /** The JellyContext being adapted */ private JellyContext jellyContext; /** The store for Velocity in the event the adpater is read-only */ private HashMap privateContext = new HashMap(); /** * Constructor. * * @param jellyContext The JellyContext to adapt */ public JellyContextAdapter( JellyContext jellyContext ) { this.jellyContext = jellyContext; } /** * Sets the read-only flag for this adapter. If the read-only flag * is set, changes to the Velocity Context will not be propogated to * the JellyContext. Turning the read-only flag off enables changes * to propogate. * * @param readOnly If this parameter is <tt>true</tt>, the adapter * becomes read-only. Setting the parameter to <tt>false</tt> the * adapter becomes read-write. */ public void setReadOnly(boolean readOnly) { this.readOnly = readOnly; } /** * Tests if the adapter is read-only. * * @return <tt>true</tt> if the adpater is read-only; otherwise * returns <tt>false</tt>. */ public boolean isReadOnly() { return readOnly; } public boolean containsKey( Object key ) { if ( key == null ) { return false; } if ( readOnly && privateContext.containsKey( key ) ) { return true; } return jellyContext.getVariable( key.toString() ) != null ? true : false; } public Object get( String key ) { if ( key == null ) { return null; } if ( readOnly && privateContext.containsKey( key ) ) { return privateContext.get( key ); } return jellyContext.getVariable( key ); } public Object[] getKeys() { Set keys = jellyContext.getVariables().keySet(); if ( readOnly ) { HashSet combinedKeys = new HashSet( keys ); combinedKeys.addAll( privateContext.keySet() ); keys = combinedKeys; } return keys.toArray(); } public Object put( String key, Object value ) { Object oldValue; if ( key == null || value == null ) { return null; } if ( readOnly ) { oldValue = privateContext.put( key, value ); } else { oldValue = jellyContext.getVariable( key ); jellyContext.setVariable( key, value ); } return oldValue; } public Object remove( Object key ) { Object oldValue; if ( key == null ) { return null; } if ( readOnly ) { oldValue = privateContext.remove( key ); } else { oldValue = jellyContext.getVariable( key.toString() ); jellyContext.removeVariable( key.toString() ); } return oldValue; } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/src/java/org/apache/commons/jelly/tags/velocity/MergeTag.java Index: MergeTag.java =================================================================== package org.apache.commons.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 [EMAIL PROTECTED] * * 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.JellyException; import org.apache.commons.jelly.XMLOutput; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.StringWriter; import java.io.Writer; /** * 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:[EMAIL PROTECTED]">Pete Kazmier</a> * @version $Id: MergeTag.java,v 1.1 2003/01/07 03:33:31 dion Exp $ */ public class MergeTag extends VelocityTagSupport { private static final String ENCODING = "ISO-8859-1"; private String var; private String name; private String basedir; private String template; private String inputEncoding; private String outputEncoding; private boolean readOnly = true; // -- Tag interface ----------------------------------------------------- public void doTag( final XMLOutput output ) throws Exception { if ( basedir == null || template == null ) { throw new JellyException( "This tag must define 'basedir' and 'template'" ); } if ( name != null ) { Writer writer = new OutputStreamWriter( new FileOutputStream( name ), outputEncoding == null ? ENCODING : 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 base directory used for loading of templates by the * Velocity file resource loader. * * @param basedir The directory where templates can be located by * the Velocity file resource loader. */ public void setBasedir( String basedir ) { this.basedir = basedir; } /** * 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 read-only flag for this adapter which prevents * modifications in the Velocity context from propogating to the * JellyContext. * * @param readOnly <tt>true</tt> prevents modifications from * propogating (the default), or <tt>false</tt> which permits * modifications. */ public void setReadOnly( boolean readOnly ) { this.readOnly = readOnly; } /** * 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 { JellyContextAdapter adapter = new JellyContextAdapter( getContext() ); adapter.setReadOnly( readOnly ); getVelocityEngine( basedir ).mergeTemplate( template, inputEncoding == null ? ENCODING : inputEncoding, adapter, writer ); } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/project.xml Index: project.xml =================================================================== <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE project [ <!-- see file for description --> <!ENTITY commonDeps SYSTEM "file:../../commonDependencies.ent"> ]> <project> <extend>../tag-project.xml</extend> <id>commons-jelly-tags-velocity</id> <name>commons-jelly-tags-velocity</name> <package>org.apache.commons.jelly.tags.velocity</package> <description> This is a Jelly interface for Velocity. </description> <shortDescription>Commons Jelly Velocity Tag Library</shortDescription> <siteDirectory>/www/jakarta.apache.org/commons/sandbox/jelly/tags/velocity</siteDirectory> <distributionDirectory>/www/jakarta.apache.org/builds/jakarta-commons-sandbox/jelly/tags/velocity</distributionDirectory> <repository> <connection>scm:cvs:pserver:[EMAIL PROTECTED]:/home/cvspublic:jakarta-commons-sandbox/jelly/jelly-tags/velocity/</connection> <url>http://cvs.apache.org/viewcvs/jakarta-commons-sandbox/jelly/jelly-tags/velocity/</url> </repository> <dependencies> &commonDeps; <!-- START for compilation --> <dependency> <id>commons-jelly</id> <version>SNAPSHOT</version> </dependency> <dependency> <id>velocity</id> <version>1.3</version> <url>http://jakarta.apache.org/velocity/</url> </dependency> <!-- END for compilation --> </dependencies> </project> 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/.cvsignore Index: .cvsignore =================================================================== maven.log target 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/project.properties Index: project.properties =================================================================== # ------------------------------------------------------------------- # P R O J E C T P R O P E R T I E S # ------------------------------------------------------------------- maven.junit.fork=true maven.compile.deprecation = on # Installation dir maven.dist.install.dir = /usr/local/jelly maven.checkstyle.properties=../tag-checkstyle.properties 1.1 jakarta-commons-sandbox/jelly/jelly-tags/velocity/maven.xml Index: maven.xml =================================================================== <project default="java:jar"> </project>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>