001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.template;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    
026    import java.util.Collections;
027    import java.util.List;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public abstract class BaseTemplateHandler implements TemplateHandler {
033    
034            @Override
035            public List<Element> getDefaultTemplateElements() throws Exception {
036                    String templatesConfigPath = getTemplatesConfigPath();
037    
038                    if (Validator.isNull(templatesConfigPath)) {
039                            return Collections.emptyList();
040                    }
041    
042                    Class<?> clazz = getClass();
043    
044                    String xml = StringUtil.read(
045                            clazz.getClassLoader(), templatesConfigPath, false);
046    
047                    Document document = SAXReaderUtil.read(xml);
048    
049                    Element rootElement = document.getRootElement();
050    
051                    return rootElement.elements("template");
052            }
053    
054            @Override
055            public String[] getRestrictedVariables(String language) {
056                    if (language.equals(TemplateConstants.LANG_TYPE_FTL)) {
057                            return PropsUtil.getArray(
058                                    PropsKeys.FREEMARKER_ENGINE_RESTRICTED_VARIABLES);
059                    }
060                    else if (language.equals(TemplateConstants.LANG_TYPE_VM)) {
061                            return PropsUtil.getArray(
062                                    PropsKeys.VELOCITY_ENGINE_RESTRICTED_VARIABLES);
063                    }
064    
065                    return new String[0];
066            }
067    
068            @Override
069            public String getTemplatesHelpPath(String language) {
070                    return PropsUtil.get(
071                            getTemplatesHelpPropertyKey(), new Filter(language));
072            }
073    
074            @Override
075            public String getTemplatesHelpPropertyKey() {
076                    return PropsKeys.PORTLET_DISPLAY_TEMPLATES_HELP;
077            }
078    
079            protected String getTemplatesConfigPath() {
080                    return null;
081            }
082    
083    }