Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* gvSIG. Geographic Information System of the Valencian Government
 *
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
 * of the Valencian Government (CIT)
 * 
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 * 
 */

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

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

import org.w3c.dom.Document;

public class Main {
    /**
     * Transforms a memory document with XML format into an string according an
     * XSL file, and stores it in a file
     * 
     * @param doc
     *            The document to read
     * @param xslFile
     *            The name of the XSL file which allows transformate XML into
     *            String according its style
     * 
     * @return String value of a DOM
     * 
     * @throws FileNotFoundException
     *             java.io.FileNotFoundException
     * @throws TransformerException
     *             javax.xml.transform.TransformerException
     */
    public static String write_DOM_into_an_String(Document doc, String xslFile)
            throws FileNotFoundException, TransformerException {
        // An instance of a object transformer factory
        TransformerFactory tFactory = TransformerFactory.newInstance();

        // Creates a transformer object associated to the XSL file
        Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));

        // Holds a tree with the information of a document in the form of a DOM
        // tree
        DOMSource sourceId = new DOMSource(doc);

        // Makes the transformation from the source in XML to the output in
        // stream according the transformer in XSL
        ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
        transformer.transform(sourceId, new StreamResult(bAOS));

        // Returns the string value of the stream
        return bAOS.toString();
    }
}