2 * Created on 09.08.2003
5 package net.sourceforge.phpdt.internal.ui.util;
7 import org.eclipse.core.resources.IFile;
13 public class PHPFileUtil {
14 public final static char[] SUFFIX_php = ".php".toCharArray(); //$NON-NLS-1$
15 public final static char[] SUFFIX_PHP = ".PHP".toCharArray(); //$NON-NLS-1$
16 public final static char[] SUFFIX_php3 = ".php3".toCharArray(); //$NON-NLS-1$
17 public final static char[] SUFFIX_PHP3 = ".PHP3".toCharArray(); //$NON-NLS-1$
18 public final static char[] SUFFIX_php4 = ".php4".toCharArray(); //$NON-NLS-1$
19 public final static char[] SUFFIX_PHP4 = ".PHP4".toCharArray(); //$NON-NLS-1$
20 public final static char[] SUFFIX_php5 = ".php5".toCharArray(); //$NON-NLS-1$
21 public final static char[] SUFFIX_PHP5 = ".PHP5".toCharArray(); //$NON-NLS-1$
22 public final static char[] SUFFIX_phtml = ".phtml".toCharArray(); //$NON-NLS-1$
23 public final static char[] SUFFIX_PHTML = ".PHTML".toCharArray(); //$NON-NLS-1$
24 public final static char[] SUFFIX_module = ".module".toCharArray(); //$NON-NLS-1$
25 public final static char[] SUFFIX_MODULE = ".MODULE".toCharArray(); //$NON-NLS-1$
27 public final static char[] SUFFIX_inc = ".inc".toCharArray(); //$NON-NLS-1$
28 public final static char[] SUFFIX_INC = ".INC".toCharArray(); //$NON-NLS-1$
29 public final static char[] SUFFIX_html = ".html".toCharArray(); //$NON-NLS-1$
30 public final static char[] SUFFIX_HTML = ".HTML".toCharArray(); //$NON-NLS-1$
31 public final static char[] SUFFIX_tpl = ".tpl".toCharArray(); //$NON-NLS-1$
32 public final static char[] SUFFIX_TPL = ".TPL".toCharArray(); //$NON-NLS-1$
34 public static boolean isPHPFile(IFile file) {
35 String extension = file.getFileExtension();
36 return isPHPFileName(file.getLocation().toString());
40 * Returns true iff str.toLowerCase().endsWith(".php")
41 * implementation is not creating extra strings.
43 public final static boolean isPHPFileName(String name) {
44 return isPHP_FileName(name) ||
45 isPHP3_FileName(name) ||
46 isPHP4_FileName(name) ||
47 isPHP5_FileName(name) ||
48 isModule_FileName(name) ||
49 isPHTML_FileName(name) ||
52 // static public boolean isPHPFile(String extension) {
53 // if ("php".equalsIgnoreCase(extension)
54 // || "php3".equalsIgnoreCase(extension)
55 // || "php4".equalsIgnoreCase(extension)
56 // || "inc".equalsIgnoreCase(extension)) {
63 * Returns true iff str.toLowerCase().endsWith(".php")
64 * implementation is not creating extra strings.
66 private final static boolean isPHP_FileName(String name) {
67 int nameLength = name == null ? 0 : name.length();
68 int suffixLength = SUFFIX_PHP.length;
69 if (nameLength < suffixLength)
72 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
73 char c = name.charAt(offset + i);
74 if (c != SUFFIX_php[i] && c != SUFFIX_PHP[i])
81 * Returns true iff str.toLowerCase().endsWith(".php3")
82 * implementation is not creating extra strings.
84 private final static boolean isPHP3_FileName(String name) {
85 int nameLength = name == null ? 0 : name.length();
86 int suffixLength = SUFFIX_PHP3.length;
87 if (nameLength < suffixLength)
90 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
91 char c = name.charAt(offset + i);
92 if (c != SUFFIX_php3[i] && c != SUFFIX_PHP3[i])
99 * Returns true iff str.toLowerCase().endsWith(".php4")
100 * implementation is not creating extra strings.
102 private final static boolean isPHP4_FileName(String name) {
103 int nameLength = name == null ? 0 : name.length();
104 int suffixLength = SUFFIX_PHP4.length;
105 if (nameLength < suffixLength)
108 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
109 char c = name.charAt(offset + i);
110 if (c != SUFFIX_php4[i] && c != SUFFIX_PHP4[i])
117 * Returns true iff str.toLowerCase().endsWith(".php5")
118 * implementation is not creating extra strings.
120 private final static boolean isPHP5_FileName(String name) {
121 int nameLength = name == null ? 0 : name.length();
122 int suffixLength = SUFFIX_PHP5.length;
123 if (nameLength < suffixLength)
126 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
127 char c = name.charAt(offset + i);
128 if (c != SUFFIX_php5[i] && c != SUFFIX_PHP5[i])
134 * Returns true iff str.toLowerCase().endsWith(".module")
135 * implementation is not creating extra strings.
137 private final static boolean isModule_FileName(String name) {
138 int nameLength = name == null ? 0 : name.length();
139 int suffixLength = SUFFIX_MODULE.length;
140 if (nameLength < suffixLength)
143 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
144 char c = name.charAt(offset + i);
145 if (c != SUFFIX_module[i] && c != SUFFIX_MODULE[i])
151 * Returns true iff str.toLowerCase().endsWith(".phtml")
152 * implementation is not creating extra strings.
154 private final static boolean isPHTML_FileName(String name) {
155 int nameLength = name == null ? 0 : name.length();
156 int suffixLength = SUFFIX_PHTML.length;
157 if (nameLength < suffixLength)
160 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
161 char c = name.charAt(offset + i);
162 if (c != SUFFIX_phtml[i] && c != SUFFIX_PHTML[i])
169 * Returns true iff str.toLowerCase().endsWith(".inc")
170 * implementation is not creating extra strings.
172 private final static boolean isINC_FileName(String name) {
173 int nameLength = name == null ? 0 : name.length();
174 int suffixLength = SUFFIX_INC.length;
175 if (nameLength < suffixLength)
178 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
179 char c = name.charAt(offset + i);
180 if (c != SUFFIX_inc[i] && c != SUFFIX_INC[i])
187 * Returns true iff str.toLowerCase().endsWith(".html")
188 * implementation is not creating extra strings.
190 public final static boolean isHTML_FileName(String name) {
191 int nameLength = name == null ? 0 : name.length();
192 int suffixLength = SUFFIX_HTML.length;
193 if (nameLength < suffixLength)
196 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
197 char c = name.charAt(offset + i);
198 if (c != SUFFIX_html[i] && c != SUFFIX_HTML[i])
204 * Returns true iff str.toLowerCase().endsWith(".tpl")
205 * implementation is not creating extra strings.
207 public final static boolean isTPL_FileName(String name) {
208 int nameLength = name == null ? 0 : name.length();
209 int suffixLength = SUFFIX_TPL.length;
210 if (nameLength < suffixLength)
213 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
214 char c = name.charAt(offset + i);
215 if (c != SUFFIX_tpl[i] && c != SUFFIX_TPL[i])
222 * Returns true iff the file extension is a valid PHP Unit name
223 * implementation is not creating extra strings.
225 public final static boolean isValidPHPUnitName(String filename) {
226 return PHPFileUtil.isPHPFileName(filename) ||
227 PHPFileUtil.isHTML_FileName(filename) ||
228 PHPFileUtil.isTPL_FileName(filename);