gpt4 book ai didi

java - JAXP 撒克逊-he : XMLfile StreamSource doesn't release file access after parsing error

转载 作者:行者123 更新时间:2023-12-01 09:33:08 25 4
gpt4 key购买 nike

我正在使用 JAXP 规范 API 与 Saxon-HE API 相结合,主要目的是开发一个应用程序,该应用程序使用可配置的 XSLT 样式表转换 XML 文件,能够覆盖生成的输出文档。我跳过详细信息,因为我创建了一个示例项目来说明遇到的问题:

用例:如果出现转换错误,将 xml 文件移动到另一个目录(可能是错误目录)会引发访问异常。

当我基于 File 实例(指向 XML 文件)实例化 StreamSource 时,如果发生某些解析错误,移动文件会引发“该进程无法访问该文件,因为它正在被另一个进程使用。”异常(exception)。

这是我为说明问题而编写的一个主要单类应用程序:

package com.sample.xslt.application;

import net.sf.saxon.Configuration;
import net.sf.saxon.lib.FeatureKeys;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;

public class XsltApplicationSample {

public static void main(String[] args) throws Exception {

if (args.length != 2) {
throw new RuntimeException("Two arguments are expected : <xslFilePath> <inputFilePath>");
}
String xslFilePath = args[0];
String xmlFilePath = args[1];

TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(FeatureKeys.ALLOW_MULTITHREADING, Boolean.TRUE);
factory.setAttribute(FeatureKeys.RECOVERY_POLICY,
new Integer(Configuration.RECOVER_WITH_WARNINGS));

Source xslSource = new StreamSource(new File(xslFilePath));
Source xmlSource = new StreamSource(new File(xmlFilePath));
Transformer transformer = factory.newTransformer(xslSource);

try {
transformer.transform(xmlSource, new DOMResult());

} catch (TransformerException e) {
System.out.println(e.getMessage());
}

// move input file to tmp directory (for example, could be configured error dir)

File srcFile = Paths.get(xmlFilePath).toFile();
File tempDir = new File(System.getProperty("java.io.tmpdir"));

Path destFilePath = new File(tempDir, srcFile.getName()).toPath();

try {
Files.move(srcFile.toPath(), destFilePath, StandardCopyOption.REPLACE_EXISTING);
} catch (SecurityException | IOException e) {
System.out.println(e.getMessage());
}
}
}

配置的 xslt 转换文件内容必须有效才能重现。如果输入的xml文件为空,则会产生转换/解析错误,但不会发生访问文件错误。

要重现的输入文件示例:

<root>
<elem>
</root>

标准输出示例:

JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null
Error on line 3 column 3 of input_err.xml:
SXXP0003: Error reported by XML parser: The element type "elem" must be terminated by the
matching end-tag "</elem>".
org.xml.sax.SAXParseException; systemId: file:/C:/<path>/input_err.xml; lineNumber: 3; columnNumber: 3; The element type "elem" must be terminated by the matching end-tag "</elem>".
C:\<path>\input_err.xml -> C:\<path>\AppData\Local\Temp\input_err.xml: The process cannot access the file because it is being used by another process.

使用命令行(我使用 Eclipse):

java ... -Djaxp.debug=1 -Dfile.encoding=UTF-8 -classpath <...> com.sample.xslt.application.XsltApplicationSample C:\<path>\transform.xsl C:\<path>\input_err.xml

使用的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>XsltExampleProject</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>XsltExampleProject</name>
<description>XSLT example project</description>

<dependencies>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.7.0-7</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

我使用的解决方法是将 xml 输入文件的内容作为字符串加载到内存中,请参阅以下内容:

String xmlContent = FileUtils.readFileToString(new File(xmlFilePath), StandardCharsets.UTF_8);

Source xslSource = new StreamSource(new File(xslFilePath));
Source xmlSource = new StreamSource(new StringReader(xmlContent));

初始化 Transformer 时我错过了什么吗?默认解析的 SAX 解析器应该重写为 Saxon 推荐的另一个 API?我认为Xerces解析器是根据调试日志记录使用的,但是它与Saxon提供的变压器实现完全兼容吗?我对这个有点困惑..

感谢您的帮助!

最佳答案

从问题后面的评论线程来看,这似乎是 JDK 提供的 XML 解析器中的错误/缺陷。您的选择是:

(a) 报告错误并耐心等待修复

(b) 使用 Apache Xerces 解析器

(c) 不提供 File,而是提供 FileInputStream,然后自行关闭它。

我的建议是 (b),因为 Apache Xerces 解析器比 JDK 中的版本可靠得多。

关于java - JAXP 撒克逊-he : XMLfile StreamSource doesn't release file access after parsing error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com