今天介绍一个如何通过Dom4J的Visitor模式替换XML文件中正则表达式的方法,感兴趣的话可以看看。
Vistor模式不是本文关注的重点,感兴趣可以看一下本文:http://www.patterndepot.com/put/8/visitor.pdf
Dom4J提供了一个接收Vistor的接口,可以通过自定义Vistor实现类对XML文件中的正则表达式进行替换,原理很简单,就不在此赘述了,直接上例子吧。
首先是XML配置文件: deployment.xml
<deployment id="${deploy.id}">
<build>
<release>${build.release}</release>
<type>${build.type}</type>
<number>${build.number}</number>
</build>
<host>
<id>${deploy.host}</id>
<localdir>#{deploy.host:LocalHome}</localdir>
<user>#{deploy.host:User}</user>
<password>#{deploy.host:Password}</password>
</host>
</deployment>
然后是XMLVariableTransformer,这个类是我自定义的一个类,用来封装替换正则表示的一些方法。
package com.javaeye.terrencexu.dom4j;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
public class XMLVariableTransformer {
private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{([\\w\\d\\./_]+)}");
private static final Pattern HOST_VARIABLE_PATTERN = Pattern.compile("#\\{([\\w\\d\\./_]+):([\\w\\d\\./_]+)}");
private Properties config;
private Map<String, Properties> hostConfigs;
public XMLVariableTransformer(Properties config, Map<String, Properties> hostConfigs) {
this.config = config;
this.hostConfigs = hostConfigs;
}
public String transform(String xml) {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(new ByteArrayInputStream(xml.getBytes()));
doc.accept(new VariableVistor());
return doc.asXML();
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException("Unable to read xml data", e);
}
}
public String transform(File xml) {
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(xml);
doc.accept(new VariableVistor());
return doc.asXML();
} catch (DocumentException e) {
e.printStackTrace();
throw new RuntimeException("Unable to read xml data", e);
}
}
private class VariableVistor extends VisitorSupport {
public void visit(Attribute attr) {
parseNode(attr);
}
public void visit(Element node) {
parseNode(node);
}
private void parseNode(Node node) {
String substitution = getSubstitution(node.getText());
if(substitution != null) {
node.setText(substitution);
}
}
private String getSubstitution(String expression) {
Matcher m = XMLVariableTransformer.VARIABLE_PATTERN.matcher(expression);
if(m.matches()) {
return config.getProperty(m.group(1));
}
m = XMLVariableTransformer.HOST_VARIABLE_PATTERN.matcher(expression);
if(m.matches()) {
String host = config.getProperty(m.group(1));
return hostConfigs.get(host).getProperty(m.group(2));
}
return null;
}
}
}
该类中的内嵌类VariableVisitor继承了接口Visitor的默认实现类VisitorSupport,通过该类可以在遍历deployment.xml的每个节点以及attribute的时候按规则替换正则表达式。
最后是一个测试
package com.javaeye.terrencexu.dom4j;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
public class Test {
public static void main(String[] args) throws IOException {
Properties config = new Properties();
config.setProperty("deploy.id", "test");
config.setProperty("build.release", "release1");
config.setProperty("build.type", "type1");
config.setProperty("build.number", "latest");
config.setProperty("deploy.host", "localhost");
Map<String, Properties> hostConfigs = new HashMap<String, Properties>();
Properties hostConfig = new Properties();
hostConfig.setProperty("LocalHome", "c:\\install\\");
hostConfig.setProperty("User", "Administrator");
hostConfig.setProperty("Password", "abc123_");
hostConfigs.put("localhost", hostConfig);
File xmlFile = new File("C:\\eclipse\\workspace1\\Terrence-JavaStudy\\study-dom4j\\conf\\deployment.xml");
String xml = FileUtils.readFileToString(xmlFile);
XMLVariableTransformer tranformer = new XMLVariableTransformer(config, hostConfigs);
String deployment = tranformer.transform(xml);
System.out.println(deployment);
}
}
运行结果如下:
<?xml version="1.0" encoding="UTF-8"?>
<deployment id="test">
<build>
<release>release1</release>
<type>type1</type>
<number>latest</number>
</build>
<host>
<id>localhost</id>
<localdir>c:\install\</localdir>
<user>Administrator</user>
<password>abc123_</password>
</host>
</deployment>
-- Done --
分享到:
相关推荐
C++设计模式代码资源24_Vistor_访问器.zip
C#设计模式之1 --- Visitor (附原码)
vistor虚拟带库安装程序linux下
NULL 博文链接:https://wy649898543.iteye.com/blog/1436414
vistor虚拟带库linux所需的rpm包,我打包了
C++设计模式课件24_Vistor_访问器.pdf
近期学习tsm备份,用vistor虚拟带库搭建实验环境。文档涵盖了整个环境的搭建过程,每一步很详细。其中vistor_2_1_1.tgz 就一个安装包,还有安装说明。
访问者设计模式是众多设计模式中比较复杂的一项,可以用来处理二维交叉组合情况.特别合适其中一维不变. 访问速度快.
近期学习tsm备份,用vistor虚拟带库搭建实验环境。文档涵盖了整个环境的搭建过程,每一步很详细。
Visitor模式.docVisitor模式.doc
我们在使用visual studio进行编译C++代码时,你只是在菜单中选择了Build,然后visual studio就开始了一堆的编译工作;你应该知道,因为你的一个简单的Build动作,编译器在后台会进行语法分析,生成中间代码,生成...
设计模式的简要介绍,factory,vistor等,想了解的同学看一下吧
访问者模式表示一个作用于某对象结构中各元素的操作。它可以在不修改各元素类的前提下定义作用于这些元素的新操作,即动态的增加具体访问者角色。 访问者模式利用了双重分派。先将访问者传入元素对象的Accept方法中...
struts2.1.6 convertion插件(即注释方式配置)的helloworld 默认调用index()方法 文档说明用struts.xml中标签设置值,好象不用也行 rest插件例子 默认调用 create()方法 struts2.1.6 vistor校验例子 都是我测试例子,...
SQLancer(综合查询Lancer)是一种工具,用于自动测试数据库管理系统(DBMS),以发现其实现中的逻辑错误。 我们将逻辑错误称为导致DBMS提取错误结果集(例如,通过省略记录)的错误。 SQLancer分为以下两个阶段:...
HKU vistor map and guide
一.移动通信常用英文缩写: BSC Base Station Controller ...HLR Home Location Register 归属位置登记器 VLR Vistor Location Register 拜访位置登记器 MS Mobile Station 移动台 ISDN Intergrated Service Digital N