001: /*
002: * $Id: CharModule.java,v 1.10 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.util;
011:
012: import anvil.core.Any;
013:
014: /// @module anvil.char
015: /// Set of functions for checking whether given string falls into
016: /// certain character class. These checks are locale-specific.
017:
018: /**
019: * class CharModule
020: *
021: * @author: Jani Lehtimäki
022: */
023: public class CharModule {
024:
025: public static final Any UNASSIGNED = Any
026: .create(Character.UNASSIGNED);
027: public static final Any UPPERCASE_LETTER = Any
028: .create(Character.UPPERCASE_LETTER);
029: public static final Any LOWERCASE_LETTER = Any
030: .create(Character.LOWERCASE_LETTER);
031: public static final Any TITLECASE_LETTER = Any
032: .create(Character.TITLECASE_LETTER);
033: public static final Any MODIFIER_LETTER = Any
034: .create(Character.MODIFIER_LETTER);
035: public static final Any OTHER_LETTER = Any
036: .create(Character.OTHER_LETTER);
037: public static final Any NON_SPACING_MARK = Any
038: .create(Character.NON_SPACING_MARK);
039: public static final Any ENCLOSING_MARK = Any
040: .create(Character.ENCLOSING_MARK);
041: public static final Any COMBINING_SPACING_MARK = Any
042: .create(Character.COMBINING_SPACING_MARK);
043: public static final Any DECIMAL_DIGIT_NUMBER = Any
044: .create(Character.DECIMAL_DIGIT_NUMBER);
045: public static final Any LETTER_NUMBER = Any
046: .create(Character.LETTER_NUMBER);
047: public static final Any OTHER_NUMBER = Any
048: .create(Character.OTHER_NUMBER);
049: public static final Any SPACE_SEPARATOR = Any
050: .create(Character.SPACE_SEPARATOR);
051: public static final Any LINE_SEPARATOR = Any
052: .create(Character.LINE_SEPARATOR);
053: public static final Any PARAGRAPH_SEPARATOR = Any
054: .create(Character.PARAGRAPH_SEPARATOR);
055: public static final Any CONTROL = Any.create(Character.CONTROL);
056: public static final Any FORMAT = Any.create(Character.FORMAT);
057: public static final Any PRIVATE_USE = Any
058: .create(Character.PRIVATE_USE);
059: public static final Any SURROGATE = Any.create(Character.SURROGATE);
060: public static final Any DASH_PUNCTUATION = Any
061: .create(Character.DASH_PUNCTUATION);
062: public static final Any START_PUNCTUATION = Any
063: .create(Character.START_PUNCTUATION);
064: public static final Any END_PUNCTUATION = Any
065: .create(Character.END_PUNCTUATION);
066: public static final Any CONNECTOR_PUNCTUATION = Any
067: .create(Character.CONNECTOR_PUNCTUATION);
068: public static final Any OTHER_PUNCTUATION = Any
069: .create(Character.OTHER_PUNCTUATION);
070: public static final Any MATH_SYMBOL = Any
071: .create(Character.MATH_SYMBOL);
072: public static final Any CURRENCY_SYMBOL = Any
073: .create(Character.CURRENCY_SYMBOL);
074: public static final Any MODIFIER_SYMBOL = Any
075: .create(Character.MODIFIER_SYMBOL);
076: public static final Any OTHER_SYMBOL = Any
077: .create(Character.OTHER_SYMBOL);
078:
079: /// @function getType
080: /// Returns the characters class of given parameter.
081: /// Character class is one of the constants declared in this library.
082: /// @synopsis int getType(string char)
083: /// @synopsis int getType(int charcode)
084: public static final Any getType(Any value) {
085: if (value.isInt()) {
086: return Any.create(Character.getType((char) value.toInt()));
087: } else {
088: String str = value.toString();
089: if (str.length() > 0) {
090: return Any.create(Character.getType(str.charAt(0)));
091: } else {
092: return UNASSIGNED;
093: }
094: }
095: }
096:
097: /// @function isDigit
098: /// Tests if whole string consists of digits.
099: /// @synopsis boolean isDigit(string str)
100: public static final Any isDigit(String str) {
101: int n = str.length();
102: for (int i = 0; i < n; i++) {
103: if (!Character.isDigit(str.charAt(i))) {
104: return Any.FALSE;
105: }
106: }
107: return Any.TRUE;
108: }
109:
110: /// @function isLetter
111: /// Tests if whole string consists of letters.
112: /// @synopsis boolean isLetter(string str)
113: public static final Any isLetter(String str) {
114: int n = str.length();
115: for (int i = 0; i < n; i++) {
116: if (!Character.isLetter(str.charAt(i))) {
117: return Any.FALSE;
118: }
119: }
120: return Any.TRUE;
121: }
122:
123: /// @function isLetterOrDigit
124: /// Tests if whole string consists of letters or digits.
125: /// @synopsis boolean isLetterOrDigit(string str)
126: public static final Any isLetterOrDigit(String str) {
127: int n = str.length();
128: for (int i = 0; i < n; i++) {
129: if (!Character.isLetterOrDigit(str.charAt(i))) {
130: return Any.FALSE;
131: }
132: }
133: return Any.TRUE;
134: }
135:
136: /// @function isLowerCase
137: /// Tests if whole string consists of lowercase characters.
138: /// @synopsis boolean isLowerCase(string str)
139: public static final Any isLowerCase(String str) {
140: int n = str.length();
141: for (int i = 0; i < n; i++) {
142: if (!Character.isLowerCase(str.charAt(i))) {
143: return Any.FALSE;
144: }
145: }
146: return Any.TRUE;
147: }
148:
149: /// @function isUpperCase
150: /// Tests if whole string consists of uppercase characters.
151: /// @synopsis boolean isUpperCase(string str)
152: public static final Any isUpperCase(String str) {
153: int n = str.length();
154: for (int i = 0; i < n; i++) {
155: if (!Character.isLowerCase(str.charAt(i))) {
156: return Any.FALSE;
157: }
158: }
159: return Any.TRUE;
160: }
161:
162: /// @function isSpace
163: /// Tests if whole string consists of space characters.
164: /// @synopsis boolean isSpace(string str)
165: public static final Any isSpace(String str) {
166: int n = str.length();
167: for (int i = 0; i < n; i++) {
168: if (!Character.isSpaceChar(str.charAt(i))) {
169: return Any.FALSE;
170: }
171: }
172: return Any.TRUE;
173: }
174:
175: public static final anvil.script.compiler.NativeNamespace __module__ = new anvil.script.compiler.NativeNamespace(
176: "char", CharModule.class,
177: new Class[] {},
178: //DOC{{
179: ""
180: + " @module anvil.char \n"
181: + " Set of functions for checking whether given string falls into\n"
182: + " certain character class. These checks are locale-specific.\n"
183: + " @function getType\n"
184: + " Returns the characters class of given parameter.\n"
185: + " Character class is one of the constants declared in this library.\n"
186: + " @synopsis int getType(string char)\n"
187: + " @synopsis int getType(int charcode)\n"
188: + " @function isDigit\n"
189: + " Tests if whole string consists of digits.\n"
190: + " @synopsis boolean isDigit(string str)\n"
191: + " @function isLetter\n"
192: + " Tests if whole string consists of letters.\n"
193: + " @synopsis boolean isLetter(string str)\n"
194: + " @function isLetterOrDigit\n"
195: + " Tests if whole string consists of letters or digits.\n"
196: + " @synopsis boolean isLetterOrDigit(string str)\n"
197: + " @function isLowerCase\n"
198: + " Tests if whole string consists of lowercase characters. \n"
199: + " @synopsis boolean isLowerCase(string str)\n"
200: + " @function isUpperCase\n"
201: + " Tests if whole string consists of uppercase characters. \n"
202: + " @synopsis boolean isUpperCase(string str)\n"
203: + " @function isSpace\n"
204: + " Tests if whole string consists of space characters. \n"
205: + " @synopsis boolean isSpace(string str)\n"
206: //}}DOC
207: );
208:
209: }
|