Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2012-2014 Blake Dickie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.Document;

import java.io.File;

import java.io.InputStream;

import java.io.StringWriter;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    /**
     * Applies an XSLT.
     *
     * @param xml          The document to translate.
     * @param xsltFilename The filename of the xsl file to use.
     * @param params       A map of parameters to pass to the transformation.
     *                     if null is passed none are used.
     *
     * @return The string representing of the result of the transformation.
     */
    public static String applyXSLT(Document xml, String xsltFilename, Map params) {
        return applyXSLT(new DOMSource(xml), xsltFilename, params);
    }

    /**
     * Applies an XSLT.
     *
     * @param xml          The inputstream to read the xml from.
     * @param xsltFilename The filename of the xsl file to use.
     * @param params       A map of parameters to pass to the transformation.
     *                     if null is passed none are used.
     *
     * @return The string representing of the result of the transformation.
     */
    public static String applyXSLT(InputStream in, String xsltFilename, Map params) {
        StreamSource src = new StreamSource(in);
        return applyXSLT(src, xsltFilename, params);
    }

    /**
     * Applies an XSLT.
     *
     * @param xml          The soucrce to translate.
     * @param xsltFilename The filename of the xsl file to use.
     * @param params       A map of parameters to pass to the transformation.
     *                     if null is passed none are used.
     *
     * @return The string representing of the result of the transformation.
     */
    public static String applyXSLT(Source xml, String xsltFilename, Map params) {
        try {
            String fullPath = xsltFilename;
            File xslt = new File(fullPath);
            TransformerFactory f = TransformerFactory.newInstance();
            StreamSource xsltSrc = new StreamSource(xslt);
            StringWriter out = new StringWriter();
            StreamResult res = new StreamResult(out);
            Transformer t = f.newTransformer(xsltSrc);

            if (params != null) {
                Iterator i = params.keySet().iterator();
                while (i.hasNext()) {
                    Object obj = i.next();
                    t.setParameter(obj.toString(), params.get(obj));
                }
            }
            t.transform(xml, res);

            return out.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String applyXSLT(Source xml, URL xsltFilename, Map params) {
        try {
            TransformerFactory f = TransformerFactory.newInstance();
            StreamSource xsltSrc = new StreamSource(xsltFilename.openConnection().getInputStream());
            StringWriter out = new StringWriter();
            StreamResult res = new StreamResult(out);
            Transformer t = f.newTransformer(xsltSrc);

            if (params != null) {
                Iterator i = params.keySet().iterator();
                while (i.hasNext()) {
                    Object obj = i.next();
                    t.setParameter(obj.toString(), params.get(obj));
                }
            }
            t.transform(xml, res);

            return out.toString();
        } catch (Exception e) {
            return null;
        }
    }
}