| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 64 | public class TitlePermalinkProvider extends PermalinkProviderSupport { |
| 57 | |
|
| 58 | |
|
| 59 | |
private static final String BLOG_ENTRY_PERMALINK_REGEX = "/\\d\\d\\d\\d/\\d\\d/\\d\\d/[\\w]*.html"; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 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 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 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 | |
|
| 163 | |
|
| 164 | 40 | if (blogEntry.getLocalPermalink().endsWith(uri)) { |
| 165 | 20 | return blogEntry; |
| 166 | |
} |
| 167 | 0 | } catch (BlogServiceException e) { |
| 168 | |
|
| 169 | 20 | } |
| 170 | |
} |
| 171 | |
|
| 172 | 8 | return null; |
| 173 | |
} |
| 174 | |
|
| 175 | |
} |