1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package fr.ens.transcriptome.doelan.io;
23
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.OutputStream;
31 import java.io.PrintWriter;
32 import java.io.Reader;
33
34 import org.dom4j.DocumentException;
35
36 import fr.ens.transcriptome.doelan.DoelanException;
37 import fr.ens.transcriptome.doelan.data.QualityTestSuiteList;
38
39 /***
40 * @author Laurent Jourdren
41 */
42 class QualityTestSuiteListXMLIO implements QualityTestSuiteListIO {
43
44 private String filename;
45 private InputStream in;
46 private OutputStream out;
47
48
49
50
51
52 /***
53 * Get the filename.
54 * @return the filename
55 */
56 public String getFilename() {
57 return filename;
58 }
59
60 /***
61 * Get the inputstream.
62 * @return The inputstream
63 */
64 public InputStream getInputStream() {
65 return in;
66 }
67
68 /***
69 * Get the outputstream.
70 * @return The outputstream
71 */
72 public OutputStream getOutputStream() {
73 return out;
74 }
75
76
77
78
79
80 /***
81 * Set the filename.
82 * @param string
83 */
84 public void setFilename(final String string) {
85 filename = string;
86 }
87
88 /***
89 * Set the inputstream.
90 * @param stream The inputStream to set
91 */
92 public void setInputStream(final InputStream stream) {
93 in = stream;
94 }
95
96 /***
97 * Set the outputStream
98 * @param stream The outputStream to set
99 */
100 public void setOutputStream(final OutputStream stream) {
101 out = stream;
102 }
103
104
105
106
107
108 /***
109 * Write a test suite list.
110 * @param suite Test list suite to write
111 * @throws DoelanException if an error occurs while reading data
112 */
113 public void write(final QualityTestSuiteList suite) throws DoelanException {
114
115 if (getFilename() == null && getOutputStream() == null)
116 throw new DoelanException("Filename is null");
117
118 try {
119
120 if (getOutputStream() == null)
121 setOutputStream(new FileOutputStream(getFilename()));
122
123 final PrintWriter pw = new PrintWriter(getOutputStream());
124 final QualityTestSuiteListXMLDocument doc = new QualityTestSuiteListXMLDocument(
125 suite);
126 doc.createDocument();
127 doc.write(pw);
128 pw.close();
129
130 } catch (FileNotFoundException e) {
131 throw new DoelanException("File not found: " + getFilename());
132 } catch (IOException e) {
133 throw new DoelanException("IOException: " + e);
134 } catch (DocumentException e) {
135 throw new DoelanException("Error while creating XML document");
136 }
137
138 }
139
140 /***
141 * Read a test suite list.
142 * @return A new QualityTestSuiteList object
143 * @throws DoelanException if an error occurs while wrinting data
144 */
145 public QualityTestSuiteList read() throws DoelanException {
146
147 if (getFilename() == null && getInputStream() == null)
148 throw new DoelanException("Filename is null");
149
150 QualityTestSuiteList result = null;
151
152 try {
153 if (getInputStream() == null)
154 setInputStream(new FileInputStream(getFilename()));
155
156 final Reader r = new InputStreamReader(getInputStream());
157
158 result = QualityTestSuiteListXMLDocument.parse(r);
159
160 getInputStream().close();
161 } catch (DocumentException e) {
162 throw new DoelanException("Error while reading data");
163 } catch (FileNotFoundException e) {
164 throw new DoelanException("File not found: " + getFilename());
165 } catch (IOException e) {
166 throw new DoelanException("Error while reading data");
167 }
168
169 return result;
170 }
171
172 }