jvanzyl 00/12/18 21:32:06
Modified: src/java/org/apache/velocity/runtime/directive Include.java
Parse.java
Log:
- Changed the #include directive to use the ResourceMangager via
the Runtime.
Remove the saftey checks for .. in the directives and moved
them to the FileResourceLoader.
Revision Changes Path
1.12 +14 -57
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Include.java
Index: Include.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Include.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Include.java 2000/12/11 19:42:12 1.11
+++ Include.java 2000/12/19 05:32:05 1.12
@@ -1,3 +1,5 @@
+package org.apache.velocity.runtime.directive;
+
/*
* The Apache Software License, Version 1.1
*
@@ -52,15 +54,13 @@
* <http://www.apache.org/>.
*/
-package org.apache.velocity.runtime.directive;
-
import java.io.*;
import org.apache.velocity.runtime.parser.*;
import org.apache.velocity.Context;
-import org.apache.velocity.runtime.configuration.*;
import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.runtime.parser.node.Node;
+import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.util.StringUtils;
/**
@@ -94,7 +94,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: Include.java,v 1.11 2000/12/11 19:42:12 jon Exp $
+ * @version $Id: Include.java,v 1.12 2000/12/19 05:32:05 jvanzyl Exp $
*/
public class Include extends Directive
{
@@ -136,7 +136,8 @@
Node n = node.jjtGetChild(i);
- if ( n.getType() == ParserTreeConstants.JJTSTRINGLITERAL ||
n.getType() == ParserTreeConstants.JJTREFERENCE )
+ if ( n.getType() == ParserTreeConstants.JJTSTRINGLITERAL ||
+ n.getType() == ParserTreeConstants.JJTREFERENCE )
{
if (!renderOutput( n, context, writer ))
outputErrorToStream( writer, "error with arg " + i + " please
see log.");
@@ -184,66 +185,22 @@
* get the path
*/
arg = value.toString();
-
- /*
- * everything must be under the template root TEMPLATE_PATH
- */
- String[] includePaths = Runtime.getIncludePaths();
-
- /*
- * for security, we will not accept anything with .. in the path
- */
- arg = StringUtils.normalizePath(arg);
- if ( arg == null || arg.length() == 0 )
- {
- Runtime.error( "#include() error : argument " + arg +
- " contains .. and may be trying to access " +
- "content outside of template root. Rejected." );
- return false;
- }
- /*
- * if a / leads off, then just nip that :)
- */
- if ( arg.startsWith("/") )
- arg = arg.substring(1);
+ Resource resource = null;
- /*
- * Try to locate the file in all of the possible
- * locations. If we can't even find a trace of existence
- * report the problem and leave.
- */
- File file = null;
- for (int i = 0; i < includePaths.length; i++)
+ try
{
- file = new File(includePaths[i], arg);
- if (file.exists())
- break;
- else
- file = null;
+ resource = Runtime.getContent(arg);
}
-
- if (file == null)
+ catch (Exception e)
{
- Runtime.error("#include() : " + arg + " doesn't exist!");
- return false;
+ Runtime.error("#include : cannot find " + arg + " template!");
}
- try
- {
- BufferedReader reader = new BufferedReader( new
FileReader(file.getAbsolutePath()));
-
- char buf[] = new char[1024];
- int len = 0;
-
- while ( ( len = reader.read( buf, 0, 1024 )) != -1)
- writer.write( buf, 0, len );
- }
- catch ( Exception e )
- {
- Runtime.error("#include() : " + e.toString() );
+ if ( resource == null )
return false;
- }
+
+ writer.write((String)resource.getData());
return true;
}
1.11 +3 -23
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Parse.java
Index: Parse.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Parse.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Parse.java 2000/12/11 19:42:13 1.10
+++ Parse.java 2000/12/19 05:32:05 1.11
@@ -83,7 +83,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: Parse.java,v 1.10 2000/12/11 19:42:13 jon Exp $
+ * @version $Id: Parse.java,v 1.11 2000/12/19 05:32:05 jvanzyl Exp $
*/
public class Parse extends Directive
{
@@ -139,27 +139,6 @@
*/
String arg = value.toString();
- /*
- * for security, we will not accept anything with .. in the path
- */
- arg = StringUtils.normalizePath(arg);
- if ( arg == null || arg.length() == 0 )
- {
- Runtime.error( "#parse() error : argument " + arg +
- " contains .. and may be trying to access" +
- " content outside of template root. Rejected." );
- return false;
- }
-
- /*
- * if a / leads off, then just nip that :)
- */
- if ( arg.startsWith( "/") )
- arg = arg.substring(1);
-
- /*
- * we will put caching here in the future...
- */
Template t = null;
try
{
@@ -175,7 +154,8 @@
try
{
- nodeTree = t.getDocument();
+ //nodeTree = t.getDocument();
+ nodeTree = (SimpleNode) t.getData();
ParseDirectiveVisitor v = new ParseDirectiveVisitor();
v.setDepth( parseDepth );