2 * Created on 09.08.2003
5 package net.sourceforge.phpdt.internal.ui.util;
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
11 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
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;
24 public class PHPFileUtil {
25 // private static String[] PHP_EXTENSIONS = null;
27 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
29 public static boolean isPHPFile(IFile file) {
30 // String extension = file.getFileExtension();
31 return isPHPFileName(file.getLocation().toString());
34 // public final static String getFileExtension(String name) {
35 // int index = name.lastIndexOf('.');
38 // if (index == (name.length() - 1))
39 // return null; //$NON-NLS-1$
40 // return name.substring(index + 1);
44 * Returns true iff str.toLowerCase().endsWith(".php") implementation is not
45 * creating extra strings.
47 public final static boolean isPHPFileName(String name) {
49 // avoid handling a file without base name, e.g. ".php", which is a
51 // Eclipse resource name
52 File file = new File(name);
53 if (file.getName().startsWith(".")) {
56 IWorkbench workbench = PlatformUI.getWorkbench();
57 IEditorRegistry registry = workbench.getEditorRegistry();
58 IEditorDescriptor[] descriptors = registry.getEditors(name);
60 for (int i = 0; i < descriptors.length; i++) {
61 if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
65 // String extension = getFileExtension(name);
66 // if (extension == null) {
69 // extension = extension.toLowerCase();
70 // PHP_EXTENSIONS = getExtensions();
71 // if (PHP_EXTENSIONS == null) {
74 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
75 // if (extension.equals(PHP_EXTENSIONS[i])) {
83 * Returns true iff the file extension is a valid PHP Unit name
84 * implementation is not creating extra strings.
86 public final static boolean isValidPHPUnitName(String filename) {
87 return PHPFileUtil.isPHPFileName(filename);
91 * @return Returns the PHP extensions.
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, " ,;:/-|");
104 // while (tokenizer.hasMoreTokens()) {
105 // token = tokenizer.nextToken();
106 // if (token != null && token.length() >= 1) {
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);
118 // return PHP_EXTENSIONS;
121 * @param php_extensions
122 * The PHP extensions to set.
124 // public static void setExtensions(String[] php_extensions) {
125 // PHP_EXTENSIONS = php_extensions;
128 * Creata the file for the given absolute file path
130 * @param absoluteFilePath
132 * @return the file for the given absolute file path or <code>null</code>
133 * if no existing file can be found
135 public static IFile createFile(IPath absoluteFilePath, IProject project) {
136 if (absoluteFilePath == null || project == null) {
140 String projectPath = project.getLocation().toString();
141 String filePath = absoluteFilePath.toString().substring(
142 projectPath.length() + 1);
143 return project.getFile(filePath);
148 * Determine the path of an include name string
150 * @param includeNameString
153 * @return the path for the given include filename or <code>null</code> if
154 * no existing file can be found
156 public static IPath determineFilePath(String includeNameString,
157 IResource resource, IProject project) {
158 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
159 IPath resourcePath = resource.getProjectRelativePath();
164 // script location based
165 path = project.getLocation().append(resourcePath.removeLastSegments(1))
166 .append(includeNameString);
167 if (fileExists(path, false)) {
171 // project root based
172 path = project.getLocation().append(includeNameString);
173 if (fileExists(path, false)) {
177 // DocumentRoot (absolute path) based
178 path = documentRootPath.append(includeNameString);
179 if (fileExists(path, true)) {
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)) {
197 private static boolean fileExists(IPath path, boolean absolute) {
198 File file = path.toFile();
203 IFile ifile = FileBuffers.getWorkspaceFileAtLocation(path);
205 file = ifile.getLocation().toFile();