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.util; 018 019import org.apache.commons.vfs2.FileObject; 020import org.apache.commons.vfs2.FileSystemException; 021import org.apache.commons.vfs2.impl.DecoratedFileObject; 022import org.apache.commons.vfs2.provider.AbstractFileObject; 023 024/** 025 * Stuff to get some strange things from an FileObject. 026 */ 027public final class FileObjectUtils { 028 private FileObjectUtils() { 029 } 030 031 /** 032 * Get access to the base object even if decorated. 033 * 034 * @param fileObject The FileObject. 035 * @return The decorated FileObject or null. 036 * @throws FileSystemException if an error occurs. 037 */ 038 public static AbstractFileObject getAbstractFileObject(final FileObject fileObject) throws FileSystemException { 039 Object searchObject = fileObject; 040 while (searchObject instanceof DecoratedFileObject) { 041 searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject(); 042 } 043 if (searchObject instanceof AbstractFileObject) { 044 return (AbstractFileObject) searchObject; 045 } 046 if (searchObject == null) { 047 return null; 048 } 049 050 throw new FileSystemException("vfs.util/find-abstract-file-object.error", 051 fileObject == null ? "null" : fileObject.getClass().getName()); 052 } 053 054 /** 055 * Check if the given FileObject is instance of given class argument. 056 * 057 * @param fileObject The FileObject. 058 * @param wantedClass The Class to check. 059 * @return true if fileObject is an instance of the specified Class. 060 * @throws FileSystemException if an error occurs. 061 */ 062 public static boolean isInstanceOf(final FileObject fileObject, final Class<?> wantedClass) 063 throws FileSystemException { 064 Object searchObject = fileObject; 065 while (searchObject instanceof DecoratedFileObject) { 066 if (wantedClass.isInstance(searchObject)) { 067 return true; 068 } 069 070 searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject(); 071 } 072 073 if (wantedClass.isInstance(searchObject)) { 074 return true; 075 } 076 077 return false; 078 } 079}