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    * Creata the file for the given absolute file path
 
 109    * @param absoluteFilePath
 
 111    * @return the file for the given absolute file path or <code>null</code> if no existing file can be found
 
 113   public static IFile createFile(IPath absoluteFilePath, IProject project) {
 
 114     if (absoluteFilePath == null || project == null) {
 
 118     String projectPath = project.getLocation().toString();
 
 119     String filePath = absoluteFilePath.toString().substring(projectPath.length() + 1);
 
 120     return project.getFile(filePath);
 
 125    * Determine the path of an include name string
 
 127    * @param includeNameString
 
 130    * @return the path for the given include filename or <code>null</code> if no existing file can be found
 
 132   public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
 
 133     IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
 
 134     IPath resourcePath = resource.getProjectRelativePath();
 
 138     path = documentRootPath.append(includeNameString);
 
 139     file = path.toFile();
 
 144     if (includeNameString.startsWith("../")) {
 
 145       path = project.getLocation().append(resourcePath.removeLastSegments(1));
 
 146       path = path.append(includeNameString);
 
 147       file = path.toFile();
 
 153     //    int index = includeNameString.indexOf('/');
 
 155     // includeNameString contains no path separator
 
 156     path = project.getLocation().append(resourcePath.removeLastSegments(1));
 
 157     path = path.append(includeNameString);
 
 158     file = path.toFile();
 
 164     List includePaths = ProjectPrefUtil.getIncludePaths(project);
 
 165     if (includePaths.size() > 0) {
 
 166       for (int i = 0; i < includePaths.size(); i++) {
 
 167         path = new Path(includePaths.get(i).toString()).append(includeNameString);
 
 168         file = path.toFile();