Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
55   175   18   13,75
18   90   0,33   4
4     4,5  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  TitlePermalinkProvider       Line # 56 55 0% 18 0 100% 1.0
 
  (18)
 
1    /*
2    * Copyright (c) 2003-2006, 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    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  74 toggle public synchronized String getPermalink(BlogEntry blogEntry) {
67  74 if (blogEntry.getTitle() == null || blogEntry.getTitle().length() == 0) {
68  4 return buildPermalink(blogEntry) + ".html";
69    } else {
70  70 BlogService service = new BlogService();
71  70 Day day = getBlog().getBlogForDay(blogEntry.getDate());
72  70 List entries = day.getBlogEntries();
73  70 int count = 0;
74  94 for (int i = entries.size()-1; i > entries.indexOf(blogEntry.getId()); i--) {
75  24 try {
76  24 BlogEntry entry = service.getBlogEntry(getBlog(), (String)entries.get(i));
77  24 if (entry.getTitle().equals(blogEntry.getTitle())) {
78  2 count++;
79    }
80    } catch (BlogServiceException e) {
81    // do nothing
82    }
83    }
84   
85  70 if (count == 0) {
86  68 return buildPermalink(blogEntry) + ".html";
87    } else {
88  2 return buildPermalink(blogEntry) + "_" + blogEntry.getId() + ".html";
89    }
90    }
91    }
92   
 
93  74 toggle private String buildPermalink(BlogEntry blogEntry) {
94  74 String title = blogEntry.getTitle();
95  74 if (title == null || title.length() == 0) {
96  4 title = "" + blogEntry.getId();
97    } else {
98  70 title = title.toLowerCase();
99  70 title = title.replaceAll("[\\. ,;/\\\\-]", "_");
100  70 title = title.replaceAll("[^a-z0-9_]", "");
101  70 title = title.replaceAll("_+", "_");
102  70 title = title.replaceAll("^_*", "");
103  70 title = title.replaceAll("_*$", "");
104    }
105   
106    // if the title has been blanked out, use the blog entry instead
107  74 if (title == null || title.length() == 0) {
108  2 title = "" + blogEntry.getId();
109    }
110   
111  74 Blog blog = blogEntry.getBlog();
112  74 Date date = blogEntry.getDate();
113  74 DateFormat year = new SimpleDateFormat("yyyy");
114  74 year.setTimeZone(blog.getTimeZone());
115  74 DateFormat month = new SimpleDateFormat("MM");
116  74 month.setTimeZone(blog.getTimeZone());
117  74 DateFormat day = new SimpleDateFormat("dd");
118  74 day.setTimeZone(blog.getTimeZone());
119   
120  74 StringBuffer buf = new StringBuffer();
121  74 buf.append("/");
122  74 buf.append(year.format(date));
123  74 buf.append("/");
124  74 buf.append(month.format(date));
125  74 buf.append("/");
126  74 buf.append(day.format(date));
127  74 buf.append("/");
128  74 buf.append(title);
129   
130  74 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  14 toggle public boolean isBlogEntryPermalink(String uri) {
141  14 if (uri != null) {
142  12 return uri.matches(BLOG_ENTRY_PERMALINK_REGEX);
143    } else {
144  2 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  14 toggle public BlogEntry getBlogEntry(String uri) {
155  14 BlogService service = new BlogService();
156  14 Day day = getDay(uri);
157   
158  14 Iterator it = day.getBlogEntries().iterator();
159  24 while (it.hasNext()) {
160  20 try {
161  20 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  20 if (blogEntry.getLocalPermalink().endsWith(uri)) {
165  10 return blogEntry;
166    }
167    } catch (BlogServiceException e) {
168    // do nothing
169    }
170    }
171   
172  4 return null;
173    }
174   
175    }