@Test void test() throws IOException { File testRunDir = new File("target/test-run"); testRunDir.mkdirs(); File inFile = new File(testRunDir, "TestFile.rst"); copyResourceToFile("/TestFile.rst", inFile); FileChecker checker = new FileChecker(inFile, inFile.getParent().length() + 1, testRunDir); checker.process(); boolean match = true; File outputFile = new File(testRunDir, "Code_TestFile.java"); try (InputStream fis = new FileInputStream(outputFile); InputStreamReader fir = new InputStreamReader(fis, UTF_8); BufferedReader reader1 = new BufferedReader(fir); InputStream ris = getClass().getResourceAsStream("/Code_TestFile.java"); InputStreamReader rir = new InputStreamReader(ris, UTF_8); BufferedReader reader2 = new BufferedReader(rir)) { String line1, line2; while ((line1 = reader1.readLine()) != null & (line2 = reader2.readLine()) != null) { if (!line1.equals(line2)) { match = false; break; } } if (line1 != null) { match = false; } if (line2 != null) { match = false; } } if (!match) { copyResourceToFile("/Code_TestFile.java", new File(testRunDir, "expectedCode_TestFile.java")); } assertTrue(match, "Generated file does not match expected one"); }
public int process() throws IOException { LOGGER.info("Processing: " + this.fileName); collectImports(); final List<List<String>> codeBlocks = collectCodeBlocks(); if (codeBlocks.isEmpty()) { return 0; } LOGGER.debug("Writing Class: " + this.className); try (FileOutputStream fos = new FileOutputStream(this.outputFile); JavaFileWriter writer = new JavaFileWriter(fos)) { writer.packageDef("org.spongepowered.docs.generated.codeblocks") .imports(this.imports) .imported("static org.spongepowered.docs.generated.Utils.*") .classDef(this.className); int i = 0; for (List<String> codeBlock : codeBlocks) { boolean indended = true; String firstLine = codeBlock.get(0); int firstLineIndex = 0; while (firstLine.startsWith("@") || firstLine.startsWith(" firstLine = codeBlock.get(++firstLineIndex); } if (firstLine.startsWith("private ")) { writer.writeLine(" writer.classDef("Inner" + i++, true); } else if (firstLine.contains(" = ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("if ") || firstLine.startsWith("try ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.matches("^[a-zA-Z]+(\\(\\))?\\.[a-z].*")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("return ")) { writer.writeLine(" writer.methodDef("inner" + i++, "Object"); } else if (firstLine.contains(" class ") || firstLine.contains(" interface ")) { writer.writeLine(" indended = false; } else { writer.writeLine(" writer.classDef("Inner" + i++, true); } for (String line : codeBlock) { if (line.isEmpty()) { writer.emptyLine(); continue; } if (line.startsWith("package ")) { line = " } else if (line.contains(" class ")) { if (!line.contains(" static ")) { line = line.replace(" class ", " static class "); } } else if (line.contains(" interface ")) { if (!line.contains(" static ")) { line = line.replace(" interface ", " static interface "); } } else if (line.trim().equals("[...]")) { line = " } else if (line.trim().endsWith(" = ...;")) { line = line.replace(" = ...;", " = magic();"); } else if (line.trim().contains("(...)")) { line = line.replace("(...)", "(magic())"); } writer.writeLine(line); } writer.emptyLine(); if (indended) { writer.closingBracket(); } } writer.closingBracket(); } return codeBlocks.size(); }
FileChecker { public int process() throws IOException { LOGGER.info("Processing: " + this.fileName); collectImports(); final List<List<String>> codeBlocks = collectCodeBlocks(); if (codeBlocks.isEmpty()) { return 0; } LOGGER.debug("Writing Class: " + this.className); try (FileOutputStream fos = new FileOutputStream(this.outputFile); JavaFileWriter writer = new JavaFileWriter(fos)) { writer.packageDef("org.spongepowered.docs.generated.codeblocks") .imports(this.imports) .imported("static org.spongepowered.docs.generated.Utils.*") .classDef(this.className); int i = 0; for (List<String> codeBlock : codeBlocks) { boolean indended = true; String firstLine = codeBlock.get(0); int firstLineIndex = 0; while (firstLine.startsWith("@") || firstLine.startsWith(" firstLine = codeBlock.get(++firstLineIndex); } if (firstLine.startsWith("private ")) { writer.writeLine(" writer.classDef("Inner" + i++, true); } else if (firstLine.contains(" = ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("if ") || firstLine.startsWith("try ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.matches("^[a-zA-Z]+(\\(\\))?\\.[a-z].*")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("return ")) { writer.writeLine(" writer.methodDef("inner" + i++, "Object"); } else if (firstLine.contains(" class ") || firstLine.contains(" interface ")) { writer.writeLine(" indended = false; } else { writer.writeLine(" writer.classDef("Inner" + i++, true); } for (String line : codeBlock) { if (line.isEmpty()) { writer.emptyLine(); continue; } if (line.startsWith("package ")) { line = " } else if (line.contains(" class ")) { if (!line.contains(" static ")) { line = line.replace(" class ", " static class "); } } else if (line.contains(" interface ")) { if (!line.contains(" static ")) { line = line.replace(" interface ", " static interface "); } } else if (line.trim().equals("[...]")) { line = " } else if (line.trim().endsWith(" = ...;")) { line = line.replace(" = ...;", " = magic();"); } else if (line.trim().contains("(...)")) { line = line.replace("(...)", "(magic())"); } writer.writeLine(line); } writer.emptyLine(); if (indended) { writer.closingBracket(); } } writer.closingBracket(); } return codeBlocks.size(); } }
FileChecker { public int process() throws IOException { LOGGER.info("Processing: " + this.fileName); collectImports(); final List<List<String>> codeBlocks = collectCodeBlocks(); if (codeBlocks.isEmpty()) { return 0; } LOGGER.debug("Writing Class: " + this.className); try (FileOutputStream fos = new FileOutputStream(this.outputFile); JavaFileWriter writer = new JavaFileWriter(fos)) { writer.packageDef("org.spongepowered.docs.generated.codeblocks") .imports(this.imports) .imported("static org.spongepowered.docs.generated.Utils.*") .classDef(this.className); int i = 0; for (List<String> codeBlock : codeBlocks) { boolean indended = true; String firstLine = codeBlock.get(0); int firstLineIndex = 0; while (firstLine.startsWith("@") || firstLine.startsWith(" firstLine = codeBlock.get(++firstLineIndex); } if (firstLine.startsWith("private ")) { writer.writeLine(" writer.classDef("Inner" + i++, true); } else if (firstLine.contains(" = ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("if ") || firstLine.startsWith("try ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.matches("^[a-zA-Z]+(\\(\\))?\\.[a-z].*")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("return ")) { writer.writeLine(" writer.methodDef("inner" + i++, "Object"); } else if (firstLine.contains(" class ") || firstLine.contains(" interface ")) { writer.writeLine(" indended = false; } else { writer.writeLine(" writer.classDef("Inner" + i++, true); } for (String line : codeBlock) { if (line.isEmpty()) { writer.emptyLine(); continue; } if (line.startsWith("package ")) { line = " } else if (line.contains(" class ")) { if (!line.contains(" static ")) { line = line.replace(" class ", " static class "); } } else if (line.contains(" interface ")) { if (!line.contains(" static ")) { line = line.replace(" interface ", " static interface "); } } else if (line.trim().equals("[...]")) { line = " } else if (line.trim().endsWith(" = ...;")) { line = line.replace(" = ...;", " = magic();"); } else if (line.trim().contains("(...)")) { line = line.replace("(...)", "(magic())"); } writer.writeLine(line); } writer.emptyLine(); if (indended) { writer.closingBracket(); } } writer.closingBracket(); } return codeBlocks.size(); } FileChecker(final File file, final int pathPrefixLength, File outputDirectory); }
FileChecker { public int process() throws IOException { LOGGER.info("Processing: " + this.fileName); collectImports(); final List<List<String>> codeBlocks = collectCodeBlocks(); if (codeBlocks.isEmpty()) { return 0; } LOGGER.debug("Writing Class: " + this.className); try (FileOutputStream fos = new FileOutputStream(this.outputFile); JavaFileWriter writer = new JavaFileWriter(fos)) { writer.packageDef("org.spongepowered.docs.generated.codeblocks") .imports(this.imports) .imported("static org.spongepowered.docs.generated.Utils.*") .classDef(this.className); int i = 0; for (List<String> codeBlock : codeBlocks) { boolean indended = true; String firstLine = codeBlock.get(0); int firstLineIndex = 0; while (firstLine.startsWith("@") || firstLine.startsWith(" firstLine = codeBlock.get(++firstLineIndex); } if (firstLine.startsWith("private ")) { writer.writeLine(" writer.classDef("Inner" + i++, true); } else if (firstLine.contains(" = ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("if ") || firstLine.startsWith("try ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.matches("^[a-zA-Z]+(\\(\\))?\\.[a-z].*")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("return ")) { writer.writeLine(" writer.methodDef("inner" + i++, "Object"); } else if (firstLine.contains(" class ") || firstLine.contains(" interface ")) { writer.writeLine(" indended = false; } else { writer.writeLine(" writer.classDef("Inner" + i++, true); } for (String line : codeBlock) { if (line.isEmpty()) { writer.emptyLine(); continue; } if (line.startsWith("package ")) { line = " } else if (line.contains(" class ")) { if (!line.contains(" static ")) { line = line.replace(" class ", " static class "); } } else if (line.contains(" interface ")) { if (!line.contains(" static ")) { line = line.replace(" interface ", " static interface "); } } else if (line.trim().equals("[...]")) { line = " } else if (line.trim().endsWith(" = ...;")) { line = line.replace(" = ...;", " = magic();"); } else if (line.trim().contains("(...)")) { line = line.replace("(...)", "(magic())"); } writer.writeLine(line); } writer.emptyLine(); if (indended) { writer.closingBracket(); } } writer.closingBracket(); } return codeBlocks.size(); } FileChecker(final File file, final int pathPrefixLength, File outputDirectory); String getFileName(); int process(); }
FileChecker { public int process() throws IOException { LOGGER.info("Processing: " + this.fileName); collectImports(); final List<List<String>> codeBlocks = collectCodeBlocks(); if (codeBlocks.isEmpty()) { return 0; } LOGGER.debug("Writing Class: " + this.className); try (FileOutputStream fos = new FileOutputStream(this.outputFile); JavaFileWriter writer = new JavaFileWriter(fos)) { writer.packageDef("org.spongepowered.docs.generated.codeblocks") .imports(this.imports) .imported("static org.spongepowered.docs.generated.Utils.*") .classDef(this.className); int i = 0; for (List<String> codeBlock : codeBlocks) { boolean indended = true; String firstLine = codeBlock.get(0); int firstLineIndex = 0; while (firstLine.startsWith("@") || firstLine.startsWith(" firstLine = codeBlock.get(++firstLineIndex); } if (firstLine.startsWith("private ")) { writer.writeLine(" writer.classDef("Inner" + i++, true); } else if (firstLine.contains(" = ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("if ") || firstLine.startsWith("try ")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.matches("^[a-zA-Z]+(\\(\\))?\\.[a-z].*")) { writer.writeLine(" writer.methodDef("inner" + i++, "void"); } else if (firstLine.startsWith("return ")) { writer.writeLine(" writer.methodDef("inner" + i++, "Object"); } else if (firstLine.contains(" class ") || firstLine.contains(" interface ")) { writer.writeLine(" indended = false; } else { writer.writeLine(" writer.classDef("Inner" + i++, true); } for (String line : codeBlock) { if (line.isEmpty()) { writer.emptyLine(); continue; } if (line.startsWith("package ")) { line = " } else if (line.contains(" class ")) { if (!line.contains(" static ")) { line = line.replace(" class ", " static class "); } } else if (line.contains(" interface ")) { if (!line.contains(" static ")) { line = line.replace(" interface ", " static interface "); } } else if (line.trim().equals("[...]")) { line = " } else if (line.trim().endsWith(" = ...;")) { line = line.replace(" = ...;", " = magic();"); } else if (line.trim().contains("(...)")) { line = line.replace("(...)", "(magic())"); } writer.writeLine(line); } writer.emptyLine(); if (indended) { writer.closingBracket(); } } writer.closingBracket(); } return codeBlocks.size(); } FileChecker(final File file, final int pathPrefixLength, File outputDirectory); String getFileName(); int process();  }