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.core.util.PrettyXMLWriter;
024import org.w3c.dom.Document;
025import org.w3c.dom.NamedNodeMap;
026import org.w3c.dom.Node;
027import org.w3c.dom.NodeList;
028import org.xml.sax.InputSource;
029
030import javax.xml.parsers.DocumentBuilder;
031import javax.xml.parsers.DocumentBuilderFactory;
032import java.io.IOException;
033import java.io.PrintWriter;
034import java.io.StringReader;
035import java.io.StringWriter;
036import java.util.SortedSet;
037import java.util.TreeSet;
038
039public class RepresentativeXMLConverter {
040
041
042        public static final String toXML(SortedSet<String> representatives){
043                StringWriter sw = new StringWriter();
044                PrintWriter writer = new PrintWriter(sw);
045
046                PrettyXMLWriter xml = new PrettyXMLWriter(writer);
047                try {
048                        xml.openTag("representatives");
049
050                        for ( String repr : representatives){
051                                xml.openTag("pdbChain");
052                                xml.attribute("name", repr);
053                                xml.closeTag("pdbChain");
054                        }
055                        xml.closeTag("representatives");
056                } catch(IOException ex){
057                        ex.printStackTrace();
058                }
059                return sw.toString();
060        }
061
062        public static final SortedSet<String> fromXML(String xml){
063                SortedSet<String> representatives = new TreeSet<String>();
064                try {
065                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
066                        DocumentBuilder db = factory.newDocumentBuilder();
067                        InputSource inStream = new InputSource();
068                        inStream.setCharacterStream(new StringReader(xml));
069                        Document doc = db.parse(inStream);
070
071                        // normalize text representation
072                        doc.getDocumentElement().normalize();
073
074
075                        //Element rootElement = doc.getDocumentElement();
076
077                        NodeList listOfPairs = doc.getElementsByTagName("pdbChain");
078                        //int numArrays = listOfArrays.getLength();
079
080                        // go over the blocks
081                        for(int i=0; i<listOfPairs.getLength() ; i++)
082                        {
083                                Node pair       = listOfPairs.item(i);
084                                //NodeList valList = pair.getChildNodes();
085                                //int numChildren  = valList.getLength();
086
087                                NamedNodeMap map = pair.getAttributes();
088
089                                String name =  map.getNamedItem("name").getTextContent();
090                                representatives.add(name);
091                        }
092
093                } catch (Exception e){
094                        e.printStackTrace();
095                }
096
097                return representatives;
098        }
099}