File Source: titlepermalinkprovider.java
/*
P/P * Method: net.sourceforge.pebble.permalink.TitlePermalinkProvider__static_init
*/
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 */
/*
P/P * Method: void net.sourceforge.pebble.permalink.TitlePermalinkProvider()
*/
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 public synchronized String getPermalink(BlogEntry blogEntry) {
/*
P/P * Method: String getPermalink(BlogEntry)
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getBlogForDay(...)@71 != null
* net.sourceforge.pebble.domain.BlogEntry:getTitle(...)@67 != null
* net.sourceforge.pebble.domain.BlogEntry:getTitle(...)@77 != null
* net.sourceforge.pebble.domain.BlogService:getBlogEntry(...)@76 != null
* net.sourceforge.pebble.domain.Day:getBlogEntries(...)@72 != null
*
* Preconditions:
* blogEntry != null
* (soft) this.blog != null
*
* Test Vectors:
* net.sourceforge.pebble.domain.BlogEntry:getTitle(...)@67: Addr_Set{null}, Inverse{null}
*
* Presumptions:
* java.util.List:indexOf(...)@74 - java.util.List:size(...)@74 in -232..6_442_450_942
* java.util.List:size(...)@74 >= -231+1
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.lang.String:equals(...)@77: {0}, {1}
* java.lang.String:length(...)@67: {1..232-1}, {0}
*/
67 if (blogEntry.getTitle() == null || blogEntry.getTitle().length() == 0) {
68 return buildPermalink(blogEntry) + ".html";
69 } else {
70 BlogService service = new BlogService();
71 Day day = getBlog().getBlogForDay(blogEntry.getDate());
72 List entries = day.getBlogEntries();
73 int count = 0;
74 for (int i = entries.size()-1; i > entries.indexOf(blogEntry.getId()); i--) {
75 try {
76 BlogEntry entry = service.getBlogEntry(getBlog(), (String)entries.get(i));
77 if (entry.getTitle().equals(blogEntry.getTitle())) {
78 count++;
79 }
80 } catch (BlogServiceException e) {
81 // do nothing
82 }
83 }
84
85 if (count == 0) {
86 return buildPermalink(blogEntry) + ".html";
87 } else {
88 return buildPermalink(blogEntry) + "_" + blogEntry.getId() + ".html";
89 }
90 }
91 }
92
93 private String buildPermalink(BlogEntry blogEntry) {
/*
P/P * Method: String buildPermalink(BlogEntry)
*
* Presumptions:
* net.sourceforge.pebble.domain.BlogEntry:getBlog(...)@111 != null
*
* Test Vectors:
* net.sourceforge.pebble.domain.BlogEntry:getTitle(...)@94: Addr_Set{null}, Inverse{null}
*
* Preconditions:
* blogEntry != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.lang.String:length(...)@107: {1..232-1}, {0}
* java.lang.String:length(...)@95: {1..232-1}, {0}
*/
94 String title = blogEntry.getTitle();
95 if (title == null || title.length() == 0) {
96 title = "" + blogEntry.getId();
97 } else {
98 title = title.toLowerCase();
99 title = title.replaceAll("[\\. ,;/\\\\-]", "_");
100 title = title.replaceAll("[^a-z0-9_]", "");
101 title = title.replaceAll("_+", "_");
102 title = title.replaceAll("^_*", "");
103 title = title.replaceAll("_*$", "");
104 }
105
106 // if the title has been blanked out, use the blog entry instead
107 if (title == null || title.length() == 0) {
108 title = "" + blogEntry.getId();
109 }
110
111 Blog blog = blogEntry.getBlog();
112 Date date = blogEntry.getDate();
113 DateFormat year = new SimpleDateFormat("yyyy");
114 year.setTimeZone(blog.getTimeZone());
115 DateFormat month = new SimpleDateFormat("MM");
116 month.setTimeZone(blog.getTimeZone());
117 DateFormat day = new SimpleDateFormat("dd");
118 day.setTimeZone(blog.getTimeZone());
119
120 StringBuffer buf = new StringBuffer();
121 buf.append("/");
122 buf.append(year.format(date));
123 buf.append("/");
124 buf.append(month.format(date));
125 buf.append("/");
126 buf.append(day.format(date));
127 buf.append("/");
128 buf.append(title);
129
130 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) {
/*
P/P * Method: bool isBlogEntryPermalink(String)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* uri: Addr_Set{null}, Inverse{null}
*/
141 if (uri != null) {
142 return uri.matches(BLOG_ENTRY_PERMALINK_REGEX);
143 } else {
144 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) {
/*
P/P * Method: BlogEntry getBlogEntry(String)
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getBlogForDay(...)@154 != null
* net.sourceforge.pebble.domain.BlogEntry:getLocalPermalink(...)@164 != null
* net.sourceforge.pebble.domain.BlogService:getBlogEntry(...)@161 != null
* net.sourceforge.pebble.domain.Day:getBlogEntries(...)@158 != null
*
* Preconditions:
* this.blog != null
* uri != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:endsWith(...)@164: {0}, {1}
* java.util.Iterator:hasNext(...)@159: {1}, {0}
*/
155 BlogService service = new BlogService();
156 Day day = getDay(uri);
157
158 Iterator it = day.getBlogEntries().iterator();
159 while (it.hasNext()) {
160 try {
161 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 if (blogEntry.getLocalPermalink().endsWith(uri)) {
165 return blogEntry;
166 }
167 } catch (BlogServiceException e) {
168 // do nothing
169 }
170 }
171
172 return null;
173 }
174
175 }
SofCheck Inspector Build Version : 2.22510
| titlepermalinkprovider.java |
2010-Jun-25 19:40:32 |
| titlepermalinkprovider.class |
2010-Jul-19 20:23:38 |