View Javadoc

1   /*
2    *                Doelan development code
3    *
4    * This code may be freely distributed and modified under the
5    * terms of the GNU General Public Licence.  This should
6    * be distributed with the code. If you do not have a copy,
7    * see:
8    *
9    *      http://www.gnu.org/copyleft/gpl.txt
10   *
11   * Copyright (c) 2004-2005 ENS Microarray Platform
12   * Copyright for this code is held jointly by the individual
13   * authors.  These should be listed in @author doc comments.
14   *
15   * For more information on the Doelan project and its aims,
16   * or to join the Doelan mailing list, visit the home page
17   * at:
18   *
19   *      http://www.transcriptome.ens.fr/doelan
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    // Getters
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    // Setters
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   // Methods from QualityTestSuiteIO
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 }