You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Boon Hian Tek <bt...@bhtek.dyndns.org> on 2003/04/17 05:01:50 UTC

[Bugfix] Bug #13685 - Paginator produces wrong navigation

Hi,

I am not sure if I am fixing the right problem since the description of the
bug was kind of confusing to me.

I ran into similar problems myself, found the code a little confusing,
no offence, Stefano.
Changed it so that adding new page happens when requred (@
processStart instead of @processEnd).

Not really tested hard yet, but seems to be working.

Comments?

Boon

P.S. The diff include "suggestions" I made in a previous email. I included
it because otherwise the code will not compile, sorry. The change for 
the bug
fix are mainly in Pagesheet.java only.

Index: PageRules.java
===================================================================
RCS file: 
/home/cvspublic/cocoon-2.1/src/scratchpad/src/org/apache/cocoon/transformation/pagination/PageRules.java,v
retrieving revision 1.2
diff -u -r1.2 PageRules.java
--- PageRules.java    16 Mar 2003 18:03:55 -0000    1.2
+++ PageRules.java    17 Apr 2003 02:53:32 -0000
@@ -51,6 +51,9 @@
 
 package org.apache.cocoon.transformation.pagination;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Container class for the immutable pagination rules for each page.
  *
@@ -64,7 +67,7 @@
     public int elementCount = 0;
     public int charCount = 0;
     public int unitLinks = 0;
-    public int rangeLink = 0;
+    private List rangeLinks = new ArrayList();
 
     public boolean match(String element, String namespace) {
         boolean elementMatches = ((this.elementName != null) && 
this.elementName.equals(element));
@@ -77,5 +80,21 @@
 
     public boolean match(String namespace) {
         return ((this.elementURI != null) && 
(this.elementURI.equals(namespace)));
+    }
+   
+    public Integer[] getRangeLinks() {
+        return (Integer[]) this.rangeLinks.toArray(new 
Integer[this.rangeLinks.size()]);
+    }
+   
+    public void addRangeLink(Integer rangeLink) {
+        this.rangeLinks.add(rangeLink);
+    }
+   
+    public void addRangeLink(int rangeLink) {
+        this.addRangeLink(new Integer(rangeLink));
+    }
+   
+    public void addRangeLink(String rangeLink) {
+        this.addRangeLink(new Integer(rangeLink));
     }
 }
Index: Pagesheet.java
===================================================================
RCS file: 
/home/cvspublic/cocoon-2.1/src/scratchpad/src/org/apache/cocoon/transformation/pagination/Pagesheet.java,v
retrieving revision 1.2
diff -u -r1.2 Pagesheet.java
--- Pagesheet.java    16 Mar 2003 18:03:55 -0000    1.2
+++ Pagesheet.java    17 Apr 2003 02:53:33 -0000
@@ -100,6 +100,15 @@
      <link type="unit" num="5"/>
      <link type="range" value="20"/>
    </rules>
+   <rules>
+     <count type="element" name="file" 
namespace="http://apache.org/cocoon/directory/2.0" num="16"/>
+     <link type="unit" num="5"/>
+     <link type="range" value="2"/>
+     <link type="range" value="5"/>
+     <link type="range" value="10"/>
+     <link type="range" value="20"/>
+     <link type="range" value="100"/>
+   </rules>
   </pagesheet>
 
 which indicates that:
@@ -113,6 +122,13 @@
 
  3) for the rest of the pages, there are three unit links (-3 -2 -1 0 
+1 +2 +3)
     and range goes 20 (so +20 and -20).
+   
+ 4) if more than one ranges are defined, range links will be created in 
sequence
+
+ 5) range links will be from big to small (eg. 20, 10, then 5) for 
backward links,
+    range links will be from small to big (eg. 5, 10, then 20) for 
forward links
+   
+ 6) range link(s) will have an attribute 'range' to indicate the range size
 
 */
 public class Pagesheet extends DefaultHandler implements Cloneable, 
Modifiable {
@@ -132,7 +148,7 @@
 
     // Runtime information
     private ResizableContainer pages;
-    private Page currentPage = new Page(1, 1);
+    private Page currentPage = null;
     private int pageCounter = 1;
     private int elementCounter = 0;
     private int descendant = 0;
@@ -142,9 +158,14 @@
         public int elementEnd;
         public int characters;
 
-        public Page(int elementStart, int elementEnd) {
+        public Page(PageRules rules, int elementStart) {
             this.elementStart = elementStart;
-            this.elementEnd = elementEnd;
+           
+            if (rules.elementCount > 0) {
+                this.elementEnd = this.elementStart + 
rules.elementCount - 1;
+            } else {
+                this.elementEnd = this.elementStart + 1;
+            }
         }
 
         public boolean validInPage(int elementCounter) {
@@ -272,7 +293,7 @@
                         }
                     } else if (a.getValue("type").equals("range")) {
                         try {
-                            rules.rangeLink = 
Integer.parseInt(a.getValue("value"));
+                            rules.addRangeLink(a.getValue("value"));
                         } catch (NumberFormatException e) {
                             throw new SAXException("Syntax error: the 
attribute 'link/@value' must contain a number.");
                         }
@@ -323,15 +344,18 @@
         if (rules.match(name, uri)) {
             elementCounter++;
             descendant++;
-            if (currentPage == null || elementCounter == 
currentPage.elementStart) {
-                // System.out.println(">>>> " + pageCounter + ": 
Starting page!!!");
-                if (rules.elementCount > 0) {
-                    currentPage.elementEnd = currentPage.elementStart + 
rules.elementCount - 1;
-                } else {
-                    currentPage.elementEnd = currentPage.elementStart + 1;
-                }
-                pages.set(pageCounter, currentPage);
-            }
+           
+            if (currentPage == null) {
+                currentPage = new Page(rules, 1);
+            }
+           
+            if (elementCounter > currentPage.elementEnd) {
+                System.out.println(">>>> " + pageCounter + ": Starting 
new page!!! >>> " + elementCounter);
+                pageCounter++;
+                currentPage = new Page(rules, currentPage.elementEnd + 1);
+            }
+           
+            pages.set(pageCounter, currentPage);
         }
 
         if (itemGroupsPerElement != null) {
@@ -358,12 +382,6 @@
             } else if (rules.elementCount == 0) {
                 // No limit on elements is specified, and limit on 
characters is not reached yet.
                 currentPage.elementEnd ++;
-            }
-
-            if (elementCounter == currentPage.elementEnd) {
-                System.out.println(">>>> " + pageCounter + ": Ending 
page!!!");
-                pageCounter++;
-                currentPage = new Page(currentPage.elementEnd + 1, 0);
             }
         }
     }
Index: Paginator.java
===================================================================
RCS file: 
/home/cvspublic/cocoon-2.1/src/scratchpad/src/org/apache/cocoon/transformation/pagination/Paginator.java,v
retrieving revision 1.3
diff -u -r1.3 Paginator.java
--- Paginator.java    19 Mar 2003 15:42:16 -0000    1.3
+++ Paginator.java    17 Apr 2003 02:53:33 -0000
@@ -288,14 +288,23 @@
                     
atts.addAttribute(null,"current-uri","current-uri","CDATA",requestURI);
                     
atts.addAttribute(null,"clean-uri","clean-uri","CDATA",cleanURI(requestURI,page));
                     super.startElement(PAGINATE_URI, "page", 
PAGINATE_PREFIX_TOKEN + "page", atts);
-                    if ((rules.rangeLink > 0) && (page - 
rules.rangeLink >= 1)) {
-                        atts.clear();
-                        
atts.addAttribute(null,"type","type","CDATA","prev");
-                        
atts.addAttribute(null,"uri","uri","CDATA",encodeURI(requestURI,page,page 
- rules.rangeLink));
-                        
atts.addAttribute(null,"page","page","CDATA",String.valueOf(page - 
rules.rangeLink));
-                        super.startElement(PAGINATE_URI, "range-link", 
PAGINATE_PREFIX_TOKEN + "range-link", atts);
-                        super.endElement(PAGINATE_URI, "range-link", 
PAGINATE_PREFIX_TOKEN + "range-link");
+                   
+                    Integer[] rangeLinks = rules.getRangeLinks();
+                   
+                    for (int i = rangeLinks.length - 1; i > -1; i--) {
+                        int rangeLink = rangeLinks[i].intValue();
+                       
+                        if ((rangeLink > 0) && (page - rangeLink >= 1)) {
+                            atts.clear();
+                            
atts.addAttribute(null,"type","type","CDATA","prev");
+                            
atts.addAttribute(null,"range","range","CDATA",rangeLinks[i].toString());
+                            
atts.addAttribute(null,"uri","uri","CDATA",encodeURI(requestURI,page,page 
- rangeLink));
+                            
atts.addAttribute(null,"page","page","CDATA",String.valueOf(page - 
rangeLink));
+                            super.startElement(PAGINATE_URI, 
"range-link", PAGINATE_PREFIX_TOKEN + "range-link", atts);
+                            super.endElement(PAGINATE_URI, 
"range-link", PAGINATE_PREFIX_TOKEN + "range-link");
+                        }
                     }
+                   
                     for (int i = page - rules.unitLinks; i < page; i++) {
                         if (i > 0) {
                             atts.clear();
@@ -316,14 +325,21 @@
                             super.endElement(PAGINATE_URI, "link", 
PAGINATE_PREFIX_TOKEN + "link");
                         }
                     }
-                    if ((rules.rangeLink > 0) && (page + 
rules.rangeLink <= totalPages)) {
-                        atts.clear();
-                        
atts.addAttribute(null,"type","type","CDATA","next");
-                        
atts.addAttribute(null,"uri","uri","CDATA",encodeURI(requestURI,page,page 
+ rules.rangeLink));
-                        
atts.addAttribute(null,"page","page","CDATA",String.valueOf(page + 
rules.rangeLink));
-                        super.startElement(PAGINATE_URI, "range-link", 
PAGINATE_PREFIX_TOKEN + "range-link", atts);
-                        super.endElement(PAGINATE_URI, "range-link", 
PAGINATE_PREFIX_TOKEN + "range-link");
+                   
+                    for (int i = 0; i < rangeLinks.length; i++) {
+                        int rangeLink = rangeLinks[i].intValue();
+                       
+                        if ((rangeLink > 0) && (page + rangeLink <= 
totalPages)) {
+                            atts.clear();
+                            
atts.addAttribute(null,"type","type","CDATA","next");
+                            
atts.addAttribute(null,"range","range","CDATA",rangeLinks[i].toString());
+                            
atts.addAttribute(null,"uri","uri","CDATA",encodeURI(requestURI,page,page 
+ rangeLink));
+                            
atts.addAttribute(null,"page","page","CDATA",String.valueOf(page + 
rangeLink));
+                            super.startElement(PAGINATE_URI, 
"range-link", PAGINATE_PREFIX_TOKEN + "range-link", atts);
+                            super.endElement(PAGINATE_URI, 
"range-link", PAGINATE_PREFIX_TOKEN + "range-link");
+                        }
                     }
+                   
                     super.endElement(PAGINATE_URI, "page", 
PAGINATE_PREFIX_TOKEN + "page");
                     super.endPrefixMapping(PAGINATE_PREFIX);
                 } else {


Re: [Bugfix] Bug #13685 - Paginator produces wrong navigation

Posted by Stefano Mazzocchi <st...@apache.org>.
on 4/17/03 5:01 AM Boon Hian Tek wrote:


> Hi,
> 
> I am not sure if I am fixing the right problem since the description of the
> bug was kind of confusing to me.
> 
> I ran into similar problems myself, found the code a little confusing,
> no offence, Stefano.

nono, you're wrong: the code is not confusing: is a total piece of crap! :-)

So, any patch is great.

-- 
Stefano.