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.Component;
024import org.biojava.nbio.core.util.PrettyXMLWriter;
025import org.w3c.dom.NamedNodeMap;
026import org.w3c.dom.Node;
027import org.w3c.dom.NodeList;
028
029import java.io.IOException;
030import java.io.PrintWriter;
031import java.io.StringWriter;
032import java.util.HashSet;
033import java.util.Set;
034
035
036public class ComponentXMLConverter {
037        public static String toXML(Component component) throws IOException{
038                StringWriter out = new StringWriter();
039
040                PrettyXMLWriter xml = new PrettyXMLWriter(new PrintWriter(out));
041                toXML(component, xml);
042
043                return out.toString();
044        }
045
046        public static void toXML(Component component, PrettyXMLWriter xml) throws IOException{
047                xml.openTag("component");
048
049                xml.attribute("nTerminal" , component.isNTerminal()+"");
050                xml.attribute("cTerminal", component.isCTerminal()+"");
051                for (String pdbccId : component.getPdbccIds()){
052                        xml.openTag("pdbccID");
053                        xml.attribute("id", pdbccId);
054                        xml.closeTag("pdbccID");
055                }
056
057                xml.closeTag("component");
058        }
059
060        public static Component fromXML(String xml){
061                return null;
062        }
063
064        public static Component fromXML(Node componentN) {
065
066                String name = componentN.getNodeName();
067                if ( ! name.equals("component"))
068                        throw new RuntimeException("did not get component element, but " + name);
069
070                //String type = getAttribute(componentN, "type");
071                String nTerminalS = getAttribute(componentN, "nTerminal");
072                String cTerminalS = getAttribute(componentN, "cTerminal");
073
074                boolean isNTerminal = Boolean.parseBoolean(nTerminalS);
075                boolean isCTerminal = Boolean.parseBoolean(cTerminalS);
076
077                Set<String>pdbccIds = new HashSet<String>();
078
079                NodeList valList = componentN.getChildNodes();
080                int numChildren  = valList.getLength();
081
082
083                for ( int e =0; e< numChildren ; e++){
084                        Node  pdbccN = valList.item(e);
085
086                        if(!pdbccN.hasAttributes()) continue;
087
088
089                        if ( pdbccN.getNodeName().equals("pdbccID")) {
090                                String id = getAttribute(pdbccN, "id");
091                                pdbccIds.add(id);
092                        }
093
094                }
095
096                Component c = Component.of(pdbccIds, isNTerminal, isCTerminal);
097                return c;
098
099        }
100
101        private static String getAttribute(Node node, String attr){
102                if( ! node.hasAttributes())
103                        return null;
104
105                NamedNodeMap atts = node.getAttributes();
106
107                if ( atts == null)
108                        return null;
109
110                Node att = atts.getNamedItem(attr);
111                if ( att == null)
112                        return null;
113
114                String value = att.getTextContent();
115
116                return value;
117
118        }
119}