You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2016/07/15 07:23:53 UTC
svn commit: r1752785 -
/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java
Author: cbrisson
Date: Fri Jul 15 07:23:53 2016
New Revision: 1752785
URL: http://svn.apache.org/viewvc?rev=1752785&view=rev
Log:
[tools] reflect ResourceLoader API change in WebappResourceLoader
Modified:
velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java
Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java?rev=1752785&r1=1752784&r2=1752785&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java (original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java Fri Jul 15 07:23:53 2016
@@ -21,6 +21,7 @@ package org.apache.velocity.tools.view;
import java.io.File;
import java.io.InputStream;
+import java.io.Reader;
import java.util.HashMap;
import javax.servlet.ServletContext;
import org.apache.velocity.exception.ResourceNotFoundException;
@@ -118,8 +119,9 @@ public class WebappResourceLoader extend
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found
* in classpath.
+ * @deprecated use {@link #getResourceReader(String, String)}
*/
- public synchronized InputStream getResourceStream(String name)
+ public synchronized @Deprecated InputStream getResourceStream(String name)
throws ResourceNotFoundException
{
InputStream result = null;
@@ -186,6 +188,94 @@ public class WebappResourceLoader extend
return result;
}
+ /**
+ * Get an InputStream so that the Runtime can build a
+ * template with it.
+ *
+ * @param name name of template to get
+ * @param encoding asked encoding
+ * @return InputStream containing the template
+ * @throws ResourceNotFoundException if template not found
+ * in classpath.
+ * @since 2.0
+ */
+ public synchronized Reader getResourceReader(String name, String encoding)
+ throws ResourceNotFoundException
+ {
+ Reader result = null;
+
+ if (name == null || name.length() == 0)
+ {
+ throw new ResourceNotFoundException("WebappResourceLoader: No template name provided");
+ }
+
+ /* since the paths always ends in '/',
+ * make sure the name never starts with one */
+ while (name.startsWith("/"))
+ {
+ name = name.substring(1);
+ }
+
+ Exception exception = null;
+ for (int i = 0; i < paths.length; i++)
+ {
+ String path = paths[i] + name;
+ InputStream rawStream = null;
+ try
+ {
+ rawStream = servletContext.getResourceAsStream(path);
+ result = buildReader(rawStream, encoding);
+
+ /* save the path and exit the loop if we found the template */
+ if (result != null)
+ {
+ templatePaths.put(name, paths[i]);
+ break;
+ }
+ }
+ catch (NullPointerException npe)
+ {
+ /* no servletContext was set, whine about it! */
+ throw npe;
+ }
+ catch (Exception e)
+ {
+ if (rawStream != null)
+ {
+ try
+ {
+ rawStream.close();
+ }
+ catch(Exception ee) {}
+ }
+ /* only save the first one for later throwing */
+ if (exception == null)
+ {
+ log.debug("WebappResourceLoader: Could not load {}", path, e);
+ exception = e;
+ }
+ }
+ }
+
+ /* if we never found the template */
+ if (result == null)
+ {
+ String msg = "WebappResourceLoader: Resource '" + name + "' not found.";
+
+ /* convert to a general Velocity ResourceNotFoundException */
+ if (exception == null)
+ {
+ throw new ResourceNotFoundException(msg);
+ }
+ else
+ {
+ msg += " Due to: " + exception;
+ throw new ResourceNotFoundException(msg, exception);
+ }
+ }
+ return result;
+ }
+
private File getCachedFile(String rootPath, String fileName)
{
// we do this when we cache a resource,
@@ -234,7 +324,7 @@ public class WebappResourceLoader extend
if (currentFile.canRead())
{
/* stop at the first resource found
- * (just like in getResourceStream()) */
+ * (just like in getResourceReader()) */
break;
}
}