NodeletParser

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
}
}

}
标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
资源下载链接为: https://pan.quark.cn/s/9648a1f24758 Java中将Word文档转换为PDF是一种常见的技术需求,尤其在跨平台共享、保持格式一致性和便于在线预览等场景中非常实用。通常,开发者会借助专门的库来实现这一功能,其中Aspose.Words是一个非常强大的选择。Aspose.Words是由Aspose公司开发的文档处理组件,支持多种文件格式,包括Word和PDF。它提供了丰富的API,方便开发者在Java应用程序中进行文件转换、编辑和格式化操作,尤其在Word转PDF方面表现卓越。 使用Aspose.Words进行Word转PDF的步骤如下: 添加依赖:通过Maven或Gradle等工具将Aspose.Words的Java库引入项目。 加载Word文档:使用Document类加载Word文件,例如: 配置输出选项:创建PdfSaveOptions对象,用于设置PDF保存时的选项,如图像质量、安全性等。 执行转换:调用Document的save方法,传入输出路径和PdfSaveOptions对象,例如: 支持多种输出格式:Aspose.Words不仅支持将Word转换为PDF,还能转换为HTML、EPUB、XPS等多种格式,只需更换SaveOptions的子类即可。 保持格式与样式:在转换过程中,Aspose.Words能够最大程度地保留源文档的格式和样式,包括文本样式、图像位置、表格布局等。 优化性能:Aspose.Words支持并行处理和多线程技术,可以显著提高大量文档转换的速度。 处理复杂文档:它能够处理包含宏、复杂公式、图表、脚注等元素的Word文档,确保转换后的PDF内容完整且可读。 安全性与版权:在转换过程中,可以设置PDF的访问权限,例如禁止打印或复制文本,从而保护文档内容。 在实际开发中,还需要注意错误和异常的处理,以
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值