Fix Request #1431769: Support Context assistance when entering functions arguments
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / PHPFileUtil.java
1 /*
2  * Created on 09.08.2003
3  *
4  */
5 package net.sourceforge.phpdt.internal.ui.util;
6
7 import java.io.File;
8 import java.util.List;
9
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.ui.IEditorDescriptor;
19 import org.eclipse.ui.IEditorRegistry;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.PlatformUI;
22
23 public class PHPFileUtil {
24         // private static String[] PHP_EXTENSIONS = null;
25
26         public final static String[] SMARTY_EXTENSIONS = { "tpl" };
27
28         public static boolean isPHPFile(IFile file) {
29                 // String extension = file.getFileExtension();
30                 return isPHPFileName(file.getLocation().toString());
31         }
32
33         // public final static String getFileExtension(String name) {
34         // int index = name.lastIndexOf('.');
35         // if (index == -1)
36         // return null;
37         // if (index == (name.length() - 1))
38         // return null; //$NON-NLS-1$
39         // return name.substring(index + 1);
40         // }
41
42         /**
43          * Returns true iff str.toLowerCase().endsWith(".php") implementation is not
44          * creating extra strings.
45          */
46         public final static boolean isPHPFileName(String name) {
47
48                 // avoid handling a file without base name, e.g. ".php", which is a valid
49                 // Eclipse resource name
50                 File file = new File(name);
51                 if (file.getName().startsWith(".")) {
52                         return false;
53                 }
54                 IWorkbench workbench = PlatformUI.getWorkbench();
55                 IEditorRegistry registry = workbench.getEditorRegistry();
56                 IEditorDescriptor[] descriptors = registry.getEditors(name);
57
58                 for (int i = 0; i < descriptors.length; i++) {
59                         if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
60                                 return true;
61                         }
62                 }
63                 // String extension = getFileExtension(name);
64                 // if (extension == null) {
65                 // return false;
66                 // }
67                 // extension = extension.toLowerCase();
68                 // PHP_EXTENSIONS = getExtensions();
69                 // if (PHP_EXTENSIONS == null) {
70                 // return false;
71                 // }
72                 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
73                 // if (extension.equals(PHP_EXTENSIONS[i])) {
74                 // return true;
75                 // }
76                 // }
77                 return false;
78         }
79
80         /**
81          * Returns true iff the file extension is a valid PHP Unit name implementation
82          * is not creating extra strings.
83          */
84         public final static boolean isValidPHPUnitName(String filename) {
85                 return PHPFileUtil.isPHPFileName(filename);
86         }
87
88         /**
89          * @return Returns the PHP extensions.
90          */
91         // public static String[] getExtensions() {
92         // if (PHP_EXTENSIONS == null) {
93         // ArrayList list = new ArrayList();
94         // final IPreferenceStore store =
95         // PHPeclipsePlugin.getDefault().getPreferenceStore();
96         // String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
97         // extensions = extensions.trim();
98         // if (extensions.length() != 0) {
99         // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
100         // String token;
101         // while (tokenizer.hasMoreTokens()) {
102         // token = tokenizer.nextToken();
103         // if (token != null && token.length() >= 1) {
104         // list.add(token);
105         // }
106         // }
107         // if (list.size() != 0) {
108         // PHP_EXTENSIONS = new String[list.size()];
109         // for (int i = 0; i < list.size(); i++) {
110         // PHP_EXTENSIONS[i] = (String) list.get(i);
111         // }
112         // }
113         // }
114         // }
115         // return PHP_EXTENSIONS;
116         // }
117         /**
118          * @param php_extensions
119          *          The PHP extensions to set.
120          */
121         // public static void setExtensions(String[] php_extensions) {
122         // PHP_EXTENSIONS = php_extensions;
123         // }
124         /**
125          * Creata the file for the given absolute file path
126          *
127          * @param absoluteFilePath
128          * @param project
129          * @return the file for the given absolute file path or <code>null</code> if
130          *         no existing file can be found
131          */
132         public static IFile createFile(IPath absoluteFilePath, IProject project) {
133                 if (absoluteFilePath == null || project == null) {
134                         return null;
135                 }
136
137                 String projectPath = project.getLocation().toString();
138                 String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
139                 return project.getFile(filePath);
140
141         }
142
143         /**
144          * Determine the path of an include name string
145          *
146          * @param includeNameString
147          * @param resource
148          * @param project
149          * @return the path for the given include filename or <code>null</code> if
150          *         no existing file can be found
151          */
152         public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
153                 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
154                 IPath resourcePath = resource.getProjectRelativePath();
155
156                 File file = null;
157                 IPath path = null;
158                 path = documentRootPath.append(includeNameString);
159                 file = path.toFile();
160                 if (file.exists()) {
161                         return path;
162                 }
163
164                 if (includeNameString.startsWith("../")) {
165                         path = project.getLocation().append(resourcePath.removeLastSegments(1));
166                         path = path.append(includeNameString);
167                         file = path.toFile();
168                         if (file.exists()) {
169                                 return path;
170                         }
171                 }
172
173                 // includeNameString contains no path separator
174                 path = project.getLocation().append(resourcePath.removeLastSegments(1));
175                 path = path.append(includeNameString);
176                 file = path.toFile();
177                 if (file.exists()) {
178                         return path;
179                 }
180                 // }
181
182                 List includePaths = ProjectPrefUtil.getIncludePaths(project);
183                 if (includePaths.size() > 0) {
184                         for (int i = 0; i < includePaths.size(); i++) {
185                                 path = new Path(includePaths.get(i).toString()).append(includeNameString);
186                                 file = path.toFile();
187                                 if (file.exists()) {
188                                         return path;
189                                 }
190                         }
191                 }
192                 return null;
193         }
194
195 }