geirm 01/04/22 11:20:18
Modified: src/java/org/apache/velocity/app Velocity.java
Log:
Support for the per-template encoding stuff.
Fundamental change is to move from supporting InputStream on the evaluate()
functionality to Reader, to push the encoding issue upwards. It also
starts to normalize the API towards Reader.
Previous methods using InputStream should be marked deprecated.
Revision Changes Path
1.12 +53 -8 jakarta-velocity/src/java/org/apache/velocity/app/Velocity.java
Index: Velocity.java
===================================================================
RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/app/Velocity.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Velocity.java 2001/03/26 04:18:51 1.11
+++ Velocity.java 2001/04/22 18:20:17 1.12
@@ -60,6 +60,11 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
+import java.io.Reader;
+import java.io.BufferedReader;
+import java.io.StringReader;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
import org.apache.velocity.context.Context;
import org.apache.velocity.Template;
@@ -102,7 +107,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* @author <a href="[EMAIL PROTECTED]">Christoph Reck</a>
* @author <a href="[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: Velocity.java,v 1.11 2001/03/26 04:18:51 jvanzyl Exp $
+ * @version $Id: Velocity.java,v 1.12 2001/04/22 18:20:17 geirm Exp $
*/
public class Velocity implements RuntimeConstants
@@ -224,10 +229,7 @@
String logTag, String instring )
throws ParseErrorException, MethodInvocationException, IOException
{
- ByteArrayInputStream inStream =
- new ByteArrayInputStream( instring.getBytes() );
-
- return evaluate( context, out, logTag, inStream );
+ return evaluate( context, out, logTag, new BufferedReader( new
StringReader( instring )) );
}
/**
@@ -243,20 +245,57 @@
*
* @return true if successful, false otherwise. If false, see
* Velocity runtime log
+ * @deprecated
*/
public static boolean evaluate( Context context, Writer writer,
String logTag, InputStream instream )
throws ParseErrorException, MethodInvocationException, IOException
{
- SimpleNode nodeTree = null;
-
/*
* first, parse - convert ParseException if thrown
*/
+ BufferedReader br = null;
+ String encoding = null;
+
+ try
+ {
+ encoding = Runtime.getString(INPUT_ENCODING,ENCODING_DEFAULT);
+ br = new BufferedReader( new InputStreamReader( instream, encoding));
+ }
+ catch( UnsupportedEncodingException uce )
+ {
+ String msg = "Unsupported input encoding : " + encoding
+ + " for template " + logTag;
+ throw new ParseErrorException( msg );
+ }
+
+ return evaluate( context, writer, logTag, br );
+ }
+
+ /**
+ * Renders the input reader using the context into the output writer.
+ * To be used when a template is dynamically constructed, or want to
+ * use Velocity as a token replacer.
+ *
+ * @param context context to use in rendering input string
+ * @param out Writer in which to render the output
+ * @param logTag string to be used as the template name for log messages
+ * in case of error
+ * @param reader Reader containing the VTL to be rendered
+ *
+ * @return true if successful, false otherwise. If false, see
+ * Velocity runtime log
+ */
+ public static boolean evaluate( Context context, Writer writer,
+ String logTag, Reader reader )
+ throws ParseErrorException, MethodInvocationException, IOException
+ {
+ SimpleNode nodeTree = null;
+
try
{
- nodeTree = Runtime.parse( instream, logTag );
+ nodeTree = Runtime.parse( reader, logTag );
}
catch ( ParseException pex )
{
@@ -459,6 +498,12 @@
throws ResourceNotFoundException, ParseErrorException, Exception
{
return Runtime.getTemplate( name );
+ }
+
+ public static Template getTemplate(String name, String encoding)
+ throws ResourceNotFoundException, ParseErrorException, Exception
+ {
+ return Runtime.getTemplate( name, encoding );
}