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.portal.model.Company;
30  import com.liferay.portal.service.CompanyLocalServiceUtil;
31  import com.liferay.portal.util.ContentUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.velocity.VelocityResourceListener;
34  import com.liferay.util.PwdGenerator;
35  
36  import java.io.StringReader;
37  
38  import java.util.Locale;
39  import java.util.Map;
40  
41  import javax.xml.transform.Transformer;
42  import javax.xml.transform.TransformerFactory;
43  import javax.xml.transform.stream.StreamResult;
44  import javax.xml.transform.stream.StreamSource;
45  
46  /**
47   * <a href="JournalXslUtil.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Alexander Chow
50   * @author Raymond Augé
51   *
52   */
53  public class JournalXslUtil {
54  
55      public static String transform(
56              Map<String, String> tokens, String languageId, String xml,
57              String script)
58          throws Exception {
59  
60          ByteArrayMaker bam = new ByteArrayMaker();
61  
62          long companyId = GetterUtil.getLong(tokens.get("company_id"));
63          Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
64          long groupId = GetterUtil.getLong(tokens.get("group_id"));
65          String journalTemplatesPath =
66              VelocityResourceListener.JOURNAL_SEPARATOR + StringPool.SLASH +
67                  companyId + StringPool.SLASH + groupId;
68          String randomNamespace =
69              PwdGenerator.getPassword(PwdGenerator.KEY3, 4) +
70                  StringPool.UNDERLINE;
71          Locale locale = LocaleUtil.fromLanguageId(languageId);
72  
73          JournalXslErrorListener errorListener = new JournalXslErrorListener(
74              companyId, locale);
75  
76          StreamSource xmlSource = new StreamSource(new StringReader(xml));
77  
78          TransformerFactory transformerFactory =
79              TransformerFactory.newInstance();
80  
81          transformerFactory.setURIResolver(new URIResolver(tokens, languageId));
82          transformerFactory.setErrorListener(errorListener);
83  
84          try {
85              StreamSource scriptSource = new StreamSource(
86                  new StringReader(script));
87  
88              Transformer transformer = transformerFactory.newTransformer(
89                  scriptSource);
90  
91              transformer.setParameter("company", company);
92              transformer.setParameter("companyId", new Long(companyId));
93              transformer.setParameter("groupId", String.valueOf(groupId));
94              transformer.setParameter(
95                  "journalTemplatesPath", journalTemplatesPath);
96              transformer.setParameter("locale", locale);
97              transformer.setParameter("randomNamespace", randomNamespace);
98  
99              transformer.transform(xmlSource, new StreamResult(bam));
100         }
101         catch (Exception e1) {
102             String errorTemplate = ContentUtil.get(
103                 PropsValues.JOURNAL_ERROR_TEMPLATE_XSL);
104 
105             StreamSource scriptSource = new StreamSource(
106                 new StringReader(errorTemplate));
107 
108             Transformer transformer = transformerFactory.newTransformer(
109                 scriptSource);
110 
111             transformer.setParameter("company", company);
112             transformer.setParameter("companyId", new Long(companyId));
113             transformer.setParameter("groupId", String.valueOf(groupId));
114             transformer.setParameter(
115                 "journalTemplatesPath", journalTemplatesPath);
116             transformer.setParameter("locale", locale);
117             transformer.setParameter("randomNamespace", randomNamespace);
118 
119             transformer.setParameter(
120                 "exception", errorListener.getMessageAndLocation());
121             transformer.setParameter("script", script);
122 
123             if (errorListener.getLocation() != null) {
124                 transformer.setParameter(
125                     "column", new Integer(errorListener.getColumnNumber()));
126                 transformer.setParameter(
127                     "line", new Integer(errorListener.getLineNumber()));
128             }
129 
130             transformer.transform(xmlSource, new StreamResult(bam));
131         }
132 
133         return bam.toString(StringPool.UTF8);
134     }
135 
136 }