import java.io.*;
import java.net.*;

public class BibTex
{
  public BibTex() {}

  // ----------------------------------------------------------------------

  public String pmid2bibtex(String PMID)
  {
    String bibtex = "";

    java.net.URL   u;
    InputStream    ins = null;
    BufferedReader dis;
    String         s;
    StringBuffer doc = new StringBuffer();

    try
    {
      u   = new java.net.URL("http://www.bioinformatics.org/texmed/cgi-bin/list.cgi?PMID=" + PMID);
      ins = u.openStream();  // --- throws an IOException
      dis = new BufferedReader(new InputStreamReader(ins));

      while ( (s = dis.readLine()) != null ) doc.append(s + "\n");

      int copyFrom = doc.indexOf("<PRE>");

      if (copyFrom >= 0)
      {
        copyFrom += 5;
        int copyTo   = doc.indexOf("</PRE>");

        bibtex = doc.substring(copyFrom, copyTo);
      }
    }
    catch (java.lang.Exception ex)
    {
      System.out.println("Warning: PMID " + PMID + " not exported to bibtex!");
      ex.printStackTrace();
      return "";
    }

    return bibtex;
  }

  // ----------------------------------------------------------------------

/*
  public String urlPubMed(String PMID)
  {
    String url_base = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=pubmed&dopt=Abstract&list_uids=";

    return url_base + PMID;
  }
*/

  // ----------------------------------------------------------------------

}
