📄 configureutility.java
字号:
package org.speedframework.utilities;
import org.apache.log4j.Logger;
import org.speedframework.exception.SpeedFrameworkException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* Class ConfigureUtility
*
* 配置文件解析工具类,提供一系列对Document相关对象的解析
*
* @author <a href="mailto:lzfxp@126.com"> lzfxp </a>
* @version $Revision:1.0.0, $Date: Oct 9, 2007 3:45:36 PM $
*/
public class ConfigureUtility {
/** 日志 */
private static Logger log = Logger.getLogger(ConfigureUtility.class);
/**
* 返回指定配置文件的Document对象
*
* @param path
* String
*
* @return Document
*/
public static Document buildDocument(String path) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
Document doc;
DocumentBuilder builder = factory.newDocumentBuilder();
log.debug("Construct document builder success.");
doc = builder.parse(getResourceAsStream(path));
log.debug("Build xml document success.");
return doc;
} catch (ParserConfigurationException e) {
log.error("Construct document builder error:" + e);
} catch (SAXException e) {
log.error("Parse xml file error:" + e);
} catch (IOException e) {
log.error("Read xml file error:" + e);
}
return null;
}
/**
* 返回指定Document的根元素
*
* @param doc
* Document
*
* @return Element
*/
public static Element getRoot(Document doc) {
return doc.getDocumentElement();
}
/**
* 返回指定元素的指定属性值
*
* @param element
* Element
* @param attr
* String
*
* @return String
*/
public static String getElementAttr(Element element, String attr) {
return element.getAttribute(attr);
}
/**
* 返回指定元素的名字
*
* @param element
* Element
*
* @return String
*/
public static String getElementName(Element element) {
return element.getNodeName();
}
/**
* 返回指定元素的某个子节点列
*
* @param parent
* Element
* @param name
* String
*
* @return NodeList
*/
public static NodeList getElementsByName(Element parent, String name) {
return parent.getElementsByTagName(name);
}
/**
* 返回指定元素的值
*
* @param element
* Element
*
* @return String
*/
public static String getElementValue(Element element) {
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
// log.debug(element.getNodeName() + " has a Text Node.");
// return element.getFirstChild().getNodeValue();
//处理字符空格不能正确识别问题
return element.getFirstChild().getNodeValue().trim();
}
}
// log.error(element.getNodeName() + " hasn't a Text Node.");
return null;
}
/**
* 返回指定元素的子节点列
*
* @param parent
* Element
*
* @return NodeList
*/
public static NodeList getNodeList(Element parent) {
return parent.getChildNodes();
}
/**
*
*
* @param sourceFile
*
* @return
*
* @throws SpeedFrameworkException
*/
private static InputStream getResourceAsStream(String sourceFile)
throws SpeedFrameworkException {
String stripped = sourceFile.startsWith("/") ? sourceFile.substring(1)
: sourceFile;
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader != null) {
stream = classLoader.getResourceAsStream(stripped);
}
if (stream == null) {
ConfigureUtility.class.getResourceAsStream(sourceFile);
}
if (stream == null) {
stream = ConfigureUtility.class.getResourceAsStream(stripped);
}
if (stream == null) {
throw new SpeedFrameworkException(sourceFile + " not found");
}
return stream;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -