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 May 10, 2010
021 * Author: Andreas Prlic
022 *
023 */
024
025package org.biojava.nbio.structure.align.xml;
026
027import org.biojava.nbio.core.util.PrettyXMLWriter;
028import org.w3c.dom.Document;
029import org.w3c.dom.NamedNodeMap;
030import org.w3c.dom.Node;
031import org.w3c.dom.NodeList;
032import org.xml.sax.InputSource;
033import org.xml.sax.SAXException;
034import org.xml.sax.SAXParseException;
035
036import javax.xml.parsers.DocumentBuilder;
037import javax.xml.parsers.DocumentBuilderFactory;
038import java.io.IOException;
039import java.io.PrintWriter;
040import java.io.StringReader;
041import java.io.StringWriter;
042
043public class PositionInQueueXMLConverter
044{
045
046        public String toXML(int position) throws IOException{
047                StringWriter swriter = new StringWriter();
048
049                PrintWriter writer = new PrintWriter(swriter);
050                PrettyXMLWriter xml = new PrettyXMLWriter(writer);
051
052                xml.openTag("queue");
053                xml.attribute("position", position+"");
054                xml.closeTag("queue");
055                xml.close();
056                return swriter.toString();
057        }
058
059        public int fromXML(String xml){
060                int position = Integer.MIN_VALUE;
061
062                try
063                {
064                        //Convert string to XML document
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 listOfAlignments = doc.getElementsByTagName("queue");
078                        //int numArrays = listOfAlignments.getLength();
079                        //System.out.println("got " + numArrays + " alignment results.");
080                        // go over the blocks
081
082
083                        for(int afpPos=0; afpPos<listOfAlignments.getLength() ; afpPos++)
084                        {
085
086                                Node rootElement       = listOfAlignments.item(afpPos);
087
088                                String pos = getAttribute(rootElement,"position");
089
090                                try {
091                                        position = Integer.parseInt(pos);
092                                } catch (NumberFormatException f){
093                                        f.printStackTrace();
094                                }
095
096                        }
097                }
098                catch (SAXParseException err)
099                {
100                        System.out.println ("** Parsing error" + ", line "
101                                        + err.getLineNumber () + ", uri " + err.getSystemId ());
102                        System.out.println(" " + err.getMessage ());
103                }
104                catch (SAXException e)
105                {
106                        Exception x = e.getException ();
107                        ((x == null) ? e : x).printStackTrace ();
108                }
109                catch (Throwable t)
110                {
111                        t.printStackTrace ();
112                }
113
114                return position;
115        }
116
117
118        private static String getAttribute(Node node, String attr){
119                if( ! node.hasAttributes())
120                        return null;
121
122                NamedNodeMap atts = node.getAttributes();
123
124                if ( atts == null)
125                        return null;
126
127                Node att = atts.getNamedItem(attr);
128                if ( att == null)
129                        return null;
130
131                String value = att.getTextContent();
132
133                return value;
134
135        }
136}