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