001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.cms.publication;
020:
021: import java.io.File;
022:
023: /**
024: * Default DocumentIdToPathMapper implementation.
025: *
026: * @version $Id: DefaultDocumentIdToPathMapper.java 473861 2006-11-12 03:51:14Z gregor $
027: */
028: public class DefaultDocumentIdToPathMapper implements
029: DocumentIdToPathMapper, PathToDocumentIdMapper {
030:
031: /**
032: * The file name.
033: */
034: public static final String BASE_FILENAME_PREFIX = "index";
035:
036: /**
037: * @see org.apache.lenya.cms.publication.DocumentIdToPathMapper#getFile(org.apache.lenya.cms.publication.Publication,
038: * java.lang.String, java.lang.String, java.lang.String)
039: */
040: public File getFile(Publication publication, String area,
041: String uuid, String language) {
042: File file = new File(getDirectory(publication, area), getPath(
043: uuid, language));
044: return file;
045: }
046:
047: protected File getDirectory(Publication publication, String area) {
048:
049: File file = new File(publication.getDirectory(),
050: Publication.CONTENT_PATH + File.separator + area);
051:
052: return file;
053: }
054:
055: /**
056: * @see org.apache.lenya.cms.publication.DocumentIdToPathMapper#getPath(java.lang.String,
057: * java.lang.String)
058: */
059: public String getPath(String uuid, String language) {
060: if (uuid.startsWith("/")) {
061: return uuid.substring(1) + "/" + getFilename(language);
062: } else {
063: return uuid + "/" + language;
064: }
065: }
066:
067: /**
068: * Constructs the filename for a given language.
069: *
070: * @param language The language.
071: * @return A string value.
072: */
073: protected String getFilename(String language) {
074: String languageSuffix = "";
075: if (language != null && !"".equals(language)) {
076: languageSuffix = "_" + language;
077: }
078: return BASE_FILENAME_PREFIX + languageSuffix;
079: }
080:
081: /**
082: * Returns the document ID for a certain file.
083: * @param publication The publication.
084: * @param area The area.
085: * @param file The file representing the document.
086: * @return A string.
087: * @throws DocumentDoesNotExistException when the document referenced by the file does not
088: * exist.
089: */
090: public String getDocumentId(Publication publication, String area,
091: File file) throws DocumentDoesNotExistException {
092:
093: String fileName = file.getAbsolutePath();
094: String contentDirName = publication.getContentDirectory(area)
095: .getAbsolutePath();
096: if (fileName.startsWith(contentDirName)) {
097: // trim everything up to the documentId
098: String relativeFileName = fileName.substring(contentDirName
099: .length());
100: // trim everything after the documentId
101: relativeFileName = relativeFileName.substring(0,
102: relativeFileName.lastIndexOf(File.separator));
103: // and replace the os specific separator by '/'
104: return relativeFileName.replace(File.separatorChar, '/');
105: }
106: // Document does not seem to exist
107: throw new DocumentDoesNotExistException(
108: "No document associated with file" + fileName);
109: }
110:
111: /**
112: * Returns the language for a certain file
113: *
114: * @param file the document file
115: *
116: * @return the language for the given document file or null if the file has no language.
117: */
118: public String getLanguage(File file) {
119: String fileName = file.getName();
120: String language = null;
121:
122: int lastDotIndex = fileName.lastIndexOf(".");
123: String suffix = fileName.substring(lastDotIndex);
124:
125: // check if the file is of the form index.html or index_en.html
126:
127: if (fileName.startsWith(BASE_FILENAME_PREFIX)
128: && fileName.endsWith(suffix)) {
129: String languageSuffix = fileName.substring(
130: BASE_FILENAME_PREFIX.length(), fileName
131: .indexOf(suffix));
132: if (languageSuffix.length() > 0) {
133: // trim the leading '_'
134: language = languageSuffix.substring(1);
135: }
136: }
137: return language;
138: }
139: }
|