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.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.ui.IEditorDescriptor;
21 import org.eclipse.ui.IEditorRegistry;
22 import org.eclipse.ui.IWorkbench;
23 import org.eclipse.ui.PlatformUI;
25 public class PHPFileUtil {
26 // private static String[] PHP_EXTENSIONS = null;
28 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
30 public static boolean isPHPFile(IFile file) {
31 // String extension = file.getFileExtension();
32 return isPHPFileName(file.getLocation().toString());
35 // public final static String getFileExtension(String name) {
36 // int index = name.lastIndexOf('.');
39 // if (index == (name.length() - 1))
40 // return null; //$NON-NLS-1$
41 // return name.substring(index + 1);
45 * Returns true iff str.toLowerCase().endsWith(".php") implementation is not
46 * 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
52 // Eclipse resource name
53 File file = new File(name);
54 if (file.getName().startsWith(".")) {
57 IWorkbench workbench = PlatformUI.getWorkbench();
58 IEditorRegistry registry = workbench.getEditorRegistry();
59 IEditorDescriptor[] descriptors = registry.getEditors(name);
61 for (int i = 0; i < descriptors.length; i++) {
62 if (descriptors[i].getId().equals(PHPeclipsePlugin.EDITOR_ID)) {
66 // String extension = getFileExtension(name);
67 // if (extension == null) {
70 // extension = extension.toLowerCase();
71 // PHP_EXTENSIONS = getExtensions();
72 // if (PHP_EXTENSIONS == null) {
75 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
76 // if (extension.equals(PHP_EXTENSIONS[i])) {
84 * Returns true iff the file extension is a valid PHP Unit name
85 * implementation is not creating extra strings.
87 public final static boolean isValidPHPUnitName(String filename) {
88 return PHPFileUtil.isPHPFileName(filename);
92 * @return Returns the PHP extensions.
94 // public static String[] getExtensions() {
95 // if (PHP_EXTENSIONS == null) {
96 // ArrayList list = new ArrayList();
97 // final IPreferenceStore store =
98 // PHPeclipsePlugin.getDefault().getPreferenceStore();
99 // String extensions =
100 // store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
101 // extensions = extensions.trim();
102 // if (extensions.length() != 0) {
103 // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
105 // while (tokenizer.hasMoreTokens()) {
106 // token = tokenizer.nextToken();
107 // if (token != null && token.length() >= 1) {
111 // if (list.size() != 0) {
112 // PHP_EXTENSIONS = new String[list.size()];
113 // for (int i = 0; i < list.size(); i++) {
114 // PHP_EXTENSIONS[i] = (String) list.get(i);
119 // return PHP_EXTENSIONS;
122 * @param php_extensions
123 * The PHP extensions to set.
125 // public static void setExtensions(String[] php_extensions) {
126 // PHP_EXTENSIONS = php_extensions;
129 * Creata the file for the given absolute file path
131 * @param absoluteFilePath
133 * @return the file for the given absolute file path or <code>null</code>
134 * if no existing file can be found
136 public static IFile createFile(IPath absoluteFilePath, IProject project) {
137 if (absoluteFilePath == null || project == null) {
141 String projectPath = project.getLocation().toString();
142 String filePath = absoluteFilePath.toString().substring(
143 projectPath.length() + 1);
144 return project.getFile(filePath);
149 * Determine the path of an include name string
151 * @param includeNameString
154 * @return the path for the given include filename or <code>null</code> if
155 * no existing file can be found
157 public static IPath determineFilePath(String includeNameString,
158 IResource resource, IProject project) {
159 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
160 IPath resourcePath = resource.getProjectRelativePath();
164 path = documentRootPath.append(includeNameString);
165 file = path.toFile();
170 if (includeNameString.startsWith("../")) {
171 path = project.getLocation().append(
172 resourcePath.removeLastSegments(1));
173 path = path.append(includeNameString);
174 file = path.toFile();
180 // includeNameString contains no path separator
181 path = project.getLocation().append(resourcePath.removeLastSegments(1));
182 path = path.append(includeNameString);
183 file = path.toFile();
188 // check if linked resource
189 IFile ifile = FileBuffers.getWorkspaceFileAtLocation(path);
191 file = ifile.getLocation().toFile();
197 List includePaths = ProjectPrefUtil.getIncludePaths(project);
198 if (includePaths.size() > 0) {
199 for (int i = 0; i < includePaths.size(); i++) {
200 path = new Path(includePaths.get(i).toString())
201 .append(includeNameString);
202 file = path.toFile();