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 Nov 7, 2011
021 * Created by Andreas Prlic
022 *
023 * @since 3.0.2
024 */
025package org.biojava.nbio.structure.domain;
026
027
028import javax.xml.bind.JAXBContext;
029import javax.xml.bind.Marshaller;
030import javax.xml.bind.Unmarshaller;
031import javax.xml.bind.annotation.XmlAccessType;
032import javax.xml.bind.annotation.XmlAccessorType;
033import javax.xml.bind.annotation.XmlRootElement;
034import java.io.ByteArrayInputStream;
035import java.io.ByteArrayOutputStream;
036import java.io.PrintStream;
037import java.util.HashMap;
038import java.util.Map;
039
040
041@XmlRootElement(name = "AssignmentXML", namespace ="http://source.rcsb.org")
042@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
043
044public class AssignmentXMLSerializer {
045
046        Map<String, String> assignments;
047
048        static JAXBContext jaxbContext;
049        static {
050                try {
051                        jaxbContext= JAXBContext.newInstance(AssignmentXMLSerializer.class);
052                } catch (Exception e){
053                        e.printStackTrace();
054                }
055        }
056
057        public AssignmentXMLSerializer(){
058                assignments = new HashMap<String, String>();
059
060        }
061
062        public void setAssignments(Map<String, String> assignments){
063
064                this.assignments = assignments;
065
066        }
067
068        public Map<String, String> getAssignments(){
069                return assignments;
070        }
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                        e.printStackTrace();
089                }
090
091                return baos.toString();
092
093        }
094
095        public static AssignmentXMLSerializer fromXML(String xml){
096
097                AssignmentXMLSerializer job = null;
098
099                try {
100
101                        Unmarshaller un = jaxbContext.createUnmarshaller();
102
103                        ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
104
105                        job = (AssignmentXMLSerializer) un.unmarshal(bais);
106
107                } catch (Exception e){
108                        e.printStackTrace();
109                }
110
111                return job;
112        }
113
114
115}