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 31, 2011
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.scop.server;
026
027import javax.xml.bind.JAXBContext;
028import javax.xml.bind.Marshaller;
029import javax.xml.bind.Unmarshaller;
030import javax.xml.bind.annotation.XmlAccessType;
031import javax.xml.bind.annotation.XmlAccessorType;
032import javax.xml.bind.annotation.XmlRootElement;
033import java.io.ByteArrayInputStream;
034import java.io.ByteArrayOutputStream;
035import java.io.PrintStream;
036import java.io.Serializable;
037import java.util.TreeSet;
038
039
040
041@XmlRootElement(name = "TreeSetStringWrapper", namespace ="http://source.rcsb.org")
042@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
043public class TreeSetStringWrapper implements Serializable{
044
045
046        /**
047         *
048         */
049        private static final long serialVersionUID = 4193799052494327416L;
050        TreeSet<String> data;
051
052        static JAXBContext jaxbContext;
053        static {
054                try {
055                        jaxbContext= JAXBContext.newInstance(TreeSetStringWrapper.class);
056                } catch (Exception e){
057                        throw new RuntimeException("Could not initialize JAXB context for " + TreeSetStringWrapper.class, e);
058                }
059        }
060
061        public TreeSetStringWrapper(){
062                data = new TreeSet<String>();
063        }
064
065        public TreeSet<String> getData() {
066                return data;
067        }
068
069        public void setData(TreeSet<String> data) {
070                this.data = data;
071        }
072        public  String toXML(){
073
074                ByteArrayOutputStream baos = new ByteArrayOutputStream();
075
076                PrintStream ps = new PrintStream(baos);
077
078                try {
079
080                        Marshaller m = jaxbContext.createMarshaller();
081
082                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
083
084                        m.marshal( this, ps);
085
086
087                } catch (Exception e){
088                        throw new RuntimeException("Could not convert " + getClass() + " to XML", e);
089                }
090
091                return baos.toString();
092
093        }
094
095        public static TreeSetStringWrapper fromXML(String xml){
096
097                TreeSetStringWrapper job = null;
098
099                try {
100
101                        Unmarshaller un = jaxbContext.createUnmarshaller();
102
103                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
104
105                        job = (TreeSetStringWrapper) un.unmarshal(bais);
106
107                } catch (Exception e){
108                        throw new RuntimeException("Could not parse " + TreeSetStringWrapper.class + " from XML", e);
109                }
110
111                return job;
112        }
113
114
115}