001: //Copyright (c) Kristofer Johanisson 2005, Hans-Joachim Daniels 2005
002: //
003: //This program is free software; you can redistribute it and/or modify
004: //it under the terms of the GNU General Public License as published by
005: //the Free Software Foundation; either version 2 of the License, or
006: //(at your option) any later version.
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You can either finde the file LICENSE or LICENSE.TXT in the source
014: //distribution or in the .jar file of this application
015:
016: package de.uka.ilkd.key.ocl.gf;
017:
018: import java.io.File;
019: import java.util.Vector;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import javax.swing.ProgressMonitor;
024:
025: /**
026: * consists of a bunch of static methods, mostly for String
027: * @author daniels
028: *
029: */
030: public class Utils {
031: private static Logger timeLogger = Logger.getLogger(Utils.class
032: .getName()
033: + ".timer");
034: private static Logger deleteLogger = Logger.getLogger(Utils.class
035: .getName()
036: + ".delete");
037: private static Logger stringLogger = Logger.getLogger(Utils.class
038: .getName()
039: + ".string");
040:
041: private Utils() {
042: //non-instantiability enforced
043: }
044:
045: public static final String gf = "gf";
046: public static final String gfcm = "gfcm";
047:
048: /**
049: * Get the extension of a file.
050: */
051: public static String getExtension(File f) {
052: String ext = null;
053: String s = f.getName();
054: int i = s.lastIndexOf('.');
055:
056: if (i > 0 && i < s.length() - 1) {
057: ext = s.substring(i + 1).toLowerCase();
058: }
059: return ext;
060: }
061:
062: /**
063: * Sets the progress on the given ProgressMonitor and logs the current time.
064: * @param pm the monitor which is to be updated. If null, only logging is done
065: * @param progress The progress in absolute ticks
066: * @param note The note that is to be displayed above the progress monitor
067: */
068: public static void tickProgress(ProgressMonitor pm, int progress,
069: String note) {
070: if (note != null) {
071: if (timeLogger.isLoggable(Level.FINER)) {
072: timeLogger.finer(System.currentTimeMillis() + " : "
073: + note);
074: }
075: }
076: if (pm == null) {
077: return;
078: }
079: pm.setProgress(progress);
080: if (note != null) {
081: pm.setNote(note);
082: }
083: }
084:
085: /**
086: * schedules all Eng, OCL and Ger grammar files for deletion.
087: * @param grammarsDir The directory where those files are
088: */
089: public static void cleanupFromUMLTypes(String grammarsDir) {
090: String[] endings = { "Eng.gf", "Eng.gfc", "Ger.gf", "Ger.gfc",
091: "OCL.gf", "OCL.gfc", ".gf", ".gfc" };
092: for (int i = 0; i < endings.length; i++) {
093: String filename = grammarsDir + File.separator
094: + GFEditor2.modelModulName + endings[i];
095: File file = new File(filename);
096: file.deleteOnExit();
097: if (deleteLogger.isLoggable(Level.FINER)) {
098: deleteLogger
099: .fine("scheduled for deletion: " + filename);
100: }
101: }
102: File file = new File(grammarsDir);
103: file.deleteOnExit();
104: file = file.getParentFile();
105: file.deleteOnExit();
106: }
107:
108: /**
109: * Searches for the first occurace not escaped with '\' of toBeFound in s.
110: * Works like String::indexOf otherwise
111: * @param s the String to search in
112: * @param toBeFound the String to search for
113: * @return the index of toBeFound, -1 if not found (or only escaped)
114: */
115: public static int indexOfNotEscaped(String s, String toBeFound) {
116: return indexOfNotEscaped(s, toBeFound, 0);
117: }
118:
119: /**
120: * Searches for the first occurace not escaped with '\' of toBeFound in s.
121: * Works like String::indexOf otherwise
122: * @param s the String to search in
123: * @param toBeFound the String to search for
124: * @param startIndex the index in s, from which the search starts
125: * @return the index of toBeFound, -1 if not found (or only escaped)
126: */
127: public static int indexOfNotEscaped(String s, String toBeFound,
128: int startIndex) {
129: for (int from = startIndex; from < s.length();) {
130: int i = s.indexOf(toBeFound, from);
131: if (i <= 0) {
132: //-1 is not found at all, 0 can't have a '\' before
133: return i;
134: } else if (s.charAt(i - 1) != '\\') {
135: return i;
136: } else {
137: from = i + 1;
138: }
139: }
140: return -1;
141: }
142:
143: /**
144: * a simple replaceAll replacement, that uses NO regexps
145: * and thus needs no freaking amount of backslashes
146: * @param original The String in which the replacements should take place
147: * @param toReplace the String literal that is to be replaced
148: * @param replacement the replacement string
149: * @return original, but with replacements
150: */
151: public static String replaceAll(String original, String toReplace,
152: String replacement) {
153: StringBuffer sb = new StringBuffer(original);
154: for (int i = sb.indexOf(toReplace); i >= 0; i = sb.indexOf(
155: toReplace, i + replacement.length())) {
156: sb.replace(i, i + toReplace.length(), replacement);
157: }
158: return sb.toString();
159: }
160:
161: /**
162: * Removes all parts, that are inside "quotation marks" from s.
163: * Assumes no nesting of those, like in Java.
164: * " escaped with \ like \" do not count as quotation marks
165: * @param s The String, that possibly contains quotations
166: * @return a String described above, s without quotations.
167: */
168: public static String removeQuotations(String s) {
169: if (s.indexOf('"') == -1) {
170: return s;
171: }
172: for (int begin = indexOfNotEscaped(s, "\""); begin > -1; begin = indexOfNotEscaped(
173: s, "\"", begin)) {//yes, I want an unescaped '"'!
174: int end = indexOfNotEscaped(s, "\"", begin + 1);
175: if (end > -1) {
176: s = s.substring(0, begin) + s.substring(end + 1);
177: } else {
178: stringLogger.info("Strange String given: '" + s + "'");
179: s = s.substring(0, begin);
180: }
181: }
182: return s;
183: }
184:
185: /**
186: * counts the occurances of toSearch in s
187: * @param s The String in which the search shall take place
188: * @param toSearch The String that should be counted
189: * @return the number of occurances, 0 if s is null
190: */
191: public static int countOccurances(String s, String toSearch) {
192: if (s == null) {
193: return 0;
194: }
195: int result = 0;
196: for (int i = s.indexOf(toSearch); i > -1; i = s.indexOf(
197: toSearch, i)) {
198: result++;
199: i += toSearch.length();
200: }
201: return result;
202: }
203:
204: /**
205: * Takes an arbitrary Vector and executes toString on each element and
206: * puts these into a String[] of the same size as the Vector
207: * @param strings A Vector of Object, preferably String
208: * @return The Vector as a String[]
209: */
210: public static String[] vector2Array(Vector strings) {
211: String[] result = new String[strings.size()];
212: for (int i = 0; i < strings.size(); i++) {
213: result[i] = strings.get(i).toString();
214: }
215: return result;
216: }
217:
218: /**
219: * just replace sequences of spaces with one space
220: * @param s The string to be compacted
221: * @return the compacted result
222: */
223: static String compactSpaces(String s) {
224: String localResult = new String();
225: boolean spaceIncluded = false;
226:
227: for (int i = 0; i < s.length(); i++) {
228: char c = s.charAt(i);
229: if (c != ' ') { // include all non-spaces
230: localResult += String.valueOf(c);
231: spaceIncluded = false;
232: } else {// we have a space
233: if (!spaceIncluded) {
234: localResult += " ";
235: spaceIncluded = true;
236: } // else just skip
237: }
238: }
239: return localResult;
240: }
241:
242: /**
243: * Replaces all occurances of toBeReplaced, that are not escaped by '\'
244: * with replacement
245: * @param working the String in which substrings should be replaced
246: * @param toBeReplaced The substring, that should be replaced by replacement
247: * @param replacement well, the replacement string
248: * @return The String with the replaced parts
249: */
250: public static String replaceNotEscaped(String working,
251: String toBeReplaced, String replacement) {
252: StringBuffer w = new StringBuffer(working);
253: for (int i = w.indexOf(toBeReplaced); i > -1 && i < w.length(); i = w
254: .indexOf(toBeReplaced, i)) {
255: if (i == 0 || w.charAt(i - 1) != '\\') {
256: w.replace(i, i + toBeReplaced.length(), replacement);
257: i += replacement.length();
258: } else {
259: i += 1;
260: }
261: }
262: return w.toString();
263: }
264: }
|