2 * Created on 09.08.2003
5 package net.sourceforge.phpdt.internal.ui.util;
8 import java.util.ArrayList;
10 import java.util.StringTokenizer;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.ui.IEditorDescriptor;
22 import org.eclipse.ui.IEditorRegistry;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.PlatformUI;
26 public class PHPFileUtil {
27 // private static String[] PHP_EXTENSIONS = null;
29 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
31 public static boolean isPHPFile(IFile file) {
32 // String extension = file.getFileExtension();
33 return isPHPFileName(file.getLocation().toString());
36 // public final static String getFileExtension(String name) {
37 // int index = name.lastIndexOf('.');
40 // if (index == (name.length() - 1))
41 // return null; //$NON-NLS-1$
42 // return name.substring(index + 1);
46 * Returns true iff str.toLowerCase().endsWith(".php") implementation is not creating extra strings.
48 public final static boolean isPHPFileName(String name) {
50 IWorkbench workbench = PlatformUI.getWorkbench();
51 IEditorRegistry registry = workbench.getEditorRegistry();
52 IEditorDescriptor[] descriptors = registry.getEditors(name);
54 for (int i = 0; i < descriptors.length; i++) {
55 if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
59 // String extension = getFileExtension(name);
60 // if (extension == null) {
63 // extension = extension.toLowerCase();
64 // PHP_EXTENSIONS = getExtensions();
65 // if (PHP_EXTENSIONS == null) {
68 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
69 // if (extension.equals(PHP_EXTENSIONS[i])) {
77 * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
79 public final static boolean isValidPHPUnitName(String filename) {
80 return PHPFileUtil.isPHPFileName(filename);
84 * @return Returns the PHP extensions.
86 // public static String[] getExtensions() {
87 // if (PHP_EXTENSIONS == null) {
88 // ArrayList list = new ArrayList();
89 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
90 // String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
91 // extensions = extensions.trim();
92 // if (extensions.length() != 0) {
93 // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
95 // while (tokenizer.hasMoreTokens()) {
96 // token = tokenizer.nextToken();
97 // if (token != null && token.length() >= 1) {
101 // if (list.size() != 0) {
102 // PHP_EXTENSIONS = new String[list.size()];
103 // for (int i = 0; i < list.size(); i++) {
104 // PHP_EXTENSIONS[i] = (String) list.get(i);
109 // return PHP_EXTENSIONS;
113 * @param php_extensions
114 * The PHP extensions to set.
116 // public static void setExtensions(String[] php_extensions) {
117 // PHP_EXTENSIONS = php_extensions;
121 * Creata the file for the given absolute file path
123 * @param absoluteFilePath
125 * @return the file for the given absolute file path or <code>null</code> if no existing file can be found
127 public static IFile createFile(IPath absoluteFilePath, IProject project) {
128 if (absoluteFilePath == null || project == null) {
132 String projectPath = project.getLocation().toString();
133 String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
134 return project.getFile(filePath);
139 * Determine the path of an include name string
141 * @param includeNameString
144 * @return the path for the given include filename or <code>null</code> if no existing file can be found
146 public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
147 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
148 IPath resourcePath = resource.getProjectRelativePath();
152 path = documentRootPath.append(includeNameString);
153 file = path.toFile();
158 if (includeNameString.startsWith("../")) {
159 path = project.getLocation().append(resourcePath.removeLastSegments(1));
160 path = path.append(includeNameString);
161 file = path.toFile();
167 // includeNameString contains no path separator
168 path = project.getLocation().append(resourcePath.removeLastSegments(1));
169 path = path.append(includeNameString);
170 file = path.toFile();
176 List includePaths = ProjectPrefUtil.getIncludePaths(project);
177 if (includePaths.size() > 0) {
178 for (int i = 0; i < includePaths.size(); i++) {
179 path = new Path(includePaths.get(i).toString()).append(includeNameString);
180 file = path.toFile();