1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.util.Arrays ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.environment.ObjectModelHelper; 28 import org.apache.cocoon.environment.Request; 29 import org.apache.lenya.util.ServletHelper; 30 31 34 public class DocumentHelper { 35 36 private Map objectModel; 37 38 43 public DocumentHelper(Map objectModel) { 44 this.objectModel = objectModel; 45 } 46 47 58 public String getDocumentUrl(String documentId, String documentArea, String language) 59 throws ProcessingException { 60 61 String url = null; 62 try { 63 PageEnvelope envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 64 65 if (documentId == null) { 66 documentId = envelope.getDocument().getId(); 67 } 68 69 Request request = ObjectModelHelper.getRequest(objectModel); 70 71 if (documentArea == null) { 72 String webappUrl = ServletHelper.getWebappURI(request); 73 URLInformation info = new URLInformation(webappUrl); 74 String completeArea = info.getCompleteArea(); 75 documentArea = completeArea; 76 } 77 78 if (language == null) { 79 language = envelope.getDocument().getLanguage(); 80 } 81 82 Publication publication = envelope.getPublication(); 83 DocumentBuilder builder = publication.getDocumentBuilder(); 84 85 url = builder.buildCanonicalUrl(publication, documentArea, documentId, language); 86 87 String contextPath = request.getContextPath(); 88 if (contextPath == null) { 89 contextPath = ""; 90 } 91 92 url = contextPath + url; 93 94 } catch (Exception e) { 95 throw new ProcessingException(e); 96 } 97 98 return url; 99 100 } 101 102 110 public String getCompleteParentUrl() throws ProcessingException { 111 112 PageEnvelope envelope; 113 try { 114 envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 115 } catch (PageEnvelopeException e) { 116 throw new ProcessingException(e); 117 } 118 119 Document document = envelope.getDocument(); 120 Publication publication = envelope.getPublication(); 121 122 Request request = ObjectModelHelper.getRequest(objectModel); 123 String webappUrl = ServletHelper.getWebappURI(request); 124 URLInformation info = new URLInformation(webappUrl); 125 String completeArea = info.getCompleteArea(); 126 DocumentBuilder builder = publication.getDocumentBuilder(); 127 128 String parentId; 129 130 int lastSlashIndex = document.getId().lastIndexOf("/"); 131 if (lastSlashIndex > 0) { 132 parentId = document.getId().substring(0, lastSlashIndex); 133 } else { 134 parentId = "/index"; 135 } 136 137 String parentUrl = builder.buildCanonicalUrl(publication, completeArea, parentId); 138 Document parentDocument; 139 140 try { 141 parentDocument = builder.buildDocument(publication, parentUrl); 142 parentDocument = getExistingLanguageVersion(parentDocument, document.getLanguage()); 143 } catch (Exception e) { 144 throw new ProcessingException(e); 145 } 146 parentUrl = 147 builder.buildCanonicalUrl( 148 publication, 149 completeArea, 150 parentDocument.getId(), 151 parentDocument.getLanguage()); 152 153 String contextPath = request.getContextPath(); 154 if (contextPath == null) { 155 contextPath = ""; 156 } 157 158 return contextPath + parentUrl; 159 } 160 161 170 public static Document getExistingLanguageVersion(Document document) throws DocumentException { 171 return getExistingLanguageVersion(document, document.getPublication().getDefaultLanguage()); 172 } 173 184 public static Document getExistingLanguageVersion(Document document, String preferredLanguage) 185 throws DocumentException { 186 187 Publication publication = document.getPublication(); 188 DocumentBuilder builder = publication.getDocumentBuilder(); 189 190 String [] languages = document.getLanguages(); 191 192 if (languages.length == 0) { 193 throw new DocumentException( 194 "The document [" + document.getId() + "] does not exist in any language!"); 195 } 196 197 List languageList = Arrays.asList(languages); 198 199 String existingLanguage = null; 200 201 if (languageList.contains(preferredLanguage)) { 202 existingLanguage = preferredLanguage; 203 } else if (languageList.contains(publication.getDefaultLanguage())) { 204 existingLanguage = publication.getDefaultLanguage(); 205 } else { 206 existingLanguage = languages[0]; 207 } 208 209 document = builder.buildLanguageVersion(document, existingLanguage); 210 211 return document; 212 } 213 214 220 public static Document getParentDocument(Document document) throws ProcessingException { 221 222 Document parent = null; 223 String id = document.getId(); 224 int lastSlashIndex = id.lastIndexOf("/"); 225 if (lastSlashIndex > 0) { 226 String parentId = id.substring(0, lastSlashIndex); 227 Publication publication = document.getPublication(); 228 DocumentBuilder builder = publication.getDocumentBuilder(); 229 String url = 230 builder.buildCanonicalUrl( 231 publication, 232 document.getArea(), 233 parentId, 234 document.getLanguage()); 235 try { 236 parent = builder.buildDocument(publication, url); 237 } catch (DocumentBuildException e) { 238 throw new ProcessingException(e); 239 } 240 } 241 242 return parent; 243 } 244 245 250 public String getSourceUri(final Document document) { 251 return 252 "context:/" + Publication.PUBLICATION_PREFIX_URI 254 + "/" 255 + document.getPublication().getId() 256 + "/" 257 + Publication.CONTENT_PATH 258 + "/" 259 + document.getArea() 260 + "/" 261 + document.getPublication().getPathMapper().getPath( 262 document.getId(), 263 document.getLanguage()); 264 265 } 266 } 267 | Popular Tags |