-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #1: Create a application what parses xdocs with example config …
…specified
- Loading branch information
piyush kumar sadangi
authored and
piyush kumar sadangi
committed
Jun 16, 2024
1 parent
872737b
commit fe14c68
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
extractor/src/main/java/com/example/extractor/MainLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.example.extractor; | ||
|
||
import java.nio.file.*; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import java.util.regex.Pattern; | ||
import java.util.regex.Matcher; | ||
|
||
public class MainLauncher { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// Input and output directories | ||
String inputDirectory = ".ci-temp/checkstyle/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/abbreviationaswordinname"; | ||
String outputDirectory = "generated/AbbreviationAsWordInName"; | ||
|
||
// Process files in the input directory and save results to the output directory | ||
processFiles(inputDirectory, outputDirectory); | ||
} | ||
|
||
public static void processFiles(String inputDir, String outputDir) throws Exception { | ||
// Pattern to match files named Example#.java | ||
Pattern pattern = Pattern.compile("Example\\d+\\.java"); | ||
|
||
// Collect all Example#.java files in the input directory | ||
System.out.println("Walking through the input directory to collect Example#.java files..."); | ||
try (Stream<Path> paths = Files.walk(Paths.get(inputDir))) { | ||
List<String> exampleFiles = paths | ||
.filter(Files::isRegularFile) | ||
.filter(path -> { | ||
Matcher matcher = pattern.matcher(path.getFileName().toString()); | ||
return matcher.matches(); | ||
}) | ||
.map(Path::toString) | ||
.collect(Collectors.toList()); | ||
|
||
System.out.println("Found " + exampleFiles.size() + " Example#.java files."); | ||
|
||
// Determine the template file path relative to the project root | ||
String templateFilePath = "extractor/src/main/resources/config-template-treewalker.xml"; | ||
|
||
// Ensure output directory exists | ||
Path outputPath = Paths.get(outputDir).toAbsolutePath(); | ||
if (!Files.exists(outputPath)) { | ||
System.out.println("Output directory does not exist. Creating: " + outputPath); | ||
Files.createDirectories(outputPath); | ||
} else { | ||
System.out.println("Output directory already exists: " + outputPath); | ||
} | ||
|
||
// Process each file and generate corresponding config | ||
for (String exampleFile : exampleFiles) { | ||
System.out.println("Processing file: " + exampleFile); | ||
String fileContent = new String(Files.readAllBytes(Paths.get(exampleFile))); | ||
System.out.println("File content:\n" + fileContent); | ||
|
||
String generatedContent; | ||
try { | ||
System.out.println("Generating configuration for file: " + exampleFile); | ||
generatedContent = ConfigSerializer.serializeConfigToString(exampleFile, templateFilePath); | ||
System.out.println("Generated configuration:\n" + generatedContent); | ||
} catch (Exception e) { | ||
System.err.println("Failed to process file: " + exampleFile); | ||
e.printStackTrace(); | ||
continue; | ||
} | ||
|
||
String fileName = Paths.get(exampleFile).getFileName().toString().replace(".java", "-config.xml"); | ||
Path outputFilePath = outputPath.resolve(fileName); | ||
System.out.println("Writing generated configuration to: " + outputFilePath); | ||
Files.writeString(outputFilePath, generatedContent); | ||
} | ||
} catch (Exception e) { | ||
System.err.println("Error walking through the input directory or processing files."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |