1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 24 public class DefaultDocumentIdToPathMapper 25 implements DocumentIdToPathMapper, PathToDocumentIdMapper { 26 27 public static final String BASE_FILENAME_PREFIX = "index"; 28 public static final String BASE_FILENAME_SUFFIX = ".xml"; 29 30 34 public File getFile(Publication publication, String area, String documentId, String language) { 35 File file = new File (getDirectory(publication, area, documentId), getFilename(language)); 36 return file; 37 } 38 39 45 public File getDirectory(Publication publication, String area, String documentId) { 46 assert documentId.startsWith("/"); 47 documentId = documentId.substring(1); 49 documentId = documentId.replace('/', File.separatorChar); 50 51 File file = 52 new File ( 53 publication.getDirectory(), 54 Publication.CONTENT_PATH + File.separator + area + File.separator + documentId); 55 56 return file; 57 } 58 59 63 public String getPath(String documentId, String language) { 64 assert documentId.startsWith("/"); 65 documentId = documentId.substring(1); 67 return documentId + "/" + getFilename(language); 68 } 69 70 76 protected String getFilename(String language) { 77 String languageSuffix = ""; 78 if (language != null && !"".equals(language)) { 79 languageSuffix = "_" + language; 80 } 81 return BASE_FILENAME_PREFIX + languageSuffix + BASE_FILENAME_SUFFIX; 82 } 83 84 93 public String getDocumentId( 94 Publication publication, 95 String area, 96 File file) 97 throws DocumentDoesNotExistException { 98 99 String fileName = file.getAbsolutePath(); 100 String contentDirName = 101 publication.getContentDirectory(area).getAbsolutePath(); 102 if (fileName.startsWith(contentDirName)) { 103 String relativeFileName = 105 fileName.substring(contentDirName.length()); 106 relativeFileName = 108 relativeFileName.substring( 109 0, 110 relativeFileName.lastIndexOf(File.separator)); 111 return relativeFileName.replace(File.separatorChar, '/'); 113 } else { 114 throw new DocumentDoesNotExistException( 115 "No document associated with file" + fileName); 116 } 117 } 118 119 127 public String getLanguage(File file) { 128 String fileName = file.getName(); 129 String language = null; 130 131 133 if (fileName.startsWith(BASE_FILENAME_PREFIX) 134 && fileName.endsWith(BASE_FILENAME_SUFFIX)) { 135 String languageSuffix = 136 fileName.substring( 137 BASE_FILENAME_PREFIX.length(), 138 fileName.indexOf(BASE_FILENAME_SUFFIX)); 139 if (languageSuffix.length() > 0) { 140 language = languageSuffix.substring(1); 142 } 143 } 144 return language; 145 } 146 } 147 | Popular Tags |