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;
22 public class PHPFileUtil {
23 private static String[] PHP_EXTENSIONS = null;
25 public final static String[] SMARTY_EXTENSIONS = { "tpl" };
27 public static boolean isPHPFile(IFile file) {
28 String extension = file.getFileExtension();
29 return isPHPFileName(file.getLocation().toString());
32 public final static String getFileExtension(String name) {
33 int index = name.lastIndexOf('.');
36 if (index == (name.length() - 1))
37 return null; //$NON-NLS-1$
38 return name.substring(index + 1);
42 * Returns true iff str.toLowerCase().endsWith(".php") implementation is not creating extra strings.
44 public final static boolean isPHPFileName(String name) {
45 String extension = getFileExtension(name);
46 if (extension == null) {
49 extension = extension.toLowerCase();
50 PHP_EXTENSIONS = getExtensions();
51 if (PHP_EXTENSIONS == null) {
54 for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
55 if (extension.equals(PHP_EXTENSIONS[i])) {
63 * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
65 public final static boolean isValidPHPUnitName(String filename) {
66 return PHPFileUtil.isPHPFileName(filename);
70 * @return Returns the PHP extensions.
72 public static String[] getExtensions() {
73 if (PHP_EXTENSIONS == null) {
74 ArrayList list = new ArrayList();
75 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
76 String extensions = store.getString(PHPeclipsePlugin.PHP_EXTENSION_PREFS);
77 extensions = extensions.trim();
78 if (extensions.length() != 0) {
79 StringTokenizer tokenizer = new StringTokenizer(extensions, " ,;:/-|");
81 while (tokenizer.hasMoreTokens()) {
82 token = tokenizer.nextToken();
83 if (token != null && token.length() >= 1) {
87 if (list.size() != 0) {
88 PHP_EXTENSIONS = new String[list.size()];
89 for (int i = 0; i < list.size(); i++) {
90 PHP_EXTENSIONS[i] = (String) list.get(i);
95 return PHP_EXTENSIONS;
99 * @param php_extensions
100 * The PHP extensions to set.
102 public static void setExtensins(String[] php_extensions) {
103 PHP_EXTENSIONS = php_extensions;
107 * Determine the path of an include name string
109 * @param includeNameString
113 public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
114 IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
115 IPath resourcePath = resource.getProjectRelativePath();
119 path = documentRootPath.append(includeNameString);
120 file = path.toFile();
125 if (includeNameString.startsWith("../")) {
126 path = project.getLocation().append(resourcePath.removeLastSegments(1));
127 path = path.append(includeNameString);
128 file = path.toFile();
134 // int index = includeNameString.indexOf('/');
136 // includeNameString contains no path separator
137 path = project.getLocation().append(resourcePath.removeLastSegments(1));
138 path = path.append(includeNameString);
139 file = path.toFile();
145 List includePaths = ProjectPrefUtil.getIncludePaths(project);
146 if (includePaths.size() > 0) {
147 for (int i = 0; i < includePaths.size(); i++) {
148 path = new Path(includePaths.get(i).toString()).append(includeNameString);
149 file = path.toFile();