Author: hlship
Date: Thu Nov 13 14:35:44 2008
New Revision: 713845

URL: http://svn.apache.org/viewvc?rev=713845&view=rev
Log:
TAP5-96: Use namespaces in templates to reference components in libraries

Added:
    
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/component_inside_library_namespace.tml
    
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/invalid_library_namespace_path.tml
Modified:
    tapestry/tapestry5/trunk/src/site/apt/guide/templates.apt
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/TemplateParserImpl.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java

Modified: tapestry/tapestry5/trunk/src/site/apt/guide/templates.apt
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/src/site/apt/guide/templates.apt?rev=713845&r1=713844&r2=713845&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/src/site/apt/guide/templates.apt (original)
+++ tapestry/tapestry5/trunk/src/site/apt/guide/templates.apt Thu Nov 13 
14:35:44 2008
@@ -207,8 +207,8 @@
 </t:if>
 +---+
 
-  The parameter element has been deprecated (but is still fully supported, for 
backwards compatibility). The name
-  <parameter namespace> is more concise and readable.
+  The parameter element has been deprecated (but is still fully supported, for 
backwards compatibility). The
+  <parameter namespace> approach is more concise and readable.
 
 
 Expansions
@@ -296,10 +296,37 @@
    In some cases, components require some kind of enclosure; for example, all 
of the field components will throw a runtime exception
    if not enclosed by a Form component.
    
-   It is possible to place Tapestry components in subclasses.  For example, 
your application may have a package org.example.myapp.components.ajax.Dialog.
+   It is possible to place Tapestry components in sub-packages.  For example, 
your application may have a package org.example.myapp.components.ajax.Dialog.
    This component's normal type name is "ajax/dialog" (because it is in the 
ajax subfolder). This name is problematic, as
    it is not valid to define an XML element with an element name 
<<<\<t:ajax/dialog\>>>>.  Instead,
    replace the slashes with periods: <<<\<t:ajax.dialog\>>>>.
+
+Library Namespaces
+
+   If you are using many components from a common Tapestry component library, 
you can use a special namespace to
+   simplify references to those components.
+
+   The special namespace URI <<<tapestry-library:path>>> can be defined; the 
path is a prefix used in conjuction
+   with component element names.
+
+   Borrowing from the above example, all of the following are equivalent:
+
+----
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"; 
xmlns:a="tapestry-library:ajax">
+
+  <t:ajax.dialog/>
+
+  <span t:type="ajax/dialog"/>
+
+  <a:dialog/>
+
+  . . .
+---
+
+  In other words, the path, <code>ajax</code>, from the namespace URI takes 
the place of the path prefixes <code>ajax.</code>
+  seen in the first element, or <code>ajax/</code> seen in the second.  As far 
as Tapestry is concerned, they are all
+  equivalent.
+
    
 Invisible Instrumentation
 

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java?rev=713845&r1=713844&r2=713845&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
 Thu Nov 13 14:35:44 2008
@@ -432,4 +432,9 @@
     {
         return MESSAGES.get("parameter-element-does-not-allow-attributes");
     }
+
+    static String invalidPathForLibraryNamespace(String URI)
+    {
+        return MESSAGES.format("invalid-path-for-library-namespace", URI);
+    }
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/TemplateParserImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/TemplateParserImpl.java?rev=713845&r1=713844&r2=713845&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/TemplateParserImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/TemplateParserImpl.java
 Thu Nov 13 14:35:44 2008
@@ -61,7 +61,20 @@
      * Special namespace used to denote Block parameters to components, as a 
(preferred) alternative to the t:parameter
      * element.  The simple element name is the name of the parameter.
      */
-    public static final String TAPESTRY_PARAMETERS_URI = "tapestry:parameter";
+    private static final String TAPESTRY_PARAMETERS_URI = "tapestry:parameter";
+
+    /**
+     * URI prefix used to identify a Tapestry library, the remainder of the 
URI becomes a prefix on the element name.
+     */
+    private static final String LIB_NAMESPACE_URI_PREFIX = "tapestry-library:";
+
+    /**
+     * Pattern used to parse the path portion of the library namespace URI.  A 
series of simple identifiers with slashes
+     * allowed as seperators.
+     */
+
+    private static final Pattern LIBRARY_PATH_PATTERN = 
Pattern.compile("^[a-z]\\w*(/[a-z]\\w*)*$",
+                                                                        
Pattern.CASE_INSENSITIVE);
 
     private static final Pattern ID_PATTERN = Pattern.compile("^[a-z]\\w*$", 
Pattern.CASE_INSENSITIVE);
 
@@ -363,10 +376,30 @@
             return;
         }
 
+        if (uri != null && uri.startsWith(LIB_NAMESPACE_URI_PREFIX))
+        {
+            startLibraryNamespaceComponent(uri, localName, attributes);
+            return;
+        }
+
+
         startPossibleComponent(attributes, uri, localName, null);
     }
 
     /**
+     * Added in release 5.1.
+     */
+    private void startLibraryNamespaceComponent(String uri, String localName, 
Attributes attributes)
+    {
+        String path = uri.substring(LIB_NAMESPACE_URI_PREFIX.length());
+
+        if (!LIBRARY_PATH_PATTERN.matcher(path).matches())
+            throw new 
RuntimeException(ServicesMessages.invalidPathForLibraryNamespace(uri));
+
+        startPossibleComponent(attributes, uri, localName, path + "/" + 
localName);
+    }
+
+    /**
      * Checks to see if currently inside a t:body element (which should always 
be empty). Content is ignored inside a
      * body. If inside a body, then a warning is logged (but only one warning 
per body element).
      *
@@ -563,7 +596,7 @@
      * @param namespaceURI   the namespace URI for the element (or the empty 
string)
      * @param elementName    the name of the element (to be assigned to the 
new token), may be null for a component in
      *                       the Tapestry namespace
-     * @param identifiedType the type of the element, usually null, but may be 
the component type derived from elewment
+     * @param identifiedType the type of the element, usually null, but may be 
the component type derived from element
      *                       name
      */
     private void startPossibleComponent(Attributes attributes, String 
namespaceURI, String elementName,
@@ -818,6 +851,10 @@
 
         if (uri.equals(TAPESTRY_PARAMETERS_URI)) return;
 
+        // Likewise, the special URI prefix for Tapestry namespaces.
+
+        if (uri.startsWith(LIB_NAMESPACE_URI_PREFIX)) return;
+
         // The prefix may be blank, which happens when the xmlns attribute is 
used to define the
         // namespace for the default namespace, and when a document has an 
explicit DOCTYPE.
 

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties?rev=713845&r1=713844&r2=713845&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
 Thu Nov 13 14:35:44 2008
@@ -98,3 +98,4 @@
 document-missing-html-root=The root element of the rendered document was <%s>, 
not <html>. A root element of <html> is needed when linking JavaScript and 
stylesheet resources.
 add-new-method-conflict=Unable to add new method %s as it already exists.
 parameter-element-does-not-allow-attributes=A block parameter element does not 
allow any additional attributes. The element name defines the parameter name.
+invalid-path-for-library-namespace=The path portion of library namespace URI 
'%s' is not valid: it must be a simple identifier, or a series of identifiers 
seperated by slashes.
\ No newline at end of file

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java?rev=713845&r1=713844&r2=713845&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/TemplateParserImplTest.java
 Thu Nov 13 14:35:44 2008
@@ -607,6 +607,21 @@
         assertEquals(token1.getComponentType(), "subfolder/nifty");
     }
 
+    /**
+     * TAP5-66
+     */
+    @Test
+    public void component_inside_library_namespace()
+    {
+        List<TemplateToken> tokens = 
tokens("component_inside_library_namespace.tml");
+
+        assertEquals(tokens.size(), 4);
+
+        StartComponentToken token1 = get(tokens, 1);
+
+        assertEquals(token1.getComponentType(), "subfolder/nifty");
+    }
+
     @Test
     public void block_element()
     {
@@ -652,7 +667,10 @@
                         3},
 
                 {"parameter_namespace_with_attributes.tml",
-                        
ServicesMessages.parameterElementDoesNotAllowAttributes(), 4}
+                        
ServicesMessages.parameterElementDoesNotAllowAttributes(), 4},
+
+                {"invalid_library_namespace_path.tml",
+                        "The path portion of library namespace URI 
'tapestry-library:subfolder/' is not valid", 2}
         };
     }
 

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/component_inside_library_namespace.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/component_inside_library_namespace.tml?rev=713845&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/component_inside_library_namespace.tml
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/component_inside_library_namespace.tml
 Thu Nov 13 14:35:44 2008
@@ -0,0 +1,3 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"; 
xmlns:s="tapestry-library:subfolder">
+    <s:nifty t:id="foo"/>
+</html>

Added: 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/invalid_library_namespace_path.tml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/invalid_library_namespace_path.tml?rev=713845&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/invalid_library_namespace_path.tml
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/internal/services/invalid_library_namespace_path.tml
 Thu Nov 13 14:35:44 2008
@@ -0,0 +1,3 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"; 
xmlns:s="tapestry-library:subfolder/">
+    <s:nifty t:id="foo"/>
+</html>


Reply via email to