001: package Language;
002:
003: import java.util.*;
004: import java.util.zip.*;
005: import java.io.*;
006: import javax.swing.table.*;
007:
008: import Schmortopf.Utility.Vectorizable;
009: import Schmortopf.Utility.io.FileUtilities;
010:
011: public class Common {
012:
013: private Common() {
014:
015: } // Constructor
016:
017: public static void StoreVectorToFile(File f, Vector v)
018: throws Exception {
019: FileOutputStream fos = null;
020: if (f.exists()) {
021: if (!f.delete())
022: throw new Exception("file " + f
023: + " already exists and cannot be deleted");
024: } else {
025: File parent = f.getParentFile();
026: if (!parent.exists())
027: parent.mkdirs();
028: }
029: System.out.println("saving vector in " + f.getAbsolutePath());
030: try {
031: fos = new FileOutputStream(f);
032: GZIPOutputStream zipOut = new GZIPOutputStream(fos);
033: DataOutputStream dos = new DataOutputStream(zipOut);
034: FileUtilities.SendVector(v, dos, null, 0);
035: dos.flush();
036: dos.close();
037: } catch (Exception e) {
038: throw e;
039: } finally {
040: if (fos != null)
041: fos.close();
042: }
043: }
044:
045: public static Vector ReadVectorFromFile(File f) throws Exception {
046: FileInputStream fis = null;
047: try {
048: fis = new FileInputStream(f);
049: GZIPInputStream zipIn = new GZIPInputStream(fis);
050: DataInputStream dis = new DataInputStream(zipIn);
051: return FileUtilities.ReceiveVector(dis, null, 0);
052: } catch (Exception e) {
053: throw e;
054: } finally {
055: if (fis != null)
056: fis.close();
057: }
058: }
059:
060: /** @return the number of spaces at the begining of the string
061: this is used to avoid translating "hello" and " hello "
062: */
063: public static int GetNumberOfSpacesAtBegining(String s) {
064: for (int i = 0; i < s.length(); i++) {
065: if (s.charAt(i) != ' ')
066: return i;
067: }
068: return s.length();
069: }
070:
071: /** @return the number of spaces at the end of the string
072: */
073: public static int GetNumberOfSpacesAtEnd(String s) {
074: for (int i = 0; i < s.length(); i++) {
075: if (s.charAt(s.length() - i - 1) != ' ')
076: return i;
077: }
078: return s.length();
079: }
080:
081: /** @return 0 if no arguments are detected in the sentence (%)
082: 1 if one % is present
083: 2 if %1 and %2 are present
084: */
085: public static int GetNumberOfParametersInSentence(String sentence)
086: throws Exception {
087:
088: if (sentence.indexOf("%2") != -1) {
089: if (sentence.indexOf("%1") == -1) {
090: throw new Exception(
091: "Sentence has only argument %2, %1 is missing ?");
092: }
093: return 2;
094: }
095: if (sentence.indexOf("%1") != -1) {
096: if (sentence.indexOf("%2") == -1) {
097: throw new Exception(
098: "Sentence has only argument %1, %2 is missing ?");
099: }
100: return 2;
101: }
102:
103: if (sentence.indexOf("%") != -1) {
104: return 1;
105: }
106:
107: return 0;
108: }
109:
110: /** Language/hello.java => Language.hello
111: */
112: public static String ConvertPathToJavaClassPathSyntax(String _path) {
113: String path = _path;
114: // 1) supperss .java
115: if (path.endsWith(".java"))
116: path = path.substring(0, path.length() - 5);
117:
118: // 2) convert \ to .
119: int pos = -1;
120: while ((pos = path.indexOf("\\")) != -1) {
121: path = path.substring(0, pos) + "."
122: + path.substring(pos + 1);
123: }
124:
125: return path;
126: }
127:
128: } // Common
|