KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > SAXHelper


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23
24 package org.mdarad.framework.util;
25
26 import org.mdarad.framework.exception.SystemException;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
32
33 import javax.xml.transform.Result JavaDoc;
34 import javax.xml.transform.Templates JavaDoc;
35 import javax.xml.transform.TransformerConfigurationException JavaDoc;
36 import javax.xml.transform.TransformerFactory JavaDoc;
37 import javax.xml.transform.URIResolver JavaDoc;
38 import javax.xml.transform.sax.SAXResult JavaDoc;
39 import javax.xml.transform.sax.SAXSource JavaDoc;
40 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
41 import javax.xml.transform.sax.TemplatesHandler JavaDoc;
42 import javax.xml.transform.sax.TransformerHandler JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 /**
46  * Created by IntelliJ IDEA.
47  * User: François Eric
48  * Date: 2004-08-30
49  * Time: 15:30:15
50  * To change this template use File | Settings | File Templates.
51  */

52 public class SAXHelper {
53
54     public static Templates JavaDoc getTransformationTemplates(InputSource JavaDoc xslSource, URIResolver JavaDoc resolver) throws SystemException, IOException JavaDoc {
55         try {
56             // Instantiate a TransformerFactory.
57
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
58             SAXTransformerFactory JavaDoc saxTFactory = null;
59             if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
60                 saxTFactory = ((SAXTransformerFactory JavaDoc) tFactory);
61             }
62             //Set URIResolver
63
if (resolver != null) saxTFactory.setURIResolver(resolver);
64             // Create a ContentHandler to handle parsing of the stylesheet.
65
TemplatesHandler JavaDoc templatesHandler = saxTFactory.newTemplatesHandler();
66             // Create an XMLReader and set its ContentHandler.
67
XMLReader JavaDoc reader = XMLReaderFactory.createXMLReader();
68             reader.setContentHandler(templatesHandler);
69
70             // Parse the stylesheet.
71
reader.parse(xslSource);
72
73             //Get the Templates object from the ContentHandler.
74
Templates JavaDoc templates = templatesHandler.getTemplates();
75             return templates;
76         } catch (TransformerConfigurationException JavaDoc e) {
77             throw new SystemException(e);
78         } catch (SAXException JavaDoc e) {
79             throw new SystemException(e);
80         }
81     }
82
83     public static TransformerHandler JavaDoc getTransformationContentHandler(Templates JavaDoc templates, Result JavaDoc output) throws SystemException {
84         return getTransformationContentHandler(templates, output, null);
85     }
86
87     public static TransformerHandler JavaDoc getTransformationContentHandler(Templates JavaDoc templates, Result JavaDoc output, URIResolver JavaDoc resolver) throws SystemException {
88         TransformerHandler JavaDoc transformerHandler = null;
89         try {
90             // Instantiate a TransformerFactory.
91
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
92             SAXTransformerFactory JavaDoc saxTFactory = null;
93             if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
94                 saxTFactory = ((SAXTransformerFactory JavaDoc) tFactory);
95             }
96             // Create a ContentHandler to handle parsing of the XML source.
97
transformerHandler = saxTFactory.newTransformerHandler(templates);
98             if (resolver != null) transformerHandler.getTransformer().setURIResolver(resolver);
99
100             //Set result to contentHandler
101
transformerHandler.setResult(output);
102         } catch (TransformerConfigurationException JavaDoc e) {
103             throw new SystemException("Could not configure the transformer", e);
104         }
105         return transformerHandler;
106     }
107
108     public static void convertXmlWithXsl(InputSource JavaDoc xmlInputSource, InputSource JavaDoc xslSource, Result JavaDoc output) throws SystemException, IOException JavaDoc, SAXException JavaDoc {
109
110         //Get the transformation templates
111
Templates JavaDoc templates = null;
112         templates = getTransformationTemplates(xslSource, null);
113
114         //Get the output contentHandler
115
ContentHandler JavaDoc outputHandler = getTransformationContentHandler(templates, output, null);
116
117         //Read the xml file and transform it
118
XMLReader JavaDoc reader = XMLReaderFactory.createXMLReader();
119         reader.setContentHandler(outputHandler);
120         reader.parse(xmlInputSource);
121     }
122
123 }
124
Popular Tags