misc changes
[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 org.eclipse.core.resources.IFile;
8
9 /**
10  * @author khartlage
11  *
12  */
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$
26   
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$
33
34   public  static boolean isPHPFile(IFile file) {
35     String extension = file.getFileExtension();
36     return isPHPFileName(file.getLocation().toString());
37   }
38
39   /**
40    * Returns true iff str.toLowerCase().endsWith(".php")
41    * implementation is not creating extra strings.
42    */
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) || 
50            isINC_FileName(name);
51   }
52   
53   /**
54    * Returns true iff str.toLowerCase().endsWith(".php")
55    * implementation is not creating extra strings.
56    */
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)
61       return false;
62
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])
66         return false;
67     }
68     return true;
69   }
70
71   /**
72    * Returns true iff str.toLowerCase().endsWith(".php3")
73    * implementation is not creating extra strings.
74    */
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)
79       return false;
80
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])
84         return false;
85     }
86     return true;
87   }
88
89   /**
90    * Returns true iff str.toLowerCase().endsWith(".php4")
91    * implementation is not creating extra strings.
92    */
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)
97       return false;
98
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])
102         return false;
103     }
104     return true;
105   }
106
107   /**
108    * Returns true iff str.toLowerCase().endsWith(".php5")
109    * implementation is not creating extra strings.
110    */
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)
115       return false;
116
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])
120         return false;
121     }
122     return true;
123   }
124   /**
125    * Returns true iff str.toLowerCase().endsWith(".module")
126    * implementation is not creating extra strings.
127    */
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)
132       return false;
133
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])
137         return false;
138     }
139     return true;
140   }
141  /**
142   * Returns true iff str.toLowerCase().endsWith(".phtml")
143   * implementation is not creating extra strings.
144   */
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)
149      return false;
150
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])
154        return false;
155    }
156    return true;
157  }
158   
159   /**
160    * Returns true iff str.toLowerCase().endsWith(".inc")
161    * implementation is not creating extra strings.
162    */
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)
167       return false;
168
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])
172         return false;
173     }
174     return true;
175   }
176   
177   /**
178    * Returns true iff str.toLowerCase().endsWith(".html")
179    * implementation is not creating extra strings.
180    */
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)
185       return false;
186
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])
190         return false;
191     }
192     return true;
193   }
194   /**
195    * Returns true iff str.toLowerCase().endsWith(".tpl")
196    * implementation is not creating extra strings.
197    */
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)
202       return false;
203
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])
207         return false;
208     }
209     return true;
210   }
211   
212   /**
213          * Returns true iff the file extension is a valid PHP Unit name
214          * implementation is not creating extra strings.
215          */
216         public final static boolean isValidPHPUnitName(String filename) {
217                 return PHPFileUtil.isPHPFileName(filename) ||
218                        PHPFileUtil.isHTML_FileName(filename) ||
219                            PHPFileUtil.isTPL_FileName(filename);
220         }
221 }