geirm 01/02/25 19:33:24
Modified: src/java/org/apache/velocity/runtime/resource Resource.java
ResourceManager.java
src/java/org/apache/velocity/runtime/resource/loader
FileResourceLoader.java ResourceLoader.java
Log:
Changes to support the new application level exceptions
o.a.v.e.ParseErrorException and ResourceNotFoundException
Resource.java : added the throws to process()
ResourceManager.java : changed getResource() to throw the right exceptions
as needed.
ResourceLoader.jave : added the throws to getResourceStream()
FileResourceLoader.java : changed getResourceStream() do always toss an
exception and not return 'null'.
I think I got this stuff right. Please check, y'all.
Will checkin a test case for JUnit and alter Test.java to deal with these.
Will also modify the examples to show the RightWay.
Revision Changes Path
1.3 +6 -2
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/Resource.java
Index: Resource.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/Resource.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Resource.java 2001/02/16 14:26:02 1.2
+++ Resource.java 2001/02/26 03:33:19 1.3
@@ -63,13 +63,16 @@
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.exception.ParseErrorException;
+
/**
* This class represent a general text resource that
* may have been retrieved from any number of possible
* sources.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: Resource.java,v 1.2 2001/02/16 14:26:02 geirm Exp $
+ * @version $Id: Resource.java,v 1.3 2001/02/26 03:33:19 geirm Exp $
*/
public abstract class Resource
{
@@ -125,7 +128,8 @@
* the actual parsing of the input stream needs to be
* performed.
*/
- public abstract boolean process();
+ public abstract boolean process()
+ throws ResourceNotFoundException, ParseErrorException, Exception;
public boolean isSourceModified()
{
1.9 +34 -7
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/ResourceManager.java
Index: ResourceManager.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/ResourceManager.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ResourceManager.java 2001/02/16 14:26:03 1.8
+++ ResourceManager.java 2001/02/26 03:33:19 1.9
@@ -66,12 +66,16 @@
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.runtime.resource.loader.ResourceLoaderFactory;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.exception.ParseErrorException;
+
+
/**
* Class to manage the text resource for the Velocity
* Runtime.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: ResourceManager.java,v 1.8 2001/02/16 14:26:03 geirm Exp $
+ * @version $Id: ResourceManager.java,v 1.9 2001/02/26 03:33:19 geirm Exp $
*/
public class ResourceManager
{
@@ -188,8 +192,15 @@
* @param resourceName The name of the resource to retrieve.
* @param resourceType The type of resource (<code>RESOURCE_TEMPLATE</code>,
* <code>RESOURCE_CONTENT</code>, etc.).
+ * @return Resource with the template parsed and ready.
+ * @throws ResourceNotFoundException if template not found
+ * from any available source.
+ * @throws ParseErrorException if template cannot be parsed due
+ * to syntax (or other) error.
+ * @throws Exception if a problem in parse
*/
public static Resource getResource(String resourceName, int resourceType)
+ throws ResourceNotFoundException, ParseErrorException, Exception
{
Resource resource = null;
ResourceLoader resourceLoader = null;
@@ -238,11 +249,22 @@
resource.setLastModified(
resourceLoader.getLastModified( resource ));
+ }
+ catch( ResourceNotFoundException rnfe )
+ {
+ Runtime.error("ResourceManager.getResource() exception: " +
rnfe);
+ throw rnfe;
}
- catch (Exception e)
+ catch( ParseErrorException pee )
{
- Runtime.error(e);
+ Runtime.error("ResourceManager.getResource() exception: " +
pee);
+ throw pee;
}
+ catch( Exception eee )
+ {
+ Runtime.error("ResourceManager.getResource() exception: " +
eee);
+ throw eee;
+ }
}
}
@@ -285,7 +307,7 @@
* Return null if we can't find a resource.
*/
if (resource.getData() == null)
- throw new Exception("Can't find " + resourceName + "!");
+ throw new ResourceNotFoundException("Can't find " +
resourceName + "!");
resource.setLastModified(resourceLoader.getLastModified(resource));
@@ -301,11 +323,16 @@
if (resourceLoader.isCachingOn())
globalCache.put(resourceName, resource);
+ }
+ catch( ParseErrorException pee )
+ {
+ Runtime.error("ResourceManager.getResource() parse exception: " +
pee);
+ throw pee;
}
- catch (Exception e)
+ catch( Exception ee )
{
- Runtime.error(e);
- resource = null; // return null
+ Runtime.error("ResourceManager.getResource() exception: " + ee);
+ throw ee;
}
}
return resource;
1.3 +47 -17
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
Index: FileResourceLoader.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FileResourceLoader.java 2000/12/19 21:46:52 1.2
+++ FileResourceLoader.java 2001/02/26 03:33:21 1.3
@@ -58,6 +58,7 @@
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
+import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Hashtable;
@@ -66,14 +67,16 @@
import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.runtime.resource.Resource;
+import org.apache.velocity.exception.ResourceNotFoundException;
+
/**
* This is a simple template file loader.
* Currently it only supports a single path to templates.
* That'll change once we decide how we want to do configuration
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * $Revision: 1.2 $
+ * $Revision: 1.3 $
*/
public class FileResourceLoader extends ResourceLoader
{
@@ -94,44 +97,71 @@
/**
* Get an InputStream so that the Runtime can build a
* template with it.
+ *
+ * @param name name of template to get
+ * @return InputStream containing the template
+ * @throws ResourceNotFoundException if template not found
+ * in the file template path.
*/
public synchronized InputStream getResourceStream( String name )
- throws Exception
+ throws ResourceNotFoundException
{
if (name == null || name.length() == 0)
{
- throw new Exception ("Need to specify a file name or file path!");
+ /*
+ * I guess this exception is appropos..
+ */
+
+ throw new ResourceNotFoundException ("Need to specify a file name or
file path!");
}
String normalizedPath = StringUtils.normalizePath(name);
if ( normalizedPath == null || normalizedPath.length() == 0 )
{
- Runtime.error( "File resource error : argument " + normalizedPath +
+ String msg = "File resource error : argument " + normalizedPath +
" contains .. and may be trying to access " +
- "content outside of template root. Rejected." );
-
- return null;
+ "content outside of template root. Rejected.";
+
+ Runtime.error( "FileResourceLoader : " + msg );
+
+ throw new ResourceNotFoundException ( msg );
}
/*
* if a / leads off, then just nip that :)
*/
if ( normalizedPath.startsWith("/") )
+ {
normalizedPath = normalizedPath.substring(1);
+ }
- File file = new File( path, normalizedPath );
- if ( file.canRead() )
+ try
{
- return new BufferedInputStream(
- new FileInputStream(file.getAbsolutePath()));
+ File file = new File( path, normalizedPath );
+
+ if ( file.canRead() )
+ {
+ return new BufferedInputStream(
+ new FileInputStream(file.getAbsolutePath()));
+ }
+ else
+ {
+ String msg = "FileResourceLoader Error: cannot find resource " +
+ file.getAbsolutePath();
+
+ Runtime.error(msg);
+ throw new ResourceNotFoundException( msg );
+ }
}
- else
+ catch( FileNotFoundException fnfe )
{
- Runtime.error("FileResourceLoader Error: cannot find resource " +
- file.getAbsolutePath());
- }
-
- return null;
+ /*
+ * log and convert to a general Velocity ResourceNotFoundException
+ */
+
+ Runtime.error("FileResourceLoader Error : exception : " + fnfe );
+ throw new ResourceNotFoundException( fnfe.getMessage() );
+ }
}
public boolean isSourceModified(Resource resource)
1.4 +4 -2
jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java
Index: ResourceLoader.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ResourceLoader.java 2000/12/19 21:52:57 1.3
+++ ResourceLoader.java 2001/02/26 03:33:22 1.4
@@ -60,12 +60,14 @@
import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.runtime.resource.Resource;
+import org.apache.velocity.exception.ResourceNotFoundException;
+
/**
* This is abstract class the all text resource loaders should
* extend.
*
* @autor <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * $Id: ResourceLoader.java,v 1.3 2000/12/19 21:52:57 jvanzyl Exp $
+ * $Id: ResourceLoader.java,v 1.4 2001/02/26 03:33:22 geirm Exp $
*/
public abstract class ResourceLoader
{
@@ -113,7 +115,7 @@
* Get the InputStream that the Runtime will parse
* to create a template.
*/
- public abstract InputStream getResourceStream( String source ) throws Exception;
+ public abstract InputStream getResourceStream( String source ) throws
ResourceNotFoundException;
/**
* Given a template, check to see if the source of InputStream