1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public static Document outputXSLT(File xmlDoc, String xsltFile) throws InternalException
{
//Document JDOMResult, résultat de la transformation TraX
JDOMResult documentJDOMSortie = new JDOMResult();
try
{
SAXBuilder sxb = new SAXBuilder();
//On définit un transformer avec la source XSL qui va permettre la transformation
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xsltStreamSource;
try
{
xsltStreamSource = new StreamSource(ClassLoader.getSystemResourceAsStream(xsltFile));
}
catch (Exception e)
{
xsltStreamSource = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("/"+xsltFile));
}
Transformer transformer = factory.newTransformer(xsltStreamSource);
//On transforme le document JDOMEntree grâce à notre transformer.
//La méthoded transform() prend en argument le document d'entree associé au transformer
//et un document JDOMResult, résultat de la transformation TraX
JDOMSource documentJDOMSource = new JDOMSource(sxb.build(xmlDoc));
System.out.println(factory.getFeature(JDOMResult.JDOM_FEATURE)+""); // -> false avec Tomcat
transformer.transform(documentJDOMSource, documentJDOMSortie); //erreur ici sous Tomcat
//Pour récupérer le document JDOM issu de cette transformation il faut utiliser la méthode getDocument()
return documentJDOMSortie.getDocument();
}
catch(Exception e)
{
throw new InternalException("XSLT transformation failed", e, null);
}
} |