/*--------------------------------------------------------------------------- File: Interpreter.java Package: JAWAA Beta Version 0.1 Author: Will Pierson (wcp@acpub.duke.edu) Description of Contents: Reads and executes animation script --------------------------------------------------------------------------*/ /* * Susan H. Rodger, Will Pierson * Computer Science Department * Duke University * December 1996 * * Copyright (c) 1996 * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the author. The name of the author may not be used to * endorse or promote products derived from this software without * specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ import java.awt.Color; import java.net.*; import java.io.*; public class Interpreter{ StreamTokenizer input; URL animURL; Animation anim; String animName; int line; boolean inStep = false; boolean error = false; public Interpreter(Animation anim, String str, String name){ this.anim = anim; try{ // This statement used if applets served from host other // than cs.duke.edu. // animURL = new URL(str+"?"+Math.random()); animURL = new URL(str); // This statement usd if applets served from cs.duke.edu // animURL = new URL("http://www.cs.duke.edu/cgi-bin/wcp/getAnim.cgi?URL="+str); System.out.println("getting animation" + str); //grab the animation instructions from web server input = new StreamTokenizer(animURL.openStream()); input.wordChars(65, 122); input.eolIsSignificant(true); input.slashSlashComments(true); input.slashStarComments(true); input.commentChar('#'); } catch(MalformedURLException me){ System.out.println("URL malformed: " + me); } catch(IOException ioe){ System.out.println("IO Error: " + ioe); } animName = name; } //finds RGB for Color represented by string public static int findRGB(String str){ int i = 0; /* if (str == null){ System.out.println("ERROR: got null color string"); return(-1); }*/ if (str.equals("lightGray")) i = -4144960; else if (str.equals("black")) i = -16777216; else if (str.equals("green")) i = -16711936; else if (str.equals("blue")) i = -16776961; else if (str.equals("orange")) i = -14336; else if (str.equals("red")) i = -65536; else if (str.equals("yellow")) i = -256; else if (str.equals("cyan")) i = -16711681; else if (str.equals("darkGray")) i = -12566464; else if (str.equals("gray")) i = -8355712; else if (str.equals("magenta")) i = -65281; else if (str.equals("pink")) i = -20561; else if (str.equals("white")) i = -1; else if (str.equals("transparent")) i = -5544; //indicates transparent else{ System.out.println("Error: Color "+str+" not recognized"); } return i; } //returns boolean value of string public boolean findBoolean(String str){ if (str.equals("true")) return true; else if (str.equals("false")) return false; else{ System.out.println(animName+"."+line+" - ERROR: "+str+" not a boolean"); } return false; } //reads next line of animation, calls appropriate function with //parameter values listed in program public boolean readLine(){ char c; int token, i1, i2, i3; String command, name, str1, str2, s1, s2, s3; Color col1, col2; col1 = new Color(0,0,0); col2 = new Color(0,0,0); String params[] = new String[200]; try{ line = input.lineno(); command = null; while(command == null){ input.nextToken(); // get next token command = input.sval; // first word always command if (input.ttype == StreamTokenizer.TT_EOF){ return false; } } System.out.print(command); int i = 0; input.nextToken(); // get next token while(input.ttype != StreamTokenizer.TT_EOL){ if(input.ttype == StreamTokenizer.TT_NUMBER){ token = (int)input.nval; params[i] = String.valueOf(token); } else params[i] = input.sval; if (input.ttype == StreamTokenizer.TT_EOF){ return false; } System.out.print(" "+params[i]); input.nextToken(); // get next token i++; } System.out.println(); /* Match up command with the right function to call */ if (command.equals("begin")){ if(inStep){ error = true; System.out.println(animName+"."+line+" - Missing \"end\""); return false; } inStep = true; while (readLine()); inStep = false; if(error) return false; return true; } if (command.equals("end")){ return false; } if (command.equals("node")){ col1 = new Color(findRGB(params[4])); col2 = new Color(findRGB(params[5])); anim.node(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), Integer.valueOf(params[3]).intValue(), col1, col2); } else if (command.equals("connectNodes")){ col1 = new Color(findRGB(params[3])); anim.connectNodes(params[0], params[1], params[2], col1, findBoolean(params[4])); } else if (command.equals("setCircleBkgd")){ col1 = new Color(findRGB(params[1])); anim.setCircleBkgd(params[0], col1); } else if (command.equals("marker")){ col1 = new Color(findRGB(params[3])); col2 = new Color(findRGB(params[4])); anim.marker(params[0], params[1], Integer.valueOf(params[2]).intValue(), col1, col2); } else if (command.equals("moveMarker")){ col1 = new Color(findRGB(params[4])); col2 = new Color(findRGB(params[5])); anim.moveMarker(params[0], params[1], params[2], params[3], col1, col2); } else if (command.equals("text")){ col1 = new Color(findRGB(params[4])); anim.text(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), params[3], col1); } else if (command.equals("delete")){ anim.delete(params[0]); } else if (command.equals("circle")){ col1 = new Color(findRGB(params[4])); col2 = new Color(findRGB(params[5])); anim.circle(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), Integer.valueOf(params[3]).intValue(), col1, col2); } else if (command.equals("line")){ col1 = new Color(findRGB(params[5])); anim.line(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), Integer.valueOf(params[3]).intValue(), Integer.valueOf(params[4]).intValue(), col1); } else if (command.equals("rectangle")){ col1 = new Color(findRGB(params[5])); col2 = new Color(findRGB(params[6])); anim.rectangle(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), Integer.valueOf(params[3]).intValue(), Integer.valueOf(params[4]).intValue(), col1, col2); } else if (command.equals("polygon")){ int Xcoords[] = new int[20]; int Ycoords[] = new int[20]; int points, k; points = k = Integer.valueOf(params[1]).intValue(); int j = 3; for(i = 0; k > 0; k--, i++, j += 2){ Xcoords[i] = Integer.valueOf(params[j-1]).intValue(); // System.out.println("got: "+Xcoords[i]); Ycoords[i] = Integer.valueOf(params[j]).intValue(); } col1 = new Color(findRGB(params[j-1])); col2 = new Color(findRGB(params[j])); anim.polygon(params[0], points, Xcoords, Ycoords, col1, col2); } else if (command.equals("stack")){ String values[] = new String[20]; int length = Integer.valueOf(params[3]).intValue(); for (i = length-1; i >=0; i--){ values[length-1-i] = params[i+4]; // System.out.println(values[length-1-i]); } col1 = new Color(findRGB(params[length+4])); col2 = new Color(findRGB(params[length+5])); anim.stack(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), length, values, col1, col2); } else if (command.equals("pop")){ anim.pop(params[0]); } else if (command.equals("push")){ anim.push(params[0], params[1]); } else if (command.equals("queue")){ String values[] = new String[20]; int length = Integer.valueOf(params[3]).intValue(); for (i = length-1; i >=0; i--){ values[length-1-i] = params[i+4]; // System.out.println(values[length-1-i]); } col1 = new Color(findRGB(params[length+4])); col2 = new Color(findRGB(params[length+5])); anim.queue(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), length, values, col1, col2); } else if (command.equals("dequeue")){ anim.dequeue(params[0]); } else if (command.equals("enqueue")){ anim.enqueue(params[0], params[1]); } else if (command.equals("changeParam")){ anim.changeParam(params[0], params[1], params[2]); } else if (command.equals("array")){ int length, k; String values[] = new String[20]; length = k = Integer.valueOf(params[3]).intValue(); for(i = 0; k > 0; k--, i++){ values[i] = params[i+4]; } col1 = new Color(findRGB(params[i+5])); col2 = new Color(findRGB(params[i+6])); anim.array(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), length, values, params[i+4], col1, col2); } else if (command.equals("tree")){ int start[] = new int[30]; int end[] = new int[30]; int k = 5; int j = 0; while(params[k] != null){ start[j] = Integer.valueOf(params[k]).intValue(); k += 2; end[j] = Integer.valueOf(params[k]).intValue(); k += 3; j++; } start[j] = -1; end[j] = -1; int width = Integer.valueOf(params[3]).intValue(); anim.tree(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), width, start, end); } else if (command.equals("addNode")){ if (params[4] == null){ params[4] = "-1"; } anim.addNode(params[0], Integer.valueOf(params[2]).intValue(), Integer.valueOf(params[4]).intValue()); } else if (command.equals("addEdge")){ anim.addEdge(params[0], Integer.valueOf(params[2]).intValue(), Integer.valueOf(params[4]).intValue()); } else if (command.equals("graph")){ int start[] = new int[30]; int end[] = new int[30]; int k = 6; int j = 0; while(params[k] != null){ start[j] = Integer.valueOf(params[k]).intValue(); k += 2; end[j] = Integer.valueOf(params[k]).intValue(); k += 3; j++; } start[j] = -1; end[j] = -1; int width = Integer.valueOf(params[4]).intValue(); anim.graph(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), params[3], width, start, end); } else if (command.equals("moveRelative")){ anim.moveRelative(params[0], Integer.valueOf(params[1]).intValue(), Integer.valueOf(params[2]).intValue(), findBoolean(params[3])); } else if (command.equals("groupObject")){ String objs[] = new String[10]; int length, k; length = k = Integer.valueOf(params[1]).intValue(); for(i = 0; k > 0; k--, i++){ objs[i] = params[i+2]; } anim.groupObjects(params[0], length, objs); } else{ System.out.println(animName+"."+line+" - ERROR: "+command+" not recognized"); return false; } } catch(IOException ioe){ System.out.println(animName+"."+line+" - IO ERROR"); return false; } catch(NumberFormatException e){ System.out.println(animName+"."+line+" - ERROR: could not convert string to integer"); return false; } catch(NullPointerException e){ System.out.println(animName+"."+line+" - ERROR: invalid parameter"); return false; } return true; } }