diff --git a/scm-core/src/main/java/sonia/scm/io/AbstractResourceProcessor.java b/scm-core/src/main/java/sonia/scm/io/AbstractResourceProcessor.java new file mode 100644 index 0000000000..2aedaca7dd --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/io/AbstractResourceProcessor.java @@ -0,0 +1,144 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.io; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Sebastian Sdorra + */ +public abstract class AbstractResourceProcessor implements ResourceProcessor +{ + + /** + * Method description + * + * + * @param variableMap + * @param reader + * @param writer + * + * @throws IOException + */ + protected abstract void process(Map variableMap, + BufferedReader reader, BufferedWriter writer) + throws IOException; + + /** + * Method description + * + * + * @param key + * @param value + */ + @Override + public void addVariable(String key, String value) + { + variableMap.put(key, value); + } + + /** + * Method description + * + * + * @param input + * @param output + * + * @throws IOException + */ + @Override + public void process(InputStream input, OutputStream output) throws IOException + { + process(new InputStreamReader(input), new OutputStreamWriter(output)); + } + + /** + * Method description + * + * + * @param input + * @param output + * + * @throws IOException + */ + @Override + public void process(File input, File output) throws IOException + { + process(new FileReader(input), new FileWriter(input)); + } + + /** + * Method description + * + * + * @param reader + * @param writer + * + * @throws IOException + */ + @Override + public void process(Reader reader, Writer writer) throws IOException + { + BufferedReader bufferedReader = null; + BufferedWriter bufferedWriter = null; + + if (reader instanceof BufferedReader) + { + bufferedReader = (BufferedReader) reader; + } + else + { + bufferedReader = new BufferedReader(reader); + } + + if (writer instanceof BufferedWriter) + { + bufferedWriter = (BufferedWriter) writer; + } + else + { + bufferedWriter = new BufferedWriter(writer); + } + + process(variableMap, bufferedReader, bufferedWriter); + } + + /** + * Method description + * + * + * @param key + */ + @Override + public void removeVariable(String key) + { + variableMap.remove(key); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private Map variableMap = new HashMap(); +} diff --git a/scm-core/src/main/java/sonia/scm/io/AbstractWriter.java b/scm-core/src/main/java/sonia/scm/io/AbstractWriter.java index 41cd62896f..1c0cdc291e 100644 --- a/scm-core/src/main/java/sonia/scm/io/AbstractWriter.java +++ b/scm-core/src/main/java/sonia/scm/io/AbstractWriter.java @@ -7,10 +7,6 @@ package sonia.scm.io; -//~--- non-JDK imports -------------------------------------------------------- - -import sonia.scm.util.Util; - //~--- JDK imports ------------------------------------------------------------ import java.io.File; diff --git a/scm-core/src/main/java/sonia/scm/io/RegexResourceProcessor.java b/scm-core/src/main/java/sonia/scm/io/RegexResourceProcessor.java new file mode 100644 index 0000000000..c98ee96961 --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/io/RegexResourceProcessor.java @@ -0,0 +1,102 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.io; + +//~--- non-JDK imports -------------------------------------------------------- + +import sonia.scm.util.Util; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * + * @author Sebastian Sdorra + */ +public class RegexResourceProcessor extends AbstractResourceProcessor +{ + + /** Field description */ + public static final Pattern PATTERN = Pattern.compile("\\$\\{([^\\$]+)\\}"); + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param variableMap + * @param reader + * @param writer + * + * @throws IOException + */ + @Override + protected void process(Map variableMap, + BufferedReader reader, BufferedWriter writer) + throws IOException + { + try + { + String line = reader.readLine(); + + while (line != null) + { + line = parseLine(variableMap, line); + writer.write(line); + line = reader.readLine(); + + if (line != null) + { + writer.newLine(); + } + } + } + finally + { + Util.close(reader); + Util.close(writer); + } + } + + /** + * Method description + * + * + * @param variableMap + * @param line + * + * @return + */ + private String parseLine(Map variableMap, String line) + { + Matcher m = PATTERN.matcher(line); + + while (m.find()) + { + String key = m.group(1); + String value = variableMap.get(key); + + if (value != null) + { + line = m.replaceAll(value); + } + + m = PATTERN.matcher(line); + } + + return line; + } +} diff --git a/scm-core/src/main/java/sonia/scm/io/ResourceProcessor.java b/scm-core/src/main/java/sonia/scm/io/ResourceProcessor.java new file mode 100644 index 0000000000..530fc316cb --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/io/ResourceProcessor.java @@ -0,0 +1,76 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + + + +package sonia.scm.io; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; + +/** + * + * @author Sebastian Sdorra + */ +public interface ResourceProcessor +{ + + /** + * Method description + * + * + * @param key + * @param value + */ + public void addVariable(String key, String value); + + /** + * Method description + * + * + * @param input + * @param output + * + * @throws IOException + */ + public void process(InputStream input, OutputStream output) + throws IOException; + + /** + * Method description + * + * + * @param input + * @param output + * + * @throws IOException + */ + public void process(File input, File output) throws IOException; + + /** + * Method description + * + * + * @param reader + * @param writer + * + * @throws IOException + */ + public void process(Reader reader, Writer writer) throws IOException; + + /** + * Method description + * + * + * @param key + */ + public void removeVariable(String key); +}