org.rauschig.jarchivelib.AttributeAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jarchivelib Show documentation
Show all versions of jarchivelib Show documentation
A simple library that facades org.apache.commons.compress, to provide an easy-to-use API for archiving and
compressing into and out of File objects.
/**
* Copyright 2013 Thomas Rausch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rauschig.jarchivelib;
import java.io.IOException;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
import org.apache.commons.compress.archivers.arj.ArjArchiveEntry;
import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
/**
* Adapter for accessing mode flags from the different types of ArchiveEntries.
*
* @param the type of ArchiveEntry
*/
abstract class AttributeAccessor {
private E entry;
public AttributeAccessor(E entry) {
this.entry = entry;
}
/**
* Returns the entry that is being accessed
*
* @return the entry
*/
public E getEntry() {
return entry;
}
/**
* Returns the unix file mode.
*
* @return unix file mode flags
* @throws java.io.IOException
*/
public abstract int getMode() throws IOException;
/**
* Detects the type of the given ArchiveEntry and returns an appropriate AttributeAccessor for it.
*
* @param entry the adaptee
* @return a new attribute accessor instance
*/
public static AttributeAccessor> create(ArchiveEntry entry) {
if (entry instanceof TarArchiveEntry) {
return new TarAttributeAccessor((TarArchiveEntry) entry);
} else if (entry instanceof ZipArchiveEntry) {
return new ZipAttributeAccessor((ZipArchiveEntry) entry);
} else if (entry instanceof CpioArchiveEntry) {
return new CpioAttributeAccessor((CpioArchiveEntry) entry);
} else if (entry instanceof ArjArchiveEntry) {
return new ArjAttributeAccessor((ArjArchiveEntry) entry);
} else if (entry instanceof ArArchiveEntry) {
return new ArAttributeAccessor((ArArchiveEntry) entry);
}
return new FallbackAttributeAccessor(entry);
}
public static class FallbackAttributeAccessor extends AttributeAccessor {
protected FallbackAttributeAccessor(ArchiveEntry entry) {
super(entry);
}
@Override
public int getMode() {
return 0;
}
}
public static class TarAttributeAccessor extends AttributeAccessor {
public TarAttributeAccessor(TarArchiveEntry entry) {
super(entry);
}
@Override
public int getMode() {
return getEntry().getMode();
}
}
public static class ZipAttributeAccessor extends AttributeAccessor {
public ZipAttributeAccessor(ZipArchiveEntry entry) {
super(entry);
}
@Override
public int getMode() {
return getEntry().getUnixMode();
}
}
public static class CpioAttributeAccessor extends AttributeAccessor {
public CpioAttributeAccessor(CpioArchiveEntry entry) {
super(entry);
}
@Override
public int getMode() {
return (int) getEntry().getMode();
}
}
public static class ArjAttributeAccessor extends AttributeAccessor {
public ArjAttributeAccessor(ArjArchiveEntry entry) {
super(entry);
}
@Override
public int getMode() throws IOException {
return getEntry().getMode();
}
}
public static class ArAttributeAccessor extends AttributeAccessor {
public ArAttributeAccessor(ArArchiveEntry entry) {
super(entry);
}
@Override
public int getMode() throws IOException {
return getEntry().getMode();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy