You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/12/31 02:31:10 UTC

svn commit: r730292 - in /velocity/tools/trunk: examples/showcase/ examples/showcase/WEB-INF/src/ src/main/java/org/apache/velocity/tools/view/ src/main/java/org/apache/velocity/tools/view/i18n/

Author: nbubna
Date: Tue Dec 30 17:31:09 2008
New Revision: 730292

URL: http://svn.apache.org/viewvc?rev=730292&view=rev
Log:
turn MultiViewsTool into a somewhat more useful IncludeTool that can check for resource existance, not just help with i18n templates

Added:
    velocity/tools/trunk/examples/showcase/include.vm   (with props)
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/IncludeTool.java
      - copied, changed from r729996, velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/MultiViewsTool.java
Removed:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/MultiViewsTool.java
Modified:
    velocity/tools/trunk/examples/showcase/WEB-INF/src/resources.properties
    velocity/tools/trunk/examples/showcase/toolmenu.vm
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/tools.xml

Modified: velocity/tools/trunk/examples/showcase/WEB-INF/src/resources.properties
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/examples/showcase/WEB-INF/src/resources.properties?rev=730292&r1=730291&r2=730292&view=diff
==============================================================================
--- velocity/tools/trunk/examples/showcase/WEB-INF/src/resources.properties (original)
+++ velocity/tools/trunk/examples/showcase/WEB-INF/src/resources.properties Tue Dec 30 17:31:09 2008
@@ -49,7 +49,7 @@
 as some best practices for using them with the VelocityView servlets.
 index.start = To begin, choose a tool from the menu.
 index.asterisk = With the exception of the way we abuse the RenderTool and \
-Velocimacros. They've been stretched pretty far in order to allow the content \
+Velocimacros. They''ve been stretched pretty far in order to allow the content \
 to be very rapidly developed and more user-interactive than is usually needed. \
 This has entailed the loss of some performance and much clarity.  As such, the \
 way we use these two facilities should probably not be considered a best practice. :)
@@ -73,6 +73,7 @@
 tools.display = DisplayTool
 tools.esc = EscapeTool
 tools.field = FieldTool
+tools.include = IncludeTool
 tools.import = ImportTool
 tools.link = LinkTool
 tools.lists = ListTool
@@ -116,10 +117,10 @@
 alternator.make = Returns a new, default Alternator that alternates over the specified values.
 alternator.make_booleanObjectVarArgs = Returns a new Alternater that alternates of the specified values using the specified auto-alternate setting.
 alternator.auto = Returns a new, automatically alternating Alternator that alternates over the specified values.
-alternator.auto_ObjectVarArgs.param1 = ['b','c','a']
+alternator.auto_ObjectVarArgs.param1 = [''b'',''c'',''a'']
 alternator.manual = Returns a new, manually alternating Alternator that alternates over the specified values.
-alternator.manual_ObjectVarArgs.param1 = ['red','green']
-alternator.custom = $alternator.make(['b','c','a'])
+alternator.manual_ObjectVarArgs.param1 = [''red'',''green'']
+alternator.custom = $alternator.make([''b'',''c'',''a''])
 
 # class.vm resources
 class.intro = By default, the ClassTool is set to inspect \
@@ -141,7 +142,7 @@
 class.getShowDeprecated = Returns the current showDeprecated setting.
 class.getSafeMode = Returns the current safeMode setting.
 class.getType = Returns the actual <code>Class</code> being inspected.
-class.getTypes = Returns a <code>Set</code> of all <code>Class</code>es that are part of the signatures (i.e. parameters or return types) of the inspected Class's methods, constructors and fields.
+class.getTypes = Returns a <code>Set</code> of all <code>Class</code>es that are part of the signatures (i.e. parameters or return types) of the inspected Class''s methods, constructors and fields.
 class.inspectSuper = Returns a new ClassTool instance that inspects the immediate superclass of the class being inspected.
 class.inspect_Class = Returns a new ClassTool instance that inspects the specified Class.
 class.inspect_Class.param1 = $class.class
@@ -149,7 +150,7 @@
 class.inspect_Object.param1 = $class
 class.inspect_String = Returns a new ClassTool instance that inspects the Class for the specified class name \
     or <code>null</code> if no class can be found for the specified name.
-class.inspect_String.param1 = 'org.apache.velocity.tools.generic.ClassTool'
+class.inspect_String.param1 = ''org.apache.velocity.tools.generic.ClassTool''
 class.isAbstract = Returns <code>true</code> if the class being inspected is abstract.
 class.isFinal = Returns <code>true</code> if the class being inspected is final.
 class.isInterface = Returns <code>true</code> if the class being inspected is an interface.
@@ -174,7 +175,7 @@
 display.intro = This tool provides a variety of methods for controlling the output \
     displayed by various references in your templates.
 display.list = Formats a collection or array into the form "A, B and C".
-display.list_Object.param1 = ['A','B','C']
+display.list_Object.param1 = [''A'',''B'',''C'']
 display.list_ObjectString = Formats a collection or array into a string delimited by the second argument
 display.list_ObjectStringString = Formats a collection or array into a string delimited by the second argument, except the last two arguments are delimited by the third argument
 display.truncate = Limits the string output of the specified value to the configured max length in characters (default is 30 characters). If the string gets curtailed, the configured suffix (default is "...") is used as the ending of the truncated string.
@@ -183,13 +184,13 @@
 display.truncate_ObjectintString = Limits the string output of the second argument to the specified number of characters. If the string gets curtailed, the specified suffix is used as the ending of the truncated string.
 display.alt = Returns the configured default value ("null" by default) if the specified value is null.
 display.alt_ObjectObject = Returns the second argument if first argument specified is null.
-display.cell = Truncates or pads the string value of the specified object as necessary to ensure that the returned string's length equals the default cell size.
-display.cell_Objectint = Truncates or pads the string value of the specified object as necessary to ensure that the returned string's length equals the specified cell size.
-display.cell_ObjectString = Truncates or pads the string value of the specified object as necessary to ensure that the returned string's length equals the default cell size. If truncation is necessary, the specified suffix will replace the end of the string value to indicate that.
-display.cell_ObjectintString = Truncates or pads the string value of the specified object as necessary to ensure that the returned string's length equals the specified cell size. If truncation is necessary, the specified suffix will replace the end of the string value to indicate that.
+display.cell = Truncates or pads the string value of the specified object as necessary to ensure that the returned string''s length equals the default cell size.
+display.cell_Objectint = Truncates or pads the string value of the specified object as necessary to ensure that the returned string''s length equals the specified cell size.
+display.cell_ObjectString = Truncates or pads the string value of the specified object as necessary to ensure that the returned string''s length equals the default cell size. If truncation is necessary, the specified suffix will replace the end of the string value to indicate that.
+display.cell_ObjectintString = Truncates or pads the string value of the specified object as necessary to ensure that the returned string''s length equals the specified cell size. If truncation is necessary, the specified suffix will replace the end of the string value to indicate that.
 display.message = Uses <code>java.text.MessageFormat</code> to format the specified String with the specified arguments. If there are no arguments, then the String is returned directly.
-display.message_StringObjectVarArgs.param1 = 'foo {1} {0}'
-display.message_StringObjectVarArgs.param2 = ['bar', 2]
+display.message_StringObjectVarArgs.param1 = ''foo {1} {0}''
+display.message_StringObjectVarArgs.param2 = [''bar'', 2]
 display.space = Returns a string of spaces of the specified length.
 display.space_int.param1 = 4
 display.capitalize = Changes the first character of the string value of the specified object to upper case and returns the resulting string.
@@ -206,15 +207,43 @@
 # field.vm resources
 field.intro = This tool allows easy access to public static fields in classes,  such as string constants.
 field.get_String = Returns the value of the field with the specified name, if found.
-field.get_String.param1 = 'java.lang.Boolean.FALSE'
+field.get_String.param1 = ''java.lang.Boolean.FALSE''
 field.in_Class = Loads all public static fields in the specified class.
 field.in_Class.param1 = $date.class
 field.in_Object = Loads all public static fields in the Class of the specified object.
 field.in_Object.param1 = 1
 field.in_String = Looks for a class with the specified name; if found, loads all public static fields.
-field.in_String.param1 = 'java.lang.Float'
+field.in_String.param1 = ''java.lang.Float''
 field.custom = $field.in($field).INCLUDE_KEY
 
+# include.vm resources
+include.intro = Allows for transparent content negotiation in a manner mimicking Apache httpd''s \
+<a href="http://httpd.apache.org/docs-2.0/content-negotiation.html">MultiViews</a>. \
+Reads the default language out of the ViewToolContext as \
+<code>org.apache.velocity.tools.view.i18n.defaultLanguage</code>. \
+This is the successor to the MultiViewsTool in VelocityTools 1.x. \
+Please note that it does NOT do the actual #include or #parse for you, \
+but is merely to aid in include content negotiation.
+include.exists_String = Checks whether or not a resource with the specified name is available.
+include.exists_String.param1 = ''include.vm''
+include.exists_StringString = Checks whether or not a resource is available with the specified name and language suffix.
+include.exists_StringString.param1 = ''include.vm''
+include.exists_StringString.param2 = ''de''
+include.find_String = Returns the localized name for the specified resource.
+include.find_String.param1 = ''demo.vm''
+include.find_StringLocale = Returns the localized name for the specified resource, \
+with the specified Locale's language suffix. If one for that language is unavailable, \
+then it will "widen" the langauge until one is found.  If none is found at all, \
+the name parameter is returned.
+include.find_StringLocale.param1 = ''demo.vm''
+include.find_StringLocale.param2 = $text.locale
+include.find_StringString = Returns the localized name for the specified resource, \
+with the requested language suffix. If one for that language is unavailable, \
+then it will "widen" the langauge until one is found.  If none is found at all, \
+the name parameter is returned.
+include.find_StringString.param1 = ''demo.vm''
+include.find_StringString.param2 = ''fr''
+
 # loop.vm resources
 loop.intro = This tool is a convenience tool to use with #foreach loops. It wraps a \
 list to let you prematurely stop iterating, skip iterations, sync iteration over \
@@ -227,25 +256,25 @@
 xml.intro = Tool for reading/navigating XML files.  This uses dom4j under the \
 covers to provide complete XPath support for traversing XML files.  \
 Most of the methods below will do nothing, as the default tool does not \
-have any XML content to work with.  For demo's sake, there is a <code>file.xml</code> \
+have any XML content to work with.  For demo''s sake, there is a <code>file.xml</code> \
 in the classpath that is automatically loaded for you to work with, \
-and we have turned of safe mode, so you can do things like \
-<code>$xml.read('tools.xml')</code>. \
+and we have turned off safe mode, so you can do things like \
+<code>$xml.read(''tools.xml'')</code>. \
 Still, the full demo at the bottom will likely be the most useful for you.
 xml.read_Object.param1 = ''file.xml''
 xml.read_Object = Returns <code>null</code> if safe mode is on; otherwise \
 this will accept url pointing to an XML document.  It will then parse that document \
-and return a new instance with that document's root element as its node.
+and return a new instance with that document''s root element as its node.
 xml.parse_Object.param1 = ''<test><this>that</this></test>''
 xml.parse_Object = Accepts XML strings.  If the XML is valid, it will return a \
-new XmlTool instance with the XML's root as its internal node.
+new XmlTool instance with the XML''s root as its internal node.
 xml.get_Object.param1 = ''bar[2]''
 xml.get_Object = This will first attempt to find an attribute with the \
 specified name and return its value.  If no such attribute \
 exists or its value is <code>null</code>, this will attempt to convert \
 the given value to a Number and get the result of get(Number).  If the number conversion fails, \
 then this will convert the object to a string. If that string \
-does not start contain a '/', it appends the result of getPath() and a '/' to the front of it. \
+does not start contain a ''/'', it appends the result of getPath() and a ''/'' to the front of it. \
 Finally, it delegates the string to the find(String) method and returns the result of that.
 xml.getName = Asks <code>get(Object)</code> for "name". If none, this will return the result of getNodeName().
 xml.getNodeName = Returns the name of the root node. If the internal Node \
@@ -259,18 +288,18 @@
 xml.isEmpty = Returns <code>true</code> if there are no Nodes internally held by this instance. 
 xml.size = Returns the number of Nodes internally held by this instance. 
 xml.iterator = Returns an Iterator that returns new XmlTool instances for each Node held internally by this instance.
-xml.getFirst = Returns a new instance that wraps only the first Node from this instance's internal Node list.
-xml.getLast = Returns a new instance that wraps only the last Node from this instance's internal Node list.
+xml.getFirst = Returns a new instance that wraps only the first Node from this instance''s internal Node list.
+xml.getLast = Returns a new instance that wraps only the last Node from this instance''s internal Node list.
 xml.get_Number.param1 = 0
-xml.get_Number = Returns a new instance that wraps the specified Node from this instance's internal Node list.
-xml.node = Returns the first/sole Node from this instance's internal Node list, if any.
+xml.get_Number = Returns a new instance that wraps the specified Node from this instance''s internal Node list.
+xml.node = Returns the first/sole Node from this instance''s internal Node list, if any.
 xml.find_Object.param1 = ''/*/a[@*]''
 xml.find_Object = Converts the specified object to a String and calls <code>find(String)</code> with that.
 xml.find_String.param1 = ''@baz''
 xml.find_String = Performs an XPath selection on the current set of \
 Nodes held by this instance and returns a new instance that wraps those results. \
 If the specified value is null or this instance does not currently hold any nodes, then this will return  \
-<code>null</code>.  If the specified value, when converted to a string, does not contain a '/' character, then \
+<code>null</code>.  If the specified value, when converted to a string, does not contain a ''/'' character, then \
 it has "//" prepended to it.  This means that a call to <code>$xml.find("a")</code> is equivalent to calling \
 <code>$xml.find("//a")</code>.  The full range of XPath selectors is supported here.
 xml.getParent = Returns a new XmlTool instance that wraps the parent Element of the first/sole Node \

Added: velocity/tools/trunk/examples/showcase/include.vm
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/examples/showcase/include.vm?rev=730292&view=auto
==============================================================================
--- velocity/tools/trunk/examples/showcase/include.vm (added)
+++ velocity/tools/trunk/examples/showcase/include.vm Tue Dec 30 17:31:09 2008
@@ -0,0 +1,32 @@
+## Licensed to the Apache Software Foundation (ASF) under one
+## or more contributor license agreements.  See the NOTICE file
+## distributed with this work for additional information
+## regarding copyright ownership.  The ASF licenses this file
+## to you under the Apache License, Version 2.0 (the
+## "License"); you may not use this file except in compliance
+## with the License.  You may obtain a copy of the License at
+##
+##   http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing,
+## software distributed under the License is distributed on an
+## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+## KIND, either express or implied.  See the License for the
+## specific language governing permissions and limitations
+## under the License.
+#title( 'IncludeTool' )
+<p>
+$text.demo.thisPage.insert("#doclink( 'IncludeTool' false )").
+$text.get('include.intro')
+</p>
+
+#set( $toolname = 'include' )
+#set( $toolclass = $include.class )
+#set( $toollink = $doclink )
+#set( $toolDemo = 
+"${esc.d}include.exists('include.vm')
+${esc.d}include.exists('foo.vm')
+${esc.h}${esc.h}this demo is currently poor because we don't have i18n templates!"
+)
+
+#parse( 'demo.vm' )

Propchange: velocity/tools/trunk/examples/showcase/include.vm
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/tools/trunk/examples/showcase/include.vm
------------------------------------------------------------------------------
    svn:executable = *

Propchange: velocity/tools/trunk/examples/showcase/include.vm
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: velocity/tools/trunk/examples/showcase/toolmenu.vm
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/examples/showcase/toolmenu.vm?rev=730292&r1=730291&r2=730292&view=diff
==============================================================================
--- velocity/tools/trunk/examples/showcase/toolmenu.vm (original)
+++ velocity/tools/trunk/examples/showcase/toolmenu.vm Tue Dec 30 17:31:09 2008
@@ -33,6 +33,7 @@
 #toolMenuItem( $llink 'esc' )
 #toolMenuItem( $llink 'field' )
 #toolMenuItem( $llink 'import' )
+#toolMenuItem( $llink 'include' )
 #toolMenuItem( $llink 'link' )
 #toolMenuItem( $llink 'lists' )
 #toolMenuItem( $llink 'loop' )

Copied: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/IncludeTool.java (from r729996, velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/MultiViewsTool.java)
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/IncludeTool.java?p2=velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/IncludeTool.java&p1=velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/MultiViewsTool.java&r1=729996&r2=730292&rev=730292&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/MultiViewsTool.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/IncludeTool.java Tue Dec 30 17:31:09 2008
@@ -23,6 +23,7 @@
 import java.util.Map;
 import javax.servlet.ServletContext;
 import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.exception.ResourceNotFoundException;
 import org.apache.velocity.tools.Scope;
 import org.apache.velocity.tools.config.DefaultKey;
 import org.apache.velocity.tools.config.InvalidScope;
@@ -35,16 +36,21 @@
  *
  * <p>Reads the default language out of the ViewToolContext as
  * <code>org.apache.velocity.tools.view.i18n.defaultLanguage</code>.
- * See {@link #find(String, String)} and {@link
- * #find(String, Locale)} for usage.</p>
+ * See {@link #find(String, String)}, {@link
+ * #find(String, Locale)} and {@link #exists(String)} for usage.</p>
+ *
+ * <p>This is the successor to the MultiViewsTool in VelocityTools 1.x.
+ * Please note that it does NOT do the actual #include or #parse for
+ * you, but is merely to aid in include content negotiation.</p>
  *
  * @version $Id$
  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
+ * @author Nathan Bubna
  * @since VelocityTools 2.0
  */
-@DefaultKey("i18n")
+@DefaultKey("include")
 @InvalidScope(Scope.APPLICATION)
-public class MultiViewsTool
+public class IncludeTool
 {
     /**
      * The key used to search initialization, context, and JVM
@@ -127,9 +133,9 @@
      *
      * <p>Usage from a template would be something like the following:
      * <blockquote><code><pre>
-     * #parse( $i18n.find('header.vm', 'en') )
-     * #include( $i18n.find('my_page.html', 'en') )
-     * #parse( $i18n.find('footer.vm', 'en') )
+     * #parse( $include.find('header.vm', 'en') )
+     * #include( $include.find('my_page.html', 'en') )
+     * #parse( $include.find('footer.vm', 'en') )
      * </pre></code></blockquote>
      *
      * You might also wrap this method using another pull/view tool
@@ -144,8 +150,7 @@
     public String find(String name, String language)
     {
         String localizedName = name + '.' + language;
-        // templateExists() checks for static content as well
-        if (!engine.resourceExists(localizedName))
+        if (!exists(localizedName))
         {
             // Fall back to the default lanaguage.
             String defaultLangSuffix = '.' + defaultLanguage;
@@ -157,7 +162,7 @@
             else
             {
                 localizedName = name + defaultLangSuffix;
-                if (!engine.resourceExists(localizedName))
+                if (!exists(localizedName))
                 {
                     localizedName = name;
                 }
@@ -166,4 +171,43 @@
         return localizedName;
     }
 
+    /**
+     * <p>Checks to see whether a #parse-able template or
+     * #include-able resource exists under the specified name/path.</p>
+     *
+     * <p>Usage from a template would be something like the following:
+     * <blockquote><code><pre>
+     * #if( $include.exists('header.vm') )
+     *   #parse( 'header.vm' )
+     * #end
+     * </pre></code></blockquote>
+     *
+     * @see VelocityEngine#resourceExists
+     */
+    public boolean exists(String name)
+    {
+        try
+        {
+            // checks for both templates and static content
+            return engine.resourceExists(name);
+        }
+        // make sure about this...
+        catch (ResourceNotFoundException rnfe)
+        {
+            return false;
+        }
+    }
+
+    /**
+     * Checks to see whether a localized version of the
+     * named template exists for the specified language.
+     *
+     * @see #exists(String)
+     */
+    public boolean exists(String name, String language)
+    {
+        String localizedName = name + '.' + language;
+        return exists(localizedName);
+    }
+
 }

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java?rev=730292&r1=730291&r2=730292&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/i18n/MultiViewsTool.java Tue Dec 30 17:31:09 2008
@@ -20,13 +20,14 @@
  */
 
 import java.util.Locale;
+import org.apache.velocity.tools.view.IncludeTool;
 import org.apache.velocity.tools.view.ViewToolContext;
 
 /**
- * Use {@link org.apache.velocity.tools.view.MultiViewsTool}.
+ * Use {@link org.apache.velocity.tools.view.IncludeTool}.
  */
 @Deprecated
-public class MultiViewsTool extends org.apache.velocity.tools.view.MultiViewsTool
+public class MultiViewsTool extends IncludeTool
 {
     @Deprecated
     public void init(Object obj)

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/tools.xml
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/tools.xml?rev=730292&r1=730291&r2=730292&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/tools.xml (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/tools.xml Tue Dec 30 17:31:09 2008
@@ -24,6 +24,7 @@
     <toolbox scope="request">
         <tool class="org.apache.velocity.tools.view.CookieTool"/>
         <tool class="org.apache.velocity.tools.view.ImportTool"/>
+        <tool class="org.apache.velocity.tools.view.IncludeTool"/>
         <tool class="org.apache.velocity.tools.view.LinkTool"/>
         <tool class="org.apache.velocity.tools.view.PagerTool"/>
         <tool class="org.apache.velocity.tools.view.ParameterTool"/>