//Daniel Chapman
//University of Cambridge
//Lensfield Road
//Cambridge
//CB2 1EW

//Program to convert UGRIC to FMP, ISIS and back (limited at the moment).

import java.io.*;
import java.awt.*;

public class converter2 extends Frame {

static String full;
static String edit = "codswollop";
static int sup, locat, stock, umol, alt, cname, cat = 0;
static FileDialog fd;
static TextDialog2 td;
static TextField AUTHOR;
static TextField KEYWORDS;
static TextField HNMR;
static TextField CNMR;
static TextField FNMR;
static TextField IR;
static TextField MS;
static TextField QUANTITY;
static TextField LOCATION;
static TextField DATE;
static TextField MOL2D;
static TextField MOL3D;
static TextField EXPERIMENTAL;
static String fname;
static String dirname;
static String author;
static String keywords;
static String hnmr;
static String cnmr;
static String fnmr;
static String ir;
static String ms;
static String quantity;
static String location;
static String date;
static String mol2d;
static String mol3d;
static String experimental;
static DataOutputStream aDOut;

converter2() {
setLayout(new GridLayout(3,2, 10, 30));
fd = new FileDialog(this, "Open");
td = new TextDialog2(this, "DONE!", true);
td.resize(50,50);
    MenuBar mb = new MenuBar();
    Menu m = new Menu("Convert");
    m.add(new MenuItem("RDF-UGRIC"));
    m.add(new MenuItem("FMP-UGRIC"));
    m.add(new MenuItem("UGRIC-FMP"));
    m.add(new MenuItem("UGRIC-RDF"));
    m.add(new MenuItem("FMP-RDF"));
    m.add(new MenuItem("RDF-FMP"));
    mb.add(m);
    setMenuBar(mb);
    Label label1 = new Label("AUTHOR", Label.LEFT);
    add(label1);
    AUTHOR = new TextField("", 10);
    add(AUTHOR);
    Label label2 = new Label("KEYWORDS", Label.LEFT);
    add(label2);
    KEYWORDS = new TextField("", 10);
    add(KEYWORDS);
    //HNMR = new TextField("", 10);
    //add(HNMR);
    //CNMR = new TextField("", 10);
    //add(CNMR);
    //FNMR = new TextField("", 10);
    //add(FNMR);
    //IR = new TextField("", 10);
    //add(IR);
    //MS = new TextField("", 10);
    //add(MS);
    //QUANTITY = new TextField("", 10);
    //add(QUANTITY);
    //LOCATION = new TextField("", 10);
    //add(LOCATION);
    //DATE = new TextField("", 10);
    //add(DATE);
    Label label3 = new Label("MOL2D", Label.LEFT);
    add(label3);
    MOL2D = new TextField("", 10);
    add(MOL2D);
    //EXPERIMENTAL = new TextField("", 10);
    //add(EXPERIMENTAL);



}
            
  public static void main(String argv[]) {
    converter2 app = new converter2();
    app.resize(300,300);
    app.show();

}
public boolean action (Event evt, Object o) {
 String label = (String)o;
    if (evt.target instanceof MenuItem) {


       if (label.equals("RDF-FMP")) {
          fd.setFile("*.rdf");
          fd.show();
          fname = fd.getFile();
          dirname = fd.getDirectory();
          convertrdffmp();
          
       }
      if (label.equals("RDF-UGRIC")) {
         author  = AUTHOR.getText();
         keywords = KEYWORDS.getText();
         mol2d = MOL2D.getText();
          fd.setFile("*.rdf");
          fd.show();
          fname = fd.getFile();
          dirname = fd.getDirectory();
         convertrdfugric();
        }
      if (label.equals("UGRIC-RDF")) {
          fd.setFile("*.ugr");
          fd.show();
          fname = fd.getFile();
          dirname = fd.getDirectory();
         convertugricrdf();
        }

   }
return true; 
}
public void paint (Graphics g) {
       g.drawString("Choose file transfer", 10, 10);

}

//Convert rdf-fmp (paterson ISIS)

void convertrdffmp(){
try {
      DataInputStream aDI = new DataInputStream(new FileInputStream(dirname + fname));
      DataOutputStream aDOut = new DataOutputStream(new FileOutputStream("fname.fmp"));

      while (true) {

          full = aDI.readLine();

          //System.out.println(full);


          if (full.startsWith("$REREG")) {
              aDOut.writeByte ('\n');
sup = 0; locat = 0; stock = 0; umol = 0; alt = 0; cname = 0; cat = 0;
      
              }
          if (full.startsWith("$DTYPE ROOT:SUPPLIER")) {
 edit = aDI.readLine();
              aDOut.writeBytes(edit.substring(7, edit.length()) + '\t');
//System.out.println(edit + '\n');
              sup = 1;
              } 

if (full.startsWith("$DTYPE ROOT:LOCATION")) {
              String edit = aDI.readLine();
if (sup==0) {
                  aDOut.writeByte('\t');
                  }

              aDOut.writeBytes(edit.substring(7, edit.length()) + '\t');
//System.out.println(edit + '\n');
sup = 1; locat = 1; 
              }

if (full.startsWith("$DTYPE ROOT:STOCK")) {
 edit = aDI.readLine();
if (sup==0) {
                  aDOut.writeByte('\t');
                  }
if (locat==0) {
                  aDOut.writeByte('\t');
                  }

              aDOut.writeBytes(edit.substring(7, edit.length()) + '\t');
//System.out.println(edit + '\n');
sup = 1; locat = 1; stock = 1; 
              }

if (full.startsWith("$DTYPE ROOT:UMOLFORM")) {
 edit = aDI.readLine();
if (sup==0) {
                  aDOut.writeByte('\t');
                  }
if (locat==0) {
                  aDOut.writeByte('\t');
                  }
if (stock==0) {
                  aDOut.writeByte('\t');
                  }

              aDOut.writeBytes(edit.substring(7, edit.length()) + '\t');
//System.out.println(edit + '\n');
sup = 1; locat = 1; stock = 1; umol = 1; 
              }

if (full.startsWith("$DTYPE ROOT:ALTNAME")) {
 edit = aDI.readLine();
if (sup==0) {
                  aDOut.writeByte('\t');
                  }
if (locat==0) {
                  aDOut.writeByte('\t');
                  }
if (stock==0) {
                  aDOut.writeByte('\t');
                  }
if (umol==0) {
                  aDOut.writeByte('\t');
                  }

              aDOut.writeBytes(edit.substring(7, edit.length()) + '\t');
//System.out.println(edit + '\n');

 sup = 1; locat = 1; stock = 1; umol = 1; alt = 1; 
              }

if (full.startsWith("$DTYPE ROOT:CNAME")) {
 edit = aDI.readLine();
if (sup==0) {
                  aDOut.writeByte('\t');
                  }
if (locat==0) {
                  aDOut.writeByte('\t');
                  }
if (stock==0) {
                  aDOut.writeByte('\t');
                  }
if (umol==0) {
                  aDOut.writeByte('\t');
                  }
if (alt==0) {
                  aDOut.writeByte('\t');
                  }


              aDOut.writeBytes(edit.substring(7, edit.length()) + '\t');
//System.out.println(edit + '\n');
sup = 1; locat = 1; stock = 1; umol = 1; alt = 1; cname = 1; 
              }



if (full.startsWith("$DTYPE ROOT:CATNUMBER")) {
 edit = aDI.readLine();
if (sup==0) {
                  aDOut.writeByte('\t');
                  }
if (locat==0) {
                  aDOut.writeByte('\t');
                  }
if (stock==0) {
                  aDOut.writeByte('\t');
                  }
if (umol==0) {
                  aDOut.writeByte('\t');
                  }
if (alt==0) {
                  aDOut.writeByte('\t');
                  }

if (cname==0) {
                  aDOut.writeByte('\t');
                  }
aDOut.writeBytes(edit.substring(7, edit.length()) + '\n');
//System.out.println(edit + '\n');
sup = 1; locat = 1; stock = 1; umol = 1; alt = 1; cname = 1; cat = 1;
              }


    
}
   }catch (EOFException e) {System.out.println(e);}
catch (Exception e){}
popup();
}

//Convert RDF - UGRIC (works but puts tags in the order in the rdf file
// i.e does not neccessarily comply with XML DTD.

void convertrdfugric() {
try {
      DataInputStream aDI = new DataInputStream(new FileInputStream(dirname + fname));
      aDOut = new DataOutputStream(new FileOutputStream(fname.substring(0, (fname.length()-4)) + ".ugr"));
int entry = 0;
aDOut.writeBytes("<UGRIC>" + '\n');
         full = aDI.readLine();
      while (true) {

          if (full.equals("$DTYPE ROOT:" + author)) {
             edit = aDI.readLine();
             aDOut.writeBytes("<AUTHOR>" +edit.substring(7, (edit.length()))+ '\n');

             }


          if (full.equals("$DTYPE ROOT:" + keywords)) {
             edit = aDI.readLine();
             aDOut.writeBytes("<KEYWORDS>" + edit.substring(7, (edit.length()))+ '\n');
             }

          if (full.equals("$DTYPE ROOT:" + mol2d)) {
             aDOut.writeBytes("<MOL2D filetype=\"MDLmol\">"  + '\n');
             while(true) {
                  edit = aDI.readLine();
                  if (edit.startsWith("$DTYPE")) {
                     full = edit;
                     break;
                     }
                  aDOut.writeBytes(edit + '\n');

                  }
             aDOut.writeBytes("</MOL2D>" + '\n');
             }

          else {
          if ((full.startsWith("$REREG"))|(full.startsWith("$RIREG"))) {
             if (entry > 0) {
                aDOut.writeBytes("</ENTRY>" + '\n');
                }
             aDOut.writeBytes("<ENTRY>" + entry + '\n');

             entry++;
         }
         



          full = aDI.readLine();
          }

          System.out.println(full);

}

   }catch (EOFException e) {
          System.out.println(e);            
          }

// I don't realy know why this works - leave it!
catch (Exception e){System.out.println(e);
              try {
              aDOut.writeBytes("</ENTRY>\n</UGRIC>");
               }catch(Exception z){}
popup();
}
}

// Convert UGRIC - RDF

void convertugricrdf() {
try {
      DataInputStream aDI = new DataInputStream(new FileInputStream(dirname + fname));
      aDOut = new DataOutputStream(new FileOutputStream(fname.substring(0, (fname.length()-4)) + ".rdf"));
int entry = 1;
aDOut.writeBytes("$RDFILE 1\n$DATM 11/28/1997 12:6:28\n");
         full = aDI.readLine();
      while (true) {


          if (full.startsWith("<MOL2D")) {
             aDOut.writeBytes("$DTYPE ROOT:MOL2D\n$DATUM $MFMT\n");
                  aDI.readLine();
                  aDI.readLine();
                  aDI.readLine();
             while(true) {
                  edit = aDI.readLine();
                  if (edit.startsWith("</MOL2D")) {
                     full = edit;
                     break;
                     }
                  aDOut.writeBytes(edit + '\n');

                  }
             }

          else {
          if (full.startsWith("<ENTRY>")) {
             aDOut.writeBytes("$RIREG aaaaaaa\n");

         }
         
          if (full.startsWith("<AUTHOR>")) {
             aDOut.writeBytes("$DTYPE ROOT:AUTHOR\n$DATUM " +full.substring(8, (full.length()-9))+ '\n');
             }


          if (full.startsWith("<KEYWORDS>")) {
             aDOut.writeBytes("$DTYPE ROOT:KEYWORDS\n$DATUM " + full.substring(10, (full.length()-11))+ '\n');
             }


          full = aDI.readLine();
          }

          System.out.println(full);

}

   }catch (EOFException e) {
          System.out.println(e);            
          }

// Catch all exceptions - messy I know!

catch (Exception e){System.out.println(e);

popup();
}
}
void popup() {
   td.show();
} 
}
