Factor out helper method to make it reusable

This commit is contained in:
Marco Hunsicker 2016-11-14 00:33:06 +01:00
parent be585f6a8f
commit c5f73abb44
2 changed files with 36 additions and 27 deletions

View File

@ -405,7 +405,7 @@ public class Antlr4Mojo extends AbstractMojo {
getLog().debug("Grammar file '" + grammarFile.getPath() + "' detected.");
String relPathBase = findSourceSubdir(sourceDirectory, grammarFile.getPath());
String relPathBase = MojoUtils.findSourceSubdir(sourceDirectory, grammarFile);
String relPath = relPathBase + grammarFile.getName();
getLog().debug(" ... relative path is: " + relPath);
@ -450,32 +450,6 @@ public class Antlr4Mojo extends AbstractMojo {
return includes;
}
/**
* Given the source directory File object and the full PATH to a grammar,
* produce the path to the named grammar file in relative terms to the
* {@code sourceDirectory}. This will then allow ANTLR to produce output
* relative to the base of the output directory and reflect the input
* organization of the grammar files.
*
* @param sourceDirectory The source directory {@link File} object
* @param grammarFileName The full path to the input grammar file
* @return The path to the grammar file relative to the source directory
*/
private String findSourceSubdir(File sourceDirectory, String grammarFileName) {
String srcPath = sourceDirectory.getPath() + File.separator;
if (!grammarFileName.startsWith(srcPath)) {
throw new IllegalArgumentException("expected " + grammarFileName + " to be prefixed with " + sourceDirectory);
}
File unprefixedGrammarFileName = new File(grammarFileName.substring(srcPath.length()));
if (unprefixedGrammarFileName.getParent() == null) {
return "";
}
return unprefixedGrammarFileName.getParent() + File.separator;
}
private final class CustomTool extends Tool {
public CustomTool(String[] args) {

View File

@ -0,0 +1,35 @@
package org.antlr.mojo.antlr4;
import java.io.File;
class MojoUtils {
/**
* Given the source directory File object and the full PATH to a grammar, produce the
* path to the named grammar file in relative terms to the {@code sourceDirectory}.
* This will then allow ANTLR to produce output relative to the base of the output
* directory and reflect the input organization of the grammar files.
*
* @param sourceDirectory The source directory {@link File} object
* @param grammarFileName The full path to the input grammar file
*
* @return The path to the grammar file relative to the source directory
*/
public static String findSourceSubdir(File sourceDirectory, File grammarFile) {
String srcPath = sourceDirectory.getPath() + File.separator;
String path = grammarFile.getPath();
if (!path.startsWith(srcPath)) {
throw new IllegalArgumentException("expected " + path +
" to be prefixed with " + sourceDirectory);
}
File unprefixedGrammarFileName = new File(path.substring(srcPath.length()));
if (unprefixedGrammarFileName.getParent() == null) {
return "";
}
return unprefixedGrammarFileName.getParent() + File.separator;
}
}