Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart0.png 48% of files have more coverage
125   299   29   9,62
22   188   0,23   13
13     2,23  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  Latin1SeoPermalinkProvider       Line # 24 125 0% 29 160 0% 0.0
 
No Tests
 
1    package net.sourceforge.pebble.permalink;
2   
3    import net.sourceforge.pebble.api.permalink.PermalinkProvider;
4    import net.sourceforge.pebble.domain.*;
5   
6    import java.text.SimpleDateFormat;
7    import java.util.HashMap;
8    import java.util.Iterator;
9    import java.util.List;
10   
11    /**
12    * Generates permalinks based upon the blog entry title. This implementation
13    * retains characters from the latin1 character by converting
14    * them to suitable "url-friendly" counterparts.
15    *
16    * It also uses dashes instead of underscores for whitespace as this is
17    * what Google recommends.
18   
19    * For titles without characters from the latin1 character set
20    * the blog entry ID is used for the permalink instead.
21    *
22    * @author Mattias Reichel
23    */
 
24    public class Latin1SeoPermalinkProvider implements PermalinkProvider {
25   
26    /** the regex used to check for a day request */
27    private static final String DAY_PERMALINK_REGEX = "/\\d\\d\\d\\d/\\d\\d/\\d\\d";
28   
29    /** the regex used to check for a monthly blog request */
30    private static final String MONTH_PERMALINK_REGEX = "/\\d\\d\\d\\d/\\d\\d";
31   
32    /** the regex used to check for a blog entry permalink */
33    private static final String BLOG_ENTRY_PERMALINK_REGEX = "/[\\w-]*";
34   
35    /** the Blog associated with this provider instance */
36    private Blog blog;
37   
38    /**
39    * Gets the blog associated with this provider instance.
40    *
41    * @return a Blog instance
42    */
 
43  0 toggle public Blog getBlog() {
44  0 return this.blog;
45    }
46   
47    /**
48    * Sets the blog associated with this provider instance.
49    *
50    * @param blog a Blog instance
51    */
 
52  0 toggle public void setBlog(Blog blog) {
53  0 this.blog = blog;
54    }
55   
56    /**
57    * Gets the permalink for a blog entry.
58    *
59    * @return a URI as a String
60    */
 
61  0 toggle public synchronized String getPermalink(BlogEntry blogEntry) {
62  0 if (blogEntry.getTitle() == null || blogEntry.getTitle().length() == 0) {
63  0 return buildPermalink(blogEntry);
64    } else {
65  0 BlogService service = new BlogService();
66  0 List entries = getBlog().getBlogEntries();
67  0 int count = 0;
68  0 for (int i = entries.size() - 1; i > entries.indexOf(blogEntry.getId()); i--) {
69  0 try {
70  0 BlogEntry entry = service.getBlogEntry(getBlog(), "" + ((BlogEntry)entries.get(i)).getId());
71  0 if (entry.getTitle().equals(blogEntry.getTitle())) {
72  0 count++;
73    }
74    } catch (BlogServiceException e) {
75    // do nothing
76    }
77    }
78   
79  0 if (count == 1) {
80  0 return buildPermalink(blogEntry);
81    } else {
82  0 return buildPermalink(blogEntry) + "_" + blogEntry.getId();
83    }
84    }
85    }
86   
 
87  0 toggle private String buildPermalink(BlogEntry blogEntry) {
88  0 String title = blogEntry.getTitle();
89  0 if (title == null || title.length() == 0) {
90  0 title = "" + blogEntry.getId();
91    } else {
92  0 title = title.toLowerCase();
93  0 title = title.replaceAll("[\\. ,;/\\\\_]", "-"); // Change whitespace and punctuation marks to dashes
94  0 for(String search : characterSubstitutions.keySet()) {
95  0 title = title.replaceAll(search, characterSubstitutions.get(search));
96    }
97  0 title = title.replaceAll("[^a-z0-9-]", "");
98  0 title = title.replaceAll("-+", "-");
99  0 title = title.replaceAll("^-*", "");
100  0 title = title.replaceAll("-*$", "");
101    }
102   
103    // if the title has been blanked out, use the blog entry instead
104  0 if (title == null || title.length() == 0) {
105  0 title = "" + blogEntry.getId();
106    }
107   
108  0 return "/" + title;
109    }
110   
111   
 
112  0 toggle public boolean isBlogEntryPermalink(String uri) {
113  0 if(uri != null) {
114  0 return uri.matches(BLOG_ENTRY_PERMALINK_REGEX);
115    } else {
116  0 return false;
117    }
118    }
119   
 
120  0 toggle public BlogEntry getBlogEntry(String uri) {
121  0 BlogService service = new BlogService();
122  0 Iterator it = getBlog().getBlogEntries().iterator();
123  0 while(it.hasNext()) {
124  0 try {
125  0 BlogEntry blogEntry = service.getBlogEntry(getBlog(), "" + ((BlogEntry)it.next()).getId());
126    // use the local permalink, just in case the entry has been aggregated
127    // and an original permalink assigned
128  0 if (blogEntry.getLocalPermalink().endsWith(uri)) {
129  0 return blogEntry;
130    }
131    } catch (BlogServiceException e) {
132    // do nothing
133    }
134    }
135   
136  0 return null;
137    }
138   
139    /**
140    * Gets the permalink for a monthly blog.
141    *
142    * @param month a Month instance
143    * @return a URI as a String
144    */
 
145  0 toggle public String getPermalink(Month month) {
146  0 SimpleDateFormat format = new SimpleDateFormat("'/'yyyy'/'MM");
147  0 format.setTimeZone(blog.getTimeZone());
148  0 return format.format(month.getDate());
149    }
150   
151    /**
152    * Determines whether the specified URI is a monthly blog permalink.
153    *
154    * @param uri a relative URI
155    * @return true if the URI represents a permalink to a monthly blog,
156    * false otherwise
157    */
 
158  0 toggle public boolean isMonthPermalink(String uri) {
159  0 if (uri != null) {
160  0 return uri.matches(MONTH_PERMALINK_REGEX);
161    } else {
162  0 return false;
163    }
164    }
165   
166    /**
167    * Gets the monthly blog referred to by the specified URI.
168    *
169    * @param uri a relative URI
170    * @return a Month instance, or null if one can't be found
171    */
 
172  0 toggle public Month getMonth(String uri) {
173  0 String year = uri.substring(1, 5);
174  0 String month = uri.substring(6, 8);
175   
176  0 return getBlog().getBlogForMonth(Integer.parseInt(year), Integer.parseInt(month));
177    }
178   
179    /**
180    * Gets the permalink for a day.
181    *
182    * @param day a Day instance
183    * @return a URI as a String
184    */
 
185  0 toggle public String getPermalink(Day day) {
186  0 SimpleDateFormat format = new SimpleDateFormat("'/'yyyy'/'MM'/'dd");
187  0 format.setTimeZone(blog.getTimeZone());
188  0 return format.format(day.getDate());
189    }
190   
191    /**
192    * Determines whether the specified URI is a day permalink.
193    *
194    * @param uri a relative URI
195    * @return true if the URI represents a permalink to a day,
196    * false otherwise
197    */
 
198  0 toggle public boolean isDayPermalink(String uri) {
199  0 if (uri != null) {
200  0 return uri.matches(DAY_PERMALINK_REGEX);
201    } else {
202  0 return false;
203    }
204    }
205   
206    /**
207    * Gets the day referred to by the specified URI.
208    *
209    * @param uri a relative URI
210    * @return a Day instance, or null if one can't be found
211    */
 
212  0 toggle public Day getDay(String uri) {
213  0 String year = uri.substring(1, 5);
214  0 String month = uri.substring(6, 8);
215  0 String day = uri.substring(9, 11);
216   
217  0 return getBlog().getBlogForDay(Integer.parseInt(year),
218    Integer.parseInt(month), Integer.parseInt(day));
219    }
220   
221   
222    /** the List of characters that will be substituted */
223    private static final HashMap<String,String> characterSubstitutions;
 
224  0 toggle static {
225   
226  0 characterSubstitutions = new HashMap<String,String>();
227   
228  0 characterSubstitutions.put("\u00B2", "2");
229  0 characterSubstitutions.put("\u00B3", "3");
230   
231  0 characterSubstitutions.put("\u00C0", "A");
232  0 characterSubstitutions.put("\u00C1", "A");
233  0 characterSubstitutions.put("\u00C2", "A");
234  0 characterSubstitutions.put("\u00C3", "A");
235  0 characterSubstitutions.put("\u00C4", "A");
236  0 characterSubstitutions.put("\u00C5", "A");
237  0 characterSubstitutions.put("\u00C6", "AE");
238  0 characterSubstitutions.put("\u00C7", "C");
239  0 characterSubstitutions.put("\u00C8", "E");
240  0 characterSubstitutions.put("\u00C9", "E");
241  0 characterSubstitutions.put("\u00CA", "E");
242  0 characterSubstitutions.put("\u00CB", "E");
243  0 characterSubstitutions.put("\u00CC", "I");
244  0 characterSubstitutions.put("\u00CD", "I");
245  0 characterSubstitutions.put("\u00CE", "I");
246  0 characterSubstitutions.put("\u00CF", "I");
247   
248  0 characterSubstitutions.put("\u00D0", "D");
249  0 characterSubstitutions.put("\u00D1", "N");
250  0 characterSubstitutions.put("\u00D2", "O");
251  0 characterSubstitutions.put("\u00D3", "O");
252  0 characterSubstitutions.put("\u00D4", "O");
253  0 characterSubstitutions.put("\u00D5", "O");
254  0 characterSubstitutions.put("\u00D6", "O");
255  0 characterSubstitutions.put("\u00D7", "x");
256  0 characterSubstitutions.put("\u00D8", "O");
257  0 characterSubstitutions.put("\u00D9", "U");
258  0 characterSubstitutions.put("\u00DA", "U");
259  0 characterSubstitutions.put("\u00DB", "U");
260  0 characterSubstitutions.put("\u00DC", "U");
261  0 characterSubstitutions.put("\u00DD", "Y");
262  0 characterSubstitutions.put("\u00DE", "P");
263  0 characterSubstitutions.put("\u00DF", "ss");
264   
265  0 characterSubstitutions.put("\u00E0", "a");
266  0 characterSubstitutions.put("\u00E1", "a");
267  0 characterSubstitutions.put("\u00E2", "a");
268  0 characterSubstitutions.put("\u00E3", "a");
269  0 characterSubstitutions.put("\u00E4", "a");
270  0 characterSubstitutions.put("\u00E5", "a");
271  0 characterSubstitutions.put("\u00E6", "ae");
272  0 characterSubstitutions.put("\u00E7", "c");
273  0 characterSubstitutions.put("\u00E8", "e");
274  0 characterSubstitutions.put("\u00E9", "e");
275  0 characterSubstitutions.put("\u00EA", "e");
276  0 characterSubstitutions.put("\u00EB", "e");
277  0 characterSubstitutions.put("\u00EC", "i");
278  0 characterSubstitutions.put("\u00ED", "i");
279  0 characterSubstitutions.put("\u00EE", "i");
280  0 characterSubstitutions.put("\u00EF", "i");
281   
282  0 characterSubstitutions.put("\u00F0", "d");
283  0 characterSubstitutions.put("\u00F1", "n");
284  0 characterSubstitutions.put("\u00F2", "o");
285  0 characterSubstitutions.put("\u00F3", "o");
286  0 characterSubstitutions.put("\u00F4", "o");
287  0 characterSubstitutions.put("\u00F5", "o");
288  0 characterSubstitutions.put("\u00F6", "o");
289    //"\u00F7", // division sign (ignore)
290  0 characterSubstitutions.put("\u00F8", "o");
291  0 characterSubstitutions.put("\u00F9", "u");
292  0 characterSubstitutions.put("\u00FA", "u");
293  0 characterSubstitutions.put("\u00FB", "u");
294  0 characterSubstitutions.put("\u00FC", "u");
295  0 characterSubstitutions.put("\u00FD", "y");
296  0 characterSubstitutions.put("\u00FE", "p");
297  0 characterSubstitutions.put("\u00FF", "y");
298    }
299    }