001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: /*
022: * JSmoothModelPersistency.java
023: *
024: * Created on 7 août 2003, 19:42
025: */
026:
027: package net.charabia.jsmoothgen.application;
028:
029: import java.io.*;
030: import java.util.*;
031:
032: import java.beans.XMLEncoder;
033: import java.beans.XMLDecoder;
034:
035: import javax.xml.transform.stream.StreamResult;
036: import javax.xml.transform.stream.StreamSource;
037:
038: import com.wutka.jox.*;
039:
040: public class JSmoothModelPersistency {
041: public static JSmoothModelBean load(File fin) throws IOException {
042: FileReader fr = new FileReader(fin);
043: try {
044: JSmoothModelBean jobj = new JSmoothModelBean();
045: String INVALID = "INVALID";
046: jobj.setSkeletonName(INVALID);
047: JOXBeanReader jbr = new JOXBeanReader(fr);
048: jbr.readObject(jobj);
049: jbr.close();
050: fr.close();
051:
052: if (jobj.getSkeletonName() == INVALID) {
053: throw new Exception("Not a JOX File");
054: }
055: // System.out.println("Loaded jobj " + jobj + " = " + jobj.getJarLocation());
056: if ((jobj.getJarLocation() != null)
057: && (jobj.getJarLocation().length() > 0)) {
058: jobj.setEmbeddedJar(true);
059: // System.out.println("Set embeddedjar to " + jobj.getEmbeddedJar());
060: }
061:
062: return jobj;
063:
064: } catch (Exception exc) {
065: fr.close();
066:
067: try {
068: FileInputStream fis = new FileInputStream(fin);
069: XMLDecoder dec = new XMLDecoder(fis);
070: JSmoothModelBean xobj = (JSmoothModelBean) dec
071: .readObject();
072: fis.close();
073:
074: if ((xobj.getJarLocation() != null)
075: && (xobj.getJarLocation().length() > 0))
076: xobj.setEmbeddedJar(true);
077:
078: return xobj;
079:
080: } catch (Exception exc2) {
081: exc2.printStackTrace();
082: throw new IOException(exc2.toString());
083: }
084: }
085: }
086:
087: public static void save(File fout, JSmoothModelBean obj)
088: throws IOException {
089: // FileOutputStream fos = new FileOutputStream(fout);
090: try {
091: // XMLEncoder enc = new XMLEncoder(fos);
092: // enc.writeObject(obj);
093: // enc.close();
094:
095: String jarloc = obj.getJarLocation();
096: if (obj.getEmbeddedJar() == false)
097: obj.setJarLocation(null);
098:
099: FileWriter fw = new FileWriter(fout);
100: JOXBeanWriter jbw = new JOXBeanWriter(fw);
101: jbw.writeObject("jsmoothproject", obj);
102: jbw.close();
103: fw.close();
104:
105: obj.setJarLocation(jarloc);
106:
107: } catch (Exception ex) {
108: throw new IOException(ex.toString());
109: } finally {
110: // fos.close();
111: }
112: }
113:
114: // root : z:/a/b/c/d
115: // t1: z:/a/b/e/f
116: // t2: c:/t/r
117: // t3: z:/a/b/c/d/i/m
118: // t4: z:/a/b/c/d
119: static public File makePathRelativeIfPossible(File root, File f) {
120: if (f.toString().indexOf("${") >= 0)
121: return f;
122:
123: File orgfile = f;
124: try {
125: if (f.isAbsolute() == false) {
126: f = new File(root, f.toString());
127: }
128: f = f.getCanonicalFile();
129: f = f.getAbsoluteFile();
130: root = root.getCanonicalFile();
131: root = root.getAbsoluteFile();
132: } catch (IOException iox) {
133: iox.printStackTrace();
134: System.out.println("Failed, returning " + orgfile);
135:
136: return orgfile;
137: }
138: Vector rootvec = new Vector();
139: Vector targetvec = new Vector();
140: File cur;
141: cur = root;
142: while (cur != null) {
143: String n = cur.getName();
144: // lame hack, because getName() returns "" when the file is a drive (like c: or z:)
145: if (n.equals(""))
146: n = cur.getAbsolutePath();
147: rootvec.add(0, n);
148: cur = cur.getParentFile();
149: }
150:
151: cur = f;
152: while (cur != null) {
153: String n = cur.getName();
154: if (n.equals(""))
155: n = cur.getAbsolutePath();
156: targetvec.add(0, n);
157: cur = cur.getParentFile();
158: }
159:
160: // find the lowest common path
161: int cursor = 0;
162: while ((cursor < rootvec.size()) && (cursor < targetvec.size())) {
163: if (rootvec.elementAt(cursor).equals(
164: targetvec.elementAt(cursor)) == false)
165: break;
166: cursor++;
167: }
168:
169: if (cursor == 0)
170: return f;
171:
172: if ((cursor == rootvec.size()) && (cursor == targetvec.size()))
173: return new File(".");
174:
175: StringBuffer buffer = new StringBuffer();
176: for (int i = cursor; i < rootvec.size(); i++) {
177: buffer.append("../");
178: }
179:
180: for (int i = cursor; i < targetvec.size(); i++) {
181: buffer.append(targetvec.elementAt(i).toString());
182: buffer.append("/");
183: }
184:
185: return new File(buffer.toString());
186: }
187:
188: static public void main(String[] args) {
189: File root = new File("z:/a/b/c/d");
190: File t1 = new File("z:/a/b/e/f");
191: File t2 = new File("c:/t/r");
192: File t3 = new File("z:/a/b/c/d/i/m");
193: File t4 = new File("z:/a/b/c/d");
194:
195: System.out.println("Rel root, t1: "
196: + makePathRelativeIfPossible(root, t1));
197: System.out.println("Rel root, t2: "
198: + makePathRelativeIfPossible(root, t2));
199: System.out.println("Rel root, t3: "
200: + makePathRelativeIfPossible(root, t3));
201: System.out.println("Rel root, t4: "
202: + makePathRelativeIfPossible(root, t4));
203:
204: File f1 = new File("f:\\a\\b");
205: File f2 = new File("f:\\a\\c");
206: File f3 = new File(f1, f2.toString());
207: System.out.println("f3 = " + f3.toString());
208: }
209:
210: }
|