KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > xslt > sdk > XsltImpl


1 package org.objectweb.petals.engine.xslt.sdk;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5
6 import javax.xml.parsers.ParserConfigurationException JavaDoc;
7 import javax.xml.parsers.SAXParserFactory JavaDoc;
8 import javax.xml.transform.Transformer JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.TransformerFactory JavaDoc;
11 import javax.xml.transform.sax.SAXSource JavaDoc;
12 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
13 import javax.xml.transform.stream.StreamResult JavaDoc;
14 import javax.xml.transform.stream.StreamSource JavaDoc;
15
16 import org.xml.sax.InputSource JavaDoc;
17 import org.xml.sax.SAXException JavaDoc;
18 import org.xml.sax.XMLFilter JavaDoc;
19 import org.xml.sax.XMLReader JavaDoc;
20
21 public class XsltImpl implements Xslt {
22
23     public XsltImpl() {
24         super();
25     }
26
27     public String JavaDoc transform(String JavaDoc xml, String JavaDoc xslPath) throws SAXException JavaDoc,
28             ParserConfigurationException JavaDoc, TransformerException JavaDoc {
29         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
30         spf.setNamespaceAware(true);
31         XMLReader JavaDoc reader = spf.newSAXParser().getXMLReader();
32         // Load xslt sheet to tranform xml
33
SAXTransformerFactory JavaDoc stf = (SAXTransformerFactory JavaDoc) TransformerFactory
34                 .newInstance();
35         XMLFilter JavaDoc filtre1 = stf.newXMLFilter(new StreamSource JavaDoc(xslPath));
36         // Match reader with filters
37
filtre1.setParent(reader);
38         /*
39          * Creation of source: the last filter, this filter indicate
40          * transformations to do. Transform NormalizeMessage content in
41          * SAXsource.
42          */

43         byte[] msg = xml.getBytes();
44         InputSource JavaDoc inSource = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(msg));
45         SAXSource JavaDoc source = new SAXSource JavaDoc(filtre1, inSource);
46
47         ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc();
48         StreamResult JavaDoc resultat = new StreamResult JavaDoc(outStream);
49         // Transformation
50
Transformer JavaDoc transformer = stf.newTransformer();
51         transformer.transform(source, resultat);
52
53         return new String JavaDoc(outStream.toByteArray());
54
55     }
56
57 }
58
Popular Tags