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.contenttype; 017 018import org.apache.avalon.framework.component.Component; 019import org.apache.avalon.framework.configuration.Configuration; 020import org.apache.avalon.framework.configuration.ConfigurationException; 021 022import org.ametys.runtime.i18n.I18nizableText; 023 024/** 025 * Helper for content types parsing 026 */ 027public class ContentTypesParserHelper implements Component 028{ 029 /** The Avalon role */ 030 public static final String ROLE = ContentTypesParserHelper.class.getName(); 031 032 /** 033 * Parse an i18n text. 034 * @param contentType the content type 035 * @param config the configuration to use. 036 * @param name the child name. 037 * @return the i18n text. 038 * @throws ConfigurationException if the configuration is not valid. 039 */ 040 public I18nizableText parseI18nizableText(ContentTypeDescriptor contentType, Configuration config, String name) throws ConfigurationException 041 { 042 return parseI18nizableText(contentType, config, name, ""); 043 } 044 045 /** 046 * Parse an i18n text. 047 * @param contentType the content type 048 * @param config the configuration to use. 049 * @param name the child name. 050 * @param defaultValue the default value if no present 051 * @return the i18n text. 052 * @throws ConfigurationException if the configuration is not valid. 053 */ 054 public I18nizableText parseI18nizableText(ContentTypeDescriptor contentType, Configuration config, String name, String defaultValue) throws ConfigurationException 055 { 056 return I18nizableText.parseI18nizableText(config.getChild(name), contentType.getDefaultCatalog(), defaultValue); 057 } 058 059 /** 060 * Parse an icon path 061 * @param contentType the content type 062 * @param configuration the configuration to use 063 * @param name the child name. 064 * @return The icon path 065 * @throws ConfigurationException if the configuration is not valid. 066 */ 067 public String parseIcon (ContentTypeDescriptor contentType, Configuration configuration, String name) throws ConfigurationException 068 { 069 return parseIcon(contentType, configuration, name, "/plugins/cms/resources/img/contenttype/unknown-" + name + ".png"); 070 } 071 072 /** 073 * Parse an icon path 074 * @param contentType the content type 075 * @param configuration the configuration to use 076 * @param name the child name. 077 * @param defaultValue the default value. 078 * @return The icon path 079 * @throws ConfigurationException if the configuration is not valid. 080 */ 081 public String parseIcon(ContentTypeDescriptor contentType, Configuration configuration, String name, String defaultValue) throws ConfigurationException 082 { 083 Configuration iconConfig = configuration.getChild(name, false); 084 if (iconConfig != null) 085 { 086 String pluginName = iconConfig.getAttribute("plugin", contentType.getPluginName()); 087 return contentType.getIconPath(pluginName) + iconConfig.getValue(); 088 } 089 090 return defaultValue; 091 } 092 093 /** 094 * Parse the icon glyph. If not present, a default will be assigned if a icon is not yet defined 095 * @param configuration the configuration 096 * @param viewName The view name 097 * @param defaultGlyph The default glyph to assign if no glyph is defined. Can be null. 098 * @return the glyph icon 099 */ 100 public String parseIconGlyph (Configuration configuration, String viewName, String defaultGlyph) 101 { 102 String defaultValue = defaultGlyph; 103 switch (viewName) 104 { 105 case "main": 106 defaultValue = "ametysicon-document112"; 107 break; 108 case "abstract": 109 defaultValue = "ametysicon-document77"; 110 break; 111 case "link": 112 defaultValue = "ametysicon-internet58"; 113 break; 114 default: 115 break; 116 } 117 118 return configuration.getChild("glyph").getValue(defaultValue); 119 } 120}