KickJava   Java API By Example, From Geeks To Geeks.

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


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: DefaultDocumentIdToPathMapper.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.io.File JavaDoc;
23
24 public class DefaultDocumentIdToPathMapper
25     implements DocumentIdToPathMapper, PathToDocumentIdMapper {
26         
27     public static final String JavaDoc BASE_FILENAME_PREFIX = "index";
28     public static final String JavaDoc BASE_FILENAME_SUFFIX = ".xml";
29
30     /**
31      * @see org.apache.lenya.cms.publication.DocumentIdToPathMapper#getFile(org.apache.lenya.cms.publication.Publication,
32      * java.lang.String, java.lang.String, java.lang.String)
33      */

34     public File JavaDoc getFile(Publication publication, String JavaDoc area, String JavaDoc documentId, String JavaDoc language) {
35         File JavaDoc file = new File JavaDoc(getDirectory(publication, area, documentId), getFilename(language));
36         return file;
37     }
38
39     /**
40      * (non-Javadoc)
41      *
42      * @see org.apache.lenya.cms.publication.DocumentIdToPathMapper#getDirectory(org.apache.lenya.cms.publication.Publication,
43      * java.lang.String, java.lang.String)
44      */

45     public File JavaDoc getDirectory(Publication publication, String JavaDoc area, String JavaDoc documentId) {
46         assert documentId.startsWith("/");
47         // remove leading slash
48
documentId = documentId.substring(1);
49         documentId = documentId.replace('/', File.separatorChar);
50
51         File JavaDoc file =
52             new File JavaDoc(
53                 publication.getDirectory(),
54                 Publication.CONTENT_PATH + File.separator + area + File.separator + documentId);
55
56         return file;
57     }
58
59     /**
60      * @see org.apache.lenya.cms.publication.DocumentIdToPathMapper#getPath(java.lang.String,
61      * java.lang.String)
62      */

63     public String JavaDoc getPath(String JavaDoc documentId, String JavaDoc language) {
64         assert documentId.startsWith("/");
65         // remove leading slash
66
documentId = documentId.substring(1);
67         return documentId + "/" + getFilename(language);
68     }
69
70     /**
71      * Constructs the filename for a given language.
72      *
73      * @param language The language.
74      * @return A string value.
75      */

76     protected String JavaDoc getFilename(String JavaDoc language) {
77         String JavaDoc languageSuffix = "";
78         if (language != null && !"".equals(language)) {
79             languageSuffix = "_" + language;
80         }
81         return BASE_FILENAME_PREFIX + languageSuffix + BASE_FILENAME_SUFFIX;
82     }
83
84     /**
85      * Returns the document ID for a certain file.
86      *
87      * @param publication The publication.
88      * @param area The area.
89      * @param file The file representing the document.
90      * @throws DocumentDoesNotExistException when the document
91      * referenced by the file does not exist.
92      */

93     public String JavaDoc getDocumentId(
94         Publication publication,
95         String JavaDoc area,
96         File JavaDoc file)
97         throws DocumentDoesNotExistException {
98
99         String JavaDoc fileName = file.getAbsolutePath();
100         String JavaDoc contentDirName =
101             publication.getContentDirectory(area).getAbsolutePath();
102         if (fileName.startsWith(contentDirName)) {
103             // trim everything up to the documentId
104
String JavaDoc relativeFileName =
105                 fileName.substring(contentDirName.length());
106             // trim everything after the documentId
107
relativeFileName =
108                 relativeFileName.substring(
109                     0,
110                     relativeFileName.lastIndexOf(File.separator));
111             // and replace the os specific separator by '/'
112
return relativeFileName.replace(File.separatorChar, '/');
113         } else {
114             throw new DocumentDoesNotExistException(
115                 "No document associated with file" + fileName);
116         }
117     }
118     
119     /**
120      * Returns the language for a certain file
121      *
122      * @param file the document file
123      *
124      * @return the language for the given document file or null if
125      * the file has no language.
126      */

127     public String JavaDoc getLanguage(File JavaDoc file) {
128         String JavaDoc fileName = file.getName();
129         String JavaDoc language = null;
130
131         // check if the file is of the form index.html or index_en.html
132

133         if (fileName.startsWith(BASE_FILENAME_PREFIX)
134             && fileName.endsWith(BASE_FILENAME_SUFFIX)) {
135             String JavaDoc languageSuffix =
136                 fileName.substring(
137                     BASE_FILENAME_PREFIX.length(),
138                     fileName.indexOf(BASE_FILENAME_SUFFIX));
139             if (languageSuffix.length() > 0) {
140                 // trim the leading '_'
141
language = languageSuffix.substring(1);
142             }
143         }
144         return language;
145     }
146 }
147
Popular Tags