001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.welcome; 017 018import java.io.IOException; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.excalibur.source.SourceResolver; 028 029import org.ametys.runtime.plugin.component.AbstractLogEnabled; 030 031/** 032 * Helper for the welcome button / tool / page 033 */ 034public class WelcomeHelper extends AbstractLogEnabled implements Serviceable, Component 035{ 036 /** The Avalon role */ 037 public static final String ROLE = WelcomeHelper.class.getName(); 038 039 /** Root path for che welcome folder */ 040 private static final String __WELCOME_DIRECTORY_ROOT_PATH = "context://WEB-INF/param/welcome/"; 041 042 /** Source Resolver */ 043 protected SourceResolver _resolver; 044 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 048 } 049 050 /** 051 * Get the path of XSLT file for the welcome tool or null if not found 052 * @param parameters the sitemap parameters 053 * @return the XSLT file path or null if not found 054 */ 055 public String getXsltPath(Parameters parameters) 056 { 057 Map<String, Object> map = new HashMap<>(); 058 for (String name : parameters.getNames()) 059 { 060 map.put(name, parameters.getParameter(name, "")); 061 } 062 return getXsltPath(map); 063 } 064 065 /** 066 * Get the path of XSLT file for the welcome tool or null if not found 067 * @param contextualParameters the contextual parameters 068 * @return the XSLT file path or null if not found 069 */ 070 public String getXsltPath(Map<String, Object> contextualParameters) 071 { 072 String language = (String) contextualParameters.get("language"); 073 074 String languagePath = __WELCOME_DIRECTORY_ROOT_PATH + "index_" + language + ".xsl"; 075 076 try 077 { 078 if (_resolver.resolveURI(languagePath).exists()) 079 { 080 return languagePath; 081 } 082 else if (_resolver.resolveURI(__WELCOME_DIRECTORY_ROOT_PATH + "index.xsl").exists()) 083 { 084 return __WELCOME_DIRECTORY_ROOT_PATH + "index.xsl"; 085 } 086 } 087 catch (IOException e) 088 { 089 getLogger().warn("Unable to retrieve XSLT file for welcome tool", e); 090 } 091 092 return null; 093 } 094}