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


Java SAXTransformerFactory类代码示例

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


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

示例1: testcase08

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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: convertWordDocumentIntoHtml

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
/**
 * Converts a .docx document into HTML markup. This code
 * is based on <a href="http://stackoverflow.com/a/9053258/313554">this StackOverflow</a> answer.
 *
 * @param wordDocument  The converted .docx document.
 * @return
 */
public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) {
    LOGGER.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename());
    try {
        InputStream input = wordDocument.getInputStream();
        Parser parser = new OOXMLParser();

        StringWriter sw = new StringWriter();
        SAXTransformerFactory factory = (SAXTransformerFactory)
                SAXTransformerFactory.newInstance();
        TransformerHandler handler = factory.newTransformerHandler();
        handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8");
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
        handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        handler.setResult(new StreamResult(sw));

        Metadata metadata = new Metadata();
        metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8");
        parser.parse(input, handler, metadata, new ParseContext());
        return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString());
    }
    catch (IOException | SAXException | TransformerException | TikaException ex) {
        LOGGER.error("Conversion failed because an exception was thrown", ex);
        throw new DocumentConversionException(ex.getMessage(), ex);
    }
}
 
开发者ID:Vincit,项目名称:spring-boot-word-to-html-example,代码行数:33,代码来源:WordToHtmlConverter.java

示例3: xsltprocess

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
public void xsltprocess(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
    // 1. Instantiate a TransformerFactory.
    SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    
    // 2. Use the TransformerFactory to process the stylesheet Source and
    //    generate a Transformer.
    InputStream is = getClass().getResourceAsStream("xmg2pol.xsl");
    Transformer transformer = tFactory.newTransformer (new StreamSource(is));
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "polarities.dtd,xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    
    // 3. Use the Transformer to transform an XML Source and send the
    //    output to a Result object.
    try {
	    String input = args[0];
	    String output= args[1];
	    SAXSource saxs = new SAXSource(new InputSource(input));
		XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
		saxReader.setEntityResolver(new MyEntityResolver());
		saxs.setXMLReader(saxReader);
	    transformer.transform(saxs, new StreamResult(new OutputStreamWriter(new FileOutputStream(output), "utf-8")));
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
}
 
开发者ID:spetitjean,项目名称:TuLiPA-frames,代码行数:26,代码来源:TransformPolarity.java

示例4: saveWithCustomIndetation

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
/**
 * Saves the xml, contained by the specified input with the custom indentation.
 * If the input is the result of jaxb marshalling, make sure to set
 * Marshaller.JAXB_FORMATTED_OUTPUT to false in order for this method to work
 * properly.
 * 
 * @param input
 * @param fos
 * @param indentation
 */
public static void saveWithCustomIndetation(ByteArrayInputStream input, FileOutputStream fos, int indentation) {
  try {
    Transformer transformer = SAXTransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentation));
    Source xmlSource = new SAXSource(new org.xml.sax.InputSource(input));
    StreamResult res = new StreamResult(fos);
    transformer.transform(xmlSource, res);
    fos.flush();
    fos.close();
  } catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
    log.log(Level.SEVERE, e.getMessage(), e);
  }
}
 
开发者ID:gurkenlabs,项目名称:litiengine,代码行数:26,代码来源:XmlUtilities.java

示例5: createTransformerHandler

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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

示例6: getSerializer

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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

示例7: buildSingleTransformerPipeline

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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

示例8: toSAXResult

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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

示例9: SchemaToXML

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
public SchemaToXML(Schema schema, StreamResult streamResult)
{
    final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
    try
    {
        xmlOut = stf.newTransformerHandler();
    }
    catch (TransformerConfigurationException error)
    {
        throw new RuntimeException("Unable to create TransformerHandler.", error);
    }
    final Transformer t = xmlOut.getTransformer();
    try
    {
        t.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
    }
    catch (final IllegalArgumentException e)
    {
        // It was worth a try
    }
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.ENCODING, SchemaComparator.CHAR_SET);
    xmlOut.setResult(streamResult);
    
    this.schema = schema;
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:27,代码来源:SchemaToXML.java

示例10: format

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
@Override
public String format(String response) {
  try {
    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(response.getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);
    return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).trim();
  } catch (Exception e) {
    e.printStackTrace();
    Logger.e(TAG, e.getMessage(), e);
    return response;
  }
}
 
开发者ID:jainsahab,项目名称:AndroidSnooper,代码行数:18,代码来源:XmlFormatter.java

示例11: identityTransform

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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

示例12: identityTransform

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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

示例13: test3

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
@Test
public void test3() throws Exception {
    SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    Transformer t = sf.newTransformer();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder parser = dbf.newDocumentBuilder();
    Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml"));

    DOMResult r = new DOMResult();

    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());

    Node n = r.getNode().getFirstChild();
    r.setNode(n);
    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());
    Assert.assertSame(r.getNode(), n);

    r.setNextSibling(r.getNode().getFirstChild());
    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());
    Assert.assertSame(r.getNode(), n);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:Bug5072946.java

示例14: test03

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的package包/类
@Test
public void test03() {
    String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n"
            + "   <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n";

    ReaderStub.used = false;
    setSystemProperty("org.xml.sax.driver", ReaderStub.class.getName());
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        if (transFactory.getFeature(SAXTransformerFactory.FEATURE) == false) {
            System.out.println("SAXTransformerFactory not supported");
        }
        InputSource in = new InputSource(new StringReader(xsl));
        SAXSource source = new SAXSource(in);

        transFactory.newTransformer(source);
        Assert.assertTrue(printWasReaderStubCreated());
    } catch (TransformerException e) {
        Assert.fail(e.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:Bug6490921.java

示例15: testXMLStackOverflowBug

import javax.xml.transform.sax.SAXTransformerFactory; //导入依赖的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


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