1
19
20 package com.liferay.portlet.xslcontent.util;
21
22 import com.liferay.portal.kernel.util.ByteArrayMaker;
23 import com.liferay.portal.kernel.util.HttpUtil;
24
25 import java.io.IOException;
26 import java.io.StringReader;
27
28 import java.net.URL;
29
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.stream.StreamResult;
34 import javax.xml.transform.stream.StreamSource;
35
36
42 public class XSLContentUtil {
43
44 public static String DEFAULT_XML_URL =
45 "@portal_url@/html/portlet/xsl_content/example.xml";
46
47 public static String DEFAULT_XSL_URL =
48 "@portal_url@/html/portlet/xsl_content/example.xsl";
49
50 public static String transform(URL xmlURL, URL xslURL)
51 throws IOException, TransformerException {
52
53 String xml = HttpUtil.URLtoString(xmlURL);
54 String xsl = HttpUtil.URLtoString(xslURL);
55
56 StreamSource xmlSource = new StreamSource(new StringReader(xml));
57 StreamSource xslSource = new StreamSource(new StringReader(xsl));
58
59 TransformerFactory transformerFactory =
60 TransformerFactory.newInstance();
61
62 Transformer transformer =
63 transformerFactory.newTransformer(xslSource);
64
65 ByteArrayMaker bam = new ByteArrayMaker();
66
67 transformer.transform(xmlSource, new StreamResult(bam));
68
69 return bam.toString();
70 }
71
72 }