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: package org.apache.commons.vfs.provider.compressed;
018:
019: import org.apache.commons.vfs.Capability;
020: import org.apache.commons.vfs.FileName;
021: import org.apache.commons.vfs.FileObject;
022: import org.apache.commons.vfs.FileSystemException;
023: import org.apache.commons.vfs.FileType;
024: import org.apache.commons.vfs.provider.AbstractFileObject;
025:
026: /**
027: * A compressed file.<br>
028: * Such a file do only have one child (the compressed filename with stripped last extension)
029: *
030: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
031: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
032: */
033: public abstract class CompressedFileFileObject extends
034: AbstractFileObject implements FileObject {
035: private final FileObject container;
036: private String[] children;
037:
038: protected CompressedFileFileObject(FileName name,
039: FileObject container, CompressedFileFileSystem fs) {
040: super (name, fs);
041: this .container = container;
042:
043: // todo, add getBaseName(String) to FileName
044: String basename = container.getName().getBaseName();
045: int pos = basename.lastIndexOf('.');
046: basename = basename.substring(0, pos);
047:
048: children = new String[] { basename };
049: }
050:
051: /**
052: * Determines if this file can be written to.
053: *
054: * @return <code>true</code> if this file is writeable, <code>false</code> if not.
055: */
056: public boolean isWriteable() throws FileSystemException {
057: return getFileSystem().hasCapability(Capability.WRITE_CONTENT);
058: }
059:
060: /**
061: * Returns the file's type.
062: */
063: protected FileType doGetType() throws FileSystemException {
064: if (getName().getPath().endsWith("/")) {
065: return FileType.FOLDER;
066: } else {
067: return FileType.FILE;
068: }
069: }
070:
071: /**
072: * Lists the children of the file.
073: */
074: protected String[] doListChildren() {
075: return children;
076: }
077:
078: /**
079: * Returns the size of the file content (in bytes). Is only called if
080: * {@link #doGetType} returns {@link FileType#FILE}.
081: */
082: protected long doGetContentSize() {
083: return -1;
084: }
085:
086: /**
087: * Returns the last modified time of this file.
088: */
089: protected long doGetLastModifiedTime() throws Exception {
090: return container.getContent().getLastModifiedTime();
091: }
092:
093: protected FileObject getContainer() {
094: return container;
095: }
096:
097: public void createFile() throws FileSystemException {
098: container.createFile();
099: injectType(FileType.FILE);
100: }
101: }
|