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.WebUI;
14 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
16 import org.eclipse.core.filebuffers.FileBuffers;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.ui.IEditorDescriptor;
23 import org.eclipse.ui.IEditorRegistry;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.PlatformUI;
27 public class PHPFileUtil {
28 // private static String[] PHP_EXTENSIONS = null;
30 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
32 public static boolean isPHPFile(IFile file) {
33 return isPHPFileName(file.getFullPath().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
47 * creating extra strings.
49 public final static boolean isPHPFileName(String name) {
51 // avoid handling a file without base name, e.g. ".php", which is a
53 // Eclipse resource name
54 File file = new File(name);
55 if (file.getName().startsWith(".")) {
58 IWorkbench workbench = PlatformUI.getWorkbench();
59 IEditorRegistry registry = workbench.getEditorRegistry();
60 IEditorDescriptor[] descriptors = registry.getEditors(name);
62 for (int i = 0; i < descriptors.length; i++) {
63 if (descriptors[i].getId().equals(WebUI.EDITOR_ID)) {
67 // String extension = getFileExtension(name);
68 // if (extension == null) {
71 // extension = extension.toLowerCase();
72 // PHP_EXTENSIONS = getExtensions();
73 // if (PHP_EXTENSIONS == null) {
76 // for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
77 // if (extension.equals(PHP_EXTENSIONS[i])) {
85 * Returns true iff the file extension is a valid PHP Unit name
86 * implementation is not creating extra strings.
88 public final static boolean isValidPHPUnitName(String filename) {
89 return PHPFileUtil.isPHPFileName(filename);
93 * @return Returns the PHP extensions.
95 // public static String[] getExtensions() {
96 // if (PHP_EXTENSIONS == null) {
97 // ArrayList list = new ArrayList();
98 // final IPreferenceStore store =
99 // PHPeclipsePlugin.getDefault().getPreferenceStore();
100 // String extensions =
101 // store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
102 // extensions = extensions.trim();
103 // if (extensions.length() != 0) {
104 // StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
106 // while (tokenizer.hasMoreTokens()) {
107 // token = tokenizer.nextToken();
108 // if (token != null && token.length() >= 1) {
112 // if (list.size() != 0) {
113 // PHP_EXTENSIONS = new String[list.size()];
114 // for (int i = 0; i < list.size(); i++) {
115 // PHP_EXTENSIONS[i] = (String) list.get(i);
120 // return PHP_EXTENSIONS;
123 * @param php_extensions
124 * The PHP extensions to set.
126 // public static void setExtensions(String[] php_extensions) {
127 // PHP_EXTENSIONS = php_extensions;
130 * Creata the file for the given absolute file path
132 * @param absoluteFilePath
134 * @return the file for the given absolute file path or <code>null</code>
135 * if no existing file can be found
137 public static IFile createFile(IPath absoluteFilePath, IProject project) {
138 if (absoluteFilePath == null || project == null) {
142 String projectPath = project.getFullPath().toString();
143 String filePath = absoluteFilePath.toString().substring(
144 projectPath.length() + 1);
145 return project.getFile(filePath);
150 * Determine the path of an include name string
152 * @param includeNameString
155 * @return the path for the given include filename or <code>null</code> if
156 * no existing file can be found
158 public static IPath determineFilePath(String includeNameString,
159 IResource resource, IProject project) {
160 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
161 IPath resourcePath = resource.getProjectRelativePath();
165 // script location based
166 path = project.getFullPath().append(resourcePath.removeLastSegments(1))
167 .append(includeNameString);
169 if (fileExists(path, false)) {
172 // project root based
173 path = project.getFullPath().append(includeNameString);
174 if (fileExists(path, false)) {
178 // DocumentRoot (absolute path) based
179 path = documentRootPath.append(includeNameString);
180 if (fileExists(path, true)) {
184 // IncludePaths settings (absolute path) based
185 List includePaths = ProjectPrefUtil.getIncludePaths(project);
186 if (includePaths.size() > 0) {
187 for (int i = 0; i < includePaths.size(); i++) {
188 path = new Path(includePaths.get(i).toString())
189 .append(includeNameString);
190 if (fileExists(path, true)) {
198 private static boolean fileExists(IPath path, boolean absolute) {
199 File file = path.toFile();
204 IFile ifile = FileBuffers.getWorkspaceFileAtLocation(path);
206 IResource resource = ifile;
207 if (resource.exists()) {