001    /**
002     * Copyright (c) 2000-2012 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.xsl;
016    
017    import com.liferay.portal.kernel.template.Template;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.template.TemplateContextType;
020    import com.liferay.portal.kernel.template.TemplateManager;
021    import com.liferay.portal.kernel.template.TemplateResource;
022    import com.liferay.portal.template.PACLTemplateWrapper;
023    import com.liferay.portal.template.RestrictedTemplate;
024    import com.liferay.portal.template.TemplateContextHelper;
025    
026    import java.util.Map;
027    
028    /**
029     * @author Tina Tian
030     */
031    public class XSLManager implements TemplateManager {
032    
033            public void destroy() {
034                    if (_templateContextHelper == null) {
035                            return;
036                    }
037    
038                    _templateContextHelper.removeAllHelperUtilities();
039    
040                    _templateContextHelper = null;
041            }
042    
043            public void destroy(ClassLoader classLoader) {
044                    _templateContextHelper.removeHelperUtilities(classLoader);
045            }
046    
047            public String getName() {
048                    return TemplateConstants.LANG_TYPE_XSL;
049            }
050    
051            public Template getTemplate(
052                    TemplateResource templateResource,
053                    TemplateContextType templateContextType) {
054    
055                    return getTemplate(templateResource, null, templateContextType);
056            }
057    
058            public Template getTemplate(
059                    TemplateResource templateResource,
060                    TemplateResource errorTemplateResource,
061                    TemplateContextType templateContextType) {
062    
063                    Template template = null;
064    
065                    XSLTemplateResource xslTemplateResource =
066                            (XSLTemplateResource)templateResource;
067    
068                    Map<String, Object> context = _templateContextHelper.getHelperUtilities(
069                            templateContextType);
070    
071                    if (templateContextType.equals(TemplateContextType.EMPTY)) {
072                            template = new XSLTemplate(
073                                    xslTemplateResource, errorTemplateResource, null,
074                                    _templateContextHelper);
075                    }
076                    else if (templateContextType.equals(TemplateContextType.RESTRICTED)) {
077                            template = new RestrictedTemplate(
078                                    new XSLTemplate(
079                                            xslTemplateResource, errorTemplateResource, context,
080                                            _templateContextHelper),
081                                    _templateContextHelper.getRestrictedVariables());
082                    }
083                    else if (templateContextType.equals(TemplateContextType.STANDARD)) {
084                            template = new XSLTemplate(
085                                    xslTemplateResource, errorTemplateResource, context,
086                                    _templateContextHelper);
087                    }
088    
089                    return PACLTemplateWrapper.getTemplate(template);
090            }
091    
092            public void init() {
093            }
094    
095            public void setTemplateContextHelper(
096                    TemplateContextHelper templateContextHelper) {
097    
098                    _templateContextHelper = templateContextHelper;
099            }
100    
101            private TemplateContextHelper _templateContextHelper;
102    
103    }