01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.vfs.util;
18:
19: import org.apache.commons.vfs.FileObject;
20: import org.apache.commons.vfs.FileSystemException;
21: import org.apache.commons.vfs.impl.DecoratedFileObject;
22: import org.apache.commons.vfs.provider.AbstractFileObject;
23:
24: /**
25: * Stuff to get some strange things from an FileObject
26: *
27: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
28: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
29: */
30: public class FileObjectUtils {
31: private FileObjectUtils() {
32: }
33:
34: /**
35: * get access to the base object even if decorated
36: */
37: public static AbstractFileObject getAbstractFileObject(
38: final FileObject fileObject) throws FileSystemException {
39: Object searchObject = fileObject;
40: while (searchObject instanceof DecoratedFileObject) {
41: searchObject = ((DecoratedFileObject) searchObject)
42: .getDecoratedFileObject();
43: }
44: if (searchObject instanceof AbstractFileObject) {
45: return (AbstractFileObject) searchObject;
46: }
47: if (searchObject == null) {
48: return null;
49: }
50:
51: throw new FileSystemException(
52: "vfs.util/find-abstract-file-object.error",
53: fileObject == null ? "null" : fileObject.getClass()
54: .getName());
55: }
56:
57: /**
58: * check if the given FileObject is instance of given class argument
59: */
60: public static boolean isInstanceOf(final FileObject fileObject,
61: final Class wantedClass) throws FileSystemException {
62: Object searchObject = fileObject;
63: while (searchObject instanceof DecoratedFileObject) {
64: if (wantedClass.isInstance(searchObject)) {
65: return true;
66: }
67:
68: searchObject = ((DecoratedFileObject) searchObject)
69: .getDecoratedFileObject();
70: }
71:
72: if (wantedClass.isInstance(searchObject)) {
73: return true;
74: }
75:
76: return false;
77: }
78: }
|