You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by gr...@apache.org on 2007/07/06 03:28:47 UTC

svn commit: r553688 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry/Link.java main/java/org/apache/tapestry/internal/services/LinkImpl.java test/java/org/apache/tapestry/internal/services/LinkImplTest.java

Author: gredler
Date: Thu Jul  5 18:28:46 2007
New Revision: 553688

URL: http://svn.apache.org/viewvc?view=rev&rev=553688
Log:
TAPESTRY-1506: Add support for anchors when generating links.

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Link.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/LinkImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/LinkImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Link.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Link.java?view=diff&rev=553688&r1=553687&r2=553688
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Link.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/Link.java Thu Jul  5 18:28:46 2007
@@ -21,8 +21,8 @@
 
 /**
  * A link is the Tapestry representation of a URL or URI that triggers dynamic behavior. This link
- * is in two parts: a path portion and a set of query parameters. A request for a link will
- * ultimately be recognized by a {@link Dispatcher}.
+ * is in three parts: a path portion, an optional anchor, and a set of query parameters. A request
+ * for a link will ultimately be recognized by a {@link Dispatcher}.
  * <p>
  * Query parameter values are kept separate from the path portion to support encoding those values
  * into hidden form fields (where appropriate).
@@ -30,19 +30,20 @@
 public interface Link
 {
     /**
-     * The names of any additional query parameters for the URI. Query parameters store less regular
-     * or less often used values that can not be expressed in the path. They also are used to store,
-     * or link to, persistent state.
+     * Returns the names of any additional query parameters for the URI. Query parameters store less
+     * regular or less often used values that can not be expressed in the path. They also are used
+     * to store, or link to, persistent state.
      * 
      * @return list of query parameter names, is alphabetical order
      */
     List<String> getParameterNames();
 
     /**
-     * Returns the value of a specifically named query parameter, or null if no such query parameter
-     * is stored in the link.
+     * Returns the value of a specifically named query parameter, or <tt>null</tt> if no such
+     * query parameter is stored in the link.
+     * 
+     * @return the value of the named parameter
      */
-
     String getParameterValue(String name);
 
     /**
@@ -66,6 +67,25 @@
      */
     String toURI();
 
-    /** Returns the link as a redirect URI. The URI includes any query parameters. */
+    /**
+     * Returns the link as a redirect URI. The URI includes any query parameters.
+     */
     String toRedirectURI();
+
+    /**
+     * Returns the link anchor. If this link does not have an anchor, this method returns
+     * <tt>null</tt>.
+     * 
+     * @return the link anchor
+     */
+    String getAnchor();
+
+    /**
+     * Sets the link anchor. Null and empty anchors will be ignored when building the link URI.
+     * 
+     * @param anchor
+     *            the link anchor
+     */
+    void setAnchor(String anchor);
+
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/LinkImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/LinkImpl.java?view=diff&rev=553688&r1=553687&r2=553688
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/LinkImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/LinkImpl.java Thu Jul  5 18:28:46 2007
@@ -20,7 +20,7 @@
 import org.apache.tapestry.services.Response;
 
 /**
- * Starting implementation of {@link Link}. Currently does not support query parameters.
+ * Default implementation of {@link Link}.
  */
 public class LinkImpl implements Link
 {
@@ -32,6 +32,8 @@
 
     private final boolean _forForm;
 
+    private String _anchor;
+
     public LinkImpl(Response encoder, String contextPath, String targetPath)
     {
         this(encoder, contextPath, targetPath, false);
@@ -78,12 +80,27 @@
         builder.append(_contextPath);
         builder.append("/");
         builder.append(_invocation.buildURI(_forForm));
+        if (_anchor != null && _anchor.length() > 0)
+        {
+            builder.append("#");
+            builder.append(_anchor);
+        }
         return builder.toString();
     }
 
     public String toRedirectURI()
     {
         return _response.encodeRedirectURL(buildURI());
+    }
+
+    public String getAnchor()
+    {
+        return _anchor;
+    }
+
+    public void setAnchor(String anchor)
+    {
+        _anchor = anchor;
     }
 
     public ComponentInvocation getInvocation()

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/LinkImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/LinkImplTest.java?view=diff&rev=553688&r1=553687&r2=553688
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/LinkImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/LinkImplTest.java Thu Jul  5 18:28:46 2007
@@ -123,4 +123,60 @@
 
         verify();
     }
+
+    @Test
+    public void url_with_anchor()
+    {
+        url_with_anchor("wilma", "/foo/bar#wilma");
+    }
+
+    @Test
+    public void url_with_null_anchor()
+    {
+        url_with_anchor(null, "/foo/bar");
+    }
+
+    @Test
+    public void url_with_empty_anchor()
+    {
+        url_with_anchor("", "/foo/bar");
+    }
+
+    private void url_with_anchor(String anchor, String url)
+    {
+        Response response = mockResponse();
+
+        train_encodeURL(response, url, ENCODED);
+
+        replay();
+
+        Link link = new LinkImpl(response, "/foo", "bar");
+
+        link.setAnchor(anchor);
+
+        assertEquals(link.toURI(), ENCODED);
+
+        verify();
+    }
+
+    @Test
+    public void url_with_anchor_and_parameters()
+    {
+        Response response = mockResponse();
+
+        train_encodeURL(response, "/foo/bar?barney=rubble&fred=flintstone#wilma", ENCODED);
+
+        replay();
+
+        Link link = new LinkImpl(response, "/foo", "bar");
+
+        link.addParameter("fred", "flintstone");
+        link.addParameter("barney", "rubble");
+        link.setAnchor("wilma");
+
+        assertEquals(link.toURI(), ENCODED);
+
+        verify();
+    }
+
 }