ShapeGenerator { public static ShapeTable generate(Reader sourceReader) throws IOException { Objects.requireNonNull(sourceReader); ShapeTable st = new ShapeTable(); LineNumberReader reader = new LineNumberReader(sourceReader); String line = reader.readLine(); Consumer<String> shapeConsumer = null; while (line != null) { int comment = line.indexOf(';'); if (comment > -1) line = line.substring(0, comment); line = line.trim(); String[] parts = line.split("\\s+"); String command = parts[0]; String label = parts.length > 1 ? parts[1] : null; switch (command.toLowerCase()) { case ".short": VectorShape shortShape = new VectorShape(label); st.shapes.add(shortShape); shapeConsumer = shortShape::appendShortCommands; break; case ".long": VectorShape longShape = new VectorShape(label); st.shapes.add(longShape); shapeConsumer = longShape::appendLongCommands; break; case ".bitmap": BitmapShape bitmapShape = new BitmapShape(label); st.shapes.add(bitmapShape); shapeConsumer = bitmapShape::appendBitmapRow; break; case ".external": ExternalShapeImporter importer = new ExternalShapeImporter(st, label); shapeConsumer = importer::process; break; default: if (line.length() == 0) { } else if (shapeConsumer != null) { try { shapeConsumer.accept(line); } catch (Throwable t) { String message = String.format("Error at line #%d - %s", reader.getLineNumber(), t.getMessage()); throw new IOException(message, t); } } else { throw new IOException("Unexpected command: " + line); } break; } line = reader.readLine(); } return st; }  static ShapeTable generate(Reader sourceReader); static ShapeTable generate(InputStream inputStream); static ShapeTable generate(File file); static ShapeTable generate(Path path);  }
@Test public void generateBoxShortformTest() throws IOException { ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/box-shortform.st")); assertShapeIsBox(st); assertShapeBoxVectors(st, "label-short"); }
@Test public void generateBoxLongformTest() throws IOException { ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/box-longform.st")); assertShapeIsBox(st); assertShapeBoxVectors(st, "label-long"); }
@Test public void generateBoxBitmapTest() throws IOException { ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/box-bitmap.st")); assertShapeIsBox(st); assertEquals("label-bitmap", st.shapes.get(0).toBitmap().label); }
@Test public void testMouseShape() throws IOException { final String mouse = "+--------------+\n" + "|..........*X..|\n" + "|....XXXX.XX...|\n" + "|...XXXXXXXX...|\n" + "|.XXXXXXXXXXX..|\n" + "|XX.XXXXXXX.XX.|\n" + "|X...XXXXXXXXXX|\n" + "|XX............|\n" + "|.XXX.XX.......|\n" + "|...XXX........|\n" + "+--------------+\n"; ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/mouse-bitmap.st")); assertNotNull(st); assertEquals(1, st.shapes.size()); Shape shape = st.shapes.get(0); assertNotNull(shape); assertShapeMatches(mouse, shape); Shape vectorShape = shape.toVector(); assertNotNull(vectorShape); assertShapeMatches(mouse, vectorShape); }
@Test public void testRobotShape() throws IOException { final String robot = "+-------------+\n" + "|....XXXXX...+|\n" + "|XXXXX...XX...|\n" + "|....XXXXX....|\n" + "|.............|\n" + "|..XX..XXX....|\n" + "|...XX.XXX....|\n" + "|...XX.XXXX...|\n" + "|..XX.XXXXX...|\n" + "|....XXXXXX...|\n" + "|.XXXXXXXXXXX.|\n" + "|XX.........XX|\n" + "|XX.........XX|\n" + "|.XXXXXXXXXXX.|\n" + "+-------------+\n"; ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/robot-bitmap.st")); assertNotNull(st); assertEquals(1, st.shapes.size()); Shape shape = st.shapes.get(0); assertNotNull(shape); assertShapeMatches(robot, shape); Shape vectorShape = shape.toVector(); assertNotNull(vectorShape); assertShapeMatches(robot, vectorShape); }