01 /*
02 * Copyright (c) 2006, The University of Sheffield.
03 *
04 * This file is part of GATE (see http://gate.ac.uk/), and is free
05 * software, licenced under the GNU Library General Public License,
06 * Version 2, June 1991 (in the distribution as file licence.html,
07 * and also available at http://gate.ac.uk/gate/licence.html).
08 *
09 * Ian Roberts 30/10/2006
10 *
11 * $Id: UpdateSavedApp.java 7769 2006-10-30 21:51:39Z ian_roberts $
12 *
13 */
14 package gate.util.persistence;
15
16 import gate.util.persistence.*;
17 import java.io.*;
18 import com.thoughtworks.xstream.XStream;
19
20 /* (non-Javadoc)
21 * Note that this class is not part of the GATE persistence API, but must be in
22 * the gate.util.persistence package to have access to the package-private
23 * class GateApplication.
24 */
25
26 /**
27 * <p>Handy command-line utility that loads a saved application state in the
28 * old serialized-object format and resaves it in XML format. Note that this
29 * utility does not load the application into GATE and resave it, but merely
30 * converts the persistent representation from one format to another. If you
31 * have an old-style saved state that will not load (because, for example, it
32 * refers to a plugin that is not available) you can convert it to XML format
33 * with this tool and then hand-edit the resulting XML to fix it.</p>
34 *
35 * <p>Usage: java -classpath <gate.jar and lib/*.jar>
36 * gate.util.persistence.UpdateSavedApp <oldFormatFile>
37 * <newFormatFile></p>
38 */
39 public class UpdateSavedApp {
40 public static void main(String[] argv) throws Exception {
41 if(argv.length < 2) {
42 System.err.println("Usage:");
43 System.err.println(" UpdateSavedApp <oldFile> <newFile>");
44 System.exit(1);
45 }
46
47 File oldFile = new File(argv[0]);
48 File newFile = new File(argv[1]);
49
50 // make sure not to clobber an existing file
51 if(newFile.exists()) {
52 System.err.println(newFile + " already exists.");
53 System.err.println("Please move it out of the way. This tool will "
54 + "not overwrite an existing file,");
55 System.err.println("in particular the new file must not be the same as "
56 + "the old.");
57 System.exit(1);
58 }
59
60 // open old file for reading
61 FileInputStream fis = new FileInputStream(oldFile);
62 BufferedInputStream bis = new BufferedInputStream(fis);
63 ObjectInputStream ois = new ObjectInputStream(bis);
64
65
66 // load URL list and app from old file
67 Object oldUrlList = ois.readObject();
68 Object obj = ois.readObject();
69
70 // close input stream
71 ois.close();
72
73 // put them together in a GateApplication
74 GateApplication persistApp = new GateApplication();
75 persistApp.urlList = oldUrlList;
76 persistApp.application = obj;
77
78 // create XStream for writing new file
79 XStream xs = new XStream();
80
81 // save XML application
82 FileWriter fw = new FileWriter(newFile);
83 BufferedWriter bw = new BufferedWriter(fw);
84 xs.toXML(persistApp, bw);
85
86 bw.close();
87 }
88 }
|