001/**
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Aug 30, 2011
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.scop.server;
026
027import org.biojava.nbio.structure.domain.pdp.Domain;
028import org.biojava.nbio.structure.scop.ScopDescription;
029import org.biojava.nbio.structure.scop.ScopDomain;
030import org.biojava.nbio.structure.scop.ScopNode;
031
032import javax.xml.bind.JAXBContext;
033import javax.xml.bind.JAXBException;
034import javax.xml.bind.Marshaller;
035import javax.xml.bind.Unmarshaller;
036import java.io.ByteArrayInputStream;
037import java.io.ByteArrayOutputStream;
038import java.io.PrintStream;
039import java.util.List;
040import java.util.SortedSet;
041import java.util.TreeSet;
042
043
044/** Utility classes for the XML serialization and de-serialization of SCOP.
045 *
046 * @author Andreas Prlic
047 * @since 3.0.2
048 *
049 */
050public class XMLUtil {
051
052        static JAXBContext jaxbContextScopDescription;
053        static {
054                try {
055                        jaxbContextScopDescription= JAXBContext.newInstance(ScopDescription.class);
056                } catch (JAXBException e){
057                        throw new RuntimeException("Could not initialize JAXB context", e);
058                }
059        }
060
061        static JAXBContext jaxbContextScopDomain;
062        static {
063                try {
064                        jaxbContextScopDomain= JAXBContext.newInstance(ScopDomain.class);
065                } catch (JAXBException e){
066                        throw new RuntimeException("Could not initialize JAXB context", e);
067                }
068        }
069
070        static JAXBContext jaxbContextScopNode;
071        static {
072                try {
073                        jaxbContextScopNode= JAXBContext.newInstance(ScopNode.class);
074                } catch (JAXBException e){
075                        throw new RuntimeException("Could not initialize JAXB context", e);
076                }
077        }
078
079        static JAXBContext jaxbContextDomains;
080        static {
081                try {
082                        jaxbContextDomains= JAXBContext.newInstance(TreeSet.class);
083                } catch (JAXBException e){
084                        throw new RuntimeException("Could not initialize JAXB context", e);
085                }
086        }
087
088        static JAXBContext jaxbContextStringSortedSet;
089        static {
090                try {
091                        jaxbContextStringSortedSet= JAXBContext.newInstance(TreeSetStringWrapper.class);
092                } catch (JAXBException e){
093                        throw new RuntimeException("Could not initialize JAXB context", e);
094                }
095        }
096
097        static JAXBContext jaxbContextComments;
098        static {
099                try {
100                        jaxbContextComments = JAXBContext.newInstance(ListStringWrapper.class);
101                } catch( JAXBException e){
102                        throw new RuntimeException("Could not initialize JAXB context", e);
103                }
104        }
105
106
107        public static String getScopDescriptionXML(ScopDescription desc){
108
109                return converScopDescription(desc);
110
111        }
112
113        public static ScopDescription getScopDescriptionFromXML(String xml){
114
115                ScopDescription job = null;
116
117                try {
118
119                        Unmarshaller un = jaxbContextScopDescription.createUnmarshaller();
120
121                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
122
123                        job = (ScopDescription) un.unmarshal(bais);
124
125                } catch (JAXBException e){
126                        throw new RuntimeException("Could not parse from XML", e);
127                }
128
129                return job;
130        }
131
132        private static String converScopDescription(ScopDescription desc) {
133                ByteArrayOutputStream baos = new ByteArrayOutputStream();
134
135                PrintStream ps = new PrintStream(baos);
136
137                try {
138
139                        Marshaller m = jaxbContextScopDescription.createMarshaller();
140
141                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
142
143                        m.marshal( desc, ps);
144
145
146                } catch (JAXBException e){
147                        throw new RuntimeException("Could not parse from XML", e);
148                }
149
150                return baos.toString();
151        }
152
153        public static String getScopDescriptionsXML(List<ScopDescription> descriptions){
154
155                ScopDescriptions container = new ScopDescriptions();
156                container.setScopDescription(descriptions);
157
158                return container.toXML();
159
160        }
161
162
163
164        public static String getCommentsXML(List<String> comments ){
165                ByteArrayOutputStream baos = new ByteArrayOutputStream();
166
167                PrintStream ps = new PrintStream(baos);
168
169                try {
170
171                        Marshaller m = jaxbContextComments.createMarshaller();
172
173                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
174
175                        ListStringWrapper wrapper = new ListStringWrapper();
176                        wrapper.setData(comments);
177
178                        m.marshal( wrapper, ps);
179
180
181                } catch (JAXBException e){
182                        throw new RuntimeException("Could not parse from XML", e);
183                }
184
185                return baos.toString();
186        }
187        public static List<String> getCommentsFromXML(String xml){
188
189                List<String> comments = null;
190
191                try {
192
193                        Unmarshaller un = jaxbContextComments.createUnmarshaller();
194
195                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
196
197                        ListStringWrapper wrapper = (ListStringWrapper) un.unmarshal(bais);
198                        comments = wrapper.getData();
199
200                } catch (JAXBException e){
201                        throw new RuntimeException("Could not parse from XML", e);
202                }
203
204                return comments;
205        }
206
207
208        public static String getScopNodeXML(ScopNode scopNode){
209                ByteArrayOutputStream baos = new ByteArrayOutputStream();
210
211                PrintStream ps = new PrintStream(baos);
212
213                try {
214
215                        Marshaller m = jaxbContextScopNode.createMarshaller();
216
217                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
218
219                        m.marshal( scopNode, ps);
220
221
222                } catch (JAXBException e){
223                        throw new RuntimeException("Could not parse from XML", e);
224                }
225
226                return baos.toString();
227        }
228
229        public static ScopNode getScopNodeFromXML(String xml){
230                ScopNode job = null;
231
232                try {
233
234                        Unmarshaller un = jaxbContextScopNode.createUnmarshaller();
235
236                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
237
238                        job = (ScopNode) un.unmarshal(bais);
239
240                } catch (JAXBException e){
241                        throw new RuntimeException("Could not parse from XML", e);
242                }
243
244                return job;
245        }
246
247        public static String getScopNodesXML(List<ScopNode> nodes) {
248                ScopNodes container = new ScopNodes();
249                container.setScopNode(nodes);
250
251                return container.toXML();
252        }
253
254        public static String getScopDomainXML(ScopDomain domain){
255                ByteArrayOutputStream baos = new ByteArrayOutputStream();
256
257                PrintStream ps = new PrintStream(baos);
258
259                try {
260
261                        Marshaller m = jaxbContextScopDomain.createMarshaller();
262
263                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
264
265                        m.marshal( domain, ps);
266
267
268                } catch (JAXBException e){
269                        throw new RuntimeException("Could not serialize to XML", e);
270                }
271
272                return baos.toString();
273        }
274
275        public static ScopDomain getScopDomainFromXML(String xml){
276                ScopDomain job = null;
277
278                try {
279
280                        Unmarshaller un = jaxbContextScopDomain.createUnmarshaller();
281
282                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
283
284                        job = (ScopDomain) un.unmarshal(bais);
285
286                } catch (JAXBException e){
287                        throw new RuntimeException("Could not serialize to XML", e);
288                }
289
290                return job;
291        }
292
293        public static String getScopDomainsXML(List<ScopDomain> domains) {
294                ScopDomains container = new ScopDomains();
295                container.setScopDomain(domains);
296
297                return container.toXML();
298        }
299
300
301        public static String getDomainsXML(SortedSet<Domain> domains){
302                ByteArrayOutputStream baos = new ByteArrayOutputStream();
303
304                PrintStream ps = new PrintStream(baos);
305
306                try {
307
308                        Marshaller m = jaxbContextDomains.createMarshaller();
309
310                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
311
312                        m.marshal( domains, ps);
313
314
315                } catch (JAXBException e){
316                        throw new RuntimeException("Could not serialize to XML", e);
317                }
318
319                return baos.toString();
320        }
321        @SuppressWarnings("unchecked")
322        public static SortedSet<Domain> getDomainsFromXML(String xml) {
323
324                SortedSet<Domain> domains = null;
325                try {
326
327                        Unmarshaller un = jaxbContextDomains.createUnmarshaller();
328
329                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
330
331                        domains = (SortedSet<Domain>) un.unmarshal(bais);
332
333                } catch (JAXBException e){
334                        throw new RuntimeException("Could not serialize to XML", e);
335                }
336
337                return domains;
338        }
339
340        public static String getDomainRangesXML(SortedSet<String> domainRanges){
341                if ( ! (domainRanges instanceof TreeSet)) {
342                        throw new IllegalArgumentException("SortedSet needs to be a TreeSet!");
343                }
344                TreeSet<String> data = (TreeSet<String>)domainRanges;
345                ByteArrayOutputStream baos = new ByteArrayOutputStream();
346
347                PrintStream ps = new PrintStream(baos);
348
349                try {
350
351                        Marshaller m = jaxbContextStringSortedSet.createMarshaller();
352
353                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
354                        TreeSetStringWrapper wrapper = new TreeSetStringWrapper();
355                        wrapper.setData(data);
356                        m.marshal( wrapper, ps);
357
358
359                } catch (JAXBException e){
360                        throw new RuntimeException("Could not serialize to XML", e);
361                }
362
363                return baos.toString();
364        }
365
366        public static SortedSet<String> getDomainRangesFromXML(String xml){
367                SortedSet<String> domains = null;
368                try {
369
370                        Unmarshaller un = jaxbContextStringSortedSet.createUnmarshaller();
371
372                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
373
374                        TreeSetStringWrapper wrapper = (TreeSetStringWrapper) un.unmarshal(bais);
375                        domains = wrapper.getData();
376
377                } catch (JAXBException e){
378                        throw new RuntimeException("Could not serialize to XML", e);
379                }
380
381                return domains;
382        }
383}