KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > CtypeModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.quercus.lib;
31
32 import com.caucho.quercus.env.LongValue;
33 import com.caucho.quercus.env.StringValue;
34 import com.caucho.quercus.env.Value;
35 import com.caucho.quercus.module.AbstractQuercusModule;
36
37 public class CtypeModule
38   extends AbstractQuercusModule
39 {
40   public String JavaDoc []getLoadedExtensions()
41   {
42     return new String JavaDoc[] { "ctype" };
43   }
44
45   public static boolean ctype_alnum(Value value)
46   {
47     if (value instanceof LongValue)
48       return isalnum(value.toInt());
49     else if (value instanceof StringValue) {
50       String JavaDoc string = value.toString();
51
52       if (string.length() == 0)
53         return false;
54
55       for (int i = 0; i < string.length(); i++) {
56         if (! isalnum(string.charAt(i)))
57           return false;
58       }
59
60       return true;
61     }
62     else
63       return false;
64   }
65
66   public static boolean ctype_alpha(Value value)
67   {
68     if (value instanceof LongValue)
69       return isalpha(value.toInt());
70     else if (value instanceof StringValue) {
71       String JavaDoc string = value.toString();
72
73       if (string.length() == 0)
74         return false;
75
76       for (int i = 0; i < string.length(); i++) {
77         if (! isalpha(string.charAt(i)))
78           return false;
79       }
80
81       return true;
82     }
83     else
84       return false;
85   }
86
87   public static boolean ctype_cntrl(Value value)
88   {
89     if (value instanceof LongValue)
90       return iscntrl(value.toInt());
91     else if (value instanceof StringValue) {
92       String JavaDoc string = value.toString();
93
94       if (string.length() == 0)
95         return false;
96
97       for (int i = 0; i < string.length(); i++) {
98         if (! iscntrl(string.charAt(i)))
99           return false;
100       }
101
102       return true;
103     }
104     else
105       return false;
106   }
107
108   public static boolean ctype_digit(Value value)
109   {
110     if (value instanceof LongValue)
111       return isdigit(value.toInt());
112     else if (value instanceof StringValue) {
113       String JavaDoc string = value.toString();
114
115       if (string.length() == 0)
116         return false;
117
118       for (int i = 0; i < string.length(); i++) {
119         if (! isdigit(string.charAt(i)))
120           return false;
121       }
122
123       return true;
124     }
125     else
126       return false;
127   }
128
129
130   public static boolean ctype_graph(Value value)
131   {
132     if (value instanceof LongValue)
133       return isgraph(value.toInt());
134     else if (value instanceof StringValue) {
135       String JavaDoc string = value.toString();
136
137       if (string.length() == 0)
138         return false;
139
140       for (int i = 0; i < string.length(); i++) {
141         if (! isgraph(string.charAt(i)))
142           return false;
143       }
144
145       return true;
146     }
147     else
148       return false;
149   }
150
151   public static boolean ctype_lower(Value value)
152   {
153     if (value instanceof LongValue)
154       return islower(value.toInt());
155     else if (value instanceof StringValue) {
156       String JavaDoc string = value.toString();
157
158       if (string.length() == 0)
159         return false;
160
161       for (int i = 0; i < string.length(); i++) {
162         if (! islower(string.charAt(i)))
163           return false;
164       }
165
166       return true;
167     }
168     else
169       return false;
170   }
171
172
173   public static boolean ctype_print(Value value)
174   {
175     if (value instanceof LongValue)
176       return isprint(value.toInt());
177     else if (value instanceof StringValue) {
178       String JavaDoc string = value.toString();
179
180       if (string.length() == 0)
181         return false;
182
183       for (int i = 0; i < string.length(); i++) {
184         if (! isprint(string.charAt(i)))
185           return false;
186       }
187
188       return true;
189     }
190     else
191       return false;
192   }
193
194   public static boolean ctype_punct(Value value)
195   {
196     if (value instanceof LongValue)
197       return ispunct(value.toInt());
198     else if (value instanceof StringValue) {
199       String JavaDoc string = value.toString();
200
201       if (string.length() == 0)
202         return false;
203
204       for (int i = 0; i < string.length(); i++) {
205         if (! ispunct(string.charAt(i)))
206           return false;
207       }
208
209       return true;
210     }
211     else
212       return false;
213   }
214
215   public static boolean ctype_space(Value value)
216   {
217     if (value instanceof LongValue)
218       return isspace(value.toInt());
219     else if (value instanceof StringValue) {
220       String JavaDoc string = value.toString();
221
222       if (string.length() == 0)
223         return false;
224
225       for (int i = 0; i < string.length(); i++) {
226         if (! isspace(string.charAt(i)))
227           return false;
228       }
229
230       return true;
231     }
232     else
233       return false;
234   }
235
236   public static boolean ctype_upper(Value value)
237   {
238     if (value instanceof LongValue)
239       return isupper(value.toInt());
240     else if (value instanceof StringValue) {
241       String JavaDoc string = value.toString();
242
243       if (string.length() == 0)
244         return false;
245
246       for (int i = 0; i < string.length(); i++) {
247         if (! isupper(string.charAt(i)))
248           return false;
249       }
250
251       return true;
252     }
253     else
254       return false;
255   }
256
257   public static boolean ctype_xdigit(Value value)
258   {
259     if (value instanceof LongValue)
260       return isxdigit(value.toInt());
261     else if (value instanceof StringValue) {
262       String JavaDoc string = value.toString();
263
264       if (string.length() == 0)
265         return false;
266
267       for (int i = 0; i < string.length(); i++) {
268         if (! isxdigit(string.charAt(i)))
269           return false;
270       }
271
272       return true;
273     }
274     else
275       return false;
276   }
277
278   public static boolean isalnum(int ch)
279   {
280     return Character.isLetterOrDigit(ch);
281   }
282
283   public static boolean isalpha(int ch)
284   {
285     return Character.isLetter(ch);
286   }
287
288   public static boolean iscntrl(int ch)
289   {
290     return Character.isISOControl(ch);
291   }
292
293   public static boolean isdigit(int ch)
294   {
295     return Character.isDigit(ch);
296   }
297
298   public static boolean isgraph(int ch)
299   {
300     return isprint(ch) && !isspace(ch);
301   }
302
303   public static boolean islower(int ch)
304   {
305     return Character.isLowerCase(ch);
306   }
307
308   public static boolean isprint(int ch)
309   {
310     return Character.isValidCodePoint(ch) && !Character.isISOControl(ch);
311   }
312
313   public static boolean ispunct(int ch)
314   {
315     return isprint(ch) && !isspace(ch) && !isalnum(ch);
316   }
317
318   public static boolean isspace(int ch)
319   {
320     return Character.isSpaceChar(ch);
321   }
322
323   public static boolean isupper(int ch)
324   {
325     return Character.isUpperCase(ch);
326   }
327
328   public static boolean isxdigit(int ch)
329   {
330     return ((ch >= '0' && ch <= '9')
331         || (ch >= 'a' && ch <= 'f')
332         || (ch >= 'A' && ch <= 'F'));
333   }
334 }
335
Popular Tags