001/* 002 * Copyright 2018 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.runtime.model; 017 018import org.apache.avalon.framework.configuration.Configuration; 019import org.apache.avalon.framework.configuration.ConfigurationException; 020import org.apache.avalon.framework.service.ServiceManager; 021 022import org.ametys.runtime.i18n.I18nizableText; 023 024/** 025 * Parser for categorized {@link ElementDefinition} 026 */ 027public class CategorizedElementDefinitionParser 028{ 029 private ElementDefinitionParser _parser; 030 031 /** 032 * Creates a categorized element definition parser. 033 * @param parser the element definition parser 034 */ 035 public CategorizedElementDefinitionParser(ElementDefinitionParser parser) 036 { 037 _parser = parser; 038 } 039 040 /** 041 * Parses an element definition from a XML configuration. 042 * @param serviceManager the service manager 043 * @param pluginName the plugin name declaring this item. 044 * @param definitionConfig the XML configuration of the model item. 045 * @param model the model which defines the model item 046 * @param parent the parent of the model item to create. Can be null if the model item to parse has no parent 047 * @return the parsed model item. 048 * @throws ConfigurationException if the configuration is not valid. 049 */ 050 @SuppressWarnings("unchecked") 051 public CategorizedElementDefinitionWrapper parse(ServiceManager serviceManager, String pluginName, Configuration definitionConfig, Model model, ModelItemGroup parent) throws ConfigurationException 052 { 053 ElementDefinition definition = _parser.parse(serviceManager, pluginName, definitionConfig, model, parent); 054 CategorizedElementDefinitionWrapper elementDefinitionWrapper = _createElementDefinitionWrapper(); 055 056 elementDefinitionWrapper.setDefinition(definition); 057 elementDefinitionWrapper.setDisplayCategory(_parseI18nizableText(definitionConfig, pluginName, "category")); 058 elementDefinitionWrapper.setDisplayGroup(_parseI18nizableText(definitionConfig, pluginName, "group")); 059 elementDefinitionWrapper.setPosition(definitionConfig.getChild("order").getValueAsLong(-1)); 060 061 return elementDefinitionWrapper; 062 } 063 064 /** 065 * Create the element definition wrapper to populate it. 066 * @return the wrapper instantiated. 067 */ 068 protected CategorizedElementDefinitionWrapper _createElementDefinitionWrapper() 069 { 070 return new CategorizedElementDefinitionWrapper<>(); 071 } 072 073 /** 074 * Parses an i18n text. 075 * @param config the configuration to use. 076 * @param pluginName the current plugin name. 077 * @param name the child name. 078 * @return the i18n text. 079 * @throws ConfigurationException if the configuration is not valid. 080 */ 081 protected I18nizableText _parseI18nizableText(Configuration config, String pluginName, String name) throws ConfigurationException 082 { 083 return I18nizableText.parseI18nizableText(config.getChild(name), "plugin." + pluginName); 084 } 085}