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