Coverage Report - net.sourceforge.pebble.permalink.TitlePermalinkProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
TitlePermalinkProvider
96%
56/58
95%
23/24
5.5
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.permalink;
 33  
 
 34  
 import net.sourceforge.pebble.domain.*;
 35  
 
 36  
 import java.text.DateFormat;
 37  
 import java.text.SimpleDateFormat;
 38  
 import java.util.Date;
 39  
 import java.util.Iterator;
 40  
 import java.util.List;
 41  
 
 42  
 /**
 43  
  * Generates permalinks based upon the blog entry title. This implementation
 44  
  * only uses the following characters from the title:
 45  
  * <ul>
 46  
  * <li>a-z</li>
 47  
  * <li>A-Z</li>
 48  
  * <li>0-9</li>
 49  
  * <li>_ (underscore)</li>
 50  
  * </ul>
 51  
  * For titles without these characters (e.g. those using an extended character
 52  
  * set) the blog entry ID is used for the permalink instead.
 53  
  *
 54  
  * @author Simon Brown
 55  
  */
 56  64
 public class TitlePermalinkProvider extends PermalinkProviderSupport {
 57  
 
 58  
   /** the regex used to check for a blog entry permalink */
 59  
   private static final String BLOG_ENTRY_PERMALINK_REGEX = "/\\d\\d\\d\\d/\\d\\d/\\d\\d/[\\w]*.html";
 60  
 
 61  
   /**
 62  
    * Gets the permalink for a blog entry.
 63  
    *
 64  
    * @return  a URI as a String
 65  
    */
 66  
   public synchronized String getPermalink(BlogEntry blogEntry) {
 67  148
     if (blogEntry.getTitle() == null || blogEntry.getTitle().length() == 0) {
 68  8
       return buildPermalink(blogEntry) + ".html";
 69  
     } else {
 70  140
       BlogService service = new BlogService();
 71  140
       Day day = getBlog().getBlogForDay(blogEntry.getDate());
 72  140
       List entries = day.getBlogEntries();
 73  140
       int count = 0;
 74  188
       for (int i = entries.size()-1; i > entries.indexOf(blogEntry.getId()); i--) {
 75  
         try {
 76  48
           BlogEntry entry = service.getBlogEntry(getBlog(), (String)entries.get(i));
 77  48
           if (entry.getTitle().equals(blogEntry.getTitle())) {
 78  4
             count++;
 79  
           }
 80  0
         } catch (BlogServiceException e) {
 81  
           // do nothing
 82  48
         }
 83  
       }
 84  
 
 85  140
       if (count == 0) {
 86  136
         return buildPermalink(blogEntry) + ".html";
 87  
       } else {
 88  4
         return buildPermalink(blogEntry) + "_" + blogEntry.getId() + ".html";
 89  
       }
 90  
     }
 91  
   }
 92  
 
 93  
   private String buildPermalink(BlogEntry blogEntry) {
 94  148
     String title = blogEntry.getTitle();
 95  148
     if (title == null || title.length() == 0) {
 96  8
       title = "" + blogEntry.getId();
 97  
     } else {
 98  140
       title = title.toLowerCase();
 99  140
       title = title.replaceAll("[\\. ,;/\\\\-]", "_");
 100  140
       title = title.replaceAll("[^a-z0-9_]", "");
 101  140
       title = title.replaceAll("_+", "_");
 102  140
       title = title.replaceAll("^_*", "");
 103  140
       title = title.replaceAll("_*$", "");
 104  
     }
 105  
 
 106  
     // if the title has been blanked out, use the blog entry instead
 107  148
     if (title == null || title.length() == 0) {
 108  4
       title = "" + blogEntry.getId();
 109  
     }
 110  
 
 111  148
     Blog blog = blogEntry.getBlog();
 112  148
     Date date = blogEntry.getDate();
 113  148
     DateFormat year = new SimpleDateFormat("yyyy");
 114  148
     year.setTimeZone(blog.getTimeZone());
 115  148
     DateFormat month = new SimpleDateFormat("MM");
 116  148
     month.setTimeZone(blog.getTimeZone());
 117  148
     DateFormat day = new SimpleDateFormat("dd");
 118  148
     day.setTimeZone(blog.getTimeZone());
 119  
 
 120  148
     StringBuffer buf = new StringBuffer();
 121  148
     buf.append("/");
 122  148
     buf.append(year.format(date));
 123  148
     buf.append("/");
 124  148
     buf.append(month.format(date));
 125  148
     buf.append("/");
 126  148
     buf.append(day.format(date));
 127  148
     buf.append("/");
 128  148
     buf.append(title);
 129  
 
 130  148
     return buf.toString();
 131  
   }
 132  
 
 133  
   /**
 134  
    * Determines whether the specified URI is a blog entry permalink.
 135  
    *
 136  
    * @param uri   a relative URI
 137  
    * @return      true if the URI represents a permalink to a blog entry,
 138  
    *              false otherwise
 139  
    */
 140  
   public boolean isBlogEntryPermalink(String uri) {
 141  28
     if (uri != null) {
 142  24
       return uri.matches(BLOG_ENTRY_PERMALINK_REGEX);
 143  
     } else {
 144  4
       return false;
 145  
     }
 146  
   }
 147  
 
 148  
   /**
 149  
    * Gets the blog entry referred to by the specified URI.
 150  
    *
 151  
    * @param uri   a relative URI
 152  
    * @return  a BlogEntry instance, or null if one can't be found
 153  
    */
 154  
   public BlogEntry getBlogEntry(String uri) {
 155  28
     BlogService service = new BlogService();
 156  28
     Day day = getDay(uri);
 157  
 
 158  28
     Iterator it = day.getBlogEntries().iterator();
 159  48
     while (it.hasNext()) {
 160  
       try {
 161  40
         BlogEntry blogEntry = service.getBlogEntry(getBlog(), (String)it.next());
 162  
         // use the local permalink, just in case the entry has been aggregated
 163  
         // and an original permalink assigned
 164  40
         if (blogEntry.getLocalPermalink().endsWith(uri)) {
 165  20
           return blogEntry;
 166  
         }
 167  0
       } catch (BlogServiceException e) {
 168  
         // do nothing
 169  20
       }
 170  
     }
 171  
 
 172  8
     return null;
 173  
   }
 174  
 
 175  
 }