| JournalXslUtil.java |
1 /**
2 * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portlet.journal.util;
24
25 import com.liferay.portal.kernel.util.ByteArrayMaker;
26 import com.liferay.portlet.journal.TransformException;
27
28 import java.io.StringReader;
29 import java.io.UnsupportedEncodingException;
30
31 import java.util.Map;
32
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerException;
35 import javax.xml.transform.TransformerFactory;
36 import javax.xml.transform.stream.StreamResult;
37 import javax.xml.transform.stream.StreamSource;
38
39 /**
40 * <a href="JournalXslUtil.java.html"><b><i>View Source</i></b></a>
41 *
42 * @author Alexander Chow
43 *
44 */
45 public class JournalXslUtil {
46
47 public static String transform(
48 Map tokens, String languageId, String xml, String script)
49 throws TransformException, UnsupportedEncodingException {
50
51 ByteArrayMaker bam = new ByteArrayMaker();
52
53 try {
54 StreamSource xmlSource = new StreamSource(new StringReader(xml));
55 StreamSource scriptSource = new StreamSource(
56 new StringReader(script));
57
58 TransformerFactory transformerFactory =
59 TransformerFactory.newInstance();
60
61 transformerFactory.setURIResolver(
62 new URIResolver(tokens, languageId));
63
64 Transformer transformer =
65 transformerFactory.newTransformer(scriptSource);
66
67 transformer.transform(xmlSource, new StreamResult(bam));
68 }
69 catch (TransformerException te) {
70 throw new TransformException(te.getMessage());
71 }
72
73 return bam.toString("UTF-8");
74 }
75
76 }