roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r424090 - in /incubator/roller/branches/roller_3.0/src/org/apache/roller: ui/rendering/model/ ui/rendering/servlets/ util/
Date Thu, 20 Jul 2006 22:21:00 GMT
Author: agilliland
Date: Thu Jul 20 15:20:59 2006
New Revision: 424090

URL: http://svn.apache.org/viewvc?rev=424090&view=rev
Log:
adding new URLUtilites class and updating some url construction logic in models and pagers.

- URLModel now uses URLUtilites

- URLModel is now locale sensitive, so it maintains the locale from the request in all urls

- SearchResultsPager now uses URLUtilities class

- WeblogEntriesPagerImpl now uses URLUtilities class

- small tweak to servlets to ensure all rendering servlets include a "weblogRequest" object
in their model init data.


Added:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/util/URLUtilities.java
Modified:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsPager.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/URLModel.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPagerImpl.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/FeedServlet.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PageServlet.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PreviewServlet.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsPager.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsPager.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsPager.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsPager.java
Thu Jul 20 15:20:59 2006
@@ -22,6 +22,7 @@
 import java.util.ResourceBundle;
 import org.apache.roller.pojos.WebsiteData;
 import org.apache.roller.ui.rendering.util.WeblogSearchRequest;
+import org.apache.roller.util.URLUtilities;
 
 
 /**
@@ -37,7 +38,6 @@
     private String      category = null;
     private int         page = 0;
     private boolean     moreResults = false;
-    private URLModel    urlBuilder = null;
     
     private static ResourceBundle bundle =
             ResourceBundle.getBundle("ApplicationResources");
@@ -59,8 +59,6 @@
         
         // does this pager have more results?
         this.moreResults = more;
-        
-        urlBuilder = new URLModel(weblog);
     }
     
     
@@ -81,7 +79,7 @@
     
     public String getNextLink() {
         if(moreResults) {
-            return urlBuilder.search(query, category, page + 1);
+            return URLUtilities.getWeblogSearchURL(weblog, locale, query, category, page
+ 1, false);
         }
         return null;
     }
@@ -95,7 +93,7 @@
 
     public String getPrevLink() {
         if(page > 0) {
-            return urlBuilder.search(query, category, page - 1);
+            return URLUtilities.getWeblogSearchURL(weblog, locale, query, category, page
- 1, false);
         }
         return null;
     }

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/URLModel.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/URLModel.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/URLModel.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/URLModel.java
Thu Jul 20 15:20:59 2006
@@ -18,9 +18,7 @@
 
 package org.apache.roller.ui.rendering.model;
 
-import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
-import java.net.URLEncoder;
 import java.util.HashMap;
 import java.util.Map;
 import javax.servlet.jsp.PageContext;
@@ -30,6 +28,8 @@
 import org.apache.roller.config.RollerRuntimeConfig;
 import org.apache.roller.pojos.WebsiteData;
 import org.apache.roller.ui.core.RequestConstants;
+import org.apache.roller.ui.rendering.util.WeblogRequest;
+import org.apache.roller.util.URLUtilities;
 import org.apache.struts.util.RequestUtils;
 
 
@@ -51,16 +51,13 @@
     private static Log log = LogFactory.getLog(URLModel.class);
     
     private WebsiteData weblog = null;
+    private String locale = null;
     
     /** TODO 3.0: remove dependency on pageContext */
     private PageContext pageContext = null;
     
-    public URLModel() {        
-    }
-
-    public URLModel(WebsiteData weblog) {
-        this.weblog = weblog;
-    }
+    
+    public URLModel() {}
     
     public String getModelName() {
         return "url";
@@ -68,12 +65,15 @@
     
     public void init(Map initData) throws RollerException {
         
-        // need a weblog to base the weblog specific urls off of
-        weblog = (WebsiteData) initData.get("weblog");
-        if(weblog == null) {
-            throw new RollerException("Expected 'weblog' init param!");
+        // need a weblog request so that we can know the weblog and locale
+        WeblogRequest weblogRequest = (WeblogRequest) initData.get("weblogRequest");
+        if(weblogRequest == null) {
+            throw new RollerException("Expected 'weblogRequest' init param!");
         }
         
+        this.weblog = weblogRequest.getWeblog();
+        this.locale = weblogRequest.getLocale();
+        
         // need page context as well :(
         pageContext = (PageContext) initData.get("pageContext");
     }
@@ -114,157 +114,101 @@
             log.error("ERROR forming Struts URL: ", mue);
         }
         return returnURL;
-    }    
+    }
+    
+    
+    public String themeResource(String theme, String filePath) {
+        return getSite()+RollerRuntimeConfig.getProperty("users.themes.path")+"/"+theme+"/"+filePath;
+    }
     
     
     public String getHome() {
-        return weblog.getURL();
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, null, null, -1, false);
     }
     
     
     public String home(int pageNum) {
-        String url = getHome();
-        if(pageNum > 0)
-            url += "?page="+pageNum;
-        return url;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, null, null, pageNum, false);
     }
     
     
-    public String entry(String anchor, String catPath) {
-        String ret = weblog.getURL()+"/entry/"+anchor;
-        if (catPath != null) {
-            ret += "?cat="+catPath;
-        }
-        return ret;
+    public String entry(String anchor) {
+        return URLUtilities.getWeblogEntryURL(weblog, locale, anchor, true);
+    }
+    
+    
+    public String comments(String anchor) {
+        return URLUtilities.getWeblogCommentsURL(weblog, locale, anchor, true);
     }
     
+    
     public String trackback(String anchor) {
-        return weblog.getURL()+"/entry/"+anchor;
+        return URLUtilities.getWeblogEntryURL(weblog, locale, anchor, true);
     }
 
+    
     public String date(String dateString) {
-        return weblog.getURL()+"/date/"+dateString;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, null, dateString, -1,
false);
     }
     
     
     public String date(String dateString, int pageNum) {
-        String url = date(dateString);
-        if(pageNum > 0)
-            url += "?page="+pageNum;
-        return url;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, null, dateString, pageNum,
false);
     }
     
     
     public String category(String catPath) {
-        String cat = catPath;
-        if (cat.length() > 1 && cat.startsWith("/")) cat = cat.substring(1);
-        try {
-            cat = URLEncoder.encode(catPath, "UTF-8");
-        } catch (UnsupportedEncodingException ex) {
-            log.error("url encoding problem", ex);
-        }
-        return weblog.getURL()+"/category/"+cat;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, null, -1, false);
     }
     
     
     public String category(String catPath, int pageNum) {
-        String url = category(catPath);
-        if(pageNum > 0)
-            url += "?page="+pageNum;
-        return url;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, null, pageNum,
false);
     }
     
     
     public String collection(String dateString, String catPath) {
-        String url = null;
-        if(dateString != null && catPath != null) {
-            url = weblog.getURL();
-            url += "?date="+dateString;
-            url += "&cat="+catPath;
-        } else if(dateString != null) {
-            url = date(dateString);
-        } else if(catPath != null) {
-            url = category(catPath);
-        }
-        return url;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, dateString, -1,
false);
     }
     
     
     public String collection(String dateString, String catPath, int pageNum) {
-        String url = null;
-        if(dateString != null && catPath != null) {
-            url = collection(dateString, catPath);
-            if(pageNum > 0)
-                url += "&page="+pageNum;
-        } else if(dateString != null) {
-            url = date(dateString, pageNum);
-        } else if(catPath != null) {
-            url = category(catPath, pageNum);
-        }
-        return url;
+        return URLUtilities.getWeblogCollectionURL(weblog, locale, catPath, dateString, pageNum,
false);
     }
     
     
     public String getSearch() {
-        return weblog.getURL()+"/search";
+        return URLUtilities.getWeblogSearchURL(weblog, locale, null, null, -1, false);
     }
     
+    
     public String search(String query, int pageNum) {
-        return weblog.getURL()+"/search?q="+query+"&page="+pageNum;
+        return URLUtilities.getWeblogSearchURL(weblog, locale, query, null, pageNum, false);
     }
     
+    
     public String search(String query, String catPath, int pageNum) {
-        String ret = weblog.getURL()+"/search?q="+query+"&page="+pageNum;
-        if (catPath != null) {
-            ret += "?cat="+catPath;
-        }
-        return ret;
+        return URLUtilities.getWeblogSearchURL(weblog, locale, query, catPath, pageNum, false);
     }
     
+    
     public String page(String pageLink) {
-        return weblog.getURL()+"/page/"+pageLink;
+        return URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, null, null,
-1, false);
     }
     
     
     public String page(String pageLink, String dateString, String catPath, int pageNum) {
-        String url = page(pageLink);
-        String qString = "?";
-        if(dateString != null) {
-            qString += "date="+dateString;
-        }
-        if(catPath != null) {
-            if(!qString.endsWith("?"))
-                qString += "&";
-            qString += "cat="+catPath;
-        }
-        if(pageNum > 0) {
-            if(!qString.endsWith("?"))
-                qString += "&";
-            qString += "page="+pageNum;
-        }
-        return url;
+        return URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, catPath, dateString,
pageNum, false);
     }
     
     
     public String resource(String filePath) {
-        return weblog.getURL()+"/resource/"+filePath;
-    }
-    
-    
-    public String themeResource(String filePath) {
-        return getSite()+RollerRuntimeConfig.getProperty("users.themes.path")+"/"+weblog.getEditorTheme()+"/"+filePath;
-    }
-    
-    /**
-     * TODO 3.0: eliminate the need for themeName
-     */
-    public String themeResource(String themeName, String filePath) {
-        return getSite()+RollerRuntimeConfig.getProperty("users.themes.path")+"/"+themeName+"/"+filePath;
+        return URLUtilities.getWeblogResourceURL(weblog, filePath, false);
     }
     
     
     public String getRsd() {
-        return weblog.getURL()+"/rsd";
+        return URLUtilities.getWeblogRsdURL(weblog, false);
     }
     
     
@@ -334,102 +278,38 @@
     public class EntryFeedURLS {
         
         public String getRss() {
-            return weblog.getURL()+"/feed/entries/rss";
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "rss", null,
false, false);
         }
         
         public String rss(String catPath, boolean excerpts) {
-            String url = getRss();
-            if(catPath != null) {
-                String cat = catPath;
-                try {
-                    cat = URLEncoder.encode(catPath, "UTF-8");
-                } catch (UnsupportedEncodingException ex) {
-                    log.error("Error encoding url", ex);
-                }
-                url += "?cat="+cat;
-                
-                if(excerpts) {
-                    url += "&excerpts=true";
-                }
-            } else if(excerpts) {
-                url += "?excerpts=true";
-            }
-            return url;
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "rss", catPath,
excerpts, false);
         }
         
         public String getAtom() {
-            return weblog.getURL()+"/feed/entries/atom";
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "atom", null,
false, false);
         }
         
         public String atom(String catPath, boolean excerpts) {
-            String url = getAtom();
-            if(catPath != null) {
-                String cat = catPath;
-                try {
-                    cat = URLEncoder.encode(catPath, "UTF-8");
-                } catch (UnsupportedEncodingException ex) {
-                    log.error("Error encoding url", ex);
-                }
-                url += "?cat="+cat;
-                
-                if(excerpts) {
-                    url += "&excerpts=true";
-                }
-            } else if(excerpts) {
-                url += "?excerpts=true";
-            }
-            return url;
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "entries", "atom", catPath,
excerpts, false);
         }
     }
     
     public class CommentFeedURLS {
         
         public String getRss() {
-            return weblog.getURL()+"/feed/comments/rss";
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "rss", null,
false, false);
         }
         
         public String rss(String catPath, boolean excerpts) {
-            String url = getRss();
-            if(catPath != null) {
-                String cat = catPath;
-                try {
-                    cat = URLEncoder.encode(catPath, "UTF-8");
-                } catch (UnsupportedEncodingException ex) {
-                    log.error("Error encoding url", ex);
-                }
-                url += "?cat="+cat;
-                
-                if(excerpts) {
-                    url += "&excerpts=true";
-                }
-            } else if(excerpts) {
-                url += "?excerpts=true";
-            }
-            return url;
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "rss", catPath,
excerpts, false);
         }
         
         public String getAtom() {
-            return weblog.getURL()+"/feed/comments/atom";
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "atom", null,
false, false);
         }
         
         public String atom(String catPath, boolean excerpts) {
-            String url = getAtom();
-            if(catPath != null) {
-                String cat = catPath;
-                try {
-                    cat = URLEncoder.encode(catPath, "UTF-8");
-                } catch (UnsupportedEncodingException ex) {
-                    log.error("Error encoding url", ex);
-                }
-                url += "?cat="+cat;
-                
-                if(excerpts) {
-                    url += "&excerpts=true";
-                }
-            } else if(excerpts) {
-                url += "?excerpts=true";
-            }
-            return url;
+            return URLUtilities.getWeblogFeedURL(weblog, locale, "comments", "atom", catPath,
excerpts, false);
         }
         
     }

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPagerImpl.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPagerImpl.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPagerImpl.java
Thu Jul 20 15:20:59 2006
@@ -44,6 +44,7 @@
 import org.apache.roller.pojos.WebsiteData;
 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
 import org.apache.roller.util.DateUtil;
+import org.apache.roller.util.URLUtilities;
 import org.apache.roller.util.Utilities;
 
 
@@ -691,26 +692,18 @@
             String             dateString, 
             String             locale) {
         
-        URLModel urlBuilder = new URLModel(website);
         int pageNum = page + pageAdd;
         String catPath = (cat != null) ? cat.getPath() : null;
-        String pageLink = (weblogPage != null) ? weblogPage.getLink() : null;    
+        String pageLink = (weblogPage != null) ? weblogPage.getLink() : null;
+        String anchor = (entry != null) ? entry.getAnchor() : null;
         
         if (weblogPage != null) {
-            return urlBuilder.page(pageLink, dateString, catPath, pageNum);
+            return URLUtilities.getWeblogPageURL(website, locale, pageLink, anchor, dateString,
catPath, pageNum, false);
         } 
         else if (entry != null) {
-            return urlBuilder.entry(entry.getAnchor(), catPath); 
+            return URLUtilities.getWeblogEntryURL(website, locale, anchor, true);
         } 
-        else if (cat != null && dateString == null) {
-            return urlBuilder.category(catPath, page + pageAdd);               
-        } 
-        else if (dateString != null && cat == null) {
-            return urlBuilder.date(dateString, page + pageAdd);          
-        } 
-        else if (dateString != null && cat != null) {
-            return urlBuilder.collection(dateString, catPath, pageNum);
-        }
-        return urlBuilder.home(pageNum);
+        return URLUtilities.getWeblogCollectionURL(website, locale, catPath, dateString,
pageNum, false);            
     }
+    
 } 

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/FeedServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/FeedServlet.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/FeedServlet.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/FeedServlet.java
Thu Jul 20 15:20:59 2006
@@ -175,9 +175,9 @@
             
             // populate the rendering model
             Map initData = new HashMap();
-            initData.put("weblog", weblog);
             initData.put("request", request);
             initData.put("feedRequest", feedRequest);
+            initData.put("weblogRequest", feedRequest);
             
             // Feeds get the weblog specific page model
             ModelLoader.loadFeedModels(model, initData);

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PageServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PageServlet.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PageServlet.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PageServlet.java
Thu Jul 20 15:20:59 2006
@@ -270,9 +270,9 @@
             
             // populate the rendering model
             Map initData = new HashMap();
-            initData.put("weblog", weblog);
             initData.put("request", request);
             initData.put("pageRequest", pageRequest);
+            initData.put("weblogRequest", pageRequest);
             initData.put("pageContext", pageContext);
             
             // Feeds get the weblog specific page model

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PreviewServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PreviewServlet.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PreviewServlet.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/PreviewServlet.java
Thu Jul 20 15:20:59 2006
@@ -140,9 +140,9 @@
             
             // populate the rendering model
             Map initData = new HashMap();
-            initData.put("weblog", weblog);
             initData.put("request", request);
             initData.put("pageRequest", previewRequest);
+            initData.put("weblogRequest", previewRequest);
             initData.put("pageContext", pageContext);
             
             // standard weblog models

Modified: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java?rev=424090&r1=424089&r2=424090&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
(original)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
Thu Jul 20 15:20:59 2006
@@ -117,9 +117,9 @@
             
             // populate the rendering model
             Map initData = new HashMap();
-            initData.put("weblog", weblog);
             initData.put("request", request);
             initData.put("searchRequest", searchRequest);
+            initData.put("weblogRequest", searchRequest);
             initData.put("pageContext", pageContext);
             
             // we need to add a simple page request because the search

Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/util/URLUtilities.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/util/URLUtilities.java?rev=424090&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/util/URLUtilities.java (added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/util/URLUtilities.java Thu
Jul 20 15:20:59 2006
@@ -0,0 +1,313 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import org.apache.roller.pojos.WebsiteData;
+
+
+/**
+ * Utilities class for building urls.  This class is meant to centralize the
+ * logic behind building urls so that logic isn't duplicated throughout the
+ * code.
+ */
+public final class URLUtilities {
+    
+    // non-intantiable
+    private URLUtilities() {}
+    
+    
+    /**
+     * Get root url for a given weblog.  Optionally for a certain locale.
+     */
+    public static final String getWeblogURL(WebsiteData weblog,
+                                            String locale,
+                                            boolean absolute) {
+        
+        String url = null;
+        
+        if(absolute) {
+            url = weblog.getAbsoluteURL();
+        } else {
+            url = weblog.getURL();
+        }
+        
+        if (locale != null) {
+            url += "/"+locale;
+        }
+        
+        return url;
+    }
+    
+    
+    /**
+     * Get url for a single weblog entry on a given weblog.
+     */
+    public static final String getWeblogEntryURL(WebsiteData weblog,
+                                                 String locale,
+                                                 String entryAnchor,
+                                                 boolean absolute) {
+        
+        StringBuffer url = new StringBuffer();
+        
+        url.append(getWeblogURL(weblog, locale, absolute));
+        url.append("/entry/").append(encode(entryAnchor));
+        
+        return url.toString();
+    }
+    
+    
+    /**
+     * Get url for a single weblog entry comments on a given weblog.
+     */
+    public static final String getWeblogCommentsURL(WebsiteData weblog,
+                                                    String locale,
+                                                    String entryAnchor,
+                                                    boolean absolute) {
+        
+        return getWeblogEntryURL(weblog, locale, entryAnchor, absolute)+"#comments";
+    }
+    
+    
+    /**
+     * Get url for a collection of entries on a given weblog.
+     */
+    public static final String getWeblogCollectionURL(WebsiteData weblog,
+                                                      String locale,
+                                                      String category,
+                                                      String dateString,
+                                                      int pageNum,
+                                                      boolean absolute) {
+        
+        StringBuffer pathinfo = new StringBuffer();
+        Map params = new HashMap();
+        
+        pathinfo.append(getWeblogURL(weblog, locale, absolute));
+        
+        if(category != null && dateString == null) {
+            String cat = category;
+            if(category.startsWith("/")) {
+                cat = category.substring(1);
+            }
+            
+            pathinfo.append("/category/").append(encode(cat));   
+            
+        } else if(dateString != null && category == null) {
+            pathinfo.append("/date/").append(dateString);  
+            
+        } else {
+            if(dateString != null) params.put("date", dateString);
+            if(category != null) params.put("cat", encode(category));
+        }
+
+        if(pageNum > 0) {
+            params.put("page", Integer.toString(pageNum));
+        }
+        
+        return pathinfo.toString() + getQueryString(params);
+    }
+    
+    
+    /**
+     * Get url for a custom page on a given weblog.
+     */
+    public static final String getWeblogPageURL(WebsiteData weblog,
+                                                String locale,
+                                                String pageLink,
+                                                String entryAnchor,
+                                                String category,
+                                                String dateString,
+                                                int pageNum,
+                                                boolean absolute) {
+        
+        StringBuffer pathinfo = new StringBuffer();
+        Map params = new HashMap();
+        
+        pathinfo.append(getWeblogURL(weblog, locale, absolute));
+        
+        if(pageLink != null) {
+            pathinfo.append("/page/").append(pageLink);
+            
+            // for custom pages we only allow query params
+            if(dateString != null) {
+                params.put("date", dateString);
+            }
+            if(category != null) {
+                params.put("cat", encode(category));
+            }
+            
+        } else if(category != null && dateString == null) {
+            String cat = category;
+            if(category.startsWith("/")) {
+                cat = category.substring(1);
+            }
+            
+            pathinfo.append("/category/").append(encode(cat));   
+            
+        } else if(dateString != null && category == null) {
+            pathinfo.append("/date/").append(dateString);  
+            
+        } else {
+            if(dateString != null) params.put("date", dateString);
+            if(category != null) params.put("cat", encode(category));
+        }
+
+        if(pageNum > 0) {
+            params.put("page", Integer.toString(pageNum));
+        }
+        
+        return pathinfo.toString() + getQueryString(params);
+    }
+    
+    
+    /**
+     * Get url for a feed on a given weblog.
+     */
+    public static final String getWeblogFeedURL(WebsiteData weblog,
+                                                String locale,
+                                                String type,
+                                                String format,
+                                                String category,
+                                                boolean excerpts,
+                                                boolean absolute) {
+        
+        StringBuffer url = new StringBuffer();
+        
+        url.append(getWeblogURL(weblog, locale, absolute));
+        url.append("/feed/").append(type).append("/").append(format);
+        
+        Map params = new HashMap();
+        if(category != null) {
+            params.put("cat", encode(category));
+        }
+        if(excerpts) {
+            params.put("excerpts", Boolean.TRUE);
+        }
+        
+        return url.toString() + getQueryString(params);
+    }
+    
+    
+    /**
+     * Get url to search endpoint on a given weblog.
+     */
+    public static final String getWeblogSearchURL(WebsiteData weblog,
+                                                  String locale,
+                                                  String query,
+                                                  String category,
+                                                  int pageNum,
+                                                  boolean absolute) {
+        
+        StringBuffer url = new StringBuffer();
+        
+        url.append(getWeblogURL(weblog, locale, absolute));
+        url.append("/search");
+        
+        Map params = new HashMap();
+        if(query != null) {
+            params.put("query", encode(query));
+            
+            // other stuff only makes sense if there is a query
+            if(category != null) {
+                params.put("cat", encode("cat"));
+            }
+            if(pageNum > 0) {
+                params.put("page", Integer.toString(pageNum));
+            }
+        }
+        
+        return url.toString() + getQueryString(params);
+    }
+    
+    
+    /**
+     * Get url to a resource on a given weblog.
+     */
+    public static final String getWeblogResourceURL(WebsiteData weblog,
+                                                    String filePath,
+                                                    boolean absolute) {
+        
+        StringBuffer url = new StringBuffer();
+        
+        url.append(getWeblogURL(weblog, null, absolute));
+        url.append("/resource/");
+        
+        if(filePath.startsWith("/")) {
+            url.append(filePath.substring(1));
+        } else {
+            url.append(filePath);
+        }
+        
+        return url.toString();
+    }
+    
+    
+    /**
+     * Get url to rsd file on a given weblog.
+     */
+    public static final String getWeblogRsdURL(WebsiteData weblog,
+                                               boolean absolute) {
+        return getWeblogURL(weblog, null, absolute)+"/rsd";
+    }
+    
+    
+    /**
+     * Compose a map of key=value params into a query string.
+     */
+    public static final String getQueryString(Map params) {
+        
+        StringBuffer queryString = new StringBuffer();
+        
+        for(Iterator keys = params.keySet().iterator(); keys.hasNext();) {
+            String key = (String) keys.next();
+            String value = (String) params.get(key);
+            
+            if (queryString.length() == 0) {
+                queryString.append("?");
+            } else {
+                queryString.append("&");
+            }
+            
+            queryString.append(key);
+            queryString.append("=");
+            queryString.append(value);
+        }
+        
+        return queryString.toString();
+    }
+    
+    
+    /**
+     * URL encode a string using UTF-8.
+     */
+    public static final String encode(String str) {
+        String encodedStr = str;
+        try {
+            encodedStr = URLEncoder.encode(str, "UTF-8");
+        } catch (UnsupportedEncodingException ex) {
+            // ignored
+        }
+        return encodedStr;
+    }
+    
+}



Mime
View raw message