Common Digester附带了一个插件机制,可以实现很好的地址,下面是我的一个简单实现:
1、业务接口:
public interface Transform {
public String transform(String s);
}
2、业务接口的实现类:
public class CaseTransformer implements Transform {
private boolean upper;
public void setCase(String s) {
upper = "upper".equalsIgnoreCase(s.trim());
}
public String transform(String s) {
if (s == null) {
return "";
}
return upper ? s.toUpperCase() : s.toLowerCase();
}
}
3、插件创建规则:
package mydigest;
import org.xml.sax.Attributes;
public class MyPluginCreateRule extends MyRule {
private Class<?> baseClass;
public static final String DEFT_PLUGIN_CLASS_ATTR = "plugin-class";
public MyPluginCreateRule(Class<?> clazz) {
baseClass = clazz;
}
/** *//** 开始处理 */
public void begin(String namespace, String name, Attributes attributes) throws Exception {
String value = attributes.getValue(DEFT_PLUGIN_CLASS_ATTR);
Class<?> clazz = dig.getClassLoader().loadClass(value);
Object instance = clazz.newInstance();
if (baseClass.isAssignableFrom(clazz)) {
dig.push(instance);
}
}
/** *//** 处理结束 */
public void end(String namespace, String name) throws Exception {
dig.pop();
}
}
4、配置文件upperconfig.xml:
<piple>
<src file="input.txt"/>
<transform plugin-class="mydigest.CaseTransformer" case="upper"/>
<dest file="output.txt"/>
</piple>
5、测试:
package mydigest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Piple {
private String src;
private Transform transformer;
private String dest;
public static void main(String[] args) {
String file = "upperconfig.xml";
MyDigester dig = new MyDigester();
dig.addObjectCreate("piple", Piple.class);
// 源文件
dig.addCallMethod("piple/src", "setSrc", 1);
dig.addCallParam("piple/src", 0, "file");
// 添加插件
MyPluginCreateRule pcr = new MyPluginCreateRule(Transform.class);
dig.addRule("piple/transform", pcr);
dig.addSetProperties("piple/transform");
dig.addSetNext("piple/transform", "setTransform");
// 目标文件
dig.addCallMethod("piple/dest", "setDest", 1);
dig.addCallParam("piple/dest", 0, "file");
try {
Piple piple = (Piple) dig.parse(new File(file));
piple.exec();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setSrc(String src) {
this.src = src;
}
public void setTransform(Transform transformer) {
this.transformer = transformer;
}
public void setDest(String dest) {
this.dest = dest;
}
public void exec() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(src));
FileWriter writer = new FileWriter(new File(dest));
String line = reader.readLine();
while (line != null) {
writer.write(this.transformer.transform(line) + '\n');
line = reader.readLine();
}
writer.close();
reader.close();
System.out.println("exec done!");
}
}
inut.txt文件:
Copyright
Copyright 2004 O'Reilly Media, Inc.
Printed in the United States of America.
Published by O'Reilly MediaInc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
得到的output.xml文件:
COPYRIGHT
COPYRIGHT 2004 O'REILLY MEDIA, INC.
PRINTED IN THE UNITED STATES OF AMERICA.
PUBLISHED BY O'REILLY MEDIAINC., 1005 GRAVENSTEIN HIGHWAY NORTH, SEBASTOPOL, CA 95472.