001    /**
002     * Copyright (c) 2000-present 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.portal.kernel.template;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.configuration.Filter;
020    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.kernel.xml.SAXReaderUtil;
029    
030    import java.io.IOException;
031    
032    import java.util.Collections;
033    import java.util.List;
034    
035    /**
036     * @author Jorge Ferrer
037     */
038    @ProviderType
039    public abstract class BaseTemplateHandler implements TemplateHandler {
040    
041            @Override
042            public List<Element> getDefaultTemplateElements() throws Exception {
043                    String templatesConfigPath = getTemplatesConfigPath();
044    
045                    if (Validator.isNull(templatesConfigPath)) {
046                            return Collections.emptyList();
047                    }
048    
049                    Class<?> clazz = getClass();
050    
051                    String xml = StringUtil.read(
052                            clazz.getClassLoader(), templatesConfigPath, false);
053    
054                    Document document = SAXReaderUtil.read(xml);
055    
056                    Element rootElement = document.getRootElement();
057    
058                    return rootElement.elements("template");
059            }
060    
061            @Override
062            public String[] getRestrictedVariables(String language) {
063                    if (language.equals(TemplateConstants.LANG_TYPE_FTL)) {
064                            return PropsUtil.getArray(
065                                    PropsKeys.FREEMARKER_ENGINE_RESTRICTED_VARIABLES);
066                    }
067                    else if (language.equals(TemplateConstants.LANG_TYPE_VM)) {
068                            return PropsUtil.getArray(
069                                    PropsKeys.VELOCITY_ENGINE_RESTRICTED_VARIABLES);
070                    }
071    
072                    return new String[0];
073            }
074    
075            @Override
076            public String getTemplatesHelpContent(String language) {
077                    String content = StringPool.BLANK;
078    
079                    try {
080                            Class<?> clazz = getClass();
081    
082                            content = StringUtil.read(
083                                    clazz.getClassLoader(), getTemplatesHelpPath(language));
084                    }
085                    catch (IOException ioe1) {
086                            try {
087                                    content = StringUtil.read(
088                                            PortalClassLoaderUtil.getClassLoader(),
089                                            getTemplatesHelpPath(language));
090                            }
091                            catch (IOException ioe2) {
092                            }
093                    }
094    
095                    return content;
096            }
097    
098            @Override
099            public String getTemplatesHelpPath(String language) {
100                    return PropsUtil.get(
101                            getTemplatesHelpPropertyKey(), new Filter(language));
102            }
103    
104            @Override
105            public String getTemplatesHelpPropertyKey() {
106                    return PropsKeys.PORTLET_DISPLAY_TEMPLATES_HELP;
107            }
108    
109            protected String getTemplatesConfigPath() {
110                    return null;
111            }
112    
113    }