geirm 01/01/09 21:57:44
Added: whiteboard/geir Test.java geir.cmp geir.vm geir1.cmp
geir1.vm geir2.cmp geir2.vm geir3.cmp geir3.vm
geir5.cmp geir5.vm geir8.cmp geir8.vm jdom-b5.jar
output test.sh test.xml velocity-0.8.jar
velocity.log xerces-1.2.1.jar xml.vm
Log:
Big pile of stuff to look at... there will be a post to the list...
Revision Changes Path
1.1 jakarta-velocity/whiteboard/geir/Test.java
Index: Test.java
===================================================================
package org.apache.velocity.test.misc;
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Velocity", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Stack;
import java.util.Vector;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.test.provider.TestProvider;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
/**
* This class the testbed for Velocity. It is used to
* test all the directives support by Velocity.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* @version $Id: Test.java,v 1.1 2001/01/10 05:57:43 geirm Exp $
*/
public class Test
{
/**
* Cache of writers
*/
private static Stack writerStack = new Stack();
public Test(String templateFile)
{
Writer writer = null;
TestProvider provider = new TestProvider();
ArrayList al = provider.getCustomers();
Hashtable h = new Hashtable();
/*
* put this in to test introspection $h.Bar or $h.get("Bar") etc
*/
h.put("Bar", "this is from a hashtable!");
/*
* adding simple vector with strings for testing late introspection stuff
*/
Vector v = new Vector();
v.addElement( new String("hello") );
v.addElement( new String("hello2") );
try
{
/*
* this is another way to do properties when initializing Runtime.
* make a Properties
*/
Properties p = new Properties();
/*
* now, if you want to, load it from a file (or whatever)
*/
try
{
FileInputStream fis = new FileInputStream(
new File("velocity.properties" ));
if( fis != null)
p.load( fis );
}
catch (Exception ex)
{
/* no worries. no file... */
}
/*
* add some individual properties if you wish
*/
p.setProperty(Runtime.RUNTIME_LOG_ERROR_STACKTRACE, "true");
p.setProperty(Runtime.RUNTIME_LOG_WARN_STACKTRACE, "true");
p.setProperty(Runtime.RUNTIME_LOG_INFO_STACKTRACE, "true");
/*
* and now call init
*/
Runtime.init(p);
/*
* now, do what we want to do. First, get the Template
*/
if (templateFile == null)
templateFile = "examples/example.vm";
Template template = Runtime.getTemplate(templateFile);
/*
* suck in the xml
*/
SAXBuilder builder;
Document root = null;
try {
builder = new SAXBuilder( "org.apache.xerces.parsers.SAXParser" );
root = builder.build("test.xml");
}
catch( Exception ee)
{
System.out.println("ee");
}
/*
* now, make a Context object and populate it.
*/
VelocityContext context = new VelocityContext();
context.put("provider", provider);
context.put("name", "jason");
context.put("providers", provider.getCustomers2());
context.put("list", al);
context.put("hashtable", h);
context.put("search", provider.getSearch());
context.put("relatedSearches", provider.getRelSearches());
context.put("searchResults", provider.getRelSearches());
context.put("menu", provider.getMenu());
context.put("stringarray", provider.getArray());
context.put("vector", v);
context.put("mystring", new String());
context.put("root", root);
context.put("stack", new Stack() );
/*
* make a writer, and merge the template 'against' the context
*/
writer = new BufferedWriter(new OutputStreamWriter(System.out));
template.merge( context , writer);
writer.flush();
writer.close();
}
catch( Exception e )
{
Runtime.error(e);
}
}
public static void main(String[] args)
{
Test t;
t = new Test(args[0]);
}
}
1.1 jakarta-velocity/whiteboard/geir/geir.cmp
Index: geir.cmp
===================================================================
Bang! : 0
Bang! : 1
Bang! : 2
Bang! : 3
Bang! : 4
Tests the basic arg passing for VMs
We want to pass through two VMs
("hello geir")
( geir )
( 1 )
( true )
Hello from foo!
foo: a
foo: b
Hello from foo!
foo: 1
foo: 2
foo: 3
foo: 4
Hello from foo!
foo: 4
foo: 5
foo: 6
foo: 7
foo: 8
foo: 9
foo: 10
This tests the pass-by-name feature
>0<
bar : 1
floog : 2
bar : 3
floog : 4
1.1 jakarta-velocity/whiteboard/geir/geir.vm
Index: geir.vm
===================================================================
Tests the basic arg passing for VMs
We want to pass through two VMs
#macro( foo $a )
Hello from foo!
#foreach($i in $a)
foo: $i
#end
#end
#macro( floog $a)
floog : $a
#end
#set($woogie = "geir")
#set($x = 4)
#set($y = 10)
#bar("hello $woogie")
#bar( $woogie )
#bar( 1 )
#bar( true )
#foo( ["a","b"] )
#foo( [1..4] )
#foo( [$x..$y] )
This tests the pass-by-name feature
#macro( bar $b )
bar : $b
#floog( $b )
#end
>$provider.bang()<
#bar( $provider.bang() )
#bar( $provider.bang() )
1.1 jakarta-velocity/whiteboard/geir/geir1.cmp
Index: geir1.cmp
===================================================================
SetBangStart() : called with val = 4
SetBangStart() : called with val = 5
This tests the pass by name some more, seeing if
we can assign to a complete reference that
we pass in (bangstartII) or, for the truly insane
pass in a reference that a property is assumed
within (BangstartII)
Note that you wont see anything rendered, but
SetBangStart will output to stdout
1.1 jakarta-velocity/whiteboard/geir/geir1.vm
Index: geir1.vm
===================================================================
This tests the pass by name some more, seeing if
we can assign to a complete reference that
we pass in (bangstartII) or, for the truly insane
pass in a reference that a property is assumed
within (BangstartII)
Note that you wont see anything rendered, but
SetBangStart will output to stdout
#macro( bangstart $a)
#set( $a.BangStart = 4 )
#end
#macro( bangstartII $a)
#set( $a = 5 )
#end
#macro( bar $b )
$b.bang()
#end
#bangstart( $provider )
#bangstartII( $provider.BangStart )
1.1 jakarta-velocity/whiteboard/geir/geir2.cmp
Index: geir2.cmp
===================================================================
global recurse 5
global recurse 4
global recurse 3
global recurse 2
global recurse 1
global recurse 5
global recurse 4
global recurse 3
global recurse 2
global recurse 1
1.1 jakarta-velocity/whiteboard/geir/geir2.vm
Index: geir2.vm
===================================================================
#macro( recurse $a )
#set($a = $a - 1 )
#if( $a > 0 )
#recurse($a)
#end
#end
#set($val = 5)
#recurse($val)
#recurse(5 )
1.1 jakarta-velocity/whiteboard/geir/geir3.cmp
Index: geir3.cmp
===================================================================
This tests to make sure that we only alter either
the local arg in a VM, or the global context.
So in foo, we are accessing the global 'b', and
the fact that we were called from bar that has it's
arg of 'b' is irrelevant.
precall $b = 0
foo : $b = 0
foo after : $b = 8
woobie post foo : 4
bar post woobie : 4
postcall $b = 8
1.1 jakarta-velocity/whiteboard/geir/geir3.vm
Index: geir3.vm
===================================================================
This tests to make sure that we only alter either
the local arg in a VM, or the global context.
So in foo, we are accessing the global 'b', and
the fact that we were called from bar that has it's
arg of 'b' is irrelevant.
#macro( foo $a )
foo : \$b = $b
#set ($b = $a * 2)
foo after : \$b = $b
#end
#macro( bar $b)
#woobie($b)
bar post woobie : $b
#end
#macro( woobie $b)
#foo($b)
woobie post foo : $b
#end
#set($b = 0 )
#set($arg = 4)
precall \$b = $b
#bar( $arg)
postcall \$b = $b
1.1 jakarta-velocity/whiteboard/geir/geir5.cmp
Index: geir5.cmp
===================================================================
This is simple : tests to ensure that
something passed through a chain of VMS
is actually updated correctly in the context
2
1.1 jakarta-velocity/whiteboard/geir/geir5.vm
Index: geir5.vm
===================================================================
This is simple : tests to ensure that
something passed through a chain of VMS
is actually updated correctly in the context
#macro(bar $a)
#set($b = $a + $a)
#end
#macro( foo $b)
#set($b = 1)
#bar($b)
#end
#set($b = 0)
#foo($b)
$b
1.1 jakarta-velocity/whiteboard/geir/geir8.cmp
Index: geir8.cmp
===================================================================
Tests to make sure string lits are working
Hello geir
Hello bill
1.1 jakarta-velocity/whiteboard/geir/geir8.vm
Index: geir8.vm
===================================================================
Tests to make sure string lits are working
#macro(foo $string $thename)
$string
#set($thename = "bill")
$string
#end
#set($name = "geir")
#foo("Hello $name" $name )
1.1 jakarta-velocity/whiteboard/geir/jdom-b5.jar
<<Binary file>>
1.1 jakarta-velocity/whiteboard/geir/output
Index: output
===================================================================
<document>
<properties>
<author>
<email>
[EMAIL PROTECTED]
</email>
<name>
<last>
<full>
Magnusson
</full>
<firstinitial>
M
</firstinitial>
</last>
<first>
Geir
</first>
</name>
</author>
<title>
The Jakarta Project
</title>
</properties>
<body>
I am the body
</body>
</document>
1.1 jakarta-velocity/whiteboard/geir/test.sh
Index: test.sh
===================================================================
#!/bin/sh
CLASSPATH=.
for jar in *.jar
do
CLASSPATH=${CLASSPATH}:${jar}
done
java -cp ${CLASSPATH} org.apache.velocity.test.misc.Test $1 > output 2>&1
1.1 jakarta-velocity/whiteboard/geir/test.xml
Index: test.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<document>
<properties>
<author>
<email>[EMAIL PROTECTED]</email>
<name>
<last>
<full>Magnusson</full>
<firstinitial>M</firstinitial>
</last>
<first>
Geir
</first>
</name>
</author>
<title>
The Jakarta Project
</title>
</properties>
<body>
I am the body
</body>
</document>
1.1 jakarta-velocity/whiteboard/geir/velocity-0.8.jar
<<Binary file>>
1.1 jakarta-velocity/whiteboard/geir/velocity.log
Index: velocity.log
===================================================================
Wed Jan 10 01:44:04 EST 2001 [info] Default Properties File:
org/apache/velocity/runtime/defaults/velocity.properties
Wed Jan 10 01:44:04 EST 2001 [info] ** Property Override :
runtime.log.warn.stacktrace = true
Wed Jan 10 01:44:04 EST 2001 [info] ** Property Override :
runtime.log.info.stacktrace = true
Wed Jan 10 01:44:04 EST 2001 [info] ** Property Override :
runtime.log.error.stacktrace = true
Wed Jan 10 01:44:04 EST 2001 [info] Log file being used is:
/home/gmj/velocity/jakarta-velocity/whiteboard/geir/velocity.log
Wed Jan 10 01:44:04 EST 2001 [info] Resource Loader Instantiated:
org.apache.velocity.runtime.resource.loader.FileResourceLoader
Wed Jan 10 01:44:04 EST 2001 [info] Resources Loaded From:
/home/gmj/velocity/jakarta-velocity/whiteboard/geir/.
Wed Jan 10 01:44:04 EST 2001 [info] Resource Loader Initialized.
Wed Jan 10 01:44:04 EST 2001 [info] Loaded Pluggable Directive:
org.apache.velocity.runtime.directive.Literal
Wed Jan 10 01:44:04 EST 2001 [info] Loaded Pluggable Directive:
org.apache.velocity.runtime.directive.Macro
Wed Jan 10 01:44:04 EST 2001 [info] Loaded Pluggable Directive:
org.apache.velocity.runtime.directive.Parse
Wed Jan 10 01:44:04 EST 2001 [info] Loaded Pluggable Directive:
org.apache.velocity.runtime.directive.Include
Wed Jan 10 01:44:04 EST 2001 [info] Loaded Pluggable Directive:
org.apache.velocity.runtime.directive.Foreach
Wed Jan 10 01:44:05 EST 2001 [info] Created: 20 parsers.
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : initialization starting.
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : adding VMs from global VM
library template : VM_global_library.vm
Wed Jan 10 01:44:05 EST 2001 [info] Attempting to find VM_global_library.vm with
org.apache.velocity.runtime.resource.loader.FileResourceLoader
Wed Jan 10 01:44:05 EST 2001 [error] FileResourceLoader Error: cannot find resource
/home/gmj/velocity/jakarta-velocity/whiteboard/geir/./VM_global_library.vm
Wed Jan 10 01:44:05 EST 2001 [error] java.lang.Exception: Can't find
VM_global_library.vm!
at
org.apache.velocity.runtime.resource.ResourceManager.getResource(ResourceManager.java:256)
at org.apache.velocity.runtime.Runtime.getTemplate(Runtime.java:630)
at
org.apache.velocity.runtime.VelocimacroFactory.initVelocimacro(VelocimacroFactory.java:124)
at org.apache.velocity.runtime.Runtime.init(Runtime.java:245)
at org.apache.velocity.runtime.Runtime.init(Runtime.java:266)
at org.apache.velocity.test.misc.Test.<init>(Test.java:155)
at org.apache.velocity.test.misc.Test.main(Test.java:224)
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : global VM library template macro
registration complete.
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : no local VM library template
used.
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : allowInline = true : VMs can be
defined inline in templates
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : allowInlineToOverride = false :
VMs defined inline may NOT replace previous VM definitions
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : allowInlineLocal = false : VMs
defined inline will be global in scope if allowed.
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : messages on : VM system will
output information messages
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : initialization complete.
Wed Jan 10 01:44:05 EST 2001 [info] Velocity successfully started.
Wed Jan 10 01:44:05 EST 2001 [info] Attempting to find xml.vm with
org.apache.velocity.runtime.resource.loader.FileResourceLoader
Wed Jan 10 01:44:05 EST 2001 [info] Velocimacro : added new VM : #recursive( e
indent ) : source = xml.vm
1.1 jakarta-velocity/whiteboard/geir/xerces-1.2.1.jar
<<Binary file>>
1.1 jakarta-velocity/whiteboard/geir/xml.vm
Index: xml.vm
===================================================================
#macro ( recursive $e $indent )
#if( $e.getChildren().size() > 0 )
$indent <$e.getName()>
#foreach ($child in $e.getChildren() )
#recursive( $child "$indent " )
#end
$indent </$e.getName()>
#else
$indent <$e.getName()>
$indent $e.getTextTrim()
$indent </$e.getName()>
#end
#end
#set($i = " ")
#recursive( $root.getRootElement() $i )