Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions maven-config-processor-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.classpath
/target/
.project
.settings/
2 changes: 1 addition & 1 deletion maven-config-processor-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<version>4.12</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.code.configprocessor;

import static com.google.code.configprocessor.util.IOUtils.*;

import static org.apache.commons.lang.StringUtils.*;

import java.io.*;
Expand All @@ -32,6 +33,7 @@
import com.google.code.configprocessor.processing.*;
import com.google.code.configprocessor.processing.properties.*;
import com.google.code.configprocessor.processing.xml.*;
import com.google.code.configprocessor.util.IOUtils;

public class ConfigProcessor {

Expand Down Expand Up @@ -104,32 +106,36 @@ public void execute(ExpressionResolver resolver, Transformation transformation)
getLog().info("Using wildcard pattern based input [" + input + "]");
List<File> inputFiles = getMatchingFiles(input);
for (File inputFile : inputFiles) {
String type = getInputType(transformation, inputFile);
File outputFile;
if (actualOutputDirectory != null) {
// calculate a relative path below the output directory based on the input file
outputFile = new File(actualOutputDirectory, baseDir.toURI().relativize(inputFile.toURI()).getPath());
createOutputFile(outputFile);
} else {
outputFile = inputFile;
String type = getInputType(transformation);
if (actualOutputDirectory == null) {
throw new ConfigProcessorException("Output directory must be set");
}
// calculate a relative path below the output directory based on the input file
File outputFile = new File(actualOutputDirectory, baseDir.toURI().relativize(inputFile.toURI()).getPath());
createOutputFile(outputFile);
if (!inputFile.exists()) {
throw new ConfigProcessorException("File not found: " + inputFile);
}
process(resolver, inputFile.getPath(), inputFile, outputFile, configIdentifier, action, type);
InputStream inputStream = new FileInputStream(inputFile);
process(resolver, inputFile.getPath(), inputStream, outputFile, configIdentifier, action, type);
}
} else {
File inputFile = fileResolver.resolve(transformation.getInput());
if (!inputFile.exists()) {
throw new ConfigProcessorException("Input file [" + inputFile + "] does not exist");
InputStream inputStream;
try {
inputStream = fileResolver.resolve(transformation.getInput());
} catch (Exception e) {
throw new ConfigProcessorException("Input file [" + transformation.getInput() + "] does not exist", e);
}
// use input file as output file if output is not set
File output;
if (StringUtils.isBlank(transformation.getOutput())) {
output = inputFile;
throw new ConfigProcessorException("Output must be set");
} else {
output = new File(actualOutputDirectory, transformation.getOutput());
createOutputFile(output);
}
String type = getInputType(transformation, inputFile);
process(resolver, transformation.getInput(), inputFile, output, configIdentifier, action, type);
String type = getInputType(transformation);
process(resolver, transformation.getInput(), inputStream, output, configIdentifier, action, type);
}
}

Expand All @@ -155,13 +161,13 @@ protected Action getAction(Transformation transformation) throws ConfigProcessor
sb.append(XmlHelper.ROOT_PROCESSOR_END);
configReader = new StringReader(sb.toString());
} else {
File config = fileResolver.resolve(transformation.getConfig());
InputStream config = fileResolver.resolve(transformation.getConfig());

if (!config.exists()) {
if (config == null) {
throw new ConfigProcessorException("Configuration file [" + config + "] does not exist");
}

configReader = new InputStreamReader(new FileInputStream(config), encoding);
configReader = new InputStreamReader(config, encoding);
}

try {
Expand Down Expand Up @@ -255,13 +261,13 @@ protected List<File> getMatchingFiles(String pattern) throws ConfigProcessorExce
* contain a type
* @return Input file type.
*/
protected String getInputType(Transformation transformation, File input) {
protected String getInputType(Transformation transformation) {
String type;

String input = transformation.getInput();
if (transformation.getType() == null) {
if (input.getName().endsWith(".properties")) {
if (input.endsWith(".properties")) {
type = Transformation.PROPERTIES_TYPE;
} else if (input.getName().endsWith(".xml")) {
} else if (input.endsWith(".xml")) {
type = Transformation.XML_TYPE;
} else {
if (getLog() != null) {
Expand All @@ -283,23 +289,21 @@ protected String getInputType(Transformation transformation, File input) {
*
* @param resolver
* @param inputName Symbolic name of the input file to read from.
* @param input Input file to read from.
* @param inputStream2 Input file to read from.
* @param output Output file to write to.
* @param configName Symbolic name of the file containing rules to process the input.
* @param action Action to be performed on the input file.
* @param type Type of the input file. Properties, XML or null if it is to be auto-detected.
* @throws ConfigProcessorException If processing cannot be performed.
*/
protected void process(ExpressionResolver resolver, String inputName, File input, File output, String configName, Action action, String type) throws ConfigProcessorException {
protected void process(ExpressionResolver resolver, String inputName, InputStream inputStream, File output, String configName, Action action, String type) throws ConfigProcessorException {
getLog().info("Processing file [" + inputName + "] using config [" + configName + "], outputing to [" + output + "]");

InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;

InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
try {
inputStream = new FileInputStream(input);
outputStream = new ByteArrayOutputStream();

inputStreamReader = new InputStreamReader(inputStream, encoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
*/
package com.google.code.configprocessor.io;

import java.io.*;
import java.net.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class ClasspathFileResolver implements FileResolver {

public File resolve(String name) throws IOException {
URL url = getClass().getResource(name);
if (url == null) {
public InputStream resolve(String name) throws IOException {
InputStream inputStream = getClass().getResourceAsStream(name);
if (inputStream == null) {
throw new FileNotFoundException("Classpath resource [" + name + "] not found");
}
return new File(url.getPath());
return inputStream;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@

public class DefaultFileResolver implements FileResolver {

public File resolve(String name) throws IOException {
ClasspathFileResolver classpathFileResolver = new ClasspathFileResolver();

public InputStream resolve(String name) throws IOException {
if (name.startsWith("classpath:")) {
String resourcePath = name.replace("classpath:", "");
return classpathFileResolver.resolve(resourcePath);
}
File file = new File(name);
if (!file.exists()) {
throw new FileNotFoundException("File [" + name + "] does not exist");
}
return file;
return new FileInputStream(file);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

public interface FileResolver {

File resolve(String name) throws IOException;
InputStream resolve(String name) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MavenFileResolver implements FileResolver {

private Locator locator;
private LogAdapter logAdapter;

public MavenFileResolver(MavenProject mavenProject, ArtifactFactory artifactFactory, ArtifactResolver artifactResolver, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories, LogAdapter logAdapter) {
this.logAdapter = logAdapter;
locator = new Locator();
Expand All @@ -43,19 +43,18 @@ public MavenFileResolver(MavenProject mavenProject, ArtifactFactory artifactFact
locator.setStrategies(strategies);
}

public File resolve(String name) throws IOException {
public InputStream resolve(String name) throws IOException {
Location location = locator.resolve(name);
if (location == null) {
throw new IOException("File not found [" + name + "]\n" + locator.getMessageHolder().render());
}

try {
File file = location.getFile();
logAdapter.debug("Resolved [" + name + "] to file [" + file + "]");
return file;
} catch (IOException e) {
throw new IOException("Failed to load file [" + name + "]\n" + locator.getMessageHolder().render());

InputStream inputStream = location.getInputStream();
if (inputStream != null) {
logAdapter.debug("Resolved [" + name + "] to an inputStream [" + location + "]");
return inputStream;
}
throw new IOException("Failed to load file [" + name + "]\n" + locator.getMessageHolder().render());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public class PropertiesActionProcessor implements ActionProcessor {
private FileResolver fileResolver;
private ExpressionResolver expressionResolver;

private Set<File> appendedFiles;
private Set<String> appendedFiles;

public PropertiesActionProcessor(String encoding, FileResolver fileResolver, ExpressionResolver expressionResolver) {
this.encoding = encoding;
this.fileResolver = fileResolver;
this.expressionResolver = expressionResolver;
this.appendedFiles = new HashSet<File>();
this.appendedFiles = new HashSet<String>();
}

public void process(Reader input, Writer output, Action action) throws ParsingException, IOException {
BufferedReader reader = new BufferedReader(input);
BufferedWriter writer = new BufferedWriter(output);
process(reader, writer, action);
}

protected void process(BufferedReader reader, BufferedWriter writer, Action action) throws ParsingException, IOException {
PropertiesActionProcessingAdvisor advisor = getAdvisorFor(action);

Expand Down Expand Up @@ -165,12 +165,12 @@ protected void append(PropertiesFileItem item, BufferedWriter writer) throws IOE
writer.append(LINE_SEPARATOR);
}
}

protected void appendFile(PropertiesFileItem item, BufferedWriter writer, Action action) throws ParsingException, IOException {
FilePropertiesFileItem aux = (FilePropertiesFileItem)item;
File file = fileResolver.resolve(aux.getFile());
if (appendedFiles.add(file)) { // Prevent adding the same file twice
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), encoding);
InputStream inputStream = fileResolver.resolve(aux.getFile());
if (appendedFiles.add(aux.getFile())) { // Prevent adding the same file twice
InputStreamReader reader = new InputStreamReader(inputStream, encoding);
try {
process(reader, writer, action);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class XmlActionProcessor implements ActionProcessor {
private ExpressionResolver expressionResolver;
private MapBasedNamespaceContext namespaceContext;
private List<ParserFeature> parserFeatures;
private boolean failOnMissingXpath;
private boolean failOnMissingXpath;

public XmlActionProcessor(String encoding, int lineWidth, int indentSize, FileResolver fileResolver, ExpressionResolver expressionResolver, Map<String, String> contextMappings,
public XmlActionProcessor(String encoding, int lineWidth, int indentSize, FileResolver fileResolver, ExpressionResolver expressionResolver, Map<String, String> contextMappings,
List<ParserFeature> parserFeatures, boolean failOnMissingXpath) {
this.encoding = encoding;
this.lineWidth = lineWidth;
Expand All @@ -51,7 +51,7 @@ public XmlActionProcessor(String encoding, int lineWidth, int indentSize, FileRe
this.expressionResolver = expressionResolver;
this.namespaceContext = new MapBasedNamespaceContext(contextMappings);
this.parserFeatures = parserFeatures;
this.failOnMissingXpath = failOnMissingXpath;
this.failOnMissingXpath = failOnMissingXpath;
}

public void process(Reader input, Writer output, Action action) throws ParsingException, IOException {
Expand Down Expand Up @@ -108,8 +108,8 @@ protected XmlActionProcessingAdvisor getAdvisorFor(Action rootAction, Action act
}

protected String getProcessedFile(String name, Action action) throws ParsingException, IOException {
File file = fileResolver.resolve(name);
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), encoding);
InputStream inputStream = fileResolver.resolve(name);
InputStreamReader reader = new InputStreamReader(inputStream, encoding);
StringWriter writer = new StringWriter();
try {
process(reader, writer, action);
Expand Down
Loading