/* * *Research Project: Graphical Database for Category Theory * J. Bradbury, Dr. R. Rosebrugh, I. Rutherford, * Matt Graves, Jesse Tweedle * *File: OpenFunctor.java * *Mount Allison University 2003 * *Data: May 28, 2003 * *Description: This file will be used for opening a functor. It will * open a JFileChooser and allow the user to select a file. This class * should have a method of returning a functorNode. This file will be * used by FunctorInternalFrame * */ import javax.swing.*; import java.io.*; import java.net.*; public class OpenFunctor extends JInternalFrame { private FunctorNode fnode; private String fname;//name of file to download private boolean badfile; BufferedReader input;//used for reading the file... IniSettings ini; public OpenFunctor(String location, IniSettings ini2, boolean local) { super(); fname = location; ini = ini2; fnode = new FunctorNode(); if(local == true) { try { input = new BufferedReader(new InputStreamReader(new FileInputStream(fname))); if(fname.toLowerCase().endsWith("fun")) { //read the functor fnode.file_ = new File(fname); fnode.f.name = fname; readFUNFile(fnode.f, fnode.A, fnode.B); //create the diagram fnode.updateDiagramCategory(); addRecentFun(fname); } else if(fname.toLowerCase().endsWith("fgl")) { //System.out.println("fgl file"); fnode.file_ = new File(fname); fnode.f.name = fname; readFGLFile(fnode.f, fnode.A, fnode.B); fnode.updateDiagramCategory(); addRecentFun(fname); } else { System.out.println("invalid file type"); } input.close(); } catch (IOException ioe) {} } else {//not a local file URL fileURL; try { fileURL = new URL(fname); fnode.file_ = null; try { InputStream i = fileURL.openStream(); input = new BufferedReader(new InputStreamReader(i)); } catch (Exception e) { System.out.println("Error: Unable to load file ->"+fname); badfile = true; } //do file parsing if(fname.toLowerCase().endsWith("fun")) { //read the functor readFUNFile(fnode.f, fnode.A, fnode.B); fnode.f.name = fname; //create the diagram fnode.updateDiagramCategory(); addRecentFun(fname); } else if(fname.toLowerCase().endsWith("fgl")) { readFGLFile(fnode.f, fnode.A, fnode.B); fnode.f.name = fname; fnode.updateDiagramCategory(); addRecentFun(fname); } else { System.out.println("invalid file type"); } } catch (MalformedURLException except) { //new MessageDialog(this, "Error", "Unable to open file File unable to Load", true); System.out.println("Error opening foreign file!!!"); badfile = true; } } } public void addRecentFun(String fname) //This method adds a recently opened, loaded, or saved file //to the list of recent 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); } public FunctorNode getFunctor() { return fnode; } //read fun file public boolean readFUNFile(CatFunctor func, Category A, Category B) { if (!fname.toLowerCase().endsWith("fun")) { //new MessageDialog(this, "Error", "Invalid file - please choose a *.FUN file", true); System.out.println("Error, invalid file, please choose *.FUN file!!"); return false; } readFuncCATFile(A, false); if (badfile) return false; readFuncCATFile(B, false); if (badfile) return false; readFunctorFile(func, A, B); if (badfile) return false; try { input.close(); } catch (IOException e1) {} return true; } //end of read fun file //start of read fgl file public boolean readFGLFile(CatFunctor func, Category A, Category B) { readFuncCATFile(A, true); if (badfile) return false; readFuncCATFile(B, true); if (badfile) return false; readFunctorFile(func, A, B); if (badfile) return false; try { input.close(); } catch (IOException e1) {} return true; } //end of read fgl file //read funcCATfile public 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 + "329"); 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")) { currLine = input.readLine(); // System.out.println(currLine+"346"); 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()); } } boolean name = false; } else if (currLine.equals("objects")) { String objects = new String(); while (!currLine.equals("arrows")) { currLine = input.readLine(); // System.out.println(currLine + "365"); 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); } c.parseObjects(objects); if (badfile) return; } if (currLine.equals("arrows")) { String arrows = new String(); while (!currLine.equals("relations")) { currLine = input.readLine(); //System.out.println(currLine + "383"); if (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; } if (currLine.equals("relations")) { String relations = new String(); while (relend == false) { currLine = input.readLine(); // System.out.println(currLine); 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(); // System.out.println(currLine+"426"); 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); } } c.gml = new String(gml); gmlend = true; } } } } catch(EOFException eof) {morelines = false;} } } catch (IOException e1) {} } catch (NullPointerException npe) {} } //end of funCATfile //start of readfunctorfile //this function reads the functor part of a fgl or fun file public void readFunctorFile(CatFunctor f, Category A, Category B) //This method reads and parses the functor part of a *.FUN file { boolean arrowend = false; String comment = new String(); try { try { //Repeat until end of arrows is found while (arrowend == false) { try { //Read Current Line String currLine = input.readLine(); // System.out.println(currLine + "477"); //If current line is blank do nothing if (currLine.length() == 0) { // edit.functorObjectArea.append("EMPTY LINE\n"); } //If current line has # it is a comment else if (currLine.charAt(0) == '#') { String com = currLine.substring(1, currLine.length()); comment = comment + com; //edit.functorObjectArea.append(currLine); } //Otherwise current line is part of the functor else { if(currLine.equals("functor")) { currLine = input.readLine(); // System.out.println(currLine + "496"); for (int j=0; j < currLine.length(); j++) { if ((currLine.charAt(j) == ' ') || (currLine.charAt(j) == '\t')) { if (j == 0) currLine = currLine.substring(j+1, currLine.length()); else currLine = currLine.substring(0, j) + currLine.substring(j+1, currLine.length()); } } } else if (currLine.equals("objects")) { String objects = new String(); while (!currLine.equals("arrows")) { currLine = input.readLine(); // System.out.println(currLine + "514"); //If current line has # it is a comment if (currLine.charAt(0) == '#') { String com = currLine.substring(1, currLine.length()); comment = comment + com; } else if (!currLine.equals("arrows")) objects = objects.concat(currLine); } //Parse object string into individual objects parseFunctorObjects(objects, f, A, B); if (badfile) return; } if (currLine.equals("arrows")) { String arrows = new String(); while (arrowend == false) { currLine = input.readLine(); // System.out.println(currLine + "534"); //If current line has # it is a comment if (currLine.charAt(0) == '#') { String com = currLine.substring(1, currLine.length()); comment = comment + com; } for (int r = 0; r < currLine.length(); r++) { if (currLine.charAt(r) == '.') { arrowend = true; } } arrows = arrows.concat(currLine); } parseFunctorArrows(arrows, f, A, B); if (badfile) return; } } } catch(EOFException eof) {System.out.println("eof exception"); } } } catch (IOException e1) {System.out.println("ioe exception"); } } catch (NullPointerException npe) {System.out.println("npe exception"); npe.printStackTrace();} } //end of readfunctorfile //start of parse functorobjects void parseFunctorObjects(String objs, CatFunctor func, Category A, Category B) //This method parses a string of functor objects { int i =0; //Positions of "(", ")", "=", and "," for parsing purposes int prevcomma = -1; int bar = 0; int arrow = 0; int comma = 0; //Strings to store current object from and object to String objectfrom = new String(); String objectto = new String(); //Read until end of string is reached //and remove all tabs and spaces for (i=0; i < objs.length(); i++) { if ((objs.charAt(i) == ' ') || (objs.charAt(i) == '\t')) { objs = objs.substring(0, i) + objs.substring(i+1, objs.length()); } } if (objs.charAt(0) != '.') { //Parse string into objects for (i=0; i < objs.length(); i++) { if (objs.charAt(i) == '|') bar = i; if (objs.charAt(i) == '>') arrow = i; if ((objs.charAt(i) == ',') || (objs.charAt(i) == '.')) { comma = i; if (bar < prevcomma || comma < arrow) { //new MessageDialog(this, "Error", fname + " is not a valid functor file", true); System.out.println("Error: " + fname + " is not a valid functor file"); badfile = true; return; } objectfrom = objs.substring(prevcomma+1, bar); objectto = objs.substring(arrow+1, comma); //check to see that objectfrom and objectto are valid in categories A and B if(!A.check_obj(objectfrom)) { //new MessageDialog(this, "Error", objectfrom + " is not an object of Category " + A.name, true); System.out.println("Error: " + objectfrom + " is not an object of Category " + A.name); } else if (!B.check_obj(objectto)) { //new MessageDialog(this, "Error", objectto + " is not an object of Category " + B.name, true); System.out.println("Error: " + objectto + " is not an object of Category " + B.name); } else { func.obj[A.object_number(objectfrom)] = B.object_number(objectto); } prevcomma = comma; } } } } //end of parse functor objects //start of parse functor arrows void parseFunctorArrows(String arrs, CatFunctor func, Category A, Category B) //This method parses a string of functor objects { int i =0; int arrnum = 0; int pathfrom[] = new int[ini.getMAXWORD()]; int pathto[] = new int[ini.getMAXWORD()]; int reduced_path[] = new int[ini.getMAXWORD()]; //Positions of "(", ")", "=", and "," for parsing purposes int prevcomma = -1; int closebracket = 0; int equals = 0; int comma = 0; //Strings to store current object from and object to String objectfrom = new String(); String objectto = new String(); //Read until end of string is reached //and remove all tabs and spaces for (i=0; i < arrs.length(); i++) { if ((arrs.charAt(i) == ' ') || (arrs.charAt(i) == '\t')) { arrs = arrs.substring(0, i) + arrs.substring(i+1, arrs.length()); } } if (arrs.charAt(0) != '.') { //Parse string into objects for (i=0; i < arrs.length(); i++) { if (arrs.charAt(i) == '|') closebracket = i; if (arrs.charAt(i) == '>') equals = i; if ((arrs.charAt(i) == ',') || (arrs.charAt(i) == '.')) { comma = i; objectfrom = arrs.substring(prevcomma+1, closebracket); objectto = arrs.substring(equals+1, comma); //System.out.println("Fron: " + objectfrom + " TO: " + objectto); //edit.functorObjectArea.append("FROM = " + objectfrom + " TO = " + objectto + "\n"); //checks to see that the string the user enters //contains only arrows that exist in category A if ((objectfrom.equals("1")) || (objectto.equals("1"))) /// i think maybe thte "Objectfrom" part may not work { func.arr[arrnum] = B.copy_path(B.string_to_path(objectto)); arrnum++; // this doesn't do anything //path = copy_path(path,string_to_path(path_string, cat)); //func.arr[editsub] = copy_path(func.arr[editsub],path); } else { if(!(A.check_string(objectfrom))) { //new MessageDialog(this, "Error", "One or more of the arrows specified do not exist in Category" + A.name, true); System.out.println("Error: One or more of the arrows specified do not exist in Category" + A.name); } //checks to see that the string the user enters //contains only arrows that exist in category B else if(!(B.check_string(objectto))) { //new MessageDialog(this, "Error", "One or more of the arrows specified do not exist in Category" + B.name, true); System.out.println("Error: One or more of the arrows specified do not exist in Category" + B.name); } else { pathto = B.copy_path(B.string_to_path(objectto)); //checks to see that domains and codomains of each arrow in path match up if(!(B.check_path(pathto))) { System.out.println("Error: Domains and codomains of each arrow in path do not match"); //new MessageDialog(this, "Error", "Domains and codomains of each arrow in path do not match", true); } else { //Checks to see that domain and codomain of path //match up with domain and codomain of arrow under the functor if((!(func.check_domain(pathto, arrnum, A, B))) || (!(func.check_codomain(pathto, arrnum, A, B)))) { //viewbox.append("if((!(check_domain(path,editsub,func,A,B))) || (!(check_codomain(path,editsub,func,A,B))))"); } else { reduced_path = B.reduce(pathto); //reduced_path = pathto; func.arr[arrnum] = B.copy_path(reduced_path); arrnum++; } } } } prevcomma = comma; } } } } //end of parse functor arrows }