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