KickJava   Java API By Example, From Geeks To Geeks.

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


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: DefaultDocument.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Date JavaDoc;
25
26 /**
27  * A typical CMS document.
28  */

29 public class DefaultDocument implements Document {
30     
31     private String JavaDoc id;
32     private Publication publication;
33     private DublinCore dublincore;
34
35     /**
36      * Creates a new instance of DefaultDocument.
37      * @param publication The publication the document belongs to.
38      * @param id The document ID (starting with a slash).
39      * @deprecated Use {@link DefaultDocumentBuilder} instead.
40      */

41     public DefaultDocument(Publication publication, String JavaDoc id) {
42         
43         if (id == null) {
44             throw new IllegalArgumentException JavaDoc("The document ID must not be null!");
45         }
46         if (!id.startsWith("/")) {
47             throw new IllegalArgumentException JavaDoc("The document ID must start with a slash!");
48         }
49         this.id = id;
50
51         assert(publication != null) && !"".equals(publication.getId());
52         this.publication = publication;
53         this.dublincore = new DublinCoreProxy(this);
54     }
55
56     /**
57      * Creates a new instance of DefaultDocument.
58      * The language of the document is the default language of
59      * the publication.
60      * @param publication The publication the document belongs to.
61      * @param id The document ID (starting with a slash).
62      * @param area The area.
63      */

64     protected DefaultDocument(Publication publication, String JavaDoc id, String JavaDoc area) {
65         if (id == null) {
66             throw new IllegalArgumentException JavaDoc("The document ID must not be null!");
67         }
68         if (!id.startsWith("/")) {
69             throw new IllegalArgumentException JavaDoc("The document ID must start with a slash!");
70         }
71         this.id = id;
72
73         assert(publication != null) && !"".equals(publication.getId());
74         this.publication = publication;
75
76         setArea(area);
77         setLanguage(publication.getDefaultLanguage());
78
79         this.dublincore = new DublinCoreProxy(this);
80
81     }
82
83     /**
84      * Creates a new instance of DefaultDocument.
85      *
86      * @param publication The publication the document belongs to.
87      * @param id The document ID (starting with a slash).
88      * @param area The area.
89      * @param language the language
90      */

91     protected DefaultDocument(Publication publication, String JavaDoc id, String JavaDoc area, String JavaDoc language) {
92         if (id == null) {
93             throw new IllegalArgumentException JavaDoc("The document ID must not be null!");
94         }
95         if (!id.startsWith("/")) {
96             throw new IllegalArgumentException JavaDoc("The document ID must start with a slash!");
97         }
98         this.id = id;
99
100         assert(publication != null) && !"".equals(publication.getId());
101         this.publication = publication;
102         this.language = language;
103         setArea(area);
104
105         this.dublincore = new DublinCoreProxy(this);
106
107     }
108
109     /**
110      * @see org.apache.lenya.cms.publication.Document#getId()
111      */

112     public String JavaDoc getId() {
113         return id;
114     }
115
116     /**
117      * @see org.apache.lenya.cms.publication.Document#getName()
118      */

119     public String JavaDoc getName() {
120         String JavaDoc[] ids = id.split("/");
121         String JavaDoc nodeId = ids[ids.length - 1];
122
123         return nodeId;
124     }
125
126     /**
127      * @see org.apache.lenya.cms.publication.Document#getNodeId()
128      * @deprecated replaced by getName()
129      */

130     public String JavaDoc getNodeId() {
131         return getName();
132     }
133
134     /**
135      * @see org.apache.lenya.cms.publication.Document#getPublication()
136      */

137     public Publication getPublication() {
138         return publication;
139     }
140
141     /**
142      * @see org.apache.lenya.cms.publication.Document#getLastModified()
143      */

144     public Date JavaDoc getLastModified() {
145         return new Date JavaDoc(getFile().lastModified());
146     }
147
148     /**
149      * @see org.apache.lenya.cms.publication.Document#getDublinCore()
150      */

151     public DublinCore getDublinCore() {
152         return dublincore;
153     }
154
155     /**
156      * Returns the file for this document.
157      * @return A file object.
158      */

159     public File JavaDoc getFile() {
160         return getPublication().getPathMapper().getFile(
161             getPublication(),
162             getArea(),
163             getId(),
164             getLanguage());
165     }
166
167     private String JavaDoc language = "";
168
169     /**
170      * @see org.apache.lenya.cms.publication.Document#getLanguage()
171      */

172     public String JavaDoc getLanguage() {
173         return language;
174     }
175
176     /**
177      * @see org.apache.lenya.cms.publication.Document#getLanguage()
178      */

179     public String JavaDoc[] getLanguages() throws DocumentException {
180         ArrayList JavaDoc languages = new ArrayList JavaDoc();
181         SiteTree sitetree;
182         try {
183             sitetree = getPublication().getTree(getArea());
184             if (sitetree != null) {
185                 SiteTreeNode node = sitetree.getNode(getId());
186                 if (node != null) {
187                     Label[] labels = node.getLabels();
188                     for (int i = 0; i < labels.length; i++) {
189                         languages.add(labels[i].getLanguage());
190                     }
191                 }
192             } else {
193                 languages.add(getLanguage());
194             }
195         } catch (SiteTreeException e) {
196             throw new DocumentException(e);
197         }
198
199         return (String JavaDoc[]) languages.toArray(new String JavaDoc[languages.size()]);
200     }
201
202     /**
203      * Sets the language of this document.
204      * @param language The language.
205      */

206     public void setLanguage(String JavaDoc language) {
207         assert language != null;
208         this.language = language;
209     }
210
211     /**
212      * @see org.apache.lenya.cms.publication.Document#getLabel()
213      */

214     public String JavaDoc getLabel() throws DocumentException {
215         String JavaDoc label = "";
216         try {
217             SiteTree siteTree = getPublication().getTree(getArea());
218             if (siteTree != null) {
219                 label = siteTree.getNode(getId()).getLabel(getLanguage()).getLabel();
220             }
221         } catch (SiteTreeException e) {
222             throw new DocumentException(e);
223         }
224         return label;
225     }
226
227     private String JavaDoc area;
228
229     /**
230      * @see org.apache.lenya.cms.publication.Document#getArea()
231      */

232     public String JavaDoc getArea() {
233         return area;
234     }
235
236     /**
237      * @see Document#getCompleteURL(String)
238      */

239     public String JavaDoc getCompleteURL() {
240         return "/" + getPublication().getId() + "/" + getArea() + getDocumentURL();
241     }
242
243     /**
244      * @see Document#getCompleteInfoURL(String)
245      */

246     public String JavaDoc getCompleteInfoURL() {
247         return "/"
248             + getPublication().getId()
249             + "/"
250             + Publication.INFO_AREA_PREFIX
251             + getArea()
252             + getDocumentURL();
253     }
254
255     /**
256      * @see Document#getCompleteURL(String)
257      */

258     public String JavaDoc getCompleteURLWithoutLanguage() {
259         String JavaDoc extensionSuffix = "".equals(getExtension()) ? "" : ("." + getExtension());
260
261         return "/" + getPublication().getId() + "/" + getArea() + getId() + extensionSuffix;
262     }
263
264     /**
265      * Sets the area.
266      * @param area A string.
267      */

268     protected void setArea(String JavaDoc area) {
269         if (!AbstractPublication.isValidArea(area)) {
270             throw new IllegalArgumentException JavaDoc("The area [" + area + "] is not valid!");
271         }
272         this.area = area;
273     }
274
275     private String JavaDoc extension = "html";
276
277     /**
278      * @see org.apache.lenya.cms.publication.Document#getExtension()
279      */

280     public String JavaDoc getExtension() {
281         return extension;
282     }
283
284     /**
285      * Sets the extension of the file in the URL.
286      * @param extension A string.
287      */

288     protected void setExtension(String JavaDoc extension) {
289         assert extension != null;
290         this.extension = extension;
291     }
292
293     private String JavaDoc documentURL;
294
295     /**
296      * Sets the document URL.
297      * @param url The document URL (without publication ID and area).
298      */

299     public void setDocumentURL(String JavaDoc url) {
300         assert url != null;
301         this.documentURL = url;
302     }
303
304     /**
305      * @see org.apache.lenya.cms.publication.Document#getDocumentURL()
306      */

307     public String JavaDoc getDocumentURL() {
308         return documentURL;
309     }
310
311     /** (non-Javadoc)
312      * @see org.apache.lenya.cms.publication.Document#exists()
313      */

314     public boolean exists() throws DocumentException {
315         boolean exists;
316         try {
317             SiteTree sitetree = getPublication().getTree(getArea());
318             if (sitetree != null) {
319                 SiteTreeNode node = sitetree.getNode(getId());
320                 exists = (node != null) && (node.getLabel(getLanguage()) != null);
321             } else {
322                 exists = getFile().exists();
323             }
324         } catch (SiteTreeException e) {
325             throw new DocumentException(e);
326         }
327         return exists;
328     }
329
330     /** (non-Javadoc)
331      * @see org.apache.lenya.cms.publication.Document#existsInAnyLanguage()
332      */

333     public boolean existsInAnyLanguage() throws DocumentException {
334         boolean exists = false;
335         try {
336             SiteTree sitetree = getPublication().getTree(getArea());
337             if (sitetree != null) {
338                 SiteTreeNode node = sitetree.getNode(getId());
339                 exists = node != null;
340             } else {
341                 exists = getFile().exists();
342             }
343         } catch (SiteTreeException e) {
344             throw new DocumentException(e);
345         }
346         return exists;
347     }
348
349     /**
350      * @see java.lang.Object#equals(java.lang.Object)
351      */

352     public boolean equals(Object JavaDoc object) {
353         boolean equals = false;
354         if (getClass().isInstance(object)) {
355             Document document = (Document) object;
356             equals =
357                 getPublication().equals(document.getPublication())
358                     && getId().equals(document.getId())
359                     && getArea().equals(document.getArea())
360                     && getLanguage().equals(document.getLanguage());
361         }
362         return equals;
363
364     }
365
366     /**
367      * @see java.lang.Object#hashCode()
368      */

369     public int hashCode() {
370
371         String JavaDoc key =
372             getPublication().getId()
373                 + ":"
374                 + getPublication().getServletContext()
375                 + ":"
376                 + getArea()
377                 + ":"
378                 + getId()
379                 + ":"
380                 + getLanguage();
381
382         return key.hashCode();
383     }
384
385     /**
386      * @see java.lang.Object#toString()
387      */

388     public String JavaDoc toString() {
389         return getPublication().getId() + ":" + getArea() + ":" + getId() + ":" + getLanguage();
390     }
391
392 }
393
Popular Tags