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 at 27 Mar 2014 021 * Author: ap3 022 */ 023 024package org.biojava.nbio.structure.xtal.io; 025 026import org.biojava.nbio.structure.xtal.SpaceGroup; 027 028import javax.xml.bind.JAXBContext; 029import javax.xml.bind.JAXBException; 030import javax.xml.bind.Marshaller; 031import javax.xml.bind.Unmarshaller; 032import javax.xml.bind.annotation.XmlRootElement; 033import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 034import java.io.ByteArrayInputStream; 035import java.io.ByteArrayOutputStream; 036import java.io.PrintStream; 037import java.util.TreeMap; 038 039@XmlRootElement(name="SpaceGroupMapRoot", namespace ="http://www.biojava.org") 040 041public class SpaceGroupMapRoot { 042 043 private TreeMap<Integer, SpaceGroup> mapProperty; 044 045 public SpaceGroupMapRoot() { 046 mapProperty = new TreeMap<Integer, SpaceGroup>(); 047 } 048 049 @XmlJavaTypeAdapter(SpaceGroupMapAdapter.class) 050 public TreeMap<Integer, SpaceGroup> getMapProperty() { 051 return mapProperty; 052 } 053 054 public void setMapProperty(TreeMap<Integer, SpaceGroup> map) { 055 this.mapProperty = map; 056 } 057 058 059 public String toXML() throws JAXBException { 060 061 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 062 063 PrintStream ps = new PrintStream(baos); 064 065 JAXBContext jaxbContext = JAXBContext.newInstance(SpaceGroupMapRoot.class); 066 067 Marshaller xmlConverter = jaxbContext.createMarshaller(); 068 xmlConverter.setProperty("jaxb.formatted.output",true); 069 xmlConverter.marshal(this,ps); 070 071 return baos.toString(); 072 } 073 074 public static SpaceGroupMapRoot fromXML(String xml) throws JAXBException{ 075 SpaceGroupMapRoot job = null; 076 077 JAXBContext jaxbContext = JAXBContext.newInstance(SpaceGroupMapRoot.class); 078 079 Unmarshaller un = jaxbContext.createUnmarshaller(); 080 081 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes()); 082 083 job = (SpaceGroupMapRoot) un.unmarshal(bais); 084 085 086 return job; 087 } 088}