XMLUtil.java
package ssh.util;
import java.io.File;
import java.util.Iterator;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 简单XML工具
* @author gary
* @version 1.0
*
*/
public class XMLUtil {
private static Document document;
private static Element rootElement;
public XMLUtil(){
}
public XMLUtil(String fileName){
init(fileName);
}
/**
* 初始化
* @param fileName
*/
private void init(String fileName){
SAXReader reader = new SAXReader();
try {
document = reader.read(new File(getXMLFilePath(fileName)));
} catch (DocumentException e) {
e.printStackTrace();
}
rootElement = document.getRootElement();
}
/**
* 获得指定xml文件(不包含扩展名)的路径
* @param xmlFileName
* @return
*/
public String getXMLFilePath(String xmlFileName){
File directory = new File(ServletActionContext.getServletContext().getRealPath(xmlFileName+".xml").replace("\\", "\\\\"));
String path = directory.getParent().substring(0, directory.getPath().lastIndexOf(File.separator))+File.separator+"WEB-INF"+File.separator+"classes"+File.separator+xmlFileName+".xml";
return path;
}
/**
* 获得目标指定属性的值
* @param target
* @param attribute
* @return
*/
public String getTargetValue(String target, String attribute){
if(target == null || target.equals("") || attribute == null || attribute.equals("")){
System.out.println("参数错误");
}else if(target.equals(rootElement.getName())){
for( Iterator<?> j = rootElement.elementIterator(attribute); j.hasNext();){
Element key = (Element) j.next();
return key.getStringValue();
}
}else{
for ( Iterator<?> i = rootElement.elementIterator(target); i.hasNext();) {
Element xmltarget = (Element) i.next();
for ( Iterator<?> j = xmltarget.elementIterator(attribute); j.hasNext();) {
Element key = (Element) j.next();
return key.getStringValue();
}
}
}
return null;
}
/**
* 获得目标属性
* @param target
* @param attribute
* @return
*/
public Element getTargetAttribute(String target, String attribute){
if(target == null || target.equals("") || attribute == null || attribute.equals("")){
System.out.println("参数错误");
}else if(target.equals(rootElement.getName())){
for( Iterator<?> j = rootElement.elementIterator(attribute); j.hasNext();){
Element key = (Element) j.next();
return key;
}
}else{
for ( Iterator<?> i = rootElement.elementIterator(target); i.hasNext();) {
Element xmltarget = (Element) i.next();
for ( Iterator<?> j = xmltarget.elementIterator(attribute); j.hasNext();) {
Element key = (Element) j.next();
return key;
}
}
}
return null;
}
/**
* 获取跟结点
* @param document
* @return
*/
public static Element getRootNode(Document document){
if(document == null){
return null;
}
Element root = document.getRootElement();
return root;
}
}
JXLReader.java
package ssh.util;
import java.io.File;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
/**
* JXL读取配置文件
* @author gary
* @version 1.0
*
*/
public class JXLReader {
//true:XML方式
//false:properties方式
private static boolean flag = true;
private static Map<String, String> jxl;
private static XMLUtil xmlUtil;
/**
* 构造方法,判断类型,读取数据
*/
public JXLReader(){
judgementMode();
readData();
}
/**
* 初始化,读入jxl.xml或jxl.properties文件
*/
private void readData(){
if(flag){
//XML方式
xmlUtil = new XMLUtil("jxl");
}else{
//properties方式
jxl = new HashMap<String, String>();
ResourceBundle rb = ResourceBundle.getBundle("jxl");
Enumeration<String> keys=rb.getKeys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
String value = rb.getString(key);
jxl.put(key, value);
}
}
}
/**
* 判断类型,用XML方式还是properties方式
*/
private void judgementMode(){
File file = new File(new XMLUtil().getXMLFilePath("jxl"));
if(file.exists()){
// System.out.println("文件存在,使用xml方式");
}else{
// System.out.println("文件不存在,使用properties方式");
flag = false;
}
}
/**
* 返回指定目标的属性值
* @param target
* @param attribute
* @return
*/
public String getTargetValue(String target, String attribute){
if(flag){
return xmlUtil.getTargetValue(target, attribute);
}else{
return getTargetValueP(target, attribute);
}
}
/**
* 返回指定目标的属性值,properties方式
* @param target
* @param attribute
* @return
*/
private String getTargetValueP(String target, String attribute){
return (String)jxl.get("jxl." + target + "." + attribute);
}
}