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