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


Java TransformerHandler.setResult方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: 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

示例7: 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

示例8: 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

示例9: 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

示例10: 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

示例11: 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

示例12: testStx

import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
@Test
@Ignore
public void testStx() throws Exception{
    TransformerFactory tFactory = new TransformerFactoryImpl();
    SAXTransformerFactory saxTFactory = (SAXTransformerFactory) tFactory;

    // of course the transformation source must be different
    TransformerHandler tHandler1 =
            saxTFactory.newTransformerHandler(new StreamSource("trans.stx"));
    XMLReader myReader = XMLReaderFactory.createXMLReader();
    myReader.setContentHandler(tHandler1);


    ContentHandler mySerializer = null;
    tHandler1.setResult(new SAXResult(mySerializer));
}
 
开发者ID:tblsoft,项目名称:solr-cmd-utils,代码行数:17,代码来源:JaxbTest.java

示例13: JDependProcessorXML2

import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
/**
 * Constructs a <code>JDepend</code> instance with the specified writer.
 * Creates a new instance of JDependProcessorXML
 *
 *
 * @param writer Writer.
 */
public JDependProcessorXML2(PrintWriter writer)  {
    super(writer);
    
    formatter = NumberFormat.getInstance(Locale.ENGLISH);
    formatter.setMaximumFractionDigits(2);
    
    try {
        SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler hd = tf.newTransformerHandler();
        Transformer serializer = hd.getTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT,"yes");
        
        // use getWriter as transform result destination
        hd.setResult(new StreamResult(this.getWriter()) );
        
        // create the XmlEmitter
        this.xmlEmitter = new XmlEmitter( hd );
    } catch (TransformerConfigurationException tce) {
        throw new XmlEmitterException(tce);
    }
}
 
开发者ID:bernhardhuber,项目名称:netbeansplugins,代码行数:29,代码来源:JDependProcessorXML2.java

示例14: getHtmlFromFile

import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
/**
 * Ritorna una vista HTML sul file processato. L'estrazione del testo fa
 * perdere di fatto la formattazione contenuta nel word o nel PDF. La
 * versione HTML è una versione processabile ma che mantiene il formato
 *
 * @param file file da parsare
 * @return vista HTML formattato del contenuto del documento (privato delle
 * immagini)
 */
public String getHtmlFromFile(File file) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler handler = factory.newTransformerHandler();
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
        handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        handler.setResult(new StreamResult(out));
        ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler);
        InputStream is = file.toURI().toURL().openStream();
        adp.parse(is, handler1, new Metadata());
        is.close();
        return new String(out.toByteArray(), "UTF-8").replaceAll("<img .*?</img>", "").replaceAll("<img .*?/>", "");
    } catch (TransformerConfigurationException | IllegalArgumentException | IOException | SAXException | TikaException ex) {
        return "!ERROR: " + ex.getLocalizedMessage();
    }
}
 
开发者ID:fiohol,项目名称:theSemProject,代码行数:28,代码来源:DocumentParser.java

示例15: getJAXBObject

import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
private T getJAXBObject(MCRContent source, XMLReader reader, TransformerHandler transformerHandler)
    throws JAXBException, IOException, SAXException {
    checkContext();
    JAXBResult result = new JAXBResult(context);
    transformerHandler.setResult(result);
    // Parse the source XML, and send the parse events to the
    // TransformerHandler.
    reader.parse(source.getInputSource());
    Object parsedResult = result.getResult();
    if (parsedResult instanceof JAXBElement<?>) {
        @SuppressWarnings("unchecked")
        JAXBElement<T> jaxbElement = (JAXBElement<T>) parsedResult;
        return jaxbElement.getValue();
    }
    @SuppressWarnings("unchecked")
    T jaxbResult = (T) result.getResult();
    return jaxbResult;
}
 
开发者ID:MyCoRe-Org,项目名称:mycore,代码行数:19,代码来源:MCRXSL2JAXBTransformer.java


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