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