create changelog entry
[debian/openrocket] / core / 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          * Provide an estimate of the file size when saving the document with the
73          * specified options.  This is used as an indication to the user and when estimating
74          * file save progress.
75          * 
76          * @param doc           the document.
77          * @param options       the save options, compression must be taken into account.
78          * @return                      the estimated number of bytes the storage would take.
79          */
80         public abstract long estimateFileSize(OpenRocketDocument doc, StorageOptions options);
81         
82         
83         
84         
85         public static String escapeXML(String s) {
86
87                 s = s.replace("&", "&");
88                 s = s.replace("<", "&lt;");
89                 s = s.replace(">", "&gt;");
90                 s = s.replace("\"","&quot;");
91                 s = s.replace("'", "&apos;");
92                 
93                 for (int i=0; i < s.length(); i++) {
94                         char n = s.charAt(i);
95                         if (((n < 32) && (n != 9) && (n != 10) && (n != 13)) || (n == 127)) {
96                                 s = s.substring(0,i) + "&#" + ((int)n) + ";" + s.substring(i+1);
97                         }
98                 }
99                 
100                 return s;
101         }
102 }