001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit;
025:
026: import org.apache.regexp.RE;
027: import org.apache.regexp.RESyntaxException;
028:
029: import java.util.Dictionary;
030: import java.util.Enumeration;
031:
032: /**
033: * Some useful utilities for string manipulation.
034: *
035: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
036: * @version 1.0
037: */
038: public class StringToolkit {
039: private static RE identifierRE;
040:
041: static {
042: try {
043: identifierRE = new RE("[$][{][^}]*[}]");
044: } catch (RESyntaxException res) {
045: System.err
046: .println("syntax error in regular expression for identifier detection");
047: System.exit(1);
048: }
049: }
050:
051: /**
052: * Creates a new StringToolkit.
053: */
054: public StringToolkit() {
055: }
056:
057: /**
058: * Replaces unprintable characters.
059: *
060: * @param source the source string where the characters should be replaced
061: *
062: * @return the new string where the characters have been replaced
063: */
064: public static String replaceUnprintable(String source) {
065: String result = source;
066:
067: try {
068: RE re = new RE("[\r]");
069: result = re.subst(result, "<R>", RE.REPLACE_ALL);
070: re = new RE("[\f]");
071: result = re.subst(result, "<F>", RE.REPLACE_ALL);
072: re = new RE("\n");
073: result = re.subst(result, "<ENTER>", RE.REPLACE_ALL);
074: re = new RE("\t");
075: result = re.subst(result, "<TAB>", RE.REPLACE_ALL);
076: } catch (RESyntaxException res) {
077: System.err
078: .println("syntax error in regular expression when parsing "
079: + source);
080: }
081:
082: return result;
083: }
084:
085: /**
086: * Reverses {@link #replaceUnprintable(String)}
087: *
088: * @param source DOCUMENT ME!
089: *
090: * @return DOCUMENT ME!
091: */
092: public static String reverseReplacement(String source) {
093: String result = source;
094:
095: try {
096: RE re = new RE("<F>");
097: result = re.subst(result, "\f", RE.REPLACE_ALL);
098: re = new RE("<R>");
099: result = re.subst(result, "\r", RE.REPLACE_ALL);
100: re = new RE("<TAB>");
101: result = re.subst(result, "\t", RE.REPLACE_ALL);
102: re = new RE("<ENTER>");
103: result = re.subst(result, "\n", RE.REPLACE_ALL);
104: } catch (RESyntaxException res) {
105: System.err
106: .println("syntax error in regular expression when parsing "
107: + source);
108: }
109:
110: return result;
111: }
112:
113: /**
114: * Returns whether or not the given two strings are equal. Also included <code>null</code>
115: * tests.
116: *
117: * @param string1 the first string
118: * @param string2 the second string
119: *
120: * @return DOCUMENT ME!
121: */
122: public static boolean areEqual(String string1, String string2) {
123: if ((string1 == null) && (string2 == null)) {
124: return true;
125: } else if ((string1 == null) || (string2 == null)) {
126: return false;
127: } else {
128: return string1.equals(string2);
129: }
130: }
131:
132: /**
133: * Returns whether or not a string is defined.
134: *
135: * @param string the string
136: *
137: * @return <code>true</code> if the string is not <code>null</code> and not equal "",
138: * otherwise <code>false</code>
139: */
140: public static boolean isDefined(String string) {
141: return (string != null) && !string.equals("");
142: }
143:
144: /**
145: * Replaces all occurences of identifier substrings with the value of the identifier. Names and
146: * values of identifiers are given in a dictionary. Identifier substrings are of the following
147: * structure: <code>${ID_NAME}</code>. If the value of an identifier contains identifiers
148: * itself, those will also be replaced by their values. If an identifier is not contained in
149: * the dictionary, the related substring will not be replaced.
150: *
151: * @param idString the string which contains identifiers
152: * @param idTable the dictionary containing the identifiers (names mapped to values).
153: *
154: * @return a new string with the replaced substrings
155: */
156: public static String replaceIdentifiers(String idString,
157: Dictionary idTable) {
158: String result = idString;
159:
160: // Simple test if there is an identifier
161: if (!identifierRE.match(idString)) {
162: return idString;
163: }
164:
165: // create a regular expression for every identifier
166: int numberOfIdentifiers = idTable.size();
167: RE[] regularExpressions = new RE[numberOfIdentifiers];
168: String[] values = new String[numberOfIdentifiers];
169: Enumeration en = idTable.keys();
170: int index = 0;
171:
172: while (en.hasMoreElements()) {
173: String idName = (String) en.nextElement();
174:
175: try {
176: regularExpressions[index] = new RE("[$][{]" + idName
177: + "[}]");
178: values[index] = (String) idTable.get(idName);
179: index++;
180: } catch (RESyntaxException res) {
181: System.err
182: .println("syntax error in regular expression for identifier: "
183: + idName);
184:
185: return (idString);
186: }
187: }
188:
189: String oldString = null;
190:
191: do {
192: oldString = result;
193:
194: for (int i = 0; i < numberOfIdentifiers; i++) {
195: result = regularExpressions[i].subst(result, values[i]);
196: }
197: } while (!result.equals(oldString)
198: && identifierRE.match(result));
199:
200: return (result);
201: }
202: }
|