KickJava   Java API By Example, From Geeks To Geeks.

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


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: DefaultDocumentBuilder.java 109967 2004-12-06 09:42:31Z andreas $ */
19
20 package org.apache.lenya.cms.publication;
21
22 public class DefaultDocumentBuilder implements DocumentBuilder {
23     /**
24      * Non-public constructor.
25      */

26     protected DefaultDocumentBuilder() {
27     }
28
29     private static DefaultDocumentBuilder instance;
30
31     /**
32      * Returns the singleton instance.
33      *
34      * @return A document builder.
35      */

36     public static DefaultDocumentBuilder getInstance() {
37         if (instance == null) {
38             instance = new DefaultDocumentBuilder();
39         }
40
41         return instance;
42     }
43
44     /**
45      * @see org.apache.lenya.cms.publication.DocumentBuilder#buildDocument(org.apache.lenya.cms.publication.Publication,
46      * java.lang.String)
47      */

48     public Document buildDocument(Publication publication, String JavaDoc url)
49         throws DocumentBuildException {
50
51         URLInformation info = new URLInformation(url);
52
53         String JavaDoc documentURL = info.getDocumentUrl();
54         String JavaDoc originalURL = documentURL;
55
56         String JavaDoc extension = getExtension(documentURL);
57         documentURL = removeExtensions(documentURL);
58
59         String JavaDoc language = getLanguage(documentURL);
60         String JavaDoc 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 JavaDoc 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     /**
83      * Creates a new document object.
84      * Override this method to create specific document objects,
85      * e.g., for different document IDs.
86      * @param publication The publication.
87      * @param area The area.
88      * @param documentId The document ID.
89      * @param language The language.
90      * @return A document.
91      * @throws DocumentException when something went wrong.
92      */

93     protected DefaultDocument createDocument(
94         Publication publication,
95         String JavaDoc area,
96         String JavaDoc documentId,
97         String JavaDoc language)
98         throws DocumentBuildException {
99         DefaultDocument document = new DefaultDocument(publication, documentId, area, language);
100         return document;
101     }
102
103     /**
104      * Removes all "."-separated extensions from a URL (e.g., <code>/foo.print.html</code> is
105      * transformed to <code>/foo</code>).
106      *
107      * @param url The URL to trim.
108      * @return A URL string.
109      */

110     protected String JavaDoc removeExtensions(String JavaDoc url) {
111         int dotIndex = url.indexOf(".");
112         if (dotIndex > -1) {
113             url = url.substring(0, dotIndex);
114         }
115         return url;
116     }
117
118     /**
119      * Returns the language of a URL.
120      *
121      * @param urlWithoutSuffix The URL without the suffix.
122      * @return A string.
123      */

124     protected String JavaDoc getLanguage(String JavaDoc urlWithoutSuffix) {
125
126         String JavaDoc language = "";
127         String JavaDoc url = urlWithoutSuffix;
128         
129         int languageSeparatorIndex = url.lastIndexOf("_");
130         if (languageSeparatorIndex > -1) {
131             String JavaDoc suffix = url.substring(languageSeparatorIndex + 1);
132             if (suffix.length() <= 5) {
133                 language = suffix;
134             }
135         }
136         return language;
137     }
138
139     /**
140      * Returns the extension of a URL.
141      *
142      * @param url The URL.
143      * @return The extension.
144      */

145     protected String JavaDoc getExtension(String JavaDoc url) {
146         int startOfSuffix = url.lastIndexOf('.');
147         String JavaDoc suffix = "";
148
149         if ((startOfSuffix > -1) && !url.endsWith(".")) {
150             suffix = url.substring(startOfSuffix + 1);
151         }
152
153         return suffix;
154     }
155
156     /**
157      * @see org.apache.lenya.cms.publication.DocumentBuilder#isDocument(org.apache.lenya.cms.publication.Publication,
158      * java.lang.String)
159      */

160     public boolean isDocument(Publication publication, String JavaDoc url) throws DocumentBuildException {
161         boolean isDocument = false;
162
163         String JavaDoc 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 JavaDoc area = publicationURI.substring(0, slashIndex);
170                 String JavaDoc documentUri = publicationURI.substring(slashIndex);
171                 if (AbstractPublication.isValidArea(area) && documentUri.startsWith("/")) {
172                     isDocument = true;
173                 }
174             }
175         }
176
177         return isDocument;
178     }
179
180     /**
181      * Builds the canonical document URL.
182      *
183      * @param publication The publication.
184      * @param documentid The document ID.
185      * @param language The language of the document.
186      * @return A string.
187      */

188     protected String JavaDoc buildCanonicalDocumentUrl(
189         Publication publication,
190         String JavaDoc documentid,
191         String JavaDoc language) {
192
193         String JavaDoc languageSuffix = "";
194         if (!language.equals(publication.getDefaultLanguage())) {
195             languageSuffix = "_" + language;
196         }
197
198         String JavaDoc url = documentid + languageSuffix + ".html";
199         return url;
200     }
201
202     /**
203      * @see org.apache.lenya.cms.publication.DocumentBuilder#buildCanonicalUrl(org.apache.lenya.cms.publication.Publication,
204      * java.lang.String, java.lang.String, java.lang.String)
205      */

206     public String JavaDoc buildCanonicalUrl(
207         Publication publication,
208         String JavaDoc area,
209         String JavaDoc documentid,
210         String JavaDoc language) {
211
212         String JavaDoc documentUrl = buildCanonicalDocumentUrl(publication, documentid, language);
213         String JavaDoc url = "/" + publication.getId() + "/" + area + documentUrl;
214
215         return url;
216     }
217
218     /**
219      * @see org.apache.lenya.cms.publication.DocumentBuilder#buildCanonicalUrl(org.apache.lenya.cms.publication.Publication,
220      * java.lang.String, java.lang.String)
221      */

222     public String JavaDoc buildCanonicalUrl(Publication publication, String JavaDoc area, String JavaDoc documentId) {
223         return buildCanonicalUrl(publication, area, documentId, publication.getDefaultLanguage());
224     }
225
226     /**
227      * @see org.apache.lenya.cms.publication.DocumentBuilder#buildLanguageVersion(org.apache.lenya.cms.publication.Document,
228      * java.lang.String)
229      */

230     public Document buildLanguageVersion(Document document, String JavaDoc 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 JavaDoc url =
239             buildCanonicalDocumentUrl(document.getPublication(), document.getId(), language);
240         newDocument.setDocumentURL(url);
241
242         return newDocument;
243     }
244
245 }
246
Popular Tags