1 38 package org.jpublish.util; 39 40 import java.io.File ; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.jpublish.SiteContext; 45 import org.jpublish.util.uri.RepositoryURI; 46 import org.jpublish.util.uri.TemplateURI; 47 48 53 54 public final class PathUtilities { 55 56 59 public final static String WILDCARD = "*"; 60 63 public final static String TEMPLATE_PROTOCOL = "template"; 64 67 public final static String REPOSITORY_PROTOCOL = "repository"; 68 69 private static Log log = LogFactory.getLog(PathUtilities.class); 70 71 74 75 private PathUtilities() { 76 } 78 79 86 87 public static boolean match(String requestPath, String exPath) { 88 if (requestPath == null) { 89 return exPath.equals(WILDCARD); 90 } 91 92 int wildcardIndex = exPath.indexOf(WILDCARD); 93 if (wildcardIndex == -1) { 94 return exPath.equals(requestPath); 95 } else { 96 if (log.isDebugEnabled()) { 97 log.debug("Wildcard index: " + wildcardIndex); 98 } 99 if (wildcardIndex == (exPath.length() - 1)) { 100 String checkString = exPath.substring(0, exPath.length() - 1); 101 boolean answer = requestPath.startsWith(checkString); 102 return answer; 103 } else { 104 String preMatch = exPath.substring(0, wildcardIndex); 105 String postMatch = exPath.substring(wildcardIndex + 1); 106 107 return requestPath.startsWith(preMatch) && 108 requestPath.endsWith(postMatch); 109 } 110 } 111 } 112 113 119 120 public static String extractPageName(String path) { 121 File file = new File (path); 122 123 String fileName = file.getName(); 124 int dotIndex = fileName.lastIndexOf("."); 125 if (dotIndex < 0) { 126 return null; 127 } 128 129 String pageName = fileName.substring(0, dotIndex); 130 131 return pageName; 132 } 133 134 141 142 public static String extractPagePath(String path) { 143 File file = new File (path); 144 File parentDirectory = file.getParentFile(); 145 146 String pagePath = null; 147 148 if (parentDirectory == null) { 149 pagePath = extractPageName(path); 150 } else { 151 pagePath = new File (parentDirectory.getPath(), 152 extractPageName(path)).getPath(); 153 pagePath = pagePath.replace(File.separatorChar, '/'); 154 } 155 156 return pagePath; 157 } 158 159 167 public static String extractPageParent(String path) { 168 File file = new File (path); 169 File parentDirectory = file.getParentFile(); 170 171 String pageParent = null; 172 if (parentDirectory == null) { 173 pageParent = ""; 174 } else { 175 pageParent = parentDirectory.getPath(); 176 pageParent = pageParent.replace(File.separatorChar, '/'); 177 } 178 179 if (pageParent.startsWith("/")) { 180 pageParent = pageParent.substring(1); 181 } 182 183 return pageParent; 184 } 185 186 193 public static String extractPageType(String path) { 194 File file = new File (path); 195 196 String fileName = file.getName(); 197 198 int dotIndex = fileName.lastIndexOf("."); 199 if (dotIndex < 0) { 200 return null; 201 } 202 203 String pageType = fileName.substring(dotIndex + 1); 204 205 return pageType; 206 } 207 208 216 public static String toResourcePath(String path) { 217 if (path.startsWith("/")) { 218 return path; 219 } else { 220 return "/" + path; 221 } 222 } 223 224 230 public static String toRelativePath(String path) { 231 if (path.startsWith("/")) { 232 return path.substring(1); 233 } else { 234 return path; 235 } 236 } 237 238 246 247 249 public static String makeTemplateURI(String path, 250 String templateManagerName) { 251 TemplateURI uri = new TemplateURI(); 252 uri.setTemplateManagerName(templateManagerName); 253 uri.setProtocol(TEMPLATE_PROTOCOL); 254 uri.setPath(path); 255 return uri.toURI(); 256 } 257 258 266 267 269 public static String makeRepositoryURI(String repositoryName, String path) { 270 RepositoryURI uri = new RepositoryURI(); 271 uri.setRepositoryName(repositoryName); 272 uri.setProtocol(REPOSITORY_PROTOCOL); 273 uri.setPath(path); 274 return uri.toURI(); 275 } 276 277 285 286 public static String getRealPath(SiteContext siteContext, String path) { 287 if (path == null) { 288 path = ""; 289 } 290 291 if (path.lastIndexOf(".") == -1) { 292 if (!path.endsWith("/")) { 293 path = path + "/"; 294 } 295 path = path + siteContext.getDefaultPage(); 296 } 297 298 return path; 299 } 300 301 } 302 303 | Popular Tags |