/* * Created on Jul 6, 2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author Betsy Rolland * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ package org.biomediator.wrappers; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.*; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.TreeSet; import java.util.*; import org.biomediator.util.AttribOption; import org.biomediator.util.StringUtility; import org.biomediator.xml.XMLParser; import org.biomediator.xml.XMLAttributeParser; import com.oroinc.text.regex.Perl5StreamInput; import java.io.FileReader; import org.xml.sax.XMLReader; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; public class InterProWrapper extends AbstractWrapper { public static final String IP_HELP_TEXT = " Description:\n" + " Provides an interface for searching the InterPro database.\n" + "\n" + " Usage:\n" + " Servlet examples:\n" + " ?term=term[tvalue=IPR010405,hexencoded=false,field=AccNumber]" + " ?term=term[tvalue=ipr000004,hexencoded=false,field=SecAcc]" + " ?term=term[tvalue=BRCA1*,hexencoded=false,field=AllText]" ; public static final String IP_SOURCE_TAG = "interpro_source"; public static final String IP_CMD_TERM = "term"; public static final String IP_OPTION_MAP_FIELD = "field"; public static final String IP_OPTION_MAP_TVALUE = "tvalue"; public static final String IP_OPTION_MAP_HEXENCODED = "hexencoded"; public static final int MAX_DOCSIZE = 1024 * 10000; public String proxyUrl = null; public static void main(String[] args) { InterProWrapper ip = new InterProWrapper(); //invoking the constructor ip.out = new PrintWriter(System.out, true); if (args.length > 0) { ip.processRequest(args[0]); } else { ip.processRequest(""); } } public void processRequest(String query) { // Make sure the servlet is initialized. initializeServlet(IP_HELP_TEXT, IP_SOURCE_TAG, getWrapperProperties("interpro")); String data2 = ""; // Parse the query string and process commands. if (query == null || query.equals("")) { query = CMD_HELP; } AttribOption ao = new AttribOption(query); processCommands(ao, getWrapperProperties("interpro")); // Check to see if we should exit immeditely. if (isExitCmd) { return; } String proxy = wrapperProperties.getProperty(WrapperProperties.PROP_PROXY_URL); if (proxy == null) { proxyUrl = ""; } else { proxyUrl = proxy + "?"; } String convertedQuery = null; TreeSet termTypeSet = ao.getAttribMapSet(IP_CMD_TERM); if (termTypeSet != null) { convertedQuery = constructIPQuery(termTypeSet.iterator()); } String urlBase = "http://srs.ebi.ac.uk/srsbin/cgi-bin/wgetz?[INTERPRO-"; String view = "]+-view+InterProPrintXML+-mime+text/xml"; try { // Download XML page. String queryURL = proxyUrl + urlBase + convertedQuery + view; //out.println("queryURL is " + queryURL); InputStream inStream = new BufferedInputStream(new URL(queryURL).openStream()); BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); String line = new String(); StringBuffer lineBf = new StringBuffer(); while ((line = br.readLine()) != null) lineBf.append(line + " "); String newString = lineBf.toString(); //out.println("newString is " + newString); newString = newString.replaceAll("<p>", ""); newString = newString.replaceAll("\\.</p>", ""); newString = newString.replaceAll("]*>", ""); //out.println("newString is " + newString); // ByteArrayInputStream newStream = new ByteArrayInputStream(newString.getBytes()); //BufferedReader br = new BufferedReader(new InputStreamReader(new URL(queryURL).openStream())); StringBuffer lineBuf = new StringBuffer(); lineBuf.append(HEADER_XML + "\n"); XMLAttributeParser xap = new XMLAttributeParser(newStream, lineBuf); lineBuf = xap.parseData(); //String line = null; /*while ((line = br.readLine()) != null) { lineBuf.append(line + "\n"); if (lineBuf.length() > MAX_DOCSIZE) { break; } } br.close();*/ // Isolate and process the portion of the page that contains data. String data = lineBuf.toString(); // Convert retrieved document to valid XML (order of replacements is important). data = data.replaceAll("<\\?xml version=\"1\\.0\"[^>]*>", HEADER_XML); data = data.replaceAll("]*>", ""); data = data.replaceAll("InterProEntrySet",IP_SOURCE_TAG); data = data.replaceAll("/InterProEntrySet",IP_SOURCE_TAG); // Print XML output to the client. out.print((data != null) ? data : ""); out.flush(); } //end of try catch (Exception e) { System.err.println(e); } } //end of processRequest public String constructIPQuery(Iterator queryIterator) { StringBuffer termBuf = new StringBuffer(""); HashMap groupMap = new HashMap(); String ttype = null; String tvalue = null; String field = null; String hexencoded = null; String retQuery = null; if (queryIterator.hasNext()) { while (queryIterator.hasNext()) { // Retrieve the term values that were parsed from the query URL. HashMap queryMap = (HashMap) queryIterator.next(); ttype = (String) queryMap.get(AttribOption.OPTION_PARAM_NAME); tvalue = (String) queryMap.get(IP_OPTION_MAP_TVALUE); field = (String) queryMap.get(IP_OPTION_MAP_FIELD); hexencoded = (String) queryMap.get(IP_OPTION_MAP_HEXENCODED); retQuery = field + ":" + tvalue; // Convert term values from hex to ascii if necessary. if (hexencoded != null && hexencoded.equals("true")) { retQuery = StringUtility.convertHexToASCII(retQuery); } } } return retQuery; } public void terminate() { // TODO Auto-generated method stub } }