1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
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  /**
32   * <a href="XSLContentUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
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  }