| 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 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
@author |
| 23 |
|
|
|
|
|
| 0% |
Uncovered Elements: 160 (160) |
Complexity: 29 |
Complexity Density: 0,23 |
|
| 24 |
|
public class Latin1SeoPermalinkProvider implements PermalinkProvider { |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
private static final String DAY_PERMALINK_REGEX = "/\\d\\d\\d\\d/\\d\\d/\\d\\d"; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
private static final String MONTH_PERMALINK_REGEX = "/\\d\\d\\d\\d/\\d\\d"; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
private static final String BLOG_ENTRY_PERMALINK_REGEX = "/[\\w-]*"; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
private Blog blog; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@return |
| 42 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
0
|
public Blog getBlog() {... |
| 44 |
0
|
return this.blog; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@param |
| 51 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 52 |
0
|
public void setBlog(Blog blog) {... |
| 53 |
0
|
this.blog = blog; |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@return |
| 60 |
|
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 7 |
Complexity Density: 0,54 |
|
| 61 |
0
|
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 |
|
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 5 |
Complexity Density: 0,36 |
|
| 87 |
0
|
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("[\\. ,;/\\\\_]", "-"); |
| 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 |
|
|
| 104 |
0
|
if (title == null || title.length() == 0) { |
| 105 |
0
|
title = "" + blogEntry.getId(); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
0
|
return "/" + title; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 112 |
0
|
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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0,5 |
|
| 120 |
0
|
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 |
|
|
| 127 |
|
|
| 128 |
0
|
if (blogEntry.getLocalPermalink().endsWith(uri)) { |
| 129 |
0
|
return blogEntry; |
| 130 |
|
} |
| 131 |
|
} catch (BlogServiceException e) { |
| 132 |
|
|
| 133 |
|
} |
| 134 |
|
} |
| 135 |
|
|
| 136 |
0
|
return null; |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
@param |
| 143 |
|
@return |
| 144 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 145 |
0
|
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 |
|
|
| 153 |
|
|
| 154 |
|
@param |
| 155 |
|
@return |
| 156 |
|
|
| 157 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 158 |
0
|
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 |
|
|
| 168 |
|
|
| 169 |
|
@param |
| 170 |
|
@return |
| 171 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 172 |
0
|
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 |
|
|
| 181 |
|
|
| 182 |
|
@param |
| 183 |
|
@return |
| 184 |
|
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
| 185 |
0
|
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 |
|
|
| 193 |
|
|
| 194 |
|
@param |
| 195 |
|
@return |
| 196 |
|
|
| 197 |
|
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
| 198 |
0
|
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 |
|
|
| 208 |
|
|
| 209 |
|
@param |
| 210 |
|
@return |
| 211 |
|
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
| 212 |
0
|
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 |
|
|
| 223 |
|
private static final HashMap<String,String> characterSubstitutions; |
|
|
|
| 0% |
Uncovered Elements: 66 (66) |
Complexity: 1 |
Complexity Density: 0,02 |
|
| 224 |
0
|
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 |
|
|
| 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 |
|
} |