Initial commit
[debian/openrocket] / src / net / sf / openrocket / file / RocketSaver.java
1 package net.sf.openrocket.file;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStream;
8
9 import net.sf.openrocket.document.OpenRocketDocument;
10 import net.sf.openrocket.document.StorageOptions;
11
12
13 public abstract class RocketSaver {
14         
15         /**
16          * Save the document to the specified file using the default storage options.
17          * 
18          * @param dest                  the destination file.
19          * @param document              the document to save.
20          * @throws IOException  in case of an I/O error.
21          */
22         public final void save(File dest, OpenRocketDocument document) throws IOException {
23                 save(dest, document, document.getDefaultStorageOptions());
24         }
25
26         
27         /**
28          * Save the document to the specified file using the given storage options.
29          * 
30          * @param dest                  the destination file.
31          * @param document              the document to save.
32          * @param options               the storage options.
33          * @throws IOException  in case of an I/O error.
34          */
35         public void save(File dest, OpenRocketDocument document, StorageOptions options) 
36         throws IOException {
37                 OutputStream s = new BufferedOutputStream(new FileOutputStream(dest));
38                 try {
39                         save(s, document, options);
40                 } finally {
41                         s.close();
42                 }
43         }
44         
45         
46         /**
47          * Save the document to the specified output stream using the default storage options.
48          * 
49          * @param dest                  the destination stream.
50          * @param doc                   the document to save.
51          * @throws IOException  in case of an I/O error.
52          */
53         public final void save(OutputStream dest, OpenRocketDocument doc) throws IOException {
54                 save(dest, doc, doc.getDefaultStorageOptions());
55         }
56         
57         
58         /**
59          * Save the document to the specified output stream using the given storage options.
60          * 
61          * @param dest                  the destination stream.
62          * @param doc                   the document to save.
63          * @param options               the storage options.
64          * @throws IOException  in case of an I/O error.
65          */
66         public abstract void save(OutputStream dest, OpenRocketDocument doc, 
67                         StorageOptions options) throws IOException;
68         
69         
70         
71         
72         
73         
74         
75         public static String escapeXML(String s) {
76
77                 s = s.replace("&", "&");
78                 s = s.replace("<", "&lt;");
79                 s = s.replace(">", "&gt;");
80                 s = s.replace("\"","&quot;");
81                 s = s.replace("'", "&apos;");
82                 
83                 for (int i=0; i < s.length(); i++) {
84                         char n = s.charAt(i);
85                         if (((n < 32) && (n != 9) && (n != 10) && (n != 13)) || (n == 127)) {
86                                 s = s.substring(0,i) + "&#" + ((int)n) + ";" + s.substring(i+1);
87                         }
88                 }
89                 
90                 return s;
91         }
92 }