001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.resources;
018:
019: /**
020: * A set of utilities for characters handling.
021: *
022: * @since 2.1
023: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/resources/CharUtilities.java $
024: * @version $Id: CharUtilities.java 22443 2006-10-27 20:47:22Z desruisseaux $
025: * @author Martin Desruisseaux
026: */
027: public final class CharUtilities {
028: /**
029: * Forbid object creation.
030: */
031: private CharUtilities() {
032: }
033:
034: /**
035: * Determines whether the character is a superscript. Most superscripts have
036: * unicode values from \\u2070 to \\u207F inclusive. Superscripts are the
037: * following symbols:
038: *
039: * <blockquote><code>
040: * \u2070 \u00B9 \u00B2 \u00B3 \u2074 \u2075 \u2076 \u2077
041: * \u2078 \u2079 \u207A \u207B \u207C \u207D \u207E \u207F
042: * </code></blockquote>
043: */
044: public static boolean isSuperScript(final char c) {
045: switch (c) {
046: /*1*/case '\u2071':
047: /*2*/
048: case '\u2072':
049: /*3*/
050: case '\u2073':
051: return false;
052: /*1*/case '\u00B9':
053: /*2*/
054: case '\u00B2':
055: /*3*/
056: case '\u00B3':
057: return true;
058: }
059: return (c >= '\u2070' && c <= '\u207F');
060: }
061:
062: /**
063: * Determines whether the character is a subscript. Most subscripts have
064: * unicode values from \\u2080 to \\u208E inclusive. Subscripts are the
065: * following symbols:
066: *
067: * <blockquote><code>
068: * \u2080 \u2081 \u2082 \u2083 \u2084 \u2085 \u2086 \u2087
069: * \u2088 \u2089 \u208A \u208B \u208C \u208D \u208E
070: * </code></blockquote>
071: */
072: public static boolean isSubScript(final char c) {
073: return (c >= '\u2080' && c <= '\u208E');
074: }
075:
076: /**
077: * Converts the character argument to superscript.
078: * Only the following characters can be converted
079: * (other characters are left unchanged):
080: *
081: * <blockquote><pre>
082: * 0 1 2 3 4 5 6 7 8 9 + - = ( ) n
083: * </pre></blockquote>
084: */
085: public static char toSuperScript(final char c) {
086: switch (c) {
087: case '1':
088: return '\u00B9';
089: case '2':
090: return '\u00B2';
091: case '3':
092: return '\u00B3';
093: case '+':
094: return '\u207A';
095: case '-':
096: return '\u207B';
097: case '=':
098: return '\u207C';
099: case '(':
100: return '\u207D';
101: case ')':
102: return '\u207E';
103: case 'n':
104: return '\u207F';
105: }
106: if (c >= '0' && c <= '9') {
107: return (char) (c + ('\u2070' - '0'));
108: }
109: return c;
110: }
111:
112: /**
113: * Converts the character argument to subscript.
114: * Only the following characters can be converted
115: * (other characters are left unchanged):
116: *
117: * <blockquote><pre>
118: * 0 1 2 3 4 5 6 7 8 9 + - = ( ) n
119: * </pre></blockquote>
120: */
121: public static char toSubScript(final char c) {
122: switch (c) {
123: case '+':
124: return '\u208A';
125: case '-':
126: return '\u208B';
127: case '=':
128: return '\u208C';
129: case '(':
130: return '\u208D';
131: case ')':
132: return '\u208E';
133: }
134: if (c >= '0' && c <= '9') {
135: return (char) (c + ('\u2080' - '0'));
136: }
137: return c;
138: }
139:
140: /**
141: * Converts the character argument to normal script.
142: */
143: public static char toNormalScript(final char c) {
144: switch (c) {
145: case '\u00B9':
146: return '1';
147: case '\u00B2':
148: return '2';
149: case '\u00B3':
150: return '3';
151: case '\u2071':
152: return c;
153: case '\u2072':
154: return c;
155: case '\u2073':
156: return c;
157: case '\u207A':
158: return '+';
159: case '\u207B':
160: return '-';
161: case '\u207C':
162: return '=';
163: case '\u207D':
164: return '(';
165: case '\u207E':
166: return ')';
167: case '\u207F':
168: return 'n';
169: case '\u208A':
170: return '+';
171: case '\u208B':
172: return '-';
173: case '\u208C':
174: return '=';
175: case '\u208D':
176: return '(';
177: case '\u208E':
178: return ')';
179: }
180: if (c >= '\u2070' && c <= '\u2079')
181: return (char) (c - ('\u2070' - '0'));
182: if (c >= '\u2080' && c <= '\u2089')
183: return (char) (c - ('\u2080' - '0'));
184: return c;
185: }
186: }
|