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) ||
54 * Returns true iff str.toLowerCase().endsWith(".php")
55 * implementation is not creating extra strings.
57 private final static boolean isPHP_FileName(String name) {
58 int nameLength = name == null ? 0 : name.length();
59 int suffixLength = SUFFIX_PHP.length;
60 if (nameLength < suffixLength)
63 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
64 char c = name.charAt(offset + i);
65 if (c != SUFFIX_php[i] && c != SUFFIX_PHP[i])
72 * Returns true iff str.toLowerCase().endsWith(".php3")
73 * implementation is not creating extra strings.
75 private final static boolean isPHP3_FileName(String name) {
76 int nameLength = name == null ? 0 : name.length();
77 int suffixLength = SUFFIX_PHP3.length;
78 if (nameLength < suffixLength)
81 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
82 char c = name.charAt(offset + i);
83 if (c != SUFFIX_php3[i] && c != SUFFIX_PHP3[i])
90 * Returns true iff str.toLowerCase().endsWith(".php4")
91 * implementation is not creating extra strings.
93 private final static boolean isPHP4_FileName(String name) {
94 int nameLength = name == null ? 0 : name.length();
95 int suffixLength = SUFFIX_PHP4.length;
96 if (nameLength < suffixLength)
99 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
100 char c = name.charAt(offset + i);
101 if (c != SUFFIX_php4[i] && c != SUFFIX_PHP4[i])
108 * Returns true iff str.toLowerCase().endsWith(".php5")
109 * implementation is not creating extra strings.
111 private final static boolean isPHP5_FileName(String name) {
112 int nameLength = name == null ? 0 : name.length();
113 int suffixLength = SUFFIX_PHP5.length;
114 if (nameLength < suffixLength)
117 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
118 char c = name.charAt(offset + i);
119 if (c != SUFFIX_php5[i] && c != SUFFIX_PHP5[i])
125 * Returns true iff str.toLowerCase().endsWith(".module")
126 * implementation is not creating extra strings.
128 private final static boolean isModule_FileName(String name) {
129 int nameLength = name == null ? 0 : name.length();
130 int suffixLength = SUFFIX_MODULE.length;
131 if (nameLength < suffixLength)
134 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
135 char c = name.charAt(offset + i);
136 if (c != SUFFIX_module[i] && c != SUFFIX_MODULE[i])
142 * Returns true iff str.toLowerCase().endsWith(".phtml")
143 * implementation is not creating extra strings.
145 private final static boolean isPHTML_FileName(String name) {
146 int nameLength = name == null ? 0 : name.length();
147 int suffixLength = SUFFIX_PHTML.length;
148 if (nameLength < suffixLength)
151 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
152 char c = name.charAt(offset + i);
153 if (c != SUFFIX_phtml[i] && c != SUFFIX_PHTML[i])
160 * Returns true iff str.toLowerCase().endsWith(".inc")
161 * implementation is not creating extra strings.
163 private final static boolean isINC_FileName(String name) {
164 int nameLength = name == null ? 0 : name.length();
165 int suffixLength = SUFFIX_INC.length;
166 if (nameLength < suffixLength)
169 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
170 char c = name.charAt(offset + i);
171 if (c != SUFFIX_inc[i] && c != SUFFIX_INC[i])
178 * Returns true iff str.toLowerCase().endsWith(".html")
179 * implementation is not creating extra strings.
181 public final static boolean isHTML_FileName(String name) {
182 int nameLength = name == null ? 0 : name.length();
183 int suffixLength = SUFFIX_HTML.length;
184 if (nameLength < suffixLength)
187 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
188 char c = name.charAt(offset + i);
189 if (c != SUFFIX_html[i] && c != SUFFIX_HTML[i])
195 * Returns true iff str.toLowerCase().endsWith(".tpl")
196 * implementation is not creating extra strings.
198 public final static boolean isTPL_FileName(String name) {
199 int nameLength = name == null ? 0 : name.length();
200 int suffixLength = SUFFIX_TPL.length;
201 if (nameLength < suffixLength)
204 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
205 char c = name.charAt(offset + i);
206 if (c != SUFFIX_tpl[i] && c != SUFFIX_TPL[i])
213 * Returns true iff the file extension is a valid PHP Unit name
214 * implementation is not creating extra strings.
216 public final static boolean isValidPHPUnitName(String filename) {
217 return PHPFileUtil.isPHPFileName(filename) ||
218 PHPFileUtil.isHTML_FileName(filename) ||
219 PHPFileUtil.isTPL_FileName(filename);