001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
032     * @author Brian Wing Shun Chan
033     */
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    }