mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 01:15:44 +01:00
added ResourceProcessors to sonia.scm.io
This commit is contained in:
@@ -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<String, String> 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<String, String> variableMap = new HashMap<String, String>();
|
||||
}
|
||||
@@ -7,10 +7,6 @@
|
||||
|
||||
package sonia.scm.io;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
|
||||
102
scm-core/src/main/java/sonia/scm/io/RegexResourceProcessor.java
Normal file
102
scm-core/src/main/java/sonia/scm/io/RegexResourceProcessor.java
Normal file
@@ -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<String, String> 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<String, String> 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;
|
||||
}
|
||||
}
|
||||
76
scm-core/src/main/java/sonia/scm/io/ResourceProcessor.java
Normal file
76
scm-core/src/main/java/sonia/scm/io/ResourceProcessor.java
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user