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.IOException;
25  import java.io.Reader;
26  import java.io.Writer;
27  import java.util.Iterator;
28  
29  import org.dom4j.Document;
30  import org.dom4j.DocumentException;
31  import org.dom4j.DocumentHelper;
32  import org.dom4j.Element;
33  import org.dom4j.io.OutputFormat;
34  import org.dom4j.io.SAXReader;
35  import org.dom4j.io.XMLWriter;
36  
37  import fr.ens.transcriptome.doelan.DoelanException;
38  import fr.ens.transcriptome.doelan.data.QualityTestSuiteList;
39  import fr.ens.transcriptome.doelan.data.QualityTestSuiteURL;
40  
41  /***
42   * @author Laurent jourdren
43   */
44  class QualityTestSuiteListXMLDocument {
45  
46    /*** Stores the test suite object this object operates on. */
47    private QualityTestSuiteList list;
48  
49    private Document document;
50  
51    //
52    // Getters
53    //
54  
55    /***
56     * Return the test suite list object for this document.
57     * @return the test suite list object
58     */
59    public QualityTestSuiteList getTestSuiteList() {
60      return list;
61    }
62  
63    /***
64     * Return the dom4j working document
65     * @return the dom4j working document
66     */
67    public Document getDocument() {
68      return document;
69    }
70  
71    //
72    // Setters
73    //
74  
75    /***
76     * Set the test suite object this documnet operates on.
77     * @param list the test suite list object
78     */
79    public void setTestSuiteList(final QualityTestSuiteList list) {
80      this.list = list;
81    }
82  
83    /***
84     * Set the dom4j working document
85     * @param document The dom4j working document
86     */
87    public void setDocument(final Document document) {
88      this.document = document;
89    }
90  
91    //
92    // Constructor
93    //
94  
95    /***
96     * Creates a new instance of ParametersXMLDocument and sets the paramter
97     * object to be processed.
98     * @param list the test suite list object
99     */
100   public QualityTestSuiteListXMLDocument(final QualityTestSuiteList list) {
101     setTestSuiteList(list);
102     createDocument();
103   }
104 
105   /***
106    * Create the xml document
107    * @return the xml document
108    */
109   public Document createDocument() {
110 
111     Document document = DocumentHelper.createDocument();
112     final Element root = document.addElement("qualitytestsuitelist");
113 
114     final QualityTestSuiteList l = getTestSuiteList();
115 
116     if (l != null) {
117 
118       String[] types = l.getChipTypes();
119 
120       for (int i = 0; i < types.length; i++) {
121 
122         final Element chipType = root.addElement("chiptype");
123 
124         final Element name = chipType.addElement("name");
125         name.addText(types[i]);
126 
127         QualityTestSuiteURL[] urls = l.getTestSuiteURLs(types[i]);
128 
129         for (int j = 0; j < urls.length; j++) {
130           final Element testsuiteElement = chipType
131               .addElement("qualitytestsuite");
132           Element nameTestSuiteElement = testsuiteElement.addElement("name");
133 
134           nameTestSuiteElement.addText(urls[j].getName());
135 
136           Element urlElement = testsuiteElement.addElement("url");
137           urlElement.addText(urls[j].getURL().toString());
138         }
139       } // chip types
140     } // list not null
141 
142     setDocument(document);
143     return document;
144   }
145 
146   //
147   // Writer
148   //
149 
150   /***
151    * Writes a configuration (or parts of it) to the given writer.
152    * @param out the output writer
153    * @param prefix the prefix of the subset to write; if <b>null </b>, the whole
154    *                 configuration is written
155    * @param root the name of the root element of the resulting document; <b>null
156    *                 </b> for a default name
157    * @param pretty flag for the pretty print mode
158    * @throws IOException if an IO error occurs
159    * @throws DocumentException if there is an error during processing
160    */
161   public void write(final Writer out, final String prefix, final String root,
162       final boolean pretty) throws IOException, DocumentException {
163     OutputFormat format = pretty ? OutputFormat.createPrettyPrint()
164         : OutputFormat.createCompactFormat();
165 
166     XMLWriter writer = new XMLWriter(out, format);
167     writer.write(getDocument());
168   }
169 
170   /***
171    * Writes a configuration (or parts of it) to the given writer. This
172    * overloaded version always uses pretty print mode.
173    * @param out the output writer
174    * @param prefix the prefix of the subset to write; if <b>null </b>, the whole
175    *                 configuration is written
176    * @param root the name of the root element of the resulting document; <b>null
177    *                 </b> for a default name
178    * @throws IOException if an IO error occurs
179    * @throws DocumentException if there is an error during processing
180    */
181   public void write(final Writer out, final String prefix, final String root)
182       throws IOException, DocumentException {
183     write(out, prefix, root, true);
184   }
185 
186   /***
187    * Writes a configuration (or parts of it) to the given writer. The resulting
188    * document's root element will be given a default name.
189    * @param out the output writer
190    * @param prefix the prefix of the subset to write; if <b>null </b>, the whole
191    *                 configuration is written
192    * @param pretty flag for the pretty print mode
193    * @throws IOException if an IO error occurs
194    * @throws DocumentException if there is an error during processing
195    */
196   public void write(final Writer out, final String prefix, final boolean pretty)
197       throws IOException, DocumentException {
198     write(out, prefix, null, pretty);
199   }
200 
201   /***
202    * Writes a configuration (or parts of it) to the given writer. The resulting
203    * document's root element will be given a default name. This overloaded
204    * version always uses pretty print mode.
205    * @param out the output writer
206    * @param prefix the prefix of the subset to write; if <b>null </b>, the whole
207    *                 configuration is written
208    * @throws IOException if an IO error occurs
209    * @throws DocumentException if there is an error during processing
210    */
211   public void write(final Writer out, final String prefix) throws IOException,
212       DocumentException {
213     write(out, prefix, true);
214   }
215 
216   /***
217    * Writes the wrapped configuration to the given writer. The resulting
218    * document's root element will be given a default name.
219    * @param out the output writer
220    * @param pretty flag for the pretty print mode
221    * @throws IOException if an IO error occurs
222    * @throws DocumentException if there is an error during processing
223    */
224   public void write(final Writer out, final boolean pretty) throws IOException,
225       DocumentException {
226     write(out, null, null, pretty);
227   }
228 
229   /***
230    * Writes the wrapped configuration to the given writer. The resulting
231    * document's root element will be given a default name. This overloaded
232    * version always uses pretty print mode.
233    * @param out the output writer
234    * @throws IOException if an IO error occurs
235    * @throws DocumentException if there is an error during processing
236    */
237   public void write(final Writer out) throws IOException, DocumentException {
238     write(out, true);
239   }
240 
241   //
242   // Read XML
243   //
244 
245   public static QualityTestSuiteList parse(final Reader reader)
246       throws DocumentException, DoelanException {
247 
248     SAXReader saxReader = new SAXReader();
249     Document document = saxReader.read(reader);
250     return parse(document);
251   }
252 
253   public static QualityTestSuiteList parse(final Document document)
254       throws DocumentException, DoelanException {
255 
256     Element root = document.getRootElement();
257     QualityTestSuiteList tsl = new QualityTestSuiteList();
258 
259     // iterate through child elements of root with element name "foo"
260     for (Iterator i = root.elementIterator("chiptype"); i.hasNext();) {
261       final Element chiptypeElement = (Element) i.next();
262 
263       String name = null;
264 
265       for (Iterator i2 = chiptypeElement.elementIterator("name"); i2.hasNext();) {
266         name = ((Element) i2.next()).getText();
267         tsl.addChipType(name);
268       }
269 
270       for (Iterator i3 = chiptypeElement.elementIterator("qualitytestsuite"); i3
271           .hasNext();) {
272         final Element qualitytestsuiteElement = (Element) i3.next();
273 
274         QualityTestSuiteURL tsURL = new QualityTestSuiteURL();
275 
276         for (Iterator i4 = qualitytestsuiteElement.elementIterator("name"); i4
277             .hasNext();) {
278           final Element tsNameElement = (Element) i4.next();
279           tsURL.setName(tsNameElement.getTextTrim());
280         }
281 
282         for (Iterator i5 = qualitytestsuiteElement.elementIterator("url"); i5
283             .hasNext();) {
284           final Element tsURLElement = (Element) i5.next();
285           tsURL.setUrl(tsURLElement.getTextTrim());
286         }
287 
288         tsl.addTestSuite(name, tsURL);
289 
290       } // qualitytestsuite Element
291     } // chiptype
292 
293     return tsl;
294   }
295 
296 }