001: package tide.importtools;
002:
003: import java.io.*;
004: import java.util.*;
005: import tide.project.ProjectSettings;
006: import snow.utils.storage.*;
007: import snow.utils.StringUtils;
008:
009: /** Imports Schmortopf IDE projects.
010: * Contain some copied code to import schmortopf vector streams.
011: */
012: @SuppressWarnings("unchecked")
013: public final class Schmortopf {
014: public static String fileNameEnding = ".Schmortopf";
015: private final Map<String, Object> map = new HashMap<String, Object>();
016:
017: public Schmortopf(File projFile, ProjectSettings proj)
018: throws Exception {
019: File tmpFile = new File(projFile.getAbsolutePath() + ".tmp");
020: tmpFile.deleteOnExit();
021: FileUtils.gunzip(projFile, tmpFile);
022:
023: DataInputStream dis = new DataInputStream(new FileInputStream(
024: tmpFile));
025: String fileVersion = dis.readUTF();
026: Vector sv = receiveVector(dis);
027:
028: Vector kvec = (Vector) sv.get(0);
029: Vector vvec = (Vector) sv.get(1);
030:
031: for (int i = 0; i < kvec.size(); i++) {
032: //debug: System.out.println(kvec.get(i)+": "+vvec.get(i));
033: map.put("" + kvec.get(i), vvec.get(i));
034: }
035:
036: if (proj != null) {
037: proj.setProjectName(StringUtils.removeAfterLastIncluded(
038: projFile.getName(), "."));
039: proj
040: .setSources_Home(new File(
041: getProjProp("Project directory path (which contains all java files)")));
042: proj.setClasses_Home(new File(
043: getProjProp("Outputdirectory for class files")));
044: proj
045: .setMainSourceFile(new File(
046: getProjProp("Java source file which contains the main() method")));
047: proj.setRuntimeArgs(getArgs("Arguments for the JVM"));
048: proj.setAppArgs(getArgs("Arguments passed to main()"));
049: // Schmortopf has no configurable compiler args :-(. No Xlint possible...
050: File javaC = new File(getProjProp("Java compiler pathname"));
051: if (javaC.exists()) {
052: proj
053: .setJava_Home(javaC.getParentFile()
054: .getParentFile());
055: }
056: proj
057: .setExternalJars(getCP("Additional Jar libraries or directories"));
058: proj.setProperty("JAR_SRC_DESTINATION", ""
059: + map.get("jarFilePathName"));
060: proj.setProperty("keystore_password", ""
061: + map.get("keystore_password"));
062: proj.setProperty("keystore_field", ""
063: + map.get("keystore_field"));
064:
065: }
066: /*
067: System.out.println("jarDest="+getProjProp("jarFilePathName"));
068:
069: System.out.println("src=" +getProjProp("Project directory path (which contains all java files)"));
070: System.out.println("dest_cl=" +getProjProp("Outputdirectory for class files"));
071: System.out.println("java_home="+getProjProp("Java compiler pathname")); // getParentFile()
072:
073: System.out.println("main="+getProjProp("Java source file which contains the main() method"));
074:
075: System.out.println("ma="+getArgs("Arguments passed to main()"));
076:
077: System.out.println("ca="+getArgs("Arguments for the JVM"));
078:
079: System.out.println("cp="+
080: getCP( "Additional Jar libraries or directories"));*/
081: }
082:
083: private String getArgs(String key) {
084: Object ov = map.get(key);
085: if (ov instanceof Vector) {
086: Vector v = (Vector) ov;
087: int n = (Integer) v.get(6);
088: StringBuilder args = new StringBuilder();
089: for (int i = 7; i < 7 + n; i++) {
090: args.append(" " + v.get(i));
091: }
092: return args.toString().trim();
093: }
094: return "" + ov;
095:
096: }
097:
098: private List<File> getCP(String key) {
099: List<File> lf = new ArrayList<File>();
100: Object ov = map.get(key);
101: if (ov instanceof Vector) {
102: Vector v = (Vector) ov;
103: int n = (Integer) v.get(6);
104: for (int i = 7; i < 7 + n; i++) {
105: lf.add(new File("" + v.get(i)));
106: }
107:
108: }
109: return lf;
110: }
111:
112: private String getProjProp(String key) {
113: Object ov = map.get(key);
114: if (ov instanceof Vector) {
115: return "" + ((Vector) ov).lastElement();
116: }
117:
118: return "" + ov;
119: }
120:
121: /**
122: * Reads a Vector, which was written using SendVector() from
123: * the inputstream.
124: */
125: private static final Vector receiveVector(final DataInputStream in)
126: throws Exception {
127: int vectorSize = 0;
128: // First read its size :
129: vectorSize = in.readInt();
130: // Then use the recursive worker method for the rest :
131: final Vector theVector = new Vector();
132: receiveVectorCoded(in, theVector, vectorSize); // works on theVector
133: return theVector;
134: }
135:
136: private static int IsVectorContent = 1;
137: private static int IsIntegerContent = 2;
138: private static int IsBooleanContent = 3;
139: private static int IsDoubleContent = 4;
140: private static int IsStringContent = 5;
141: private static int IsbyteArrayContent = 10;
142: private static int IsintArrayContent = 11;
143: private static int IsfloatArrayContent = 12;
144: private static int IsdoubleArrayContent = 13;
145: private static int IsLongContent = 14;
146: private static int IsStringArrayContent = 15;
147:
148: /**
149: * Recursive worker method for ReceiveVector()
150: * COPIED FROM SCHMORTOPF SOURCE
151: */
152: @SuppressWarnings("unchecked")
153: private static void receiveVectorCoded(final DataInputStream in,
154: final Vector theVector, final int numberOfElements)
155: throws Exception {
156: for (int elementIndex = 0; elementIndex < numberOfElements; elementIndex++) {
157: final int this ClassIndex = in.readInt(); // the data type keyword string
158: if (this ClassIndex == IsStringContent) {
159: String this Value = in.readUTF();
160: theVector.add(this Value);
161: } else if (this ClassIndex == IsIntegerContent) {
162: int this Value = in.readInt();
163: theVector.add(this Value);
164: } else if (this ClassIndex == IsLongContent) {
165: long this Value = in.readLong();
166: theVector.add(this Value);
167: } else if (this ClassIndex == IsDoubleContent) {
168: double this Value = in.readDouble();
169: theVector.add(this Value);
170: } else if (this ClassIndex == IsBooleanContent) {
171: boolean this Value = in.readBoolean();
172: theVector.add(this Value);
173: } else if (this ClassIndex == IsbyteArrayContent) {
174: int arraySize = in.readInt();
175: byte[] this Value = new byte[arraySize];
176: for (int index = 0; index < arraySize; index++) {
177: this Value[index] = in.readByte();
178: }
179: theVector.add(this Value);
180: } else if (this ClassIndex == IsintArrayContent) {
181: int arraySize = in.readInt();
182: int[] this Value = new int[arraySize];
183: for (int index = 0; index < arraySize; index++) {
184: this Value[index] = in.readInt();
185: }
186: theVector.add(this Value);
187: } else if (this ClassIndex == IsdoubleArrayContent) {
188: int arraySize = in.readInt();
189: double[] this Value = new double[arraySize];
190: for (int index = 0; index < arraySize; index++) {
191: this Value[index] = in.readDouble();
192: }
193: theVector.add(this Value);
194: } else if (this ClassIndex == IsfloatArrayContent) {
195: int arraySize = in.readInt();
196: float[] this Value = new float[arraySize];
197: for (int index = 0; index < arraySize; index++) {
198: this Value[index] = in.readFloat();
199: }
200: theVector.addElement(this Value);
201: } else if (this ClassIndex == IsStringArrayContent) {
202: int arraySize = in.readInt();
203: String[] this Value = new String[arraySize];
204: for (int index = 0; index < arraySize; index++) {
205: this Value[index] = in.readUTF();
206: }
207: theVector.addElement(this Value);
208: } else if (this ClassIndex == IsVectorContent) {
209: int this VectorSize = in.readInt();
210: // generate a new Vector and add it :
211: Vector this SubVector = new Vector(0, 1);
212: theVector.addElement(this SubVector);
213: // dive one recursion level deeper :
214: receiveVectorCoded(in, this SubVector, this VectorSize);
215: } else {
216: System.out
217: .println("*** FileUtilities.ReceiveVectorCoded error: Only Integer, Double and String types supported.");
218: System.out.println("*** The data had classindex = "
219: + this ClassIndex);
220: throw new Exception(
221: "FileUtilities.ReceiveVectorCoded error");
222: }
223: }
224: }
225: }
|