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