KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > DocumentHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: DocumentHelper.java 45909 2004-09-11 19:04:17Z roku $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
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 /**
32  * Helper class to handle documents from XSP.
33  */

34 public class DocumentHelper {
35
36     private Map JavaDoc objectModel;
37
38     /**
39      * Ctor.
40      *
41      * @param objectModel The Cocoon object model.
42      */

43     public DocumentHelper(Map JavaDoc objectModel) {
44         this.objectModel = objectModel;
45     }
46
47     /**
48      * Creates a document URL.<br/>
49      * If the document ID is null, the current document ID is used.<br/>
50      * If the document area is null, the current area is used.<br/>
51      * If the language is null, the current language is used.
52      * @param documentId The target document ID.
53      * @param documentArea The target area.
54      * @param language The target language.
55      * @return A string.
56      * @throws ProcessingException if something went wrong.
57      */

58     public String JavaDoc getDocumentUrl(String JavaDoc documentId, String JavaDoc documentArea, String JavaDoc language)
59         throws ProcessingException {
60
61         String JavaDoc 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 JavaDoc webappUrl = ServletHelper.getWebappURI(request);
73                 URLInformation info = new URLInformation(webappUrl);
74                 String JavaDoc 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 JavaDoc contextPath = request.getContextPath();
88             if (contextPath == null) {
89                 contextPath = "";
90             }
91
92             url = contextPath + url;
93
94         } catch (Exception JavaDoc e) {
95             throw new ProcessingException(e);
96         }
97
98         return url;
99
100     }
101
102     /**
103      * Returns the complete URL of the parent document. If the document is a top-level document,
104      * the /index document is chosen. If the parent does not exist in the appropriate language, the
105      * default language is chosen.
106      *
107      * @return A string.
108      * @throws ProcessingException when something went wrong.
109      */

110     public String JavaDoc 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 JavaDoc webappUrl = ServletHelper.getWebappURI(request);
124         URLInformation info = new URLInformation(webappUrl);
125         String JavaDoc completeArea = info.getCompleteArea();
126         DocumentBuilder builder = publication.getDocumentBuilder();
127
128         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc contextPath = request.getContextPath();
154         if (contextPath == null) {
155             contextPath = "";
156         }
157
158         return contextPath + parentUrl;
159     }
160
161     /**
162      * Returns an existing language version of a document. If the document exists in the default
163      * language, the default language version is returned. Otherwise, a random language version is
164      * returned. If no language version exists, a DocumentException is thrown.
165      *
166      * @param document The document.
167      * @return A document.
168      * @throws DocumentException when an error occurs.
169      */

170     public static Document getExistingLanguageVersion(Document document) throws DocumentException {
171         return getExistingLanguageVersion(document, document.getPublication().getDefaultLanguage());
172     }
173     /**
174      * Returns an existing language version of a document. If the document exists in the preferred
175      * language, this version is returned. Otherwise, if the document exists in the default
176      * language, the default language version is returned. Otherwise, a random language version is
177      * returned. If no language version exists, a DocumentException is thrown.
178      *
179      * @param document The document.
180      * @param preferredLanguage The preferred language.
181      * @return A document.
182      * @throws DocumentException when an error occurs.
183      */

184     public static Document getExistingLanguageVersion(Document document, String JavaDoc preferredLanguage)
185         throws DocumentException {
186
187         Publication publication = document.getPublication();
188         DocumentBuilder builder = publication.getDocumentBuilder();
189
190         String JavaDoc[] 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 JavaDoc languageList = Arrays.asList(languages);
198
199         String JavaDoc 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     /**
215      * Returns the parent document of a document in the same language.
216      * @param document The document.
217      * @return A document or <code>null</code> if the document parameter is a top-level document.
218      * @throws ProcessingException when the parent document could not be created.
219      */

220     public static Document getParentDocument(Document document) throws ProcessingException {
221
222         Document parent = null;
223         String JavaDoc id = document.getId();
224         int lastSlashIndex = id.lastIndexOf("/");
225         if (lastSlashIndex > 0) {
226             String JavaDoc parentId = id.substring(0, lastSlashIndex);
227             Publication publication = document.getPublication();
228             DocumentBuilder builder = publication.getDocumentBuilder();
229             String JavaDoc 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     /**
246      * Returns e.g. context:/lenya/pubs/default/content/authoring/tutorial/index_en.xml
247      * @param document The document to calculate the path for.
248      * @return The path of a document within Lenya (excluding the servlet context).
249      */

250     public String JavaDoc getSourceUri(final Document document) {
251         return
252             "context:/" // Use lenya:/ protocoll here later
253
+ 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