1 package org.objectweb.petals.engine.xslt.sdk; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 6 import javax.xml.parsers.ParserConfigurationException ; 7 import javax.xml.parsers.SAXParserFactory ; 8 import javax.xml.transform.Transformer ; 9 import javax.xml.transform.TransformerException ; 10 import javax.xml.transform.TransformerFactory ; 11 import javax.xml.transform.sax.SAXSource ; 12 import javax.xml.transform.sax.SAXTransformerFactory ; 13 import javax.xml.transform.stream.StreamResult ; 14 import javax.xml.transform.stream.StreamSource ; 15 16 import org.xml.sax.InputSource ; 17 import org.xml.sax.SAXException ; 18 import org.xml.sax.XMLFilter ; 19 import org.xml.sax.XMLReader ; 20 21 public class XsltImpl implements Xslt { 22 23 public XsltImpl() { 24 super(); 25 } 26 27 public String transform(String xml, String xslPath) throws SAXException , 28 ParserConfigurationException , TransformerException { 29 SAXParserFactory spf = SAXParserFactory.newInstance(); 30 spf.setNamespaceAware(true); 31 XMLReader reader = spf.newSAXParser().getXMLReader(); 32 SAXTransformerFactory stf = (SAXTransformerFactory ) TransformerFactory 34 .newInstance(); 35 XMLFilter filtre1 = stf.newXMLFilter(new StreamSource (xslPath)); 36 filtre1.setParent(reader); 38 43 byte[] msg = xml.getBytes(); 44 InputSource inSource = new InputSource (new ByteArrayInputStream (msg)); 45 SAXSource source = new SAXSource (filtre1, inSource); 46 47 ByteArrayOutputStream outStream = new ByteArrayOutputStream (); 48 StreamResult resultat = new StreamResult (outStream); 49 Transformer transformer = stf.newTransformer(); 51 transformer.transform(source, resultat); 52 53 return new String (outStream.toByteArray()); 54 55 } 56 57 } 58 | Popular Tags |