Parser detects wrong include files now
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / PHPFileUtil.java
1 /*
2  * Created on 09.08.2003
3  *
4  */
5 package net.sourceforge.phpdt.internal.ui.util;
6
7 import java.io.File;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.StringTokenizer;
11
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
14
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;
21
22 public class PHPFileUtil {
23   private static String[] PHP_EXTENSIONS = null;
24
25   public final static String[] SMARTY_EXTENSIONS = { "tpl" };
26
27   public static boolean isPHPFile(IFile file) {
28     String extension = file.getFileExtension();
29     return isPHPFileName(file.getLocation().toString());
30   }
31
32   public final static String getFileExtension(String name) {
33     int index = name.lastIndexOf('.');
34     if (index == -1)
35       return null;
36     if (index == (name.length() - 1))
37       return null; //$NON-NLS-1$
38     return name.substring(index + 1);
39   }
40
41   /**
42    * Returns true iff str.toLowerCase().endsWith(".php") implementation is not creating extra strings.
43    */
44   public final static boolean isPHPFileName(String name) {
45     String extension = getFileExtension(name);
46     if (extension == null) {
47       return false;
48     }
49     extension = extension.toLowerCase();
50     PHP_EXTENSIONS = getExtensions();
51     if (PHP_EXTENSIONS == null) {
52       return false;
53     }
54     for (int i = 0; i < PHP_EXTENSIONS.length; i++) {
55       if (extension.equals(PHP_EXTENSIONS[i])) {
56         return true;
57       }
58     }
59     return false;
60   }
61   
62   /**
63    * Returns true iff the file extension is a valid PHP Unit name implementation is not creating extra strings.
64    */
65   public final static boolean isValidPHPUnitName(String filename) {
66     return PHPFileUtil.isPHPFileName(filename);
67   }
68
69   /**
70    * @return Returns the PHP extensions.
71    */
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, " ,;:/-|");
80         String token;
81         while (tokenizer.hasMoreTokens()) {
82           token = tokenizer.nextToken();
83           if (token != null && token.length() >= 1) {
84             list.add(token);
85           }
86         }
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);
91           }
92         }
93       }
94     }
95     return PHP_EXTENSIONS;
96   }
97
98   /**
99    * @param php_extensions
100    *          The PHP extensions to set.
101    */
102   public static void setExtensins(String[] php_extensions) {
103     PHP_EXTENSIONS = php_extensions;
104   }
105
106   /**
107    * Determine the path of an include name string
108    * @param includeNameString
109    * @param resource
110    * @param project
111    */
112   public static IPath determineFilePath(String includeNameString, IResource resource, IProject project) {
113     IPath documentRootPath = ProjectPrefUtil.getDocumentRoot(project);
114     IPath resourcePath = resource.getProjectRelativePath();
115   
116     File file = null;
117     IPath path = null;
118     path = documentRootPath.append(includeNameString);
119     file = path.toFile();
120     if (file.exists()) {
121       return path;
122     }
123   
124     int index = includeNameString.indexOf('/');
125     if (index < 0) {
126       // includeNameString contains no path separator
127       path = project.getLocation().append(resourcePath.removeLastSegments(1));
128       path = path.append(includeNameString);
129       file = path.toFile();
130       if (file.exists()) {
131         return path;
132       }
133     }
134   
135     List includePaths = ProjectPrefUtil.getIncludePaths(project);
136     if (includePaths.size() > 0) {
137       for (int i = 0; i < includePaths.size(); i++) {
138         path = new Path(includePaths.get(i).toString()).append(includeNameString);
139         file = path.toFile();
140         if (file.exists()) {
141           return path;
142         }
143       }
144     }
145     return null;
146   }
147 }