All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yestech.lib.xml.XslUtils Maven / Gradle / Ivy

Go to download

A collection of classes that can be used across yestech artifacts/components, but must not be dependant on any yestech component. Most of the code is utility type code. When more than a few classes are found to be in a package or the package start to handle more that a few reposibilities then a new independant component is created and the existing code in yeslib is ported to the new component.

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright LGPL3
 * YES Technology Association
 * http://yestech.org
 *
 * http://www.opensource.org/licenses/lgpl-3.0.html
 */

package org.yestech.lib.xml;

import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
//import org.apache.xalan.processor.TransformerFactoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import java.io.StringWriter;
import java.io.File;

/**
 * Utility Methods for XSL Templating.
 *
 */
final public class XslUtils {
    public static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactoryImpl.newInstance();

    final private static Logger logger = LoggerFactory.getLogger(XslUtils.class);

    private XslUtils() {
        super();
    }

    /**
     * Transforms an xml source using an xsl source and returns the result.
     *
     * @param xmlSource XML Source
     * @param xslSource XSL Source
     * @return The result
     * @throws RuntimeException if an error happens
     */
    public static String transformMessage(Source xmlSource, Source xslSource) {
        try {
            StringWriter writer = new StringWriter();
            //TODO: cache the transformers!!!!!
            Transformer transform = XslUtils.TRANSFORMER_FACTORY.newTransformer(xslSource);
            transform.transform(xmlSource, new StreamResult(writer));
            return writer.toString();
        } catch (Exception e) {
            logger.error("trouble with xsl transformation ", e);
            throw new RuntimeException("trouble with xsl transformation : " + e + " --> " + e.getMessage());
        }

    }

    public static File locateFileInApp(String filePath)
    {
        throw new UnsupportedOperationException("Not Yet implemented");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy