001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 ||
080                                    e instanceof TemplateException) {
081    
082                                    String errorTemplateId = getErrorTemplateId();
083                                    String errorTemplateContent = getErrorTemplateContent();
084    
085                                    freeMarkerContext.put("exception", e.getMessage());
086                                    freeMarkerContext.put("script", getScript());
087    
088                                    if (e instanceof ParseException) {
089                                            ParseException pe = (ParseException)e;
090    
091                                            freeMarkerContext.put("column", pe.getColumnNumber());
092                                            freeMarkerContext.put("line", pe.getLineNumber());
093                                    }
094    
095                                    unsyncStringWriter.reset();
096    
097                                    return FreeMarkerEngineUtil.mergeTemplate(
098                                            errorTemplateId, errorTemplateContent, freeMarkerContext,
099                                            unsyncStringWriter);
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107    }