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.structure.align.xml;
022
023import org.biojava.nbio.structure.align.client.PdbPair;
024import org.biojava.nbio.structure.align.fatcat.FatCatRigid;
025import org.biojava.nbio.core.util.PrettyXMLWriter;
026import org.w3c.dom.Document;
027import org.w3c.dom.NamedNodeMap;
028import org.w3c.dom.Node;
029import org.w3c.dom.NodeList;
030import org.xml.sax.InputSource;
031
032import javax.xml.parsers.DocumentBuilder;
033import javax.xml.parsers.DocumentBuilderFactory;
034import java.io.IOException;
035import java.io.PrintWriter;
036import java.io.StringReader;
037import java.io.StringWriter;
038import java.util.SortedSet;
039import java.util.TreeSet;
040
041public class PdbPairXMLConverter {
042
043
044        public static final String DEFAULT_METHOD_NAME = FatCatRigid.algorithmName;
045
046        public static PdbPairsMessage convertXMLtoPairs(String xml) {
047                SortedSet<PdbPair>  pairs = new TreeSet<PdbPair>();
048                PdbPairsMessage message = new PdbPairsMessage();
049                try
050                {
051                        //Convert string to XML document
052                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
053                        DocumentBuilder db = factory.newDocumentBuilder();
054                        InputSource inStream = new InputSource();
055                        inStream.setCharacterStream(new StringReader(xml));
056                        Document doc = db.parse(inStream);
057
058                        // normalize text representation
059                        doc.getDocumentElement().normalize();
060
061                        NodeList algorithms =  doc.getElementsByTagName("pairs");
062                        //System.out.println(algorithms.getLength());
063                        for(int i=0; i<algorithms.getLength() ; i++) {
064
065                                Node algo       = algorithms.item(i);
066
067                                NamedNodeMap map = algo.getAttributes();
068
069                                String name = map.getNamedItem("algorithm").getTextContent();
070
071                                if ( name != null) {
072
073                                        message.setMethod(name);
074
075                                }
076
077                        }
078
079
080                        NodeList listOfPairs = doc.getElementsByTagName("pair");
081                        //System.out.println(listOfPairs.getLength());
082
083                        // go over the blocks
084                        for(int i=0; i<listOfPairs.getLength() ; i++)
085                        {
086                                Node pair       = listOfPairs.item(i);
087                                //NodeList valList = pair.getChildNodes();
088                                //int numChildren  = valList.getLength();
089
090                                NamedNodeMap map = pair.getAttributes();
091
092                                String name1 =  map.getNamedItem("name1").getTextContent();
093                                String name2 =  map.getNamedItem("name2").getTextContent();
094                                PdbPair pdbPair = new PdbPair(name1, name2);
095                                pairs.add(pdbPair);
096                        }
097
098                } catch (Exception e){
099                        e.printStackTrace();
100                }
101                message.setPairs(pairs);
102                return message;
103        }
104
105        public static String convertPairsToXML(SortedSet<PdbPair> pairs, String method){
106                StringWriter sw = new StringWriter();
107                PrintWriter writer = new PrintWriter(sw);
108
109                if (method == null){
110                        method = DEFAULT_METHOD_NAME;
111                }
112
113
114                PrettyXMLWriter xml = new PrettyXMLWriter(writer);
115                try {
116
117
118                        xml.openTag("pairs");
119                        xml.attribute("algorithm", method);
120                        for ( PdbPair pair : pairs){
121                                xml.openTag("pair");
122                                xml.attribute("name1", pair.getName1());
123                                xml.attribute("name2", pair.getName2());
124                                xml.closeTag("pair");
125                        }
126                        xml.closeTag("pairs");
127                } catch(IOException ex){
128                        ex.printStackTrace();
129                }
130
131                return sw.toString();
132        }
133
134
135}