SAXHelper.java :  » J2EE » Mdarad » org » mdarad » framework » util » Java Open Source

Java Open Source » J2EE » Mdarad 
Mdarad » org » mdarad » framework » util » SAXHelper.java
/*
    Mdarad-Toolobox is a collection of tools for Architected RAD
    (Rapid Application Development) based on an MDA approach.
    The toolbox contains frameworks and generators for many environments
    (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
    applications from a design Model
  Copyright (C) 2004-2005  Elapse Technologies Inc.

  This library 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.1 of the License, or (at your option) any later version.

  This library 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 library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package org.mdarad.framework.util;

import org.mdarad.framework.exception.SystemException;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import javax.xml.transform.Result;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TemplatesHandler;
import javax.xml.transform.sax.TransformerHandler;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Franois Eric
 * Date: 2004-08-30
 * Time: 15:30:15
 * To change this template use File | Settings | File Templates.
 */
public class SAXHelper {

    public static Templates getTransformationTemplates(InputSource xslSource, URIResolver resolver) throws SystemException, IOException {
        try {
            // Instantiate a TransformerFactory.
            TransformerFactory tFactory = TransformerFactory.newInstance();
            SAXTransformerFactory saxTFactory = null;
            if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
                saxTFactory = ((SAXTransformerFactory) tFactory);
            }
            //Set URIResolver
            if (resolver != null) saxTFactory.setURIResolver(resolver);
            // Create a ContentHandler to handle parsing of the stylesheet.
            TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler();
            // Create an XMLReader and set its ContentHandler.
            XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler(templatesHandler);

            // Parse the stylesheet.
            reader.parse(xslSource);

            //Get the Templates object from the ContentHandler.
            Templates templates = templatesHandler.getTemplates();
            return templates;
        } catch (TransformerConfigurationException e) {
            throw new SystemException(e);
        } catch (SAXException e) {
            throw new SystemException(e);
        }
    }

    public static TransformerHandler getTransformationContentHandler(Templates templates, Result output) throws SystemException {
        return getTransformationContentHandler(templates, output, null);
    }

    public static TransformerHandler getTransformationContentHandler(Templates templates, Result output, URIResolver resolver) throws SystemException {
        TransformerHandler transformerHandler = null;
        try {
            // Instantiate a TransformerFactory.
            TransformerFactory tFactory = TransformerFactory.newInstance();
            SAXTransformerFactory saxTFactory = null;
            if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
                saxTFactory = ((SAXTransformerFactory) tFactory);
            }
            // Create a ContentHandler to handle parsing of the XML source.
            transformerHandler = saxTFactory.newTransformerHandler(templates);
            if (resolver != null) transformerHandler.getTransformer().setURIResolver(resolver);

            //Set result to contentHandler
            transformerHandler.setResult(output);
        } catch (TransformerConfigurationException e) {
            throw new SystemException("Could not configure the transformer", e);
        }
        return transformerHandler;
    }

    public static void convertXmlWithXsl(InputSource xmlInputSource, InputSource xslSource, Result output) throws SystemException, IOException, SAXException {

        //Get the transformation templates
        Templates templates = null;
        templates = getTransformationTemplates(xslSource, null);

        //Get the output contentHandler
        ContentHandler outputHandler = getTransformationContentHandler(templates, output, null);

        //Read the xml file and transform it
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(outputHandler);
        reader.parse(xmlInputSource);
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.