/* * *Research Project: Graphical Database for Category Theory * J. Bradbury, Dr. R. Rosebrugh, I. Rutherford, * Matt Graves, Jesse Tweedle * *File: OpenCategory.java * *Mount Allison University 2003 * *Data: May 28, 2003 * *Description: This file will be used for opening a category. It will * open a JFileChooser and allow the user to select a file. * */ import javax.swing.*; import java.io.*; import java.net.*; public class OpenCategory extends JInternalFrame { private Category cat; private String fname;//name of file to download private boolean badfile; private BufferedReader input;//used for reading the file... private IniSettings ini; private URL fileURL; public OpenCategory(String location, IniSettings ini2, boolean local) { super(); fname = location; ini = ini2; cat = new Category(); if(local==true) {//local machine file try { input = new BufferedReader(new InputStreamReader(new FileInputStream(fname))); //System.out.println("opening local file->"+fname); if(fname.toLowerCase().endsWith("cgl")) { readFuncCATFile(cat,true); cat.file_ = new File(fname); addRecentCat(cat.file_.getPath()); } else if(fname.toLowerCase().endsWith("cat")) { readFuncCATFile(cat,false); cat.file_ = new File(fname); addRecentCat(cat.file_.getPath()); } else { //invalid file System.out.println("Invalid file type"); System.exit(1); } } catch(IOException ioe) { //System.out.println("could not open file or close file.. mabye.."); } } else {//online file try { fileURL = new URL(fname); } catch(java.net.MalformedURLException exc){} try { InputStream i = fileURL.openStream(); input = new BufferedReader(new InputStreamReader(i)); } catch (Exception e) { //new MessageDialog(this, "Error", "Unable to load file " + fname, true); System.out.println("Error: Unable to load file ->"+fname); badfile = true; } if(fname.toLowerCase().endsWith("cat")) { readFuncCATFile(cat,false); cat.file_ = null; //readCGLFile(cat); addRecentCat(fname); } else if(fname.toLowerCase().endsWith("cgl")) { readFuncCATFile(cat,true); cat.file_ = null; addRecentCat(fname); } } } public Category getCategory() { return cat; } public void addRecentCat(String fname) //This method adds a recently opened, loaded, or saved file //to the list of recent category files { int i, j; for (i=1; i <= 8; i++) { if (ini.getRecent(i).equals(fname)) { for (j=i; j < 8; j++) { ini.setRecent(j, ini.getRecent(j+1)); } ini.setRecent(8, ""); } } for (i=7; i >= 1; i--) { ini.setRecent(i+1, ini.getRecent(i)); } ini.setRecent(1, fname); } //end of parse relations //start of other parser //read funcCATfile private void readFuncCATFile(Category c, boolean graphical) //Method reads a category file (*.CAT) from a drive on the local computer { boolean morelines = true; boolean relend = false; String gml = new String(); boolean gmlend = false; try { try { while (gmlend == false) { try { String currLine = input.readLine(); //System.out.println(currLine); if (currLine.length() == 0) { //edit.functorObjectArea.append("EMPTY LINE\n"); } else if (currLine.charAt(0) == '#') { String com = currLine.substring(1, currLine.length()); c.comment = c.comment + com; //edit.functorObjectArea.append(currLine); } else { if(currLine.equals("category")) { //System.out.println("test"); currLine = input.readLine(); //have the name... put it into the category c.name = currLine; //System.out.println(currLine); for (int j=0; j < currLine.length(); j++) { if ((currLine.charAt(j) == ' ') || (currLine.charAt(j) == '\t')) { if (j == 0) currLine = currLine.substring(1, currLine.length()); else currLine = currLine.substring(0, j) + currLine.substring(j+1, currLine.length()); } } } else if (currLine.equals("objects")) { String objects = new String(); //System.out.println("Should see objects"); while (!currLine.equals("arrows")) { currLine = input.readLine(); //System.out.println("about to have a problem!! ->"+currLine); if (currLine.length()>0 && currLine.charAt(0) == '#') { String com = currLine.substring(1, currLine.length()); c.comment = c.comment + com; } else if (!currLine.equals("arrows")) objects = objects.concat(currLine); } //edit.functorObjectArea.append("OBJECTS = " + objects + "\n"); c.parseObjects(objects); if (badfile) return; } if (currLine.equals("arrows")) { String arrows = new String(); while (!currLine.equals("relations")) { currLine = input.readLine(); if (currLine.length()>0 && currLine.charAt(0) == '#') { String com = currLine.substring(1, currLine.length()); c.comment = c.comment + com; } else if (!currLine.equals("relations")) arrows = arrows.concat(currLine); } c.parseArrows(arrows); if (badfile) return; //edit.functorObjectArea.append("ARROWS = " + arrows); } if (currLine.equals("relations")) { String relations = new String(); while (relend == false) { currLine = input.readLine(); //System.out.println(currLine); //edit.objectArea.appendText("HERE"); for (int r = 0; r < currLine.length(); r++) { if (currLine.charAt(r) == '.') { relend = true; } } relations = relations.concat(currLine); } if (!graphical) gmlend = true; c.parseRelations(relations); if (badfile) return; } if (graphical == true) { if (currLine.equals("gml") && gmlend == false) { int left = 0; int right = 0; while (gmlend == false) { currLine = input.readLine(); if (currLine == null)// || currLine.length() == 0) gmlend = true; else { for (int r = 0; r < currLine.length(); r++) { if (currLine.charAt(r) == '[') left++; if (currLine.charAt(r) == ']') right++; if ((left == right) && (left != 0) && (right != 0)) gmlend = true; } gml = gml.concat(currLine + "\n"); } } c.gml = new String(gml); gmlend = true; } } } } catch(EOFException eof) { morelines = false; } } } catch (IOException e1) {} } catch (NullPointerException aie) {} }//end of cat parser }