2016-08-30 66 views
0

我使用JAXP规范API和Saxon-HE API,主要目的是开发一个应用程序,它使用可配置的XSLT样式表来转换XML文件,能够覆盖生成的输出文件。我跳过的细节,因为我创建了一个示例项目说明遇到的问题:JAXP saxon-he:XMLfile StreamSource在解析错误后不释放文件访问

使用案例:在转换错误的情况下,XML文件移动到另一个目录(可能是错误的目录)引发访问异常。

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

这里是一个主要的单级的应用程序,我写来说明这个问题:

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文件为空,它将创建转换/解析错误,但不会发生访问文件错误。输入文件的

实施例重现:STDOUT的

<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. 

使用命令行(I使用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)); 

我怀念的东西,而初始化变压器? 默认解析的SAX解析器应该优先于Saxon推荐的另一个API?我认为根据调试日志记录使用Xerces解析器,但它是否与Saxon提供的变压器实现完全兼容? 我对这个有点困惑..

感谢您的帮助!

+0

我会做进一步的检查,但我非常确定,在这种情况下,Saxon将源代码作为URI传递给XML解析器,所以流由XML解析器打开,因此应该由XML解析器关闭;撒克逊人从来没有看到这条河,所以没有机会关闭它。尝试使用Apache Xerces而不是JDK版本的Xerces; Apache版本几乎总是更可靠。或者,传递FileInputStream,然后在finally {}块中自行关闭它。 –

+0

我在pom.xml中添加了依赖xercesImpl版本2.11.0,并且该错误不会发生。所以它来自JDK附带的版本我使用:java版本“1.8.0_92”/ Java(TM)SE运行时环境(版本1.8.0_92-b14)/ Java HotSpot™64位服务器VM(版本25.92 -b14,混合模式) – PacDroid

回答

1

从问题后面的注释线程看来,它似乎是随JDK提供的XML解析器中的缺陷/缺陷。您的选项是:

(一)报告错误并等待很耐心地为它被固定

(二)使用Apache Xerces解析器,而不是

(C),而不是提供文件,供一个FileInputStream,并自己关闭它。

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

+0

正如你所做的那样,我选择了解决方案(b),但我也会报告错误 – PacDroid