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


Java TransformerHandler类代码示例

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


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

示例1: testcase08

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * Test newTransformerHandler with a Template Handler.
 *
 * @throws Exception If any errors occur.
 */
public void testcase08() throws Exception {
    String outputFile = USER_DIR + "saxtf008.out";
    String goldFile = GOLDEN_DIR + "saxtf008GF.out";

    try (FileOutputStream fos = new FileOutputStream(outputFile)) {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory)TransformerFactory.newInstance();

        TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
        reader.setContentHandler(thandler);
        reader.parse(XSLT_FILE);
        TransformerHandler tfhandler
                = saxTFactory.newTransformerHandler(thandler.getTemplates());

        Result result = new StreamResult(fos);
        tfhandler.setResult(result);

        reader.setContentHandler(tfhandler);
        reader.parse(XML_FILE);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:SAXTFactoryTest.java

示例2: createTransformerHandler

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
public static TransformerHandler createTransformerHandler(final ErrorListener errorListener,
                                                          final boolean indentOutput) throws TransformerConfigurationException {
    final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactoryFactory.newInstance();
    if (errorListener != null) {
        stf.setErrorListener(errorListener);
    }

    final TransformerHandler th = stf.newTransformerHandler();
    final Transformer transformer = th.getTransformer();
    setCommonOutputProperties(transformer, indentOutput);

    if (errorListener != null) {
        transformer.setErrorListener(errorListener);
    }

    return th;
}
 
开发者ID:gchq,项目名称:stroom-stats,代码行数:18,代码来源:XMLUtil.java

示例3: marshal

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
public static <T> String marshal(final JAXBContext context, final T obj, final XmlAdapter<?, ?>... adapters) {
    if (obj == null) {
        return null;
    }

    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final Marshaller marshaller = context.createMarshaller();

        if (adapters != null) {
            for (final XmlAdapter<?, ?> adapter : adapters) {
                marshaller.setAdapter(adapter);
            }
        }

        final TransformerHandler transformerHandler = XMLUtil.createTransformerHandler(true);
        transformerHandler.setResult(new StreamResult(out));
        marshaller.marshal(obj, transformerHandler);

        return out.toString(String.valueOf(StandardCharsets.UTF_8));
    } catch (final Throwable t) {
        throw new RuntimeException(t.getMessage(), t);
    }
}
 
开发者ID:gchq,项目名称:stroom-stats,代码行数:25,代码来源:XMLMarshallerUtil.java

示例4: getSerializer

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
@Override
public TransformerHandler getSerializer()
{
	try
	{
		SAXTransformerFactory transformerFactory = buildTransformerFactory();
		configure(transformerFactory);
		TransformerHandler transformer = buildTransformer(transformerFactory);
		configure(transformer.getTransformer());
		return transformer;
	}
	catch (TransformerConfigurationException ex)
	{
		throw new FluentXmlConfigurationException(ex);
	}
}
 
开发者ID:fluentxml4j,项目名称:fluentxml4j,代码行数:17,代码来源:SerializerConfigurerAdapter.java

示例5: buildSingleTransformerPipeline

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
private Result buildSingleTransformerPipeline(Result result)
{
	try
	{
		SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
		TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
		if (result != null)
		{
			transformerHandler.setResult(result);
		}
		return new SAXResult(transformerHandler);
	}
	catch (TransformerConfigurationException ex)
	{
		throw new FluentXmlConfigurationException(ex);
	}
}
 
开发者ID:fluentxml4j,项目名称:fluentxml4j,代码行数:18,代码来源:TransformationChain.java

示例6: toSAXResult

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
private SAXResult toSAXResult(Result result)
{
	if (result instanceof SAXResult)
	{
		return (SAXResult) result;
	}

	try
	{
		SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
		TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
		transformerHandler.setResult(result);
		return new SAXResult(transformerHandler);
	}
	catch (TransformerConfigurationException ex)
	{
		throw new FluentXmlConfigurationException(ex);
	}
}
 
开发者ID:fluentxml4j,项目名称:fluentxml4j,代码行数:20,代码来源:AbstractSAXFilter.java

示例7: identityTransform

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * Performs identity transformation.
 */
public static <T extends Result>
T identityTransform(Source src, T result) throws TransformerException, SAXException, ParserConfigurationException, IOException {
    if (src instanceof StreamSource) {
        // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
        // is not turned on by default
        StreamSource ssrc = (StreamSource) src;
        TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler();
        th.setResult(result);
        XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader();
        reader.setContentHandler(th);
        reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
        reader.parse(toInputSource(ssrc));
    } else {
        newTransformer().transform(src, result);
    }
    return result;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:21,代码来源:XmlUtil.java

示例8: identityTransform

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * Performs identity transformation.
 * @param <T>
 * @param src
 * @param result
 * @return
 * @throws javax.xml.transform.TransformerException
 * @throws java.io.IOException
 * @throws org.xml.sax.SAXException
 * @throws javax.xml.parsers.ParserConfigurationException
 */
public static <T extends Result> T identityTransform(Source src, T result)
        throws TransformerException, SAXException, ParserConfigurationException, IOException {
    if (src instanceof StreamSource) {
        // work around a bug in JAXP in JDK6u4 and earlier where the namespace processing
        // is not turned on by default
        StreamSource ssrc = (StreamSource) src;
        TransformerHandler th = ((SAXTransformerFactory) transformerFactory.get()).newTransformerHandler();
        th.setResult(result);
        XMLReader reader = saxParserFactory.get().newSAXParser().getXMLReader();
        reader.setContentHandler(th);
        reader.setProperty(LEXICAL_HANDLER_PROPERTY, th);
        reader.parse(toInputSource(ssrc));
    } else {
        newTransformer().transform(src, result);
    }
    return result;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:XmlUtil.java

示例9: testXMLStackOverflowBug

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
@Test
public final void testXMLStackOverflowBug() throws TransformerConfigurationException, IOException, SAXException {
    try {
        SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
        TransformerHandler ser = stf.newTransformerHandler();
        ser.setResult(new StreamResult(System.out));

        StringBuilder sb = new StringBuilder(4096);
        for (int x = 4096; x > 0; x--) {
            sb.append((char) x);
        }
        ser.characters(sb.toString().toCharArray(), 0, sb.toString().toCharArray().length);
        ser.endDocument();
    } catch (StackOverflowError se) {
        se.printStackTrace();
        Assert.fail("StackOverflow");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:OpenJDK100017Test.java

示例10: test

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
@Test
public void test() throws Exception {
    TransformerHandler th = ((SAXTransformerFactory) TransformerFactory.newInstance()).newTransformerHandler();

    DOMResult result = new DOMResult();
    th.setResult(result);

    th.startDocument();
    th.startElement("", "root", "root", new AttributesImpl());
    th.characters(new char[0], 0, 0);
    th.endElement("", "root", "root");
    th.endDocument();

    // there's no point in having empty text --- we should remove it
    Assert.assertEquals(0, ((Document) result.getNode()).getDocumentElement().getChildNodes().getLength());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:Bug6451633.java

示例11: testcase01

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * SAXTFactory.newTransformerhandler() method which takes SAXSource as
 * argument can be set to XMLReader. SAXSource has input XML file as its
 * input source. XMLReader has a transformer handler which write out the
 * result to output file. Test verifies output file is same as golden file.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase01() throws Exception {
    String outputFile = USER_DIR + "saxtf001.out";
    String goldFile = GOLDEN_DIR + "saxtf001GF.out";

    try (FileOutputStream fos = new FileOutputStream(outputFile)) {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory) TransformerFactory.newInstance();
        TransformerHandler handler = saxTFactory.newTransformerHandler(new StreamSource(XSLT_FILE));
        Result result = new StreamResult(fos);
        handler.setResult(result);
        reader.setContentHandler(handler);
        reader.parse(XML_FILE);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:SAXTFactoryTest.java

示例12: testcase02

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * SAXTFactory.newTransformerhandler() method which takes SAXSource as
 * argument can be set to XMLReader. SAXSource has input XML file as its
 * input source. XMLReader has a content handler which write out the result
 * to output file. Test verifies output file is same as golden file.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase02() throws Exception {
    String outputFile = USER_DIR + "saxtf002.out";
    String goldFile = GOLDEN_DIR + "saxtf002GF.out";

    try (FileOutputStream fos = new FileOutputStream(outputFile);
            FileInputStream fis = new FileInputStream(XSLT_FILE)) {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory) TransformerFactory.newInstance();
        SAXSource ss = new SAXSource();
        ss.setInputSource(new InputSource(fis));

        TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
        Result result = new StreamResult(fos);
        handler.setResult(result);
        reader.setContentHandler(handler);
        reader.parse(XML_FILE);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:SAXTFactoryTest.java

示例13: testcase03

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * Unit test for newTransformerhandler(Source). DcoumentBuilderFactory is
 * namespace awareness, DocumentBuilder parse xslt file as DOMSource.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase03() throws Exception {
    String outputFile = USER_DIR + "saxtf003.out";
    String goldFile = GOLDEN_DIR + "saxtf003GF.out";

    try (FileOutputStream fos = new FileOutputStream(outputFile)) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document document = docBuilder.parse(new File(XSLT_FILE));
        Node node = (Node)document;
        DOMSource domSource= new DOMSource(node);

        XMLReader reader = XMLReaderFactory.createXMLReader();
        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory)TransformerFactory.newInstance();
        TransformerHandler handler =
                    saxTFactory.newTransformerHandler(domSource);
        Result result = new StreamResult(fos);
        handler.setResult(result);
        reader.setContentHandler(handler);
        reader.parse(XML_FILE);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:SAXTFactoryTest.java

示例14: testcase09

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
/**
 * Test newTransformerHandler with a Template Handler along with a relative
 * URI in the style-sheet file.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase09() throws Exception {
    String outputFile = USER_DIR + "saxtf009.out";
    String goldFile = GOLDEN_DIR + "saxtf009GF.out";

    try (FileOutputStream fos = new FileOutputStream(outputFile)) {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory)TransformerFactory.newInstance();

        TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
        thandler.setSystemId("file:///" + XML_DIR);
        reader.setContentHandler(thandler);
        reader.parse(XSLT_INCL_FILE);
        TransformerHandler tfhandler=
            saxTFactory.newTransformerHandler(thandler.getTemplates());
        Result result = new StreamResult(fos);
        tfhandler.setResult(result);
        reader.setContentHandler(tfhandler);
        reader.parse(XML_FILE);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:SAXTFactoryTest.java

示例15: getTransformerHandler

import javax.xml.transform.sax.TransformerHandler; //导入依赖的package包/类
@Override
protected TransformerHandler getTransformerHandler(String xslFileName) throws SAXException, ParserConfigurationException,
        TransformerConfigurationException, IOException {
    SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    factory.setURIResolver(uriResolver);

    TemplatesHandler templatesHandler = factory.newTemplatesHandler();

    SAXParserFactory pFactory = SAXParserFactory.newInstance();
    pFactory.setNamespaceAware(true);

    XMLReader xmlreader = pFactory.newSAXParser().getXMLReader();

    // create the stylesheet input source
    InputSource xslSrc = new InputSource(xslFileName);

    xslSrc.setSystemId(filenameToURL(xslFileName));
    // hook up the templates handler as the xsl content handler
    xmlreader.setContentHandler(templatesHandler);
    // call parse on the xsl input source

    xmlreader.parse(xslSrc);

    // extract the Templates object created from the xsl input source
    return factory.newTransformerHandler(templatesHandler.getTemplates());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:TemplatesFilterFactoryImpl.java


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