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