NodeletParser

本文介绍了一种基于回调的XML解析器——Nodelet解析器的实现细节。该解析器为不同XML节点注册特定的处理程序(Nodelet),并支持多种XPath路径类型。文章详细解释了如何通过Reader或InputStream开始解析过程,并递归地遍历DOM树来调用相应的Nodelet。
package com.ibatis.common.xml;

import org.w3c.dom.*;
import org.xml.sax.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.*;


/**
* The NodeletParser is a callback based parser similar to SAX. The big
* difference is that rather than having a single callback for all nodes,
* the NodeletParser has a number of callbacks mapped to
* various nodes. The callback is called a Nodelet and it is registered
* with the NodeletParser against a specific XPath.
*/
public class NodeletParser {

private Map letMap = new HashMap();

private boolean validation;
private EntityResolver entityResolver;

/**
* Registers a nodelet for the specified XPath. Current XPaths supported
* are:
* <ul>
* <li> Text Path - /rootElement/childElement/text()
* <li> Attribute Path - /rootElement/childElement/@theAttribute
* <li> Element Path - /rootElement/childElement/theElement
* <li> All Elements Named - //theElement
* </ul>
*/

//璋冪敤璇ユ柟娉曪紝鍦℉ashMap绫诲瀷鐨刲etMap鍙橀噺澧炲姞涓�釜璺緞key鍜屼竴涓狽odelet瀵硅薄
public void addNodelet(String xpath, Nodelet nodelet) {
letMap.put(xpath, nodelet);
}

/**
* Begins parsing from the provided Reader.
*/
public void parse(Reader reader) throws NodeletException {
try {
Document doc = createDocument(reader);
parse(doc.getLastChild());
} catch (Exception e) {
throw new NodeletException("Error parsing XML. Cause: " + e, e);
}
}


public void parse(InputStream inputStream) throws NodeletException {
try {
Document doc = createDocument(inputStream);
//瑙f瀽XML鏂囦欢鐨勬牴鑺傜偣
parse(doc.getLastChild());
} catch (Exception e) {
throw new NodeletException("Error parsing XML. Cause: " + e, e);
}
}

/**
* Begins parsing from the provided Node.
*/
public void parse(Node node) {
Path path = new Path();
//澶勭悊node鑺傜偣褰㈡垚鐨刵odelet锛屽疄闄呬笂鏄皟鐢╪odelet鐨刾rocess鏂规硶
processNodelet(node, "/");
// 澶勭悊node鑺傜偣褰㈡垚鐨刵odelet锛屽疄闄呬笂鏄皟鐢╪odelet鐨刾rocess鏂规硶
process(node, path);
}

/**
* A recursive method that walkes the DOM tree, registers XPaths and
* calls Nodelets registered under those XPaths.
*/

//瀵笵OM褰㈡垚鐨勬爲杩涜閫掑綊鏂规硶璁块棶锛屾敞鍐孹Paths骞惰皟鐢ㄧ敱骞禭Path娉ㄥ唽鐢熸垚鐨凬odelets
private void process(Node node, Path path) {
if (node instanceof Element) {
// Element
//褰撹妭鐐规槸Element锛屽紑濮嬭皟鐢ㄨ鑺傜偣褰㈡垚鐨刵odelet鐨刾rocess鏂规硶
String elementName = node.getNodeName();
path.add(elementName);
processNodelet(node, path.toString());
processNodelet(node, new StringBuffer("//").append(elementName).toString());

// Attribute
//褰撹妭鐐规槸Attribute锛屽紑濮嬭皟鐢ㄨ鑺傜偣涓嬫墍鏈夊睘鎬у舰鎴愮殑nodelet鐨刾rocess鏂规硶
NamedNodeMap attributes = node.getAttributes();
int n = attributes.getLength();
for (int i = 0; i < n; i++) {
Node att = attributes.item(i);
String attrName = att.getNodeName();
path.add("@" + attrName);
processNodelet(att, path.toString());
processNodelet(node, new StringBuffer("//@").append(attrName).toString());
path.remove();
}

// Children
//瀵硅鑺傜偣涓嬬殑瀛愯妭鐐硅繘琛屽鐞嗭紝閲囩敤鐨勬槸閫掑綊绠楁硶
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
process(children.item(i), path);
}
path.add("end()");
processNodelet(node, path.toString());
path.remove();
path.remove();
} else if (node instanceof Text) {
// Text
// 褰撹妭鐐规槸Text锛屽紑濮嬭皟鐢ㄨ鑺傜偣褰㈡垚鐨刵odelet鐨刾rocess鏂规硶
path.add("text()");
processNodelet(node, path.toString());
processNodelet(node, "//text()");
path.remove();
}
}

//鏍规嵁璺緞鍚嶇О锛屾墽琛宯odelet鐨刾rocess鏂规硶锛屼紶鍏ョ殑鍙傛暟鏄疦ode鑺傜偣
private void processNodelet(Node node, String pathString) {
Nodelet nodelet = (Nodelet) letMap.get(pathString);
if (nodelet != null) {
try {
nodelet.process(node);
} catch (Exception e) {
throw new RuntimeException("Error parsing XPath '" + pathString + "'. Cause: " + e, e);
}
}
}

/**
* Creates a JAXP Document from a reader.
*/
private Document createDocument(Reader reader) throws ParserConfigurationException, FactoryConfigurationError,
SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validation);

factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);

DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(entityResolver);
builder.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
throw exception;
}

public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}

public void warning(SAXParseException exception) throws SAXException {
}
});

return builder.parse(new InputSource(reader));
}

/**
* Creates a JAXP Document from an InoutStream.
*/
private Document createDocument(InputStream inputStream) throws ParserConfigurationException, FactoryConfigurationError,
SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validation);

factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);

DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(entityResolver);
builder.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
throw exception;
}

public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}

public void warning(SAXParseException exception) throws SAXException {
}
});

return builder.parse(new InputSource(inputStream));
}

public void setValidation(boolean validation) {
this.validation = validation;
}

public void setEntityResolver(EntityResolver resolver) {
this.entityResolver = resolver;
}

/**
* Inner helper class that assists with building XPath paths.
* <p/>
* Note: Currently this is a bit slow and could be optimized.
*/
private static class Path {

private List nodeList = new ArrayList();

public Path() {
}

public Path(String path) {
StringTokenizer parser = new StringTokenizer(path, "/", false);
while (parser.hasMoreTokens()) {
nodeList.add(parser.nextToken());
}
}

public void add(String node) {
nodeList.add(node);
}

public void remove() {
nodeList.remove(nodeList.size() - 1);
}

public String toString() {
StringBuffer buffer = new StringBuffer("/");
for (int i = 0; i < nodeList.size(); i++) {
buffer.append(nodeList.get(i));
if (i < nodeList.size() - 1) {
buffer.append("/");
}
}
return buffer.toString();
}
}

}
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMap/resultMap'. Cause: java.lang.RuntimeException: Error configuring Result. Could not set ResultClass. Cause: java.lang.ClassNotFoundException: com.eagle.insure.db.model.PropKfcCompanyInsured at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:62) at com.ibatis.sqlmap.engine.builder.xml.SqlMapParser.parse(SqlMapParser.java:44) at org.springframework.orm.ibatis.SqlMapClientFactoryBean.buildSqlMapClient(SqlMapClientFactoryBean.java:351) ... 102 common frames omitted Caused by: java.lang.RuntimeException: Error parsing XPath '/sqlMap/resultMap'. Cause: java.lang.RuntimeException: Error configuring Result. Could not set ResultClass. Cause: java.lang.ClassNotFoundException: com.eagle.insure.db.model.PropKfcCompanyInsured at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:123) at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84) at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102) at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72) at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:60) ... 104 common frames omitted Caused by: java.lang.RuntimeException: Error configuring Result. Could not set ResultClass. Cause: java.lang.ClassNotFoundException: com.eagle.insure.db.model.PropKfcCompanyInsured at com.ibatis.sqlmap.engine.builder.xml.SqlMapParser$13.process(SqlMapParser.java:228) at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121) ... 108 common frames omitted Caused by: java.lang.ClassNotFoundException: com.eagle.insure.db.model.PropKfcCompanyInsured at java.net.URLClassLoader.findClass(URLClassLoader.java:387) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:371) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) at java.l
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值