001
014
015 package com.liferay.portlet.xslcontent.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019 import com.liferay.portal.kernel.util.HttpUtil;
020
021 import java.io.IOException;
022
023 import java.net.URL;
024
025 import javax.xml.transform.Transformer;
026 import javax.xml.transform.TransformerException;
027 import javax.xml.transform.TransformerFactory;
028 import javax.xml.transform.stream.StreamResult;
029 import javax.xml.transform.stream.StreamSource;
030
031
034 public class XSLContentUtil {
035
036 public static String DEFAULT_XML_URL =
037 "@portal_url@/html/portlet/xsl_content/example.xml";
038
039 public static String DEFAULT_XSL_URL =
040 "@portal_url@/html/portlet/xsl_content/example.xsl";
041
042 public static String transform(URL xmlURL, URL xslURL)
043 throws IOException, TransformerException {
044
045 String xml = HttpUtil.URLtoString(xmlURL);
046 String xsl = HttpUtil.URLtoString(xslURL);
047
048 StreamSource xmlSource = new StreamSource(new UnsyncStringReader(xml));
049 StreamSource xslSource = new StreamSource(new UnsyncStringReader(xsl));
050
051 TransformerFactory transformerFactory =
052 TransformerFactory.newInstance();
053
054 Transformer transformer =
055 transformerFactory.newTransformer(xslSource);
056
057 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
058 new UnsyncByteArrayOutputStream();
059
060 transformer.transform(
061 xmlSource, new StreamResult(unsyncByteArrayOutputStream));
062
063 return unsyncByteArrayOutputStream.toString();
064 }
065
066 }