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