001/* 002 * Copyright 2010 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.core.ui; 017 018import java.io.InputStream; 019import java.util.LinkedHashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.apache.avalon.framework.configuration.DefaultConfiguration; 026import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.xml.AttributesImpl; 029import org.apache.cocoon.xml.XMLUtils; 030import org.apache.commons.lang3.StringUtils; 031import org.apache.excalibur.source.Source; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034import org.xml.sax.ContentHandler; 035import org.xml.sax.SAXException; 036 037/** 038 * Handles the configuration of uitools factories 039 */ 040public class UIToolsConfigurationManager 041{ 042 private static Logger _logger = LoggerFactory.getLogger(UIToolsConfigurationManager.class); 043 044 /** The default opened tools */ 045 protected Map<String, String> _defaultUITools = new LinkedHashMap<>(); 046 047 /** The ui tools factories manager */ 048 protected UIToolsFactoriesManager _uitoolsFactoriesManager; 049 050 /** The sax clientside element helper */ 051 protected SAXClientSideElementHelper _saxClientSideElementHelper; 052 053 /** Additional default tools to open */ 054 protected String[] _additionalDefaultTools; 055 056 /** 057 * Constructor 058 * @param uitoolsFactoriesManager The instance of ui tools manager 059 * @param saxClientSideElementHelper The instance of sax client helper 060 * @param configSource The configuration source 061 * @param request The request to open by default additionally to those configured 062 */ 063 public UIToolsConfigurationManager (UIToolsFactoriesManager uitoolsFactoriesManager, SAXClientSideElementHelper saxClientSideElementHelper, Source configSource, Request request) 064 { 065 _uitoolsFactoriesManager = uitoolsFactoriesManager; 066 _saxClientSideElementHelper = saxClientSideElementHelper; 067 068 try 069 { 070 Configuration configuration = null; 071 072 if (configSource.exists()) 073 { 074 try (InputStream configIs = configSource.getInputStream()) 075 { 076 configuration = new DefaultConfigurationBuilder().build(configIs); 077 } 078 } 079 else 080 { 081 configuration = new DefaultConfiguration("default"); 082 } 083 084 _configure(configuration); 085 } 086 catch (Exception e) 087 { 088 throw new RuntimeException("Unable to read the configuration file", e); 089 } 090 091 _additionalDefaultTools = request.getParameterValues("uitool"); 092 } 093 094 095 096 private void _configure(Configuration configuration) throws ConfigurationException 097 { 098 if (_logger.isDebugEnabled()) 099 { 100 _logger.debug("Starting reading uitools configuration"); 101 } 102 103 Configuration[] defaultUIToolFactoryConfigurations = configuration.getChild("default").getChildren("uitool-factory"); 104 for (Configuration uitoolFactoryConfiguration : defaultUIToolFactoryConfigurations) 105 { 106 String id = uitoolFactoryConfiguration.getAttribute("id"); 107 String parameters = uitoolFactoryConfiguration.getValue(""); 108 109 _defaultUITools.put(id, parameters); 110 } 111 112 if (_logger.isDebugEnabled()) 113 { 114 _logger.debug("Ending reading uitools configuration"); 115 } 116 } 117 118 /** 119 * SAX the default state of uitools to know which ones are opened 120 * @param handler Where to SAX 121 * @param contextualParameters Contextuals parameters transmitted by the environment. 122 * @param dependenciesList The list of dependencies 123 * @throws SAXException if an error occurs 124 */ 125 public void saxDefaultState(ContentHandler handler, Map<String, Object> contextualParameters, List<ClientSideElement> dependenciesList) throws SAXException 126 { 127 handler.startPrefixMapping("i18n", "http://apache.org/cocoon/i18n/2.1"); 128 XMLUtils.startElement(handler, "uitools-factories"); 129 130 XMLUtils.startElement(handler, "default"); 131 for (String id : _defaultUITools.keySet()) 132 { 133 AttributesImpl attrs = new AttributesImpl(); 134 attrs.addCDATAAttribute("id", id); 135 XMLUtils.createElement(handler, "uitool-factory", attrs, _defaultUITools.get(id)); 136 } 137 XMLUtils.endElement(handler, "default"); 138 139 XMLUtils.startElement(handler, "additionnal"); 140 if (_additionalDefaultTools != null) 141 { 142 for (String additionnalTool : _additionalDefaultTools) 143 { 144 if (!StringUtils.isEmpty(additionnalTool)) 145 { 146 int i = additionnalTool.indexOf(","); 147 String id = additionnalTool.trim(); 148 String params = ""; 149 if (i != -1) 150 { 151 id = additionnalTool.substring(0, i).trim(); 152 params = additionnalTool.substring(i + 1).trim(); 153 } 154 155 AttributesImpl attrs = new AttributesImpl(); 156 attrs.addCDATAAttribute("id", id); 157 XMLUtils.createElement(handler, "uitool-factory", attrs, params); 158 } 159 } 160 } 161 XMLUtils.endElement(handler, "additionnal"); 162 163 if (dependenciesList != null) 164 { 165 for (ClientSideElement factory : dependenciesList) 166 { 167 _saxClientSideElementHelper.saxDefinition("uitool-factory", factory, handler, contextualParameters); 168 } 169 } 170 171 XMLUtils.endElement(handler, "uitools-factories"); 172 handler.endPrefixMapping("i18n"); 173 } 174}