當前位置: 首頁>>代碼示例>>Java>>正文


Java SAXTransformerFactory.newTemplatesHandler方法代碼示例

本文整理匯總了Java中javax.xml.transform.sax.SAXTransformerFactory.newTemplatesHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java SAXTransformerFactory.newTemplatesHandler方法的具體用法?Java SAXTransformerFactory.newTemplatesHandler怎麽用?Java SAXTransformerFactory.newTemplatesHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.transform.sax.SAXTransformerFactory的用法示例。


在下文中一共展示了SAXTransformerFactory.newTemplatesHandler方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: testcase09

import javax.xml.transform.sax.SAXTransformerFactory; //導入方法依賴的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

示例3: testcase13

import javax.xml.transform.sax.SAXTransformerFactory; //導入方法依賴的package包/類
/**
 * Unit test for TemplatesHandler setter/getter.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase13() throws Exception {
    String outputFile = USER_DIR + "saxtf013.out";
    String goldFile = GOLDEN_DIR + "saxtf013GF.out";
    try(FileInputStream fis = new FileInputStream(XML_FILE)) {
        // The transformer will use a SAX parser as it's reader.
        XMLReader reader = XMLReaderFactory.createXMLReader();

        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory) TransformerFactory.newInstance();
        TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
        // I have put this as it was complaining about systemid
        thandler.setSystemId("file:///" + USER_DIR);

        reader.setContentHandler(thandler);
        reader.parse(XSLT_FILE);
        XMLFilter filter
                = saxTFactory.newXMLFilter(thandler.getTemplates());
        filter.setParent(reader);

        filter.setContentHandler(new MyContentHandler(outputFile));
        filter.parse(new InputSource(fis));
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:SAXTFactoryTest.java

示例4: getTransformerHandler

import javax.xml.transform.sax.SAXTransformerFactory; //導入方法依賴的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

示例5: exampleSAXTransformerFactory

import javax.xml.transform.sax.SAXTransformerFactory; //導入方法依賴的package包/類
/**
 * Show the Transformer using TemplatesHandler
 */
public static boolean exampleSAXTransformerFactory (final String sourceID,
                                                    final String stxID) throws TransformerException,
                                                                        TransformerConfigurationException,
                                                                        SAXException,
                                                                        IOException
{

  final TransformerFactory tfactory = TransformerFactory.newInstance ();

  // Does this factory support SAX features?
  if (tfactory.getFeature (SAXSource.FEATURE))
  {

    // If so, we can safely cast.
    final SAXTransformerFactory stfactory = ((SAXTransformerFactory) tfactory);

    final TemplatesHandler handler = stfactory.newTemplatesHandler ();
    handler.setSystemId (_getSystemId (stxID));

    final XMLReader reader = XMLReaderFactory.createXMLReader ();
    reader.setContentHandler (handler);

    reader.parse (_getInputSource (stxID));

    final Templates templates = handler.getTemplates ();

    // transform
    final Transformer transformer = templates.newTransformer ();
    transformer.transform (_getSource (sourceID), new StreamResult (System.out));

  }

  return true;
}
 
開發者ID:phax,項目名稱:ph-stx,代碼行數:38,代碼來源:TestCases.java


注:本文中的javax.xml.transform.sax.SAXTransformerFactory.newTemplatesHandler方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。