1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 public class DefaultDocumentBuilder implements DocumentBuilder { 23 26 protected DefaultDocumentBuilder() { 27 } 28 29 private static DefaultDocumentBuilder instance; 30 31 36 public static DefaultDocumentBuilder getInstance() { 37 if (instance == null) { 38 instance = new DefaultDocumentBuilder(); 39 } 40 41 return instance; 42 } 43 44 48 public Document buildDocument(Publication publication, String url) 49 throws DocumentBuildException { 50 51 URLInformation info = new URLInformation(url); 52 53 String documentURL = info.getDocumentUrl(); 54 String originalURL = documentURL; 55 56 String extension = getExtension(documentURL); 57 documentURL = removeExtensions(documentURL); 58 59 String language = getLanguage(documentURL); 60 String fullLanguage = "".equals(language) ? "" : ("_" + language); 61 documentURL = documentURL.substring(0, documentURL.length() - fullLanguage.length()); 62 63 if ("".equals(language)) { 64 language = publication.getDefaultLanguage(); 65 } 66 67 String documentId = documentURL; 68 69 if (!documentId.startsWith("/")) { 70 throw new DocumentBuildException( 71 "Document ID [" + documentId + "] does not start with '/'!"); 72 } 73 74 DefaultDocument document = 75 createDocument(publication, info.getArea(), documentId, language); 76 document.setExtension(extension); 77 document.setDocumentURL(originalURL); 78 79 return document; 80 } 81 82 93 protected DefaultDocument createDocument( 94 Publication publication, 95 String area, 96 String documentId, 97 String language) 98 throws DocumentBuildException { 99 DefaultDocument document = new DefaultDocument(publication, documentId, area, language); 100 return document; 101 } 102 103 110 protected String removeExtensions(String url) { 111 int dotIndex = url.indexOf("."); 112 if (dotIndex > -1) { 113 url = url.substring(0, dotIndex); 114 } 115 return url; 116 } 117 118 124 protected String getLanguage(String urlWithoutSuffix) { 125 126 String language = ""; 127 String url = urlWithoutSuffix; 128 129 int languageSeparatorIndex = url.lastIndexOf("_"); 130 if (languageSeparatorIndex > -1) { 131 String suffix = url.substring(languageSeparatorIndex + 1); 132 if (suffix.length() <= 5) { 133 language = suffix; 134 } 135 } 136 return language; 137 } 138 139 145 protected String getExtension(String url) { 146 int startOfSuffix = url.lastIndexOf('.'); 147 String suffix = ""; 148 149 if ((startOfSuffix > -1) && !url.endsWith(".")) { 150 suffix = url.substring(startOfSuffix + 1); 151 } 152 153 return suffix; 154 } 155 156 160 public boolean isDocument(Publication publication, String url) throws DocumentBuildException { 161 boolean isDocument = false; 162 163 String publicationURI = url.substring(("/" + publication.getId()).length()); 164 if (publicationURI.startsWith("/")) { 165 publicationURI = publicationURI.substring(1); 166 167 int slashIndex = publicationURI.indexOf("/"); 168 if (slashIndex > -1) { 169 String area = publicationURI.substring(0, slashIndex); 170 String documentUri = publicationURI.substring(slashIndex); 171 if (AbstractPublication.isValidArea(area) && documentUri.startsWith("/")) { 172 isDocument = true; 173 } 174 } 175 } 176 177 return isDocument; 178 } 179 180 188 protected String buildCanonicalDocumentUrl( 189 Publication publication, 190 String documentid, 191 String language) { 192 193 String languageSuffix = ""; 194 if (!language.equals(publication.getDefaultLanguage())) { 195 languageSuffix = "_" + language; 196 } 197 198 String url = documentid + languageSuffix + ".html"; 199 return url; 200 } 201 202 206 public String buildCanonicalUrl( 207 Publication publication, 208 String area, 209 String documentid, 210 String language) { 211 212 String documentUrl = buildCanonicalDocumentUrl(publication, documentid, language); 213 String url = "/" + publication.getId() + "/" + area + documentUrl; 214 215 return url; 216 } 217 218 222 public String buildCanonicalUrl(Publication publication, String area, String documentId) { 223 return buildCanonicalUrl(publication, area, documentId, publication.getDefaultLanguage()); 224 } 225 226 230 public Document buildLanguageVersion(Document document, String language) { 231 DefaultDocument newDocument = 232 new DefaultDocument( 233 document.getPublication(), 234 document.getId(), 235 document.getArea(), 236 language); 237 newDocument.setExtension(document.getExtension()); 238 String url = 239 buildCanonicalDocumentUrl(document.getPublication(), document.getId(), language); 240 newDocument.setDocumentURL(url); 241 242 return newDocument; 243 } 244 245 } 246 | Popular Tags |