Refactory: replaced internal copy of Assert class with org.eclipse.core.runtime.Assert
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / refactoring / util / ResourceUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.refactoring.util;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IMember;
19 import net.sourceforge.phpdt.core.IOpenable;
20 //incastrix
21 //import net.sourceforge.phpdt.internal.corext.Assert;
22 import org.eclipse.core.runtime.Assert;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26
27 public class ResourceUtil {
28
29         private ResourceUtil() {
30         }
31
32         public static IFile[] getFiles(ICompilationUnit[] cus) {
33                 List files = new ArrayList(cus.length);
34                 for (int i = 0; i < cus.length; i++) {
35                         IResource resource = ResourceUtil.getResource(cus[i]);
36                         if (resource != null && resource.getType() == IResource.FILE)
37                                 files.add(resource);
38                 }
39                 return (IFile[]) files.toArray(new IFile[files.size()]);
40         }
41
42         public static IFile getFile(ICompilationUnit cu) {
43                 IResource resource = ResourceUtil.getResource(cu);
44                 if (resource != null && resource.getType() == IResource.FILE)
45                         return (IFile) resource;
46                 else
47                         return null;
48         }
49
50         // ----- other ------------------------------
51
52         /**
53          * Finds an <code>IResource</code> for a given
54          * <code>ICompilationUnit</code>. If the parameter is a working copy then
55          * the <code>IResource</code> for the original element is returned.
56          */
57         public static IResource getResource(ICompilationUnit cu) {
58                 return cu.getResource();
59         }
60
61         /**
62          * Returns the <code>IResource</code> that the given <code>IMember</code>
63          * is defined in.
64          * 
65          * @see #getResource
66          */
67         public static IResource getResource(IMember member) {
68                 Assert.isTrue(!member.isBinary());
69                 return getResource(member.getCompilationUnit());
70         }
71
72         public static IResource getResource(Object o) {
73                 if (o instanceof IResource)
74                         return (IResource) o;
75                 if (o instanceof IJavaElement)
76                         return getResource((IJavaElement) o);
77                 return null;
78         }
79
80         private static IResource getResource(IJavaElement element) {
81                 if (element.getElementType() == IJavaElement.COMPILATION_UNIT)
82                         return getResource((ICompilationUnit) element);
83                 else if (element instanceof IOpenable)
84                         return element.getResource();
85                 else
86                         return null;
87         }
88 }