| XSLContentUtil.java |
1 /**
2 * Copyright (c) 2000-2009 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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
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 /**
37 * <a href="XSLContentUtil.java.html"><b><i>View Source</i></b></a>
38 *
39 * @author Brian Wing Shun Chan
40 *
41 */
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 }