01: package tide.utils;
02:
03: import java.io.*;
04: import javax.xml.transform.Transformer;
05: import javax.xml.transform.TransformerFactory;
06: import javax.xml.transform.stream.*;
07:
08: /** Used for external tools XML output transform with XSLT.
09: */
10: public class XMLUtils {
11:
12: public static String transformXML(File xslt, StreamSource src)
13: throws Exception {
14: Transformer t = TransformerFactory.newInstance()
15: .newTransformer(new StreamSource(xslt));
16: StringWriter sw = new StringWriter();
17: StreamResult res = new StreamResult(sw);
18: t.transform(src, res);
19: return sw.toString();
20: }
21:
22: public static String transformXML(InputStream xslt, StreamSource src)
23: throws Exception {
24: Transformer t = TransformerFactory.newInstance()
25: .newTransformer(new StreamSource(xslt));
26: StringWriter sw = new StringWriter();
27: StreamResult res = new StreamResult(sw);
28: t.transform(src, res);
29: return sw.toString();
30: }
31:
32: /*test
33: public static void main(String[] a) throws Exception
34: {
35: //this.getClass().getClassLoader().getResourceAsStream("XMLUtils
36: String res = transformXML( new File("c:/temp/simple_pmd_report.xsl"), new StreamSource(new File("c:/temp/pmd.xml")));
37: System.out.println("RES:"+res);
38: FileUtils.saveToFile(res, new File("c:/temp/out.txt"));
39: }*/
40:
41: }
|