import java.io.File; import java.util.Scanner; import java.io.FileNotFoundException; /** * * @author nickgrifasi * @version 9/20/21 * Handles the logic behind parsing the commandFile. Reads * commands as strings, and enacts skipList methods as suited. * Prints to System.out. */ public class CommandFileScanner { private SkipList<String, Rectangle> skipList; private String file; /** * standard constructor for fileScan * * @param input String indicating the file to scan */ public CommandFileScanner(String input) { file = input; skipList = new SkipList<String, Rectangle>(); } /** * method that deals with all parsing logic. Includes steps * to take for commands * like insert, remove, intersection, etc, and prints to System.out * * @return boolean if scan was successful */ public boolean fileScan() { Scanner scanner = null; try { scanner = new Scanner(new File(file)); } catch (FileNotFoundException exception) { return false; } while (scanner.hasNext()) { String line = scanner.next(); if (line.equals("insert")) { String name = scanner.next(); int xCord = scanner.nextInt(); int yCord = scanner.nextInt(); int width = scanner.nextInt(); int height = scanner.nextInt(); // succesful insert if ((xCord + width < 1024) && (yCord + height < 1024) && (width > 0) && (height > 0) && (xCord >= 0) && (yCord >= 0) && nameChecker(line)) { Rectangle quad = new Rectangle(name, xCord, yCord, width, height); KVPair<String, Rectangle> kvpair = new KVPair<String, Rectangle>(name, quad); skipList.insert(kvpair); System.out.println("Rectangle inserted: (" + name + ", " + xCord + ", " + yCord + ", " + width + ", " + height + ")"); } // failed insert else { System.out.println("Rectangle rejected: (" + name + ", " + xCord + ", " + yCord + ", " + width + ", " + height + ")"); } continue; } if (line.equals("remove")) { String name = scanner.next(); if (Character.isAlphabetic(name.charAt(0))) { Rectangle quad = skipList.removeName(name); if (quad == null) { System.out.println("Rectangle not found: (" + name + ")"); } else { System.out.println("Rectangle removed: (" + name + ", " + quad.toString() + ")"); } } else { int xCord = Integer.parseInt(name); int yCord = scanner.nextInt(); int width = scanner.nextInt(); int height = scanner.nextInt(); if ((xCord + width < 1024) && (yCord + height < 1024) && (width > 0) && (height > 0) && (xCord >= 0) && (yCord >= 0)) { Rectangle temp = new Rectangle(null, xCord, yCord, width, height); Rectangle remove = skipList.removeData(temp); if (remove == null) { System.out.println( "Rectangle not found: (" + xCord + ", " + yCord + ", " + width + ", " + height + ")"); } else { System.out.println("Rectangle removed: (" + remove.getName() + ", " + remove.toString() + ")"); } } else { System.out.println( "Rectangle not found: (" + xCord + "," + yCord + "," + width + ", " + height + ")"); } } continue; } if (line.equals("search")) { String name = scanner.next(); SkipNode<String, Rectangle> node = skipList.search(name); if (skipList.search(name) == null) { System.out.println("Rectangle not found: " + name); } else { System.out.println("Rectangles found:"); System.out.println("(" + name + ", " + node.getPair().getNodeData().toString() + ")"); while (node.getForward()[0] != null && node .getForward()[0].key().compareTo(node.key()) == 0) { node = node.getForward()[0]; System.out.println("(" + name + ", " + node.getPair() .getNodeData().toString() + ")"); } } continue; } if (line.equals("dump")) { System.out.println("SkipList dump:"); skipList.dump(); continue; } if (line.equals("intersections")) { System.out.println("Intersection pairs:"); skipList.intersections(); continue; } if (line.equals("regionsearch")) { int xCord = scanner.nextInt(); int yCord = scanner.nextInt(); //added scanner.useDelimiter to fix negative //integers not being scanned scanner.useDelimiter(",|\\s+"); int width = scanner.nextInt(); scanner.useDelimiter(",|\\s+"); int height = scanner.nextInt(); Rectangle quad = new Rectangle("rectangle", xCord, yCord, width, height); if ((height > 0) || (width > 0)) { System.out.println("Rectangles intersecting region (" + xCord + ", " + yCord + ", " + width + ", " + height + "):"); skipList.regionsearch(quad); } else { System.out.println( "Rectangle rejected: (" + xCord + ", " + yCord + ", " + width + ", " + height + ")"); } } else { scanner.nextLine(); continue; } } return true; } /** * Internal method that checks if the scanned line * will be eligible for insertion * @param line input line to be checked * @return boolean, returns true if eligible */ private boolean nameChecker(String line) { char c = line.charAt(0); return (Character.isAlphabetic(c) && line.substring(1, line.length() - 1).matches("^\\w+$")); } }