1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.GetterUtil;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Company;
29  import com.liferay.portal.security.permission.PermissionThreadLocal;
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.ByteArrayOutputStream;
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  public class JournalXslUtil {
54  
55      public static String transform(
56              Map<String, String> tokens, String viewMode, String languageId,
57              String xml, String script)
58          throws Exception {
59  
60          ByteArrayOutputStream baos = new ByteArrayOutputStream();
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              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("viewMode", viewMode);
97              transformer.setParameter("locale", locale);
98              transformer.setParameter(
99                  "permissionChecker",
100                 PermissionThreadLocal.getPermissionChecker());
101             transformer.setParameter("randomNamespace", randomNamespace);
102 
103             transformer.transform(xmlSource, new StreamResult(baos));
104         }
105         catch (Exception e1) {
106             String errorTemplate = ContentUtil.get(
107                 PropsValues.JOURNAL_ERROR_TEMPLATE_XSL);
108 
109             StreamSource scriptSource = new StreamSource(
110                 new StringReader(errorTemplate));
111 
112             Transformer transformer = transformerFactory.newTransformer(
113                 scriptSource);
114 
115             transformer.setParameter("company", company);
116             transformer.setParameter("companyId", new Long(companyId));
117             transformer.setParameter("groupId", String.valueOf(groupId));
118             transformer.setParameter(
119                 "journalTemplatesPath", journalTemplatesPath);
120             transformer.setParameter("locale", locale);
121             transformer.setParameter("randomNamespace", randomNamespace);
122 
123             transformer.setParameter(
124                 "exception", errorListener.getMessageAndLocation());
125             transformer.setParameter("script", script);
126 
127             if (errorListener.getLocation() != null) {
128                 transformer.setParameter(
129                     "column", new Integer(errorListener.getColumnNumber()));
130                 transformer.setParameter(
131                     "line", new Integer(errorListener.getLineNumber()));
132             }
133 
134             transformer.transform(xmlSource, new StreamResult(baos));
135         }
136 
137         return baos.toString(StringPool.UTF8);
138     }
139 
140 }