001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.security.permission.PermissionThreadLocal;
024    import com.liferay.portal.service.CompanyLocalServiceUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.ContentUtil;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.velocity.VelocityResourceListener;
029    import com.liferay.util.PwdGenerator;
030    
031    import java.util.Locale;
032    import java.util.Map;
033    
034    import javax.xml.transform.Transformer;
035    import javax.xml.transform.TransformerFactory;
036    import javax.xml.transform.stream.StreamResult;
037    import javax.xml.transform.stream.StreamSource;
038    
039    /**
040     * @author Alexander Chow
041     * @author Raymond Augé
042     */
043    public class XSLTemplateParser extends BaseTemplateParser {
044    
045            protected String doTransform(
046                            ThemeDisplay themeDisplay, Map<String, String> tokens,
047                            String viewMode, String languageId, String xml, String script)
048                    throws Exception {
049    
050                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
051                            new UnsyncByteArrayOutputStream();
052    
053                    long companyId = GetterUtil.getLong(tokens.get("company_id"));
054                    Company company = CompanyLocalServiceUtil.getCompanyById(companyId);
055                    long groupId = GetterUtil.getLong(tokens.get("group_id"));
056                    String journalTemplatesPath =
057                            VelocityResourceListener.JOURNAL_SEPARATOR + StringPool.SLASH +
058                                    companyId + StringPool.SLASH + groupId;
059                    String randomNamespace =
060                            PwdGenerator.getPassword(PwdGenerator.KEY3, 4) +
061                                    StringPool.UNDERLINE;
062                    Locale locale = LocaleUtil.fromLanguageId(languageId);
063    
064                    XSLErrorListener xslErrorListener = new XSLErrorListener(locale);
065    
066                    StreamSource xmlSource = new StreamSource(new UnsyncStringReader(xml));
067    
068                    TransformerFactory transformerFactory =
069                            TransformerFactory.newInstance();
070    
071                    transformerFactory.setURIResolver(new URIResolver(tokens, languageId));
072                    transformerFactory.setErrorListener(xslErrorListener);
073    
074                    try {
075                            StreamSource scriptSource = new StreamSource(
076                                    new UnsyncStringReader(script));
077    
078                            Transformer transformer = transformerFactory.newTransformer(
079                                    scriptSource);
080    
081                            transformer.setParameter("company", company);
082                            transformer.setParameter("companyId", new Long(companyId));
083                            transformer.setParameter("groupId", String.valueOf(groupId));
084                            transformer.setParameter(
085                                    "journalTemplatesPath", journalTemplatesPath);
086                            transformer.setParameter("viewMode", viewMode);
087                            transformer.setParameter("locale", locale);
088                            transformer.setParameter(
089                                    "permissionChecker",
090                                    PermissionThreadLocal.getPermissionChecker());
091                            transformer.setParameter("randomNamespace", randomNamespace);
092    
093                            transformer.transform(
094                                    xmlSource, new StreamResult(unsyncByteArrayOutputStream));
095                    }
096                    catch (Exception e1) {
097                            String errorTemplate = ContentUtil.get(
098                                    PropsValues.JOURNAL_ERROR_TEMPLATE_XSL);
099    
100                            StreamSource scriptSource = new StreamSource(
101                                    new UnsyncStringReader(errorTemplate));
102    
103                            Transformer transformer = transformerFactory.newTransformer(
104                                    scriptSource);
105    
106                            transformer.setParameter("company", company);
107                            transformer.setParameter("companyId", new Long(companyId));
108                            transformer.setParameter("groupId", String.valueOf(groupId));
109                            transformer.setParameter(
110                                    "journalTemplatesPath", journalTemplatesPath);
111                            transformer.setParameter("locale", locale);
112                            transformer.setParameter("randomNamespace", randomNamespace);
113    
114                            transformer.setParameter(
115                                    "exception", xslErrorListener.getMessageAndLocation());
116                            transformer.setParameter("script", script);
117    
118                            if (xslErrorListener.getLocation() != null) {
119                                    transformer.setParameter(
120                                            "column", new Integer(xslErrorListener.getColumnNumber()));
121                                    transformer.setParameter(
122                                            "line", new Integer(xslErrorListener.getLineNumber()));
123                            }
124    
125                            transformer.transform(
126                                    xmlSource, new StreamResult(unsyncByteArrayOutputStream));
127                    }
128    
129                    return unsyncByteArrayOutputStream.toString(StringPool.UTF8);
130            }
131    
132    }