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 */
021package org.biojava.nbio.protmod.io;
022
023import org.biojava.nbio.protmod.structure.StructureGroup;
024import org.biojava.nbio.structure.ResidueNumber;
025import org.biojava.nbio.core.util.PrettyXMLWriter;
026import org.w3c.dom.NamedNodeMap;
027import org.w3c.dom.Node;
028
029import java.io.IOException;
030
031public class StructureGroupXMLConverter {
032
033        public static void toXML(StructureGroup group, PrettyXMLWriter xml) throws IOException{
034
035                xml.openTag("structureGroup");
036                xml.attribute("chainID", group.getChainId());
037                xml.attribute("pdbName", group.getPDBName());
038                if ( group.getInsCode() != null)
039                        xml.attribute("insCode",group.getInsCode()+"");
040                xml.attribute("residueNr", group.getResidueNumber()+"");
041                xml.attribute("isAminoAcid", Boolean.toString(group.isAminoAcid()));
042                xml.closeTag("structureGroup");
043        }
044
045        public static StructureGroup fromXML(Node n) {
046
047
048                String chainID = getAttribute(n, "chainID");
049                String pdbName = getAttribute(n, "pdbName");
050                String insCode = getAttribute(n, "insCode");
051                String resN  = getAttribute(n, "residueNr");
052                String isAminoAcid = getAttribute(n,"isAminoAcid");
053
054                ResidueNumber resNum = new ResidueNumber();
055                resNum.setChainName(chainID);
056                if ( ( insCode != null) && (! insCode.equals("null")) && insCode.length() == 1)
057                resNum.setInsCode(insCode.charAt(0));
058                resNum.setSeqNum(Integer.parseInt(resN));
059
060                StructureGroup g = new StructureGroup(resNum, pdbName, Boolean.valueOf(isAminoAcid));
061                return g;
062        }
063
064        private static String getAttribute(Node node, String attr){
065                if( ! node.hasAttributes())
066                        return null;
067
068                NamedNodeMap atts = node.getAttributes();
069
070                if ( atts == null)
071                        return null;
072
073                Node att = atts.getNamedItem(attr);
074                if ( att == null)
075                        return null;
076
077                String value = att.getTextContent();
078
079                return value;
080
081        }
082
083}