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 final String DEFAULT_XML_URL =
037 "@portal_url@/html/portlet/xsl_content/example.xml";
038
039 public static final 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 = transformerFactory.newTransformer(xslSource);
055
056 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
057 new UnsyncByteArrayOutputStream();
058
059 transformer.transform(
060 xmlSource, new StreamResult(unsyncByteArrayOutputStream));
061
062 return unsyncByteArrayOutputStream.toString();
063 }
064
065 }