001/* 002 * Copyright 2022 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 */ 016 017package org.ametys.cms.color; 018 019import java.io.IOException; 020import java.util.Map; 021 022import org.apache.avalon.framework.configuration.Configurable; 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.apache.commons.lang3.StringUtils; 032import org.xml.sax.SAXException; 033 034 035/** 036 * Generates the colors from the component 037 */ 038public class ColorsGenerator extends ServiceableGenerator implements Configurable 039{ 040 /** The colors component */ 041 protected AbstractColorsComponent _colorsComponent; 042 043 private ServiceManager _smanager; 044 045 @Override 046 public void service(ServiceManager smanager) throws ServiceException 047 { 048 super.service(smanager); 049 _smanager = smanager; 050 } 051 052 @Override 053 public void configure(Configuration configuration) throws ConfigurationException 054 { 055 String componentRole = configuration.getChild("role").getValue(); 056 if (StringUtils.isBlank(componentRole)) 057 { 058 throw new IllegalArgumentException("The child role is mandatory"); 059 } 060 061 try 062 { 063 _colorsComponent = (AbstractColorsComponent) _smanager.lookup(componentRole); 064 } 065 catch (ServiceException e) 066 { 067 throw new IllegalArgumentException("Can't get component with role " + componentRole, e); 068 } 069 } 070 071 public void generate() throws IOException, SAXException, ProcessingException 072 { 073 contentHandler.startDocument(); 074 075 AttributesImpl attrs = new AttributesImpl(); 076 attrs.addCDATAAttribute("default", _getDefaultKey()); 077 XMLUtils.startElement(contentHandler, "colors", attrs); 078 079 Map<String, Map<String, String>> colors = _getColors(); 080 for (String id : colors.keySet()) 081 { 082 AttributesImpl attrs2 = new AttributesImpl(); 083 attrs2.addCDATAAttribute("id", id); 084 XMLUtils.startElement(contentHandler, "color", attrs2); 085 086 Map<String, String> map = colors.get(id); 087 for (String key : map.keySet()) 088 { 089 XMLUtils.createElement(contentHandler, key, map.get(key)); 090 } 091 092 XMLUtils.endElement(contentHandler, "color"); 093 } 094 095 XMLUtils.endElement(contentHandler, "colors"); 096 contentHandler.endDocument(); 097 } 098 099 /** 100 * Get colors 101 * @return the colors 102 */ 103 protected Map<String, Map<String, String>> _getColors() 104 { 105 return _colorsComponent.getColors(); 106 } 107 108 /** 109 * Get default key 110 * @return the default key 111 */ 112 protected String _getDefaultKey() 113 { 114 return _colorsComponent.getDefaultKey(); 115 } 116}