2 * Created on 09.08.2003
6 /*duplicated incastrix*/
7 package net.sourceforge.phpdt.internal.ui.util;
10 import java.util.List;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
15 import org.eclipse.core.filebuffers.FileBuffers;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
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 return isPHPFileName(file.getFullPath().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.getFullPath().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 // script location based
165 path = project.getFullPath().append(resourcePath.removeLastSegments(1))
166 .append(includeNameString);
168 if (fileExists(path, false)) {
171 // project root based
172 path = project.getFullPath().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 IResource resource = ifile;
206 if (resource.exists()) {