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