2 * Created on 09.08.2003
5 package net.sourceforge.phpdt.internal.ui.util;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.ui.IEditorDescriptor;
18 import org.eclipse.ui.IEditorRegistry;
19 import org.eclipse.ui.IWorkbench;
20 import org.eclipse.ui.PlatformUI;
22 public class PHPFileUtil {
23 // private static String[] PHP_EXTENSIONS = null;
25 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
27 public static boolean isPHPFile(IFile file) {
28 // String extension = file.getFileExtension();
29 return isPHPFileName(file.getLocation().toString());
32 // public final static String getFileExtension(String name) {
33 // int index = name.lastIndexOf('.');
36 // if (index == (name.length() - 1))
37 // return null; //$NON-NLS-1$
38 // return name.substring(index + 1);
42 * Returns true iff str.toLowerCase().endsWith(".php") implementation is not creating extra strings.
44 public final static boolean isPHPFileName(String name) {
46 //avoid handling a file without base name, e.g. ".php", which is a valid Eclipse resource name
47 File file=new File(name);
48 if (file.getName().startsWith(".")) {
51 IWorkbench workbench = PlatformUI.getWorkbench();
52 IEditorRegistry registry = workbench.getEditorRegistry();
53 IEditorDescriptor[] descriptors = registry.getEditors(name);
55 for (int i = 0; i < descriptors.length; i++) {
56 if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
60 // String extension = getFileExtension(name);
61 // if (extension == null) {
64 // extension = extension.toLowerCase();
65 // PHP_EXTENSIONS = getExtensions();
66 // if (PHP_EXTENSIONS == null) {
69 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
70 // if (extension.equals(PHP_EXTENSIONS[i])) {
78 * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
80 public final static boolean isValidPHPUnitName(String filename) {
81 return PHPFileUtil.isPHPFileName(filename);
85 * @return Returns the PHP extensions.
87 // public static String[] getExtensions() {
88 // if (PHP_EXTENSIONS == null) {
89 // ArrayList list = new ArrayList();
90 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
91 // String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
92 // extensions = extensions.trim();
93 // if (extensions.length() != 0) {
94 // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
96 // while (tokenizer.hasMoreTokens()) {
97 // token = tokenizer.nextToken();
98 // if (token != null && token.length() >= 1) {
102 // if (list.size() != 0) {
103 // PHP_EXTENSIONS = new String[list.size()];
104 // for (int i = 0; i < list.size(); i++) {
105 // PHP_EXTENSIONS[i] = (String) list.get(i);
110 // return PHP_EXTENSIONS;
114 * @param php_extensions
115 * The PHP extensions to set.
117 // public static void setExtensions(String[] php_extensions) {
118 // PHP_EXTENSIONS = php_extensions;
122 * Creata the file for the given absolute file path
124 * @param absoluteFilePath
126 * @return the file for the given absolute file path or <code>null</code> if no existing file can be found
128 public static IFile createFile(IPath absoluteFilePath, IProject project) {
129 if (absoluteFilePath == null || project == null) {
133 String projectPath = project.getLocation().toString();
134 String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
135 return project.getFile(filePath);
140 * Determine the path of an include name string
142 * @param includeNameString
145 * @return the path for the given include filename or <code>null</code> if no existing file can be found
147 public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
148 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
149 IPath resourcePath = resource.getProjectRelativePath();
153 path = documentRootPath.append(includeNameString);
154 file = path.toFile();
159 if (includeNameString.startsWith("../")) {
160 path = project.getLocation().append(resourcePath.removeLastSegments(1));
161 path = path.append(includeNameString);
162 file = path.toFile();
168 // includeNameString contains no path separator
169 path = project.getLocation().append(resourcePath.removeLastSegments(1));
170 path = path.append(includeNameString);
171 file = path.toFile();
177 List includePaths = ProjectPrefUtil.getIncludePaths(project);
178 if (includePaths.size() > 0) {
179 for (int i = 0; i < includePaths.size(); i++) {
180 path = new Path(includePaths.get(i).toString()).append(includeNameString);
181 file = path.toFile();