/**
 *  This robot receives response in XML and returns the
 *  parsed org.w3c.dom.Document as result. This document
 *  may be transformed (XSLT) before returning. It is your
 *  responsibility to implement creating the report from
 *  this document.
 *
 *  Reads the sequence decription in XML. This reader gets more information about the sequence, but downloads much larger files.
 *  Internet robot, xmlReaderNCBI, that uses form, located in page
 *  http://www.ncbi.nlm.nih.gov/entrez/.
 *  It is your responsibility to find and cite appropriate references
 *  from this page.
 *  This agent returns org.w3c.dom.Document as result.
 *  Generated 2002.7.27  by Sight 2.0, Ulm university.
 */
package impl.sequence_readers;

import Sight.Agents.*;
import Sight.Agents.Redirectors.*;
import Sight.Structures.*;
import Sight.dds.*;
import java.io.*;
import org.w3c.dom.*;

public class xmlReaderNCBI extends Sight.Agents.Prototypes.xml_based_agent implements Serializable {
 /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
   /** default=NM_000068 */
   public String Id="NM_000068"; // Parameter
       /** Submit request, compute key by SHA digesting algorithm. */
       public Sight.Agents.Request submit() { return Sight.Agents.Request.submit(Public, this, getKey()); };
       /** Submit request with the known key. */
       public Sight.Agents.Request submit(String key)
        { return Sight.Agents.Request.submit(Public, this, key); };
       };

   /** Get the AMap data structure, required by post method. */
  protected AMap getFormParameters(Object req) {
    Request request = (Request) req;
    AMap map = new AMap();
    map.add("url:identifier", request.Id); // Parameter
    map.add("connect:master", "http://www.ncbi.nlm.nih.gov/entrez/"); // connect
    map.add("connect:action", "get"); // connect
    map.add("url:prefix", "http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?view=xml&txt=on&val="); // connect
    map.add("url:suffix", ""); // connect
    return map; };
  /** Get default request for this robot. */
  public static Request getDefaultRequest() {
     return Public.getRequest(); }
  private Request getRequest() { return new Request(); }; // must be non static

    /** Type definitions for Request. */
   public dStructure getRequestDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("Id","CharSequence",null,"NM_000068")                 }
    );
   }
 /* Record type, used in results for this robot. */
 /* The result of this robot is an array of records. */
  public static class Record extends Sight.Record implements Serializable {
        public CharSequence Bioseq_set_class;
        public CharSequence Bioseq_set_class_value;
        public CharSequence Org_ref_taxname;
        public CharSequence RNA_sequence;
        public CharSequence Protein_sequence;
      };
      /** The result of this robot. */
      public static class Result extends Sight.Result implements Serializable {
         public Record[] a; // array of results.
         /** Create report for one record. */
         public CharSequence getReportForRecord(Object ro, int level)
          { Record r = (Record) ro;
           switch (level) {
              case 0:  return ""+r.Bioseq_set_class+" "+r.Bioseq_set_class_value+" "+r.Org_ref_taxname;
              case 1:  return ""+r.Bioseq_set_class+" "+r.Bioseq_set_class_value+" "+r.Org_ref_taxname+" RNA:"+r.RNA_sequence.length()+"bp Protein:"+r.Protein_sequence.length()+" aa";
              default: return ""+" : "+r.Bioseq_set_class+" "+" : "+r.Bioseq_set_class_value+" "+" : "+r.Org_ref_taxname+" "+" : <textarea>"+r.RNA_sequence+"</textarea> "+" : <textarea>"+r.Protein_sequence+"</textarea> ";
            }
          };
      }

    /** Type definitions for Result. */
   public dStructure getResultDds() {
    return new Sight.dds.Records(true,
     new dField[] {
        new dField("Bioseq_set_class","CharSequence",null,""),
        new dField("Bioseq_set_class_value","CharSequence",null,""),
        new dField("Org_ref_taxname","CharSequence",null,""),
        new dField("RNA_sequence","CharSequence",null,""),
        new dField("Protein_sequence","CharSequence",null,"")                 }
    );
   }

/* Required structure: parent nodes (name ignored), inside them -
   child nodes (can be with parameters), representing fields.
   */
 public Sight.Record createRecord(Node xml)
 {
  Record r = new Record();
  r. Bioseq_set_class = getXMLField("Bioseq-set_class", xml);
  r. Bioseq_set_class_value = getXMLField("Bioseq-set_class&value", xml);
  r. Org_ref_taxname = getXMLField("Org-ref_taxname", xml);
  r. RNA_sequence = getXMLField("IUPACna", xml);
  r. Protein_sequence = getXMLField("NCBIeaa", xml);
  return r;
 };

  /** main(..) method provided to check robot quickly, launching it
   * as standalone program. */
  public static void main(String[] args) {
       try {
         Request request = getDefaultRequest();
         // (now you can modify 'request' fields if required)
         Sight.Agents.Request submission = request.submit();
         // processing now started in separate thread:
         Result response = (Result) submission.getResult();

         try {
         // to format report, we need an agent itself, because
         // Document cannot be easily extended by extra methods.
         String html = xmlReaderNCBI .getAvailableAgent().getReport(response, 2);
         System.out.println("Report: "+html);
         }
         catch (java.lang.UnsupportedOperationException ui)
          { System.out.println(ui.getMessage()); };
       } catch (Exception exc)
          { if (exc!=null) System.out.println(exc.getMessage());
            exc.printStackTrace();
          };
  }

  public xmlReaderNCBI() {
   setDescription("Reads the sequence decription in XML. This reader gets more information about the sequence, but downloads much larger files.");
   setFormURL("http://www.ncbi.nlm.nih.gov/entrez/");
   setMasterURL("http://www.ncbi.nlm.nih.gov/entrez/");
   // define pre scan section.
   preScan = new preScan();
   cn_maxConnectionLength = 80000000; // abort after 8000 Mb by default.
   }

  /** Public instance of agent for user. */
  public static xmlReaderNCBI Public = new xmlReaderNCBI();
  public static Agent getAvailableAgent() { return Public; };
  public Class getRecordClass() { return Record.class; };
   /** Get result from the given XML document */
  public Sight.Result resultFromDocument(Document node) throws Exception {
     Result r = new Result();
     r.a = (Record[]) createResultArray(node);
     return r;
   };

public String getXSLT() {
      return getXMLFromSelf("Sight:xml-xsl-insert-1");
   };
// This insert will be loaded runtime:
/* <Sight:xml-xsl-insert-1>
<?xml version="1.0"?>
<!-- See http://www.w3.org/TR/1999/REC-xslt-19991116 and http://www.w3.org/TR/1999/REC-xpath-19991116
 for transform language description. -->
<!-- $comment -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes"/>
   <!-- This will add required starting and closing tags. -->
   <xsl:template match="/">
    <Sight>
      <xsl:apply-templates/>
    </Sight>
   </xsl:template>
   <xsl:template match="text()|@*"></xsl:template>
   <!-- // mathes the path anywhere in the document: -->
   <xsl:template match="//Seq-entry/Seq-entry_set/Bioseq-set">  <!-- copy content: -->
     <Sight-record>
      <xsl:copy-of select="Bioseq-set_class"/>
      <xsl:copy-of select="Bioseq-set_descr/Seq-descr/Seqdesc/Seqdesc_source/BioSource/BioSource_org/Org-ref/Org-ref_taxname"/>
      <xsl:copy-of select="Bioseq-set_seq-set/Seq-entry/Seq-entry_seq/Bioseq/Bioseq_inst/Seq-inst/Seq-inst_seq-data/Seq-data/Seq-data_iupacna/IUPACna"/>
      <xsl:copy-of select="Bioseq-set_seq-set/Seq-entry/Seq-entry_seq/Bioseq/Bioseq_inst/Seq-inst/Seq-inst_seq-data/Seq-data/Seq-data_ncbieaa/NCBIeaa"/>
     </Sight-record>
   </xsl:template>
</xsl:stylesheet>
</Sight:xml-xsl-insert-1> */
}