java transformerfactory_Java TransformerFactory.setFeature方法代码示例

该博客介绍了如何在Java中使用TransformerFactory.setFeature方法,特别是针对ORACLE_ENABLE_EXTENSION_FUNCTION特征的设置。内容包括创建TransformerFactory实例,设置特征以启用扩展函数,并在XML转换过程中使用URIResolver来处理外部文档。测试用例展示了在处理包含'document'函数的XSLT样式表时,如何读取并验证外部文档的内容。
摘要由CSDN通过智能技术生成

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类

/**

* @bug 8062518 8153082

* Verifies that a reference to the DTM created by XSLT document function is

* actually read from the DTM by an extension function.

* @param xml Content of xml file to process

* @param xsl stylesheet content that loads external document {@code externalDoc}

* with XSLT 'document' function and then reads it with

* DocumentExtFunc.test() function

* @param externalDoc Content of the external xml document

* @param expectedResult Expected transformation result

**/

@Test(dataProvider = "document")

public void testDocument(final String xml, final String xsl,

final String externalDoc, final String expectedResult) throws Exception {

// Prepare sources for transormation

Source src = new StreamSource(new StringReader(xml));

Source xslsrc = new StreamSource(new StringReader(xsl));

// Create factory and transformer

TransformerFactory tf = TransformerFactory.newInstance();

tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);

tf.setAttribute(EXTENSION_CLASS_LOADER,

runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));

Transformer t = tf.newTransformer( xslsrc );

t.setErrorListener(tf.getErrorListener());

// Set URI Resolver to return the newly constructed xml

// stream source object from xml test string

t.setURIResolver(new URIResolver() {

@Override

public Source resolve(String href, String base)

throws TransformerException {

if (href.contains("externalDoc")) {

return new StreamSource(new StringReader(externalDoc));

} else {

return new StreamSource(new StringReader(xml));

}

}

});

// Prepare output stream

StringWriter xmlResultString = new StringWriter();

StreamResult xmlResultStream = new StreamResult(xmlResultString);

//Transform the xml

t.transform(src, xmlResultStream);

// If the document can't be accessed and the bug is in place then

// reported exception will be thrown during transformation

System.out.println("Transformation result:"+xmlResultString.toString().trim());

// Check the result - it should contain two (node name, node values) entries -

// one for original document, another for a document created with

// call to 'document' function

assertEquals(xmlResultString.toString().trim(), expectedResult);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值