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.web.parameters.view; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.configuration.Configuration; 022import org.apache.avalon.framework.configuration.ConfigurationException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.commons.lang.StringUtils; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028import org.ametys.runtime.model.ElementDefinitionParser; 029import org.ametys.runtime.model.Enumerator; 030import org.ametys.runtime.model.Model; 031import org.ametys.runtime.model.ModelItem; 032import org.ametys.runtime.model.ModelItemGroup; 033import org.ametys.runtime.parameter.Validator; 034import org.ametys.runtime.plugin.component.ThreadSafeComponentManager; 035 036/** 037 * This class parses the view parameters definition 038 */ 039public class ViewParameterDefinitionParser extends ElementDefinitionParser 040{ 041 /** The logger. */ 042 protected static final Logger _LOGGER = LoggerFactory.getLogger(ViewParameterDefinitionParser.class); 043 044 /** 045 * Creates a view parameters definition parser. 046 * @param viewParameterTypeExtensionPoint the extension point to use to get available element types 047 * @param enumeratorManager the enumerator component manager. 048 * @param validatorManager the validator component manager. 049 */ 050 public ViewParameterDefinitionParser(ViewParameterTypeExtensionPoint viewParameterTypeExtensionPoint, ThreadSafeComponentManager<Enumerator> enumeratorManager, ThreadSafeComponentManager<Validator> validatorManager) 051 { 052 super(viewParameterTypeExtensionPoint, enumeratorManager, validatorManager); 053 } 054 055 @Override 056 @SuppressWarnings("unchecked") 057 public <T extends ModelItem> T parse(ServiceManager serviceManager, String pluginName, String catalog, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException 058 { 059 ViewParameter viewParameter = (ViewParameter) super.parse(serviceManager, pluginName, catalog, definitionConfig, model, parent); 060 061 String inheritancesAsString = definitionConfig.getAttribute("inherit", null); 062 Map<String, String> inheritances = _parseInheritance(inheritancesAsString); 063 viewParameter.setInheritances(inheritances); 064 065 return (T) viewParameter; 066 } 067 068 @Override 069 protected ViewParameter _createModelItem(Configuration definitionConfig) throws ConfigurationException 070 { 071 return new ViewParameter(); 072 } 073 074 /** 075 * Parse the inheritance argument for view parameters 076 * Inheritance works only for template view parameters and zone view parameters 077 * @param inheritanceString the representation of the inheritance context for the templates 078 * @return the inheritance mapping between templates and zones 079 */ 080 private Map<String, String> _parseInheritance(String inheritanceString) 081 { 082 if (inheritanceString == null) 083 { 084 return null; 085 } 086 087 Map<String, String> inheritance = new HashMap<>(); 088 if (StringUtils.isNotEmpty(inheritanceString)) 089 { 090 String[] inheritenaceParts = inheritanceString.split("[, ]"); 091 for (String inheritenacePart : inheritenaceParts) 092 { 093 if (StringUtils.isNotEmpty(inheritenacePart)) 094 { 095 String templateName = null; 096 String zoneName = ""; 097 098 int i = inheritenacePart.indexOf("->"); 099 if (i == -1) 100 { 101 zoneName = inheritenacePart.trim(); 102 } 103 else 104 { 105 templateName = inheritenacePart.substring(0, i).trim(); 106 if ("*".equals(templateName)) 107 { 108 templateName = null; 109 } 110 111 zoneName = inheritenacePart.substring(i + 2).trim(); 112 if (StringUtils.isEmpty(zoneName)) 113 { 114 zoneName = null; 115 } 116 } 117 118 if (inheritance.get(templateName) != null) 119 { 120 _LOGGER.warn("The inheritance is declared as '" 121 + inheritanceString + "', but this declares twice the same template (" 122 + (templateName != null ? templateName : "*") 123 + "). The first declaration is kept intact."); 124 } 125 else 126 { 127 inheritance.put(templateName, zoneName); 128 } 129 } 130 } 131 } 132 133 return inheritance; 134 } 135}