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 HasResultXMLConverter 044{ 045 046 047 048 049 /** return flag if the server has a result 050 * 051 * @param hasResult 052 * @return flag if there is a result 053 */ 054 public String toXML(boolean hasResult) throws IOException{ 055 StringWriter swriter = new StringWriter(); 056 057 PrintWriter writer = new PrintWriter(swriter); 058 PrettyXMLWriter xml = new PrettyXMLWriter(writer); 059 060 xml.openTag("alignment"); 061 xml.attribute("hasResult", String.valueOf(hasResult)); 062 xml.closeTag("alignment"); 063 xml.close(); 064 return swriter.toString(); 065 } 066 067 public boolean fromXML(String xml) { 068 069 boolean hasResult = false; 070 071 try 072 { 073 //Convert string to XML document 074 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 075 DocumentBuilder db = factory.newDocumentBuilder(); 076 InputSource inStream = new InputSource(); 077 inStream.setCharacterStream(new StringReader(xml)); 078 Document doc = db.parse(inStream); 079 080 // normalize text representation 081 doc.getDocumentElement().normalize(); 082 083 084 //Element rootElement = doc.getDocumentElement(); 085 086 NodeList listOfAlignments = doc.getElementsByTagName("alignment"); 087 //int numArrays = listOfAlignments.getLength(); 088 //System.out.println("got " + numArrays + " alignment results."); 089 // go over the blocks 090 091 092 for(int afpPos=0; afpPos<listOfAlignments.getLength() ; afpPos++) 093 { 094 095 Node rootElement = listOfAlignments.item(afpPos); 096 097 String flag = getAttribute(rootElement,"hasResult"); 098 //System.out.println("got flag from server:" + flag); 099 if (flag.equals("true")) 100 hasResult = true; 101 102 } 103 } 104 catch (SAXParseException err) 105 { 106 System.out.println ("** Parsing error" + ", line " 107 + err.getLineNumber () + ", uri " + err.getSystemId ()); 108 System.out.println(" " + err.getMessage ()); 109 } 110 catch (SAXException e) 111 { 112 Exception x = e.getException (); 113 ((x == null) ? e : x).printStackTrace (); 114 } 115 catch (Throwable t) 116 { 117 t.printStackTrace (); 118 } 119 120 return hasResult; 121 } 122 123 124 private static String getAttribute(Node node, String attr){ 125 if( ! node.hasAttributes()) 126 return null; 127 128 NamedNodeMap atts = node.getAttributes(); 129 130 if ( atts == null) 131 return null; 132 133 Node att = atts.getNamedItem(attr); 134 if ( att == null) 135 return null; 136 137 String value = att.getTextContent(); 138 139 return value; 140 141 } 142}