cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Boon Hian Tek <b...@bhtek.dyndns.org>
Subject [Proposal]: Additional features for org.apache.cocoon.transformation.pagination.Paginator
Date Thu, 17 Apr 2003 00:56:48 GMT
Hi fellow cocoon users,

This is my first time posting here.
Just started using cocoon few days ago. it is great.
I made some minor modifications to the Paginator for my own use.
The range-link feature is awesome, but I like to have choices when I do 
use them.
I made it so that we can have more than one range-links.

Well, here are the tweaks... let me know if anyone is interested in the 
changes.

By the way, (just to vent) SURE HARD TO use cocoon 2.1 with Tomcat 4.1.
Fell back to the default jetty and just changing the webapp dir.

<snip>
Index: 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/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
--- 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/PageRules.java   
 16 Mar 2003 18:03:55 -0000    1.2
+++ 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/PageRules.java   
 17 Apr 2003 00:54:23 -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: 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/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
--- 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/Pagesheet.java   
 16 Mar 2003 18:03:55 -0000    1.2
+++ 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/Pagesheet.java   
 17 Apr 2003 00:54:23 -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 {
@@ -272,7 +288,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.");
                         }
Index: 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/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
--- 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/Paginator.java   
 19 Mar 2003 15:42:16 -0000    1.3
+++ 
src/scratchpad/src/org/apache/cocoon/transformation/pagination/Paginator.java   
 17 Apr 2003 00:54:23 -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 {
</snip>

Boon Hian Tek
New Cocoon User

--
An editor is one who separates the wheat from the chaff and prints the 
chaff.
                -- Adlai Stevenson


Mime
View raw message