001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023:
024: package org.mdarad.framework.util;
025:
026: import org.mdarad.framework.exception.SystemException;
027: import org.xml.sax.ContentHandler;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.XMLReader;
031: import org.xml.sax.helpers.XMLReaderFactory;
032:
033: import javax.xml.transform.Result;
034: import javax.xml.transform.Templates;
035: import javax.xml.transform.TransformerConfigurationException;
036: import javax.xml.transform.TransformerFactory;
037: import javax.xml.transform.URIResolver;
038: import javax.xml.transform.sax.SAXResult;
039: import javax.xml.transform.sax.SAXSource;
040: import javax.xml.transform.sax.SAXTransformerFactory;
041: import javax.xml.transform.sax.TemplatesHandler;
042: import javax.xml.transform.sax.TransformerHandler;
043: import java.io.IOException;
044:
045: /**
046: * Created by IntelliJ IDEA.
047: * User: François Eric
048: * Date: 2004-08-30
049: * Time: 15:30:15
050: * To change this template use File | Settings | File Templates.
051: */
052: public class SAXHelper {
053:
054: public static Templates getTransformationTemplates(
055: InputSource xslSource, URIResolver resolver)
056: throws SystemException, IOException {
057: try {
058: // Instantiate a TransformerFactory.
059: TransformerFactory tFactory = TransformerFactory
060: .newInstance();
061: SAXTransformerFactory saxTFactory = null;
062: if (tFactory.getFeature(SAXSource.FEATURE)
063: && tFactory.getFeature(SAXResult.FEATURE)) {
064: saxTFactory = ((SAXTransformerFactory) tFactory);
065: }
066: //Set URIResolver
067: if (resolver != null)
068: saxTFactory.setURIResolver(resolver);
069: // Create a ContentHandler to handle parsing of the stylesheet.
070: TemplatesHandler templatesHandler = saxTFactory
071: .newTemplatesHandler();
072: // Create an XMLReader and set its ContentHandler.
073: XMLReader reader = XMLReaderFactory.createXMLReader();
074: reader.setContentHandler(templatesHandler);
075:
076: // Parse the stylesheet.
077: reader.parse(xslSource);
078:
079: //Get the Templates object from the ContentHandler.
080: Templates templates = templatesHandler.getTemplates();
081: return templates;
082: } catch (TransformerConfigurationException e) {
083: throw new SystemException(e);
084: } catch (SAXException e) {
085: throw new SystemException(e);
086: }
087: }
088:
089: public static TransformerHandler getTransformationContentHandler(
090: Templates templates, Result output) throws SystemException {
091: return getTransformationContentHandler(templates, output, null);
092: }
093:
094: public static TransformerHandler getTransformationContentHandler(
095: Templates templates, Result output, URIResolver resolver)
096: throws SystemException {
097: TransformerHandler transformerHandler = null;
098: try {
099: // Instantiate a TransformerFactory.
100: TransformerFactory tFactory = TransformerFactory
101: .newInstance();
102: SAXTransformerFactory saxTFactory = null;
103: if (tFactory.getFeature(SAXSource.FEATURE)
104: && tFactory.getFeature(SAXResult.FEATURE)) {
105: saxTFactory = ((SAXTransformerFactory) tFactory);
106: }
107: // Create a ContentHandler to handle parsing of the XML source.
108: transformerHandler = saxTFactory
109: .newTransformerHandler(templates);
110: if (resolver != null)
111: transformerHandler.getTransformer().setURIResolver(
112: resolver);
113:
114: //Set result to contentHandler
115: transformerHandler.setResult(output);
116: } catch (TransformerConfigurationException e) {
117: throw new SystemException(
118: "Could not configure the transformer", e);
119: }
120: return transformerHandler;
121: }
122:
123: public static void convertXmlWithXsl(InputSource xmlInputSource,
124: InputSource xslSource, Result output)
125: throws SystemException, IOException, SAXException {
126:
127: //Get the transformation templates
128: Templates templates = null;
129: templates = getTransformationTemplates(xslSource, null);
130:
131: //Get the output contentHandler
132: ContentHandler outputHandler = getTransformationContentHandler(
133: templates, output, null);
134:
135: //Read the xml file and transform it
136: XMLReader reader = XMLReaderFactory.createXMLReader();
137: reader.setContentHandler(outputHandler);
138: reader.parse(xmlInputSource);
139: }
140:
141: }
|