1. opensymphony
  2. webwork

Commits

rebelutionary  committed c1d493d

Adding a simple tools bean to handle url and html encoding for the moment. This might be obviated by the implementing of velocity-tools support in the future.

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@236573baa09-0c28-0410-bef9-dab3c582ae83

  • Participants
  • Parent commits bcf5814
  • Branches master

Comments (0)

Files changed (2)

File src/java/com/opensymphony/webwork/views/velocity/VelocityManager.java

View file
  • Ignore whitespace
     public static final String WEBWORK = "webwork";
     public static final String UI = "ui";
     public static final String ACTION = "action";
+    public static final String TOOLS = "tools";
 
-    /**
- * the parent JSP tag
- */
+    /** the parent JSP tag */
     public static final String PARENT = "parent";
 
-    /**
- * the current JSP tag
- */
+    /** the current JSP tag */
     public static final String TAG = "tag";
 
     //~ Instance fields ////////////////////////////////////////////////////////
 
     private OgnlTool ognlTool = OgnlTool.getInstance();
+    private ViewToolsBean viewTools = new ViewToolsBean();
     private VelocityEngine velocityEngine;
 
     //~ Constructors ///////////////////////////////////////////////////////////
     //~ Methods ////////////////////////////////////////////////////////////////
 
     /**
- * retrieve an instance to the current VelocityManager
- * @return
- */
+     * retrieve an instance to the current VelocityManager
+     * 
+     * @return 
+     */
     public synchronized static VelocityManager getInstance() {
         if (instance == null) {
             String classname = VelocityManager.class.getName();
     }
 
     /**
- * return  a reference to the VelocityEngine used by <b>all</b> webwork velocity thingies with the exception of
- * directly accessed *.vm pages
- * @return
- */
+     * return  a reference to the VelocityEngine used by <b>all</b> webwork velocity thingies with the exception of
+     * directly accessed *.vm pages
+     * 
+     * @return 
+     */
     public VelocityEngine getVelocityEngine() {
         return velocityEngine;
     }
 
     /**
- * This method is responsible for creating the standard VelocityContext used by all WW2 velocity views.  The
- * following context parameters are defined:
- *
- * <ul>
- *   <li><strong>req</strong> - the current HttpServletRequest</li>
- *   <li><strong>res</strong> - the current HttpServletResponse</li>
- *   <li><strong>stack</strong> - the current {@link OgnlValueStack}</li>
- *   <li><strong>ognl</strong> - an {@link OgnlTool}</li>
- *   <li><strong>webwork</strong> - an instance of {@link WebWorkUtil}</li>
- *   <li><strong>action</strong> - the current WebWork action</li>
- * </ul>
- *
- * @return a new WebWorkVelocityContext
- */
+     * This method is responsible for creating the standard VelocityContext used by all WW2 velocity views.  The
+     * following context parameters are defined:
+     * <p/>
+     * <ul>
+     * <li><strong>req</strong> - the current HttpServletRequest</li>
+     * <li><strong>res</strong> - the current HttpServletResponse</li>
+     * <li><strong>stack</strong> - the current {@link OgnlValueStack}</li>
+     * <li><strong>ognl</strong> - an {@link OgnlTool}</li>
+     * <li><strong>webwork</strong> - an instance of {@link WebWorkUtil}</li>
+     * <li><strong>action</strong> - the current WebWork action</li>
+     * <li><strong>tools</strong> - an instance of {@link ViewToolsBean}</li>
+     * </ul>
+     * 
+     * @return a new WebWorkVelocityContext
+     */
     public Context createContext(OgnlValueStack stack, ServletRequest servletRequest, ServletResponse servletResponse) {
         WebWorkVelocityContext context = new WebWorkVelocityContext(stack);
         context.put(REQUEST, servletRequest);
         context.put(RESPONSE, servletResponse);
         context.put(STACK, stack);
         context.put(OGNL, ognlTool);
+        context.put(TOOLS, viewTools);
         context.put(WEBWORK, new WebWorkUtil(context));
 
         ActionInvocation invocation = (ActionInvocation) stack.getContext().get(ActionContext.ACTION_INVOCATION);
     }
 
     /**
- * initializes the VelocityManager.  this should be called during the initialization process, say by
- * ServletDispatcher.  this may be called multiple times safely although calls beyond the first won't do anything
- * @param context the current servlet context
- */
+     * initializes the VelocityManager.  this should be called during the initialization process, say by
+     * ServletDispatcher.  this may be called multiple times safely although calls beyond the first won't do anything
+     * 
+     * @param context the current servlet context
+     */
     public synchronized void init(ServletContext context) {
         if (velocityEngine == null) {
             velocityEngine = newVelocityEngine(context);
     }
 
     /**
- * load optional velocity properties using the following loading strategy
- * <ul>
- *  <li>relative to the servlet context path</li>
- *  <li>relative to the WEB-INF directory</li>
- *  <li>on the classpath</li>
- * </ul>
- * @param context the current ServletContext.  may <b>not</b> be null
- * @return the optional properties if webwork.velocity.configfile was specified, an empty Properties file otherwise
- */
+     * load optional velocity properties using the following loading strategy
+     * <ul>
+     * <li>relative to the servlet context path</li>
+     * <li>relative to the WEB-INF directory</li>
+     * <li>on the classpath</li>
+     * </ul>
+     * 
+     * @param context the current ServletContext.  may <b>not</b> be null
+     * @return the optional properties if webwork.velocity.configfile was specified, an empty Properties file otherwise
+     */
     public Properties loadConfiguration(ServletContext context) {
         if (context == null) {
             String gripe = "Error attempting to create a loadConfiguration from a null ServletContext!";
         Properties properties = new Properties();
 
         /**
- * if the user has specified an external velocity configuration file, we'll want to search for it in the
- * following order
- *
- * 1. relative to the context path
- * 2. relative to /WEB-INF
- * 3. in the class path
- */
+         * if the user has specified an external velocity configuration file, we'll want to search for it in the
+         * following order
+         *
+         * 1. relative to the context path
+         * 2. relative to /WEB-INF
+         * 3. in the class path
+         */
         String configfile;
 
         if (Configuration.isSet("webwork.velocity.configfile")) {
             log.debug("Initializing Velocity with the following properties ...");
 
             for (Iterator iter = properties.keySet().iterator();
-                    iter.hasNext();) {
+                 iter.hasNext();) {
                 String key = (String) iter.next();
                 String value = properties.getProperty(key);
 
     }
 
     /**
- * <p>
- * instantiate a new VelocityEngine
- * </p>
- * <p>
- * the following is the default Velocity configuration
- * </p>
- * <pre>
- *  resource.loader = file, class
- *  file.resource.loader.path = real path of webapp
- *  class.resource.loader.description = Velocity Classpath Resource Loader
- *  class.resource.loader.class = com.opensymphony.webwork.views.velocity.WebWorkResourceLoader
- * </pre>
- * <p>
- * this default configuration can be overridden by specifying a webwork.velocity.configfile property in the
- * webwork.properties file.  the specified config file will be searched for in the following order:
- * </p>
- * <ul>
- *  <li>relative to the servlet context path</li>
- *  <li>relative to the WEB-INF directory</li>
- *  <li>on the classpath</li>
- * </ul>
- * @param context the current ServletContext.  may <b>not</b> be null
- */
+     * <p/>
+     * instantiate a new VelocityEngine
+     * </p>
+     * <p/>
+     * the following is the default Velocity configuration
+     * </p>
+     * <pre>
+     *  resource.loader = file, class
+     *  file.resource.loader.path = real path of webapp
+     *  class.resource.loader.description = Velocity Classpath Resource Loader
+     *  class.resource.loader.class = com.opensymphony.webwork.views.velocity.WebWorkResourceLoader
+     * </pre>
+     * <p/>
+     * this default configuration can be overridden by specifying a webwork.velocity.configfile property in the
+     * webwork.properties file.  the specified config file will be searched for in the following order:
+     * </p>
+     * <ul>
+     * <li>relative to the servlet context path</li>
+     * <li>relative to the WEB-INF directory</li>
+     * <li>on the classpath</li>
+     * </ul>
+     * 
+     * @param context the current ServletContext.  may <b>not</b> be null
+     */
     protected VelocityEngine newVelocityEngine(ServletContext context) {
         if (context == null) {
             String gripe = "Error attempting to create a new VelocityEngine from a null ServletContext!";
     }
 
     /**
- * once we've loaded up the user defined configurations, we will want to apply WebWork specification configurations.
- * <ul>
- *   <li>if Velocity.RESOURCE_LOADER has not been defined, then we will use the defaults which is a joined file,
- *       class loader for unpackaed wars and a straight class loader otherwise</li>
- *   <li>we need to define the various WebWork custom user directives such as #param, #tag, and #bodytag</li>
- * </ul>
- *
- * @param context
- * @param p
- */
+     * once we've loaded up the user defined configurations, we will want to apply WebWork specification configurations.
+     * <ul>
+     * <li>if Velocity.RESOURCE_LOADER has not been defined, then we will use the defaults which is a joined file,
+     * class loader for unpackaed wars and a straight class loader otherwise</li>
+     * <li>we need to define the various WebWork custom user directives such as #param, #tag, and #bodytag</li>
+     * </ul>
+     * 
+     * @param context 
+     * @param p       
+     */
     private void applyDefaultConfiguration(ServletContext context, Properties p) {
         /**
- * Load a default resource loader definition if there isn't one present.
- * Ben Hall (22/08/2003)
- */
+         * Load a default resource loader definition if there isn't one present.
+         * Ben Hall (22/08/2003)
+         */
         if (p.getProperty(Velocity.RESOURCE_LOADER) == null) {
             p.setProperty(Velocity.RESOURCE_LOADER, "wwfile, wwclass");
         }
 
         /**
- * If there's a "real" path add it for the wwfile resource loader.
- * If there's no real path and they haven't configured a loader then we change
- * resource loader property to just use the wwclass loader
- * Ben Hall (22/08/2003)
- */
+         * If there's a "real" path add it for the wwfile resource loader.
+         * If there's no real path and they haven't configured a loader then we change
+         * resource loader property to just use the wwclass loader
+         * Ben Hall (22/08/2003)
+         */
         if (context.getRealPath("") != null) {
             p.setProperty("wwfile.resource.loader.description", "Velocity File Resource Loader");
             p.setProperty("wwfile.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
         }
 
         /**
- * Refactored the Velocity templates for the WebWork taglib into the classpath from the web path.  This will
- * enable WebWork projects to have access to the templates by simply including the WebWork jar file.
- * Unfortunately, there does not appear to be a macro for the class loader keywords
- * Matt Ho - Mon Mar 17 00:21:46 PST 2003
- */
+         * Refactored the Velocity templates for the WebWork taglib into the classpath from the web path.  This will
+         * enable WebWork projects to have access to the templates by simply including the WebWork jar file.
+         * Unfortunately, there does not appear to be a macro for the class loader keywords
+         * Matt Ho - Mon Mar 17 00:21:46 PST 2003
+         */
         p.setProperty("wwclass.resource.loader.description", "Velocity Classpath Resource Loader");
         p.setProperty("wwclass.resource.loader.class", "com.opensymphony.webwork.views.velocity.WebWorkResourceLoader");
 
         /**
- * the TagDirective and BodyTagDirective must be added to the userdirective
- * TODO: there must be a better place for this... LIKE A CONFIG FILE IN THE JAR
- * TODO: ... also, people must be allowed to define their own config that overrides this
- */
+         * the TagDirective and BodyTagDirective must be added to the userdirective
+         * TODO: there must be a better place for this... LIKE A CONFIG FILE IN THE JAR
+         * TODO: ... also, people must be allowed to define their own config that overrides this
+         */
         String userdirective = p.getProperty("userdirective");
         String directives = "com.opensymphony.webwork.views.velocity.ParamDirective" + "," + "com.opensymphony.webwork.views.velocity.TagDirective" + "," + "com.opensymphony.webwork.views.velocity.BodyTagDirective";
         p.setProperty("velocimacro.library", "webwork.vm");

File src/java/com/opensymphony/webwork/views/velocity/ViewToolsBean.java

View file
  • Ignore whitespace
+package com.opensymphony.webwork.views.velocity;
+
+import com.opensymphony.util.TextUtils;
+
+import java.net.URLEncoder;
+
+/**
+ * A simple bean with some useful utility methods for use in Velocity views.
+ *
+ * It's added as $tools in the Velocity context.
+ */
+public class ViewToolsBean
+{
+    public String urlEncode(String s)
+    {
+        return URLEncoder.encode(s);
+    }
+
+    public String htmlEncode(String s)
+    {
+        return TextUtils.htmlEncode(s);
+    }
+
+    public String textToHtml(String s)
+    {
+        return TextUtils.plainTextToHtml(s);
+    }
+}