javax.xml.transform.Templates Java Examples

The following examples show how to use javax.xml.transform.Templates. 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: TransformXml.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) {
    final ComponentLog logger = getLogger();
    final Integer cacheSize = context.getProperty(CACHE_SIZE).asInteger();
    final Long cacheTTL = context.getProperty(CACHE_TTL_AFTER_LAST_ACCESS).asTimePeriod(TimeUnit.SECONDS);

    if (cacheSize > 0) {
        CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(cacheSize);
        if (cacheTTL > 0) {
            cacheBuilder = cacheBuilder.expireAfterAccess(cacheTTL, TimeUnit.SECONDS);
        }

        cache = cacheBuilder.build(
           new CacheLoader<String, Templates>() {
               public Templates load(String path) throws TransformerConfigurationException {
                   return newTemplates(path);
               }
           });
    } else {
        cache = null;
        logger.warn("Stylesheet cache disabled because cache size is set to 0");
    }
}
 
Example #2
Source File: SmartTransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Create a Templates object that from the input stylesheet
 * Uses the com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 * @param source the stylesheet.
 * @return A Templates object.
 */
public Templates newTemplates(Source source)
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    _currFactory = _xsltcFactory;
    return _currFactory.newTemplates(source);
}
 
Example #3
Source File: SmartTransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Create an XMLFilter that uses the given source as the
 * transformation instructions. Uses
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public XMLFilter newXMLFilter(Source src)
    throws TransformerConfigurationException {
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    Templates templates = _xsltcFactory.newTemplates(src);
    if (templates == null ) return null;
    return newXMLFilter(templates);
}
 
Example #4
Source File: XsltView.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void renderMergedOutputModel(
		Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	Templates templates = this.cachedTemplates;
	if (templates == null) {
		templates = loadTemplates();
	}

	Transformer transformer = createTransformer(templates);
	configureTransformer(model, response, transformer);
	configureResponse(model, response, transformer);
	Source source = null;
	try {
		source = locateSource(model);
		if (source == null) {
			throw new IllegalArgumentException("Unable to locate Source object in model: " + model);
		}
		transformer.transform(source, createResult(response));
	}
	finally {
		closeSourceIfNecessary(source);
	}
}
 
Example #5
Source File: TransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Create an XMLFilter that uses the given source as the
 * transformation instructions.
 *
 * @param templates The source of the transformation instructions.
 * @return An XMLFilter object, or null if this feature is not supported.
 * @throws TransformerConfigurationException
 */
@Override
public XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException
{
    try {
        return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
    }
    catch (TransformerConfigurationException e1) {
        if (_errorListener != null) {
            try {
                _errorListener.fatalError(e1);
                return null;
            }
            catch (TransformerException e2) {
                new TransformerConfigurationException(e2);
            }
        }
        throw e1;
    }
}
 
Example #6
Source File: SmartTransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Create an XMLFilter that uses the given source as the
 * transformation instructions. Uses
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public XMLFilter newXMLFilter(Source src)
    throws TransformerConfigurationException {
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    Templates templates = _xsltcFactory.newTemplates(src);
    if (templates == null ) return null;
    return newXMLFilter(templates);
}
 
Example #7
Source File: TransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Create an XMLFilter that uses the given source as the
 * transformation instructions.
 *
 * @param templates The source of the transformation instructions.
 * @return An XMLFilter object, or null if this feature is not supported.
 * @throws TransformerConfigurationException
 */
@Override
public XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException
{
    try {
        return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
    }
    catch (TransformerConfigurationException e1) {
        if (_errorListener != null) {
            try {
                _errorListener.fatalError(e1);
                return null;
            }
            catch (TransformerException e2) {
                new TransformerConfigurationException(e2);
            }
        }
        throw e1;
    }
}
 
Example #8
Source File: NamespacePrefixTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testConcurrentTransformations() throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new StringReader(XSL));
    final Templates tmpl = tf.newTemplates(xslsrc);
    concurrentTestPassed.set(true);

    // Execute multiple TestWorker tasks
    for (int id = 0; id < THREADS_COUNT; id++) {
        EXECUTOR.execute(new TransformerThread(tmpl.newTransformer(), id));
    }
    // Initiate shutdown of previously submitted task
    EXECUTOR.shutdown();
    // Wait for termination of submitted tasks
    if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {
        // If not all tasks terminates during the time out force them to shutdown
        EXECUTOR.shutdownNow();
    }
    // Check if all transformation threads generated the correct namespace prefix
    assertTrue(concurrentTestPassed.get());
}
 
Example #9
Source File: SmartTransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException {
    try {
        return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
    }
    catch(TransformerConfigurationException e1) {
        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
        ErrorListener errorListener = _xsltcFactory.getErrorListener();
        if(errorListener != null) {
            try {
                errorListener.fatalError(e1);
                return null;
            }
            catch( TransformerException e2) {
                new TransformerConfigurationException(e2);
            }
        }
        throw e1;
    }
}
 
Example #10
Source File: NamespacePrefixTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testConcurrentTransformations() throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new StringReader(XSL));
    final Templates tmpl = tf.newTemplates(xslsrc);
    concurrentTestPassed.set(true);

    // Execute multiple TestWorker tasks
    for (int id = 0; id < THREADS_COUNT; id++) {
        EXECUTOR.execute(new TransformerThread(tmpl.newTransformer(), id));
    }
    // Initiate shutdown of previously submitted task
    EXECUTOR.shutdown();
    // Wait for termination of submitted tasks
    if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {
        // If not all tasks terminates during the time out force them to shutdown
        EXECUTOR.shutdownNow();
    }
    // Check if all transformation threads generated the correct namespace prefix
    assertTrue(concurrentTestPassed.get());
}
 
Example #11
Source File: SmartTransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an XMLFilter that uses the given source as the
 * transformation instructions. Uses
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public XMLFilter newXMLFilter(Source src)
    throws TransformerConfigurationException {
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    Templates templates = _xsltcFactory.newTemplates(src);
    if (templates == null ) return null;
    return newXMLFilter(templates);
}
 
Example #12
Source File: SmartTransformerFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an XMLFilter that uses the given source as the
 * transformation instructions. Uses
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public XMLFilter newXMLFilter(Source src)
    throws TransformerConfigurationException {
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    Templates templates = _xsltcFactory.newTemplates(src);
    if (templates == null ) return null;
    return newXMLFilter(templates);
}
 
Example #13
Source File: SmartTransformerFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException {
    try {
        return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
    }
    catch(TransformerConfigurationException e1) {
        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
        ErrorListener errorListener = _xsltcFactory.getErrorListener();
        if(errorListener != null) {
            try {
                errorListener.fatalError(e1);
                return null;
            }
            catch( TransformerException e2) {
                new TransformerConfigurationException(e2);
            }
        }
        throw e1;
    }
}
 
Example #14
Source File: SmartTransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a Templates object that from the input stylesheet
 * Uses the com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 * @param source the stylesheet.
 * @return A Templates object.
 */
public Templates newTemplates(Source source)
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    _currFactory = _xsltcFactory;
    return _currFactory.newTemplates(source);
}
 
Example #15
Source File: SmartTransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException {
    try {
        return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
    }
    catch(TransformerConfigurationException e1) {
        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
        ErrorListener errorListener = _xsltcFactory.getErrorListener();
        if(errorListener != null) {
            try {
                errorListener.fatalError(e1);
                return null;
            }
            catch( TransformerException e2) {
                new TransformerConfigurationException(e2);
            }
        }
        throw e1;
    }
}
 
Example #16
Source File: SmartTransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an XMLFilter that uses the given source as the
 * transformation instructions. Uses
 * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 */
public XMLFilter newXMLFilter(Source src)
    throws TransformerConfigurationException {
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    Templates templates = _xsltcFactory.newTemplates(src);
    if (templates == null ) return null;
    return newXMLFilter(templates);
}
 
Example #17
Source File: XsltView.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void renderMergedOutputModel(
		Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
		throws Exception {

	Templates templates = this.cachedTemplates;
	if (templates == null) {
		templates = loadTemplates();
	}

	Transformer transformer = createTransformer(templates);
	configureTransformer(model, response, transformer);
	configureResponse(model, response, transformer);
	Source source = null;
	try {
		source = locateSource(model);
		if (source == null) {
			throw new IllegalArgumentException("Unable to locate Source object in model: " + model);
		}
		transformer.transform(source, createResult(response));
	}
	finally {
		closeSourceIfNecessary(source);
	}
}
 
Example #18
Source File: SmartTransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a Templates object that from the input stylesheet
 * Uses the com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.
 * @param source the stylesheet.
 * @return A Templates object.
 */
public Templates newTemplates(Source source)
    throws TransformerConfigurationException
{
    if (_xsltcFactory == null) {
        createXSLTCTransformerFactory();
    }
    if (_errorlistener != null) {
        _xsltcFactory.setErrorListener(_errorlistener);
    }
    if (_uriresolver != null) {
        _xsltcFactory.setURIResolver(_uriresolver);
    }
    _currFactory = _xsltcFactory;
    return _currFactory.newTemplates(source);
}
 
Example #19
Source File: TransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Create an XMLFilter that uses the given source as the
 * transformation instructions.
 *
 * @param templates The source of the transformation instructions.
 * @return An XMLFilter object, or null if this feature is not supported.
 * @throws TransformerConfigurationException
 */
@Override
public XMLFilter newXMLFilter(Templates templates)
    throws TransformerConfigurationException
{
    try {
        return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
    }
    catch (TransformerConfigurationException e1) {
        if (_errorListener != null) {
            try {
                _errorListener.fatalError(e1);
                return null;
            }
            catch (TransformerException e2) {
                new TransformerConfigurationException(e2);
            }
        }
        throw e1;
    }
}
 
Example #20
Source File: Jdk7u21.java    From learnjavabug with MIT License 6 votes vote down vote up
public Object getObject(final String... command) throws Exception {
	final Object templates = Gadgets.createTemplatesImpl(command);

	String zeroHashCodeStr = "f5a5a608";

	HashMap map = new HashMap();
	map.put(zeroHashCodeStr, "foo");

	InvocationHandler tempHandler = (InvocationHandler) Reflections.getFirstCtor(Gadgets.ANN_INV_HANDLER_CLASS).newInstance(
       Override.class, map);
	Reflections.setFieldValue(tempHandler, "type", Templates.class);
	Templates proxy = Gadgets.createProxy(tempHandler, Templates.class);

	LinkedHashSet set = new LinkedHashSet(); // maintain order
	set.add(templates);
	set.add(proxy);

	Reflections.setFieldValue(templates, "_auxClasses", null);
	Reflections.setFieldValue(templates, "_class", null);

	map.put(zeroHashCodeStr, templates); // swap in real object

	return set;
}
 
Example #21
Source File: XPathNegativeZero.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();

    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);

    t.transform(src, res);
    return writer.toString();
}
 
Example #22
Source File: SchematronValidator.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static synchronized SchematronValidator getInstance(String schematronLocation) throws TransformerException {
   Validate.notEmpty(schematronLocation, "Location for the Schematron schema needs to be specified.");
   LOG.info(String.format("Retrieving validator for schematron file [%s]", schematronLocation));
   if (!cachedTemplates.containsKey(schematronLocation)) {
      LOG.debug(String.format("Template for schematron file [%s] not in cache, creating it", schematronLocation));
      Source dataSource = new StreamSource(SchematronValidator.class.getResource(schematronLocation).toString());
      Templates templates = transformerFactory.newTemplates(dataSource);
      cachedTemplates.put(schematronLocation, templates);
      LOG.debug(String.format("Template for schematron file [%s] created and cached", schematronLocation));
   }

   Templates template = (Templates)cachedTemplates.get(schematronLocation);
   return new SchematronValidator(template);
}
 
Example #23
Source File: XslSubstringTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private String testTransform(String xsl) throws Exception {
    //Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    //Create factory, template and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Templates tmpl = tf.newTemplates(xslsrc);
    Transformer t = tmpl.newTransformer();
    //Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);
    //Transform
    t.transform(src, xmlResultStream);
    return xmlResultString.toString().trim();
}
 
Example #24
Source File: TrAXFilter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public TrAXFilter(Templates templates)  throws
    TransformerConfigurationException
{
    _templates = templates;
    _transformer = (TransformerImpl) templates.newTransformer();
    _transformerHandler = new TransformerHandlerImpl(_transformer);
    _useServicesMechanism = _transformer.useServicesMechnism();
}
 
Example #25
Source File: XsltView.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Load the {@link Templates} instance for the stylesheet at the configured location.
 */
private Templates loadTemplates() throws ApplicationContextException {
	Source stylesheetSource = getStylesheetSource();
	try {
		Templates templates = getTransformerFactory().newTemplates(stylesheetSource);
		return templates;
	}
	catch (TransformerConfigurationException ex) {
		throw new ApplicationContextException("Can't load stylesheet from '" + getUrl() + "'", ex);
	}
	finally {
		closeSourceIfNecessary(stylesheetSource);
	}
}
 
Example #26
Source File: TransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Process the Source into a Templates object, which is a a compiled
 * representation of the source. Note that this method should not be
 * used with XSLTC, as the time-consuming compilation is done for each
 * and every transformation.
 *
 * @return A Templates object that can be used to create Transformers.
 * @throws TransformerConfigurationException
 */
@Override
public Transformer newTransformer(Source source) throws
    TransformerConfigurationException
{
    final Templates templates = newTemplates(source);
    final Transformer transformer = templates.newTransformer();
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return(transformer);
}
 
Example #27
Source File: TransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.TransformerFactory implementation.
 * Process the Source into a Templates object, which is a a compiled
 * representation of the source. Note that this method should not be
 * used with XSLTC, as the time-consuming compilation is done for each
 * and every transformation.
 *
 * @return A Templates object that can be used to create Transformers.
 * @throws TransformerConfigurationException
 */
@Override
public Transformer newTransformer(Source source) throws
    TransformerConfigurationException
{
    final Templates templates = newTemplates(source);
    final Transformer transformer = templates.newTransformer();
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return(transformer);
}
 
Example #28
Source File: TrAXFilter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public TrAXFilter(Templates templates)  throws
    TransformerConfigurationException
{
    _templates = templates;
    _transformer = (TransformerImpl) templates.newTransformer();
    _transformerHandler = new TransformerHandlerImpl(_transformer);
    _overrideDefaultParser = _transformer.overrideDefaultParser();
}
 
Example #29
Source File: TrAXFilter.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public TrAXFilter(Templates templates)  throws
    TransformerConfigurationException
{
    _templates = templates;
    _transformer = (TransformerImpl) templates.newTransformer();
    _transformerHandler = new TransformerHandlerImpl(_transformer);
    _useServicesMechanism = _transformer.useServicesMechnism();
}
 
Example #30
Source File: XsltView.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Load the {@link Templates} instance for the stylesheet at the configured location.
 */
private Templates loadTemplates() throws ApplicationContextException {
	Source stylesheetSource = getStylesheetSource();
	try {
		Templates templates = getTransformerFactory().newTemplates(stylesheetSource);
		return templates;
	}
	catch (TransformerConfigurationException ex) {
		throw new ApplicationContextException("Can't load stylesheet from '" + getUrl() + "'", ex);
	}
	finally {
		closeSourceIfNecessary(stylesheetSource);
	}
}