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 //avoid handling a file without base name, e.g. ".php", which is a valid Eclipse resource name
51 File file=new File(name);
52 if (file.getName().startsWith(".")) {
55 IWorkbench workbench = PlatformUI.getWorkbench();
56 IEditorRegistry registry = workbench.getEditorRegistry();
57 IEditorDescriptor[] descriptors = registry.getEditors(name);
59 for (int i = 0; i < descriptors.length; i++) {
60 if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
64 // String extension = getFileExtension(name);
65 // if (extension == null) {
68 // extension = extension.toLowerCase();
69 // PHP_EXTENSIONS = getExtensions();
70 // if (PHP_EXTENSIONS == null) {
73 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
74 // if (extension.equals(PHP_EXTENSIONS[i])) {
82 * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
84 public final static boolean isValidPHPUnitName(String filename) {
85 return PHPFileUtil.isPHPFileName(filename);
89 * @return Returns the PHP extensions.
91 // public static String[] getExtensions() {
92 // if (PHP_EXTENSIONS == null) {
93 // ArrayList list = new ArrayList();
94 // final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
95 // String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
96 // extensions = extensions.trim();
97 // if (extensions.length() != 0) {
98 // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
100 // while (tokenizer.hasMoreTokens()) {
101 // token = tokenizer.nextToken();
102 // if (token != null && token.length() >= 1) {
106 // if (list.size() != 0) {
107 // PHP_EXTENSIONS = new String[list.size()];
108 // for (int i = 0; i < list.size(); i++) {
109 // PHP_EXTENSIONS[i] = (String) list.get(i);
114 // return PHP_EXTENSIONS;
118 * @param php_extensions
119 * The PHP extensions to set.
121 // public static void setExtensions(String[] php_extensions) {
122 // PHP_EXTENSIONS = php_extensions;
126 * Creata the file for the given absolute file path
128 * @param absoluteFilePath
130 * @return the file for the given absolute file path or <code>null</code> if no existing file can be found
132 public static IFile createFile(IPath absoluteFilePath, IProject project) {
133 if (absoluteFilePath == null || project == null) {
137 String projectPath = project.getLocation().toString();
138 String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
139 return project.getFile(filePath);
144 * Determine the path of an include name string
146 * @param includeNameString
149 * @return the path for the given include filename or <code>null</code> if no existing file can be found
151 public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
152 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
153 IPath resourcePath = resource.getProjectRelativePath();
157 path = documentRootPath.append(includeNameString);
158 file = path.toFile();
163 if (includeNameString.startsWith("../")) {
164 path = project.getLocation().append(resourcePath.removeLastSegments(1));
165 path = path.append(includeNameString);
166 file = path.toFile();
172 // includeNameString contains no path separator
173 path = project.getLocation().append(resourcePath.removeLastSegments(1));
174 path = path.append(includeNameString);
175 file = path.toFile();
181 List includePaths = ProjectPrefUtil.getIncludePaths(project);
182 if (includePaths.size() > 0) {
183 for (int i = 0; i < includePaths.size(); i++) {
184 path = new Path(includePaths.get(i).toString()).append(includeNameString);
185 file = path.toFile();