当前位置: 首页>>代码示例>>Java>>正文


Java TransformerFactory.setFeature方法代码示例

本文整理汇总了Java中javax.xml.transform.TransformerFactory.setFeature方法的典型用法代码示例。如果您正苦于以下问题:Java TransformerFactory.setFeature方法的具体用法?Java TransformerFactory.setFeature怎么用?Java TransformerFactory.setFeature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.xml.transform.TransformerFactory的用法示例。


在下文中一共展示了TransformerFactory.setFeature方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testRedirect

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * @bug 8165116
 * Verifies that redirect works properly when extension function is enabled
 *
 * @param xml the XML source
 * @param xsl the stylesheet that redirect output to a file
 * @param output the output file
 * @param redirect the redirect file
 * @throws Exception if the test fails
 **/
@Test(dataProvider = "redirect")
public void testRedirect(String xml, String xsl, String output, String redirect) throws Exception {

    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
    Transformer t = tf.newTransformer(new StreamSource(new StringReader(xsl)));

    //Transform the xml
    tryRunWithTmpPermission(
            () -> t.transform(new StreamSource(new StringReader(xml)), new StreamResult(new StringWriter())),
            new FilePermission(output, "write"), new FilePermission(redirect, "write"));

    // Verifies that the output is redirected successfully
    String userDir = getSystemProperty("user.dir");
    Path pathOutput = Paths.get(userDir, output);
    Path pathRedirect = Paths.get(userDir, redirect);
    Assert.assertTrue(Files.exists(pathOutput));
    Assert.assertTrue(Files.exists(pathRedirect));
    System.out.println("Output to " + pathOutput + " successful.");
    System.out.println("Redirect to " + pathRedirect + " successful.");
    Files.deleteIfExists(pathOutput);
    Files.deleteIfExists(pathRedirect);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:XSLTFunctionsTest.java

示例2: createTransformerFactory

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * Returns properly configured (e.g. security features) factory
 * - securityProcessing == is set based on security processing property, default is true
 */
public static TransformerFactory createTransformerFactory(boolean disableSecureProcessing) throws IllegalStateException {
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.log(Level.FINE, "TransformerFactory instance: {0}", factory);
        }
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !isXMLSecurityDisabled(disableSecureProcessing));
        return factory;
    } catch (TransformerConfigurationException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
        throw new IllegalStateException( ex);
    } catch (AbstractMethodError er) {
        LOGGER.log(Level.SEVERE, null, er);
        throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:XmlFactory.java

示例3: getTransformerFactory

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * Returns an instance of TransformerFactory with either a custom URIResolver
 * or Catalog.
 *
 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
 * through the factory
 * @param useCatalog the value of USE_CATALOG
 * @param catalog a catalog
 * @param resolver a custom resolver
 * @return an instance of TransformerFactory
 * @throws Exception
 */
TransformerFactory getTransformerFactory(boolean setUseCatalog, boolean useCatalog,
        String catalog, URIResolver resolver)
        throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    if (setUseCatalog) {
        factory.setFeature(XMLConstants.USE_CATALOG, useCatalog);
    }
    if (catalog != null) {
        factory.setAttribute(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
    }

    // use resolver or catalog if resolver = null
    if (resolver != null) {
        factory.setURIResolver(resolver);
    }

    return factory;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:CatalogSupportBase.java

示例4: testTransform_DOM_withSM

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Test(enabled=false) //skipped due to bug JDK-8080097
public void testTransform_DOM_withSM() {
    System.out.println("Transform using DOM Source;  Security Manager is set:");
    setSystemProperty(DOM_FACTORY_ID, "MyDOMFactoryImpl");

    try {
        TransformerFactory factory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
                TransformerFactory.class.getClassLoader());
        factory.setFeature(ORACLE_FEATURE_SERVICE_MECHANISM, true);
        if ((boolean) factory.getFeature(ORACLE_FEATURE_SERVICE_MECHANISM)) {
            Assert.fail("should not override in secure mode");
        }

    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        clearSystemProperty(DOM_FACTORY_ID);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:Bug7143711Test.java

示例5: newTransformerFactory

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public static TransformerFactory newTransformerFactory(boolean secureXmlProcessingEnabled) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessingEnabled));
    } catch (TransformerConfigurationException e) {
        LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()});
    }
    return factory;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:XmlUtil.java

示例6: addParameters

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * Adds the parameters in the Target XML.
 * 
 * @param out
 *            the out
 * @return the byte array output stream
 */
public ByteArrayOutputStream addParameters(ByteArrayOutputStream out) {
	ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
	try {
		XPath xPath = createXPathInstance(inputStream, null);
		LOGGER.debug("GENRATED COMPONENTS XPATH {}", getXpathMap().toString());
		for (Map.Entry<String, ComponentsAttributeAndValue> entry : getXpathMap().entrySet()) {
			NodeList nodeList = (NodeList) xPath.compile(entry.getKey()).evaluate(doc, XPathConstants.NODESET);
			if(entry.getValue().isNewNode())
				addParameterAsNewNode(entry, nodeList,entry.getValue().hasEmptyNode());
			else
				addParameterAsAttribute(entry, nodeList);
		}
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		out.reset();

		StreamResult result = new StreamResult(out);
		transformer.transform(source, result);
		getXpathMap().clear();

	} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException
			| TransformerException e) {
		LOGGER.error("Exception occurred while parametrizing the XML", e);
	} finally {
		try {
			inputStream.close();
		} catch (IOException ioe) {
			LOGGER.error("Exception occurred while closing input stream", ioe);
		}
	}

	return out;
}
 
开发者ID:capitalone,项目名称:Hydrograph,代码行数:43,代码来源:ComponentXpath.java

示例7: newTransformerFactory

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public static TransformerFactory newTransformerFactory(boolean disableSecurity) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !xmlSecurityDisabled(disableSecurity));
    } catch (TransformerConfigurationException e) {
        LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{factory.getClass().getName()});
    }
    return factory;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:XmlUtil.java

示例8: testDocument

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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:57,代码来源:XSLTFunctionsTest.java


注:本文中的javax.xml.transform.TransformerFactory.setFeature方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。