001    /**
002     * Copyright (c) 2000-2013 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.freemarker.JournalTemplateLoader;
018    import com.liferay.portal.kernel.freemarker.FreeMarkerContext;
019    import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
020    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
021    import com.liferay.portal.kernel.templateparser.TemplateContext;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.util.ContentUtil;
026    
027    import freemarker.core.ParseException;
028    
029    import freemarker.template.TemplateException;
030    
031    /**
032     * @author Mika Koivisto
033     */
034    public class FreeMarkerTemplateParser extends VelocityTemplateParser {
035    
036            @Override
037            protected String getErrorTemplateContent() {
038                    return ContentUtil.get(PropsValues.JOURNAL_ERROR_TEMPLATE_FREEMARKER);
039            }
040    
041            @Override
042            protected String getErrorTemplateId() {
043                    return PropsValues.JOURNAL_ERROR_TEMPLATE_FREEMARKER;
044            }
045    
046            @Override
047            protected String getJournalTemplatesPath() {
048                    StringBundler sb = new StringBundler(5);
049    
050                    sb.append(JournalTemplateLoader.JOURNAL_SEPARATOR);
051                    sb.append(StringPool.SLASH);
052                    sb.append(getCompanyId());
053                    sb.append(StringPool.SLASH);
054                    sb.append(getGroupId());
055    
056                    return sb.toString();
057            }
058    
059            @Override
060            protected TemplateContext getTemplateContext() {
061                    return FreeMarkerEngineUtil.getWrappedRestrictedToolsContext();
062            }
063    
064            @Override
065            protected boolean mergeTemplate(
066                            TemplateContext templateContext,
067                            UnsyncStringWriter unsyncStringWriter)
068                    throws Exception {
069    
070                    FreeMarkerContext freeMarkerContext =
071                            (FreeMarkerContext)templateContext;
072    
073                    try {
074                            return FreeMarkerEngineUtil.mergeTemplate(
075                                    getTemplateId(), getScript(), freeMarkerContext,
076                                    unsyncStringWriter);
077                    }
078                    catch (Exception e) {
079                            if (e instanceof ParseException || e instanceof TemplateException) {
080                                    String errorTemplateId = getErrorTemplateId();
081                                    String errorTemplateContent = getErrorTemplateContent();
082    
083                                    freeMarkerContext.put("exception", e.getMessage());
084                                    freeMarkerContext.put("script", getScript());
085    
086                                    if (e instanceof ParseException) {
087                                            ParseException pe = (ParseException)e;
088    
089                                            freeMarkerContext.put("column", pe.getColumnNumber());
090                                            freeMarkerContext.put("line", pe.getLineNumber());
091                                    }
092    
093                                    unsyncStringWriter.reset();
094    
095                                    return FreeMarkerEngineUtil.mergeTemplate(
096                                            errorTemplateId, errorTemplateContent, freeMarkerContext,
097                                            unsyncStringWriter);
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103            }
104    
105    }