javax.xml.transform.sax.TemplatesHandler Java Examples

The following examples show how to use javax.xml.transform.sax.TemplatesHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TemplatesParser.java    From everrest with Eclipse Public License 2.0 6 votes vote down vote up
public Templates parseTemplates(Source source) throws TransformerConfigurationException, SAXException, IOException {
    InputSource inputSource = SAXSource.sourceToInputSource(source);
    if (inputSource == null) {
        throw new RuntimeException("Unable convert to Input Source");
    }

    SAXTransformerFactory saxTransformerFactory = createFeaturedSaxTransformerFactory();
    TemplatesHandler templateHandler = saxTransformerFactory.newTemplatesHandler();
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    if (resolver != null) {
        xmlReader.setEntityResolver(resolver);
    }
    xmlReader.setContentHandler(templateHandler);

    xmlReader.parse(inputSource);

    Templates templates = templateHandler.getTemplates();
    if (templates == null) {
        throw new RuntimeException("Unable create templates from given source");
    }
    return templates;
}
 
Example #2
Source File: XSLTTransform.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Set the xslt resource.
 * 
 * @param xsltresource
 *        an Input Source to the XSLT
 * @throws Exception
 */
public void setXslt(InputSource xsltresource) throws Exception
{
	if ( saxParserFactory == null) {
		saxParserFactory = SAXParserFactory.newInstance();
		saxParserFactory.setNamespaceAware(true);
	}
	TemplatesHandler th = factory.newTemplatesHandler();
	String systemId = xsltresource.getSystemId();
	th.setSystemId(systemId);
	SAXParser parser = saxParserFactory.newSAXParser();
	XMLReader xr = parser.getXMLReader();

	xr.setContentHandler(th);
	xr.parse(xsltresource);
	templates = th.getTemplates();
}
 
Example #3
Source File: XSLTTransform.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Set the xslt resource.
 * 
 * @param xsltresource
 *        an Input Source to the XSLT
 * @throws Exception
 */
public void setXslt(InputSource xsltresource) throws Exception
{
	if ( saxParserFactory == null) {
		saxParserFactory = SAXParserFactory.newInstance();
		saxParserFactory.setNamespaceAware(true);
	}
	TemplatesHandler th = factory.newTemplatesHandler();
	String systemId = xsltresource.getSystemId();
	th.setSystemId(systemId);
	SAXParser parser = saxParserFactory.newSAXParser();
	XMLReader xr = parser.getXMLReader();

	xr.setContentHandler(th);
	xr.parse(xsltresource);
	templates = th.getTemplates();
}
 
Example #4
Source File: TemplatesFilterFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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());
}
 
Example #5
Source File: SAXTFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #6
Source File: SAXTFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #7
Source File: SAXTFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #8
Source File: TransformerFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    // create CatalogFeatures that is accessible by the Handler
    // through the factory instance
    buildCatalogFeatures();
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #9
Source File: SmartTransformerFactoryImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #10
Source File: TransformerFactoryImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #11
Source File: SmartTransformerFactoryImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #12
Source File: TransformerFactoryImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #13
Source File: SmartTransformerFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #14
Source File: TransformerFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #15
Source File: TransformerFactoryImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    // create CatalogFeatures that is accessible by the Handler
    // through the factory instance
    buildCatalogFeatures();
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this, _hasUserErrListener);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #16
Source File: SmartTransformerFactoryImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #17
Source File: TransformerFactoryImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #18
Source File: SmartTransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #19
Source File: TransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #20
Source File: SmartTransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #21
Source File: TransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #22
Source File: SmartTransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #23
Source File: TransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #24
Source File: SmartTransformerFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #25
Source File: TransformerFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #26
Source File: SmartTransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object. Uses the
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    return _xsltcFactory.newTemplatesHandler();
}
 
Example #27
Source File: TransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TemplatesHandler object that can process SAX ContentHandler
 * events into a Templates object.
 *
 * @return A TemplatesHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TemplatesHandler newTemplatesHandler()
    throws TransformerConfigurationException
{
    final TemplatesHandlerImpl handler =
        new TemplatesHandlerImpl(_indentNumber, this);
    if (_uriResolver != null) {
        handler.setURIResolver(_uriResolver);
    }
    return handler;
}
 
Example #28
Source File: XalanTransformerFactory.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public TemplatesHandler newTemplatesHandler() throws TransformerConfigurationException {
    return delegate.newTemplatesHandler();
}
 
Example #29
Source File: TransformerFactoryImpl.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new Transformer object that performs a copy
 * of the source to the result.
 *
 * @return A Transformer object that may be used to perform a transformation
 * in a single thread, never null.
 *
 * @throws TransformerConfigurationException May throw this during
 *            the parse when it is constructing the
 *            Templates object and fails.
 */
public TemplatesHandler newTemplatesHandler()
        throws TransformerConfigurationException
{
  return new StylesheetHandler(this);
}