1   /**
2    * Copyright (c) 2000-2008 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.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.portlet.journal.TransformException;
29  
30  import java.io.StringReader;
31  import java.io.UnsupportedEncodingException;
32  
33  import java.util.Locale;
34  import java.util.Map;
35  
36  import javax.xml.transform.Transformer;
37  import javax.xml.transform.TransformerConfigurationException;
38  import javax.xml.transform.TransformerException;
39  import javax.xml.transform.TransformerFactory;
40  import javax.xml.transform.stream.StreamResult;
41  import javax.xml.transform.stream.StreamSource;
42  
43  /**
44   * <a href="JournalXslUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Alexander Chow
47   *
48   */
49  public class JournalXslUtil {
50  
51      public static String transform(
52              Map tokens, String languageId, String xml, String script)
53          throws TransformException, UnsupportedEncodingException {
54  
55          ByteArrayMaker bam = new ByteArrayMaker();
56  
57          long companyId = GetterUtil.getLong((String)tokens.get("company_id"));
58          Locale locale = LocaleUtil.fromLanguageId(languageId);
59  
60          JournalXslErrorListener errorListener = new JournalXslErrorListener(
61              companyId, locale);
62  
63          try {
64              StreamSource xmlSource = new StreamSource(new StringReader(xml));
65              StreamSource scriptSource = new StreamSource(
66                  new StringReader(script));
67  
68              TransformerFactory transformerFactory =
69                  TransformerFactory.newInstance();
70  
71              transformerFactory.setURIResolver(
72                  new URIResolver(tokens, languageId));
73              transformerFactory.setErrorListener(errorListener);
74  
75              Transformer transformer =
76                  transformerFactory.newTransformer(scriptSource);
77  
78              transformer.transform(xmlSource, new StreamResult(bam));
79          }
80          catch (TransformerConfigurationException tce) {
81              throw new TransformException(errorListener.getMessageAndLocation());
82          }
83          catch (TransformerException te) {
84              throw new TransformException(errorListener.getMessageAndLocation());
85          }
86  
87          return bam.toString("UTF-8");
88      }
89  
90  }