/*
 * Copyright 2002-2006 The Apache Software Foundation.
 *
 * 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.apache.commons.vfs.util;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.impl.DecoratedFileObject;
import org.apache.commons.vfs.provider.AbstractFileObject;

/**
 * Stuff to get some strange things from an FileObject
 *
 * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
 * @version $Revision$ $Date$
 */
public class FileObjectUtils
{
	private FileObjectUtils()
	{
	}

	/**
	 * get access to the base object even if decorated
	 */
	public static AbstractFileObject getAbstractFileObject(final FileObject fileObject) throws FileSystemException
	{
		Object searchObject = fileObject;
		while (searchObject instanceof DecoratedFileObject)
		{
			searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
		}
		if (searchObject instanceof AbstractFileObject)
		{
			return (AbstractFileObject) searchObject;
		}
		
        throw new FileSystemException("vfs.util/find-abstract-file-object.error", fileObject==null?"null":fileObject.getClass().getName());
	}

	/**
	 * check if the given FileObject is instance of given class argument 
	 */
	public static boolean isInstanceOf(final FileObject fileObject, final Class wantedClass) throws FileSystemException
	{
		Object searchObject = fileObject;
		while (searchObject instanceof DecoratedFileObject)
		{
			if (wantedClass.isInstance(searchObject))
			{
				return true;
			}
			
			searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
		}
		
		if (wantedClass.isInstance(searchObject))
		{
			return true;
		}
		
		return false;
	}
}

@@ -1548,18 +1548,95 @@
 ct;%0A%09%09%7D%0A
-%09%09
+        if (searchObject == null)%0A        %7B%0A            return null;%0A        %7D%0A
 %0A       
@@ -1846,17 +1846,16 @@
 argument
- 
 %0A%09 */%0A%09p
@@ -2142,12 +2142,9 @@
 %09%09%7D%0A
-%09%09%09
 %0A
+
 %09%09%09s
@@ -2224,19 +2224,17 @@
 ();%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09if (wa
@@ -2293,18 +2293,16 @@
 ue;%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur

return null if searchObject is null