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