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.UnsecureSAXReaderUtil;
029    
030    import java.io.IOException;
031    
032    import java.util.Collections;
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Jorge Ferrer
038     */
039    @ProviderType
040    public abstract class BaseTemplateHandler implements TemplateHandler {
041    
042            @Override
043            public Map<String, Object> getCustomContextObjects() {
044                    return Collections.emptyMap();
045            }
046    
047            @Override
048            public List<Element> getDefaultTemplateElements() throws Exception {
049                    String templatesConfigPath = getTemplatesConfigPath();
050    
051                    if (Validator.isNull(templatesConfigPath)) {
052                            return Collections.emptyList();
053                    }
054    
055                    Class<?> clazz = getClass();
056    
057                    String xml = StringUtil.read(
058                            clazz.getClassLoader(), templatesConfigPath, false);
059    
060                    Document document = UnsecureSAXReaderUtil.read(xml);
061    
062                    Element rootElement = document.getRootElement();
063    
064                    return rootElement.elements("template");
065            }
066    
067            @Override
068            public String getDefaultTemplateKey() {
069                    return null;
070            }
071    
072            @Override
073            public String[] getRestrictedVariables(String language) {
074                    TemplateManager templateManager =
075                            TemplateManagerUtil.getTemplateManager(language);
076    
077                    if (templateManager != null) {
078                            return templateManager.getRestrictedVariables();
079                    }
080    
081                    return new String[0];
082            }
083    
084            @Override
085            public String getTemplatesHelpContent(String language) {
086                    String content = StringPool.BLANK;
087    
088                    try {
089                            Class<?> clazz = getClass();
090    
091                            content = StringUtil.read(
092                                    clazz.getClassLoader(), getTemplatesHelpPath(language));
093                    }
094                    catch (IOException ioe1) {
095                            try {
096                                    content = StringUtil.read(
097                                            PortalClassLoaderUtil.getClassLoader(),
098                                            getTemplatesHelpPath(language));
099                            }
100                            catch (IOException ioe2) {
101                            }
102                    }
103    
104                    return content;
105            }
106    
107            @Override
108            public String getTemplatesHelpPath(String language) {
109                    return PropsUtil.get(
110                            getTemplatesHelpPropertyKey(), new Filter(language));
111            }
112    
113            @Override
114            public String getTemplatesHelpPropertyKey() {
115                    return PropsKeys.PORTLET_DISPLAY_TEMPLATES_HELP;
116            }
117    
118            protected String getTemplatesConfigPath() {
119                    return null;
120            }
121    
122    }