001/* 002 * Copyright 2012 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.site; 017 018import java.io.IOException; 019import java.util.List; 020import java.util.Map; 021import java.util.Map.Entry; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.cocoon.ProcessingException; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.xml.sax.SAXException; 030 031import org.ametys.cms.languages.Language; 032import org.ametys.cms.languages.LanguagesManager; 033import org.ametys.plugins.repository.UnknownAmetysObjectException; 034import org.ametys.runtime.i18n.I18nizableText; 035import org.ametys.runtime.parameter.ParameterHelper; 036import org.ametys.web.repository.site.Site; 037import org.ametys.web.repository.site.SiteManager; 038import org.ametys.web.repository.sitemap.Sitemap; 039 040/** 041 * Generates the site parameters (sorted by category and group) and their values, for a given site. 042 */ 043public class SiteParametersGenerator extends ServiceableGenerator 044{ 045 /** The site manager. */ 046 protected SiteManager _siteManager; 047 048 /** The site configuration. */ 049 protected SiteConfigurationExtensionPoint _siteConfiguration; 050 051 /** The available languages manager */ 052 protected LanguagesManager _languagesManager; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE); 059 _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE); 060 _languagesManager = (LanguagesManager) serviceManager.lookup(LanguagesManager.ROLE); 061 } 062 063 @Override 064 public void generate() throws IOException, SAXException, ProcessingException 065 { 066 String siteName = parameters.getParameter("siteName", ""); 067 068 try 069 { 070 contentHandler.startDocument(); 071 072 Site site = _siteManager.getSite(siteName); 073 074 AttributesImpl atts = new AttributesImpl(); 075 atts.addCDATAAttribute("id", site.getId()); 076 atts.addCDATAAttribute("path", site.getSitePath()); 077 atts.addCDATAAttribute("name", siteName); 078 atts.addCDATAAttribute("title", site.getTitle()); 079 XMLUtils.startElement(contentHandler, "site", atts); 080 081 _saxSitemaps(site); 082 083 _saxParameters(siteName); 084 085 XMLUtils.endElement(contentHandler, "site"); 086 contentHandler.endDocument(); 087 } 088 catch (UnknownAmetysObjectException e) 089 { 090 String message = "The site '" + siteName + "' does not exist."; 091 getLogger().error(message, e); 092 throw new ProcessingException(message, e); 093 } 094 095 } 096 097 /** 098 * Generate the site's parameters. 099 * @param siteName the site name. 100 * @throws ProcessingException if an error occurs processing the request. 101 * @throws SAXException if an error occurs SAXing the data. 102 */ 103 protected void _saxParameters(String siteName) throws ProcessingException, SAXException 104 { 105 Map<I18nizableText, Map<I18nizableText, List<SiteParameter>>> siteParameters = _siteConfiguration.getCategorizedParameters(siteName); 106 107 XMLUtils.startElement(contentHandler, "parameters"); 108 109 XMLUtils.startElement(contentHandler, "categories"); 110 111 for (Entry<I18nizableText, Map<I18nizableText, List<SiteParameter>>> categoryEntry : siteParameters.entrySet()) 112 { 113 I18nizableText categoryLabel = categoryEntry.getKey(); 114 Map<I18nizableText, List<SiteParameter>> groups = categoryEntry.getValue(); 115 116 XMLUtils.startElement(contentHandler, "category"); 117 categoryLabel.toSAX(contentHandler, "label"); 118 119 XMLUtils.startElement(contentHandler, "groups"); 120 121 for (Entry<I18nizableText, List<SiteParameter>> groupEntry : groups.entrySet()) 122 { 123 I18nizableText groupLabel = groupEntry.getKey(); 124 List<SiteParameter> groupParams = groupEntry.getValue(); 125 126 XMLUtils.startElement(contentHandler, "group"); 127 groupLabel.toSAX(contentHandler, "label"); 128 129 XMLUtils.startElement(contentHandler, "parameters"); 130 for (SiteParameter param : groupParams) 131 { 132 Object value = _siteConfiguration.getValue(siteName, param.getId()); 133 ParameterHelper.toSAXParameter(contentHandler, param, value); 134 } 135 XMLUtils.endElement(contentHandler, "parameters"); 136 137 XMLUtils.endElement(contentHandler, "group"); 138 } 139 140 XMLUtils.endElement(contentHandler, "groups"); 141 142 XMLUtils.endElement(contentHandler, "category"); 143 } 144 145 XMLUtils.endElement(contentHandler, "categories"); 146 147 XMLUtils.endElement(contentHandler, "parameters"); 148 } 149 150 /** 151 * Generate the sitemaps of a site. 152 * @param site the site. 153 * @throws SAXException if an error occurs. 154 * @throws ProcessingException if an error occurs. 155 */ 156 protected void _saxSitemaps(Site site) throws ProcessingException, SAXException 157 { 158 _saxSelectedSitemaps(site); 159 160 // Now saxing possible 161 _saxPossibleSitemaps(); 162 } 163 164 private void _saxPossibleSitemaps() throws SAXException 165 { 166 XMLUtils.startElement(contentHandler, "possible-sitemaps"); 167 168 Map<String, Language> languages = _languagesManager.getAvailableLanguages(); 169 for (String language : languages.keySet()) 170 { 171 AttributesImpl atts = new AttributesImpl(); 172 atts.addCDATAAttribute("name", language); 173 XMLUtils.startElement(contentHandler, "sitemap", atts); 174 languages.get(language).getLabel().toSAX(contentHandler); 175 XMLUtils.endElement(contentHandler, "sitemap"); 176 } 177 178 XMLUtils.endElement(contentHandler, "possible-sitemaps"); 179 } 180 181 private void _saxSelectedSitemaps(Site site) throws SAXException 182 { 183 XMLUtils.startElement(contentHandler, "sitemaps"); 184 for (Sitemap sitemap : site.getSitemaps()) 185 { 186 XMLUtils.createElement(contentHandler, "sitemap", sitemap.getName()); 187 } 188 XMLUtils.endElement(contentHandler, "sitemaps"); 189 } 190}