001/*
002 *  Copyright 2010 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.workspace;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.generation.ServiceableGenerator;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.xml.sax.SAXException;
028
029import org.ametys.cms.languages.Language;
030import org.ametys.cms.languages.LanguagesManager;
031import org.ametys.runtime.i18n.I18nizableText;
032
033/**
034 * SAX events for available languages.
035 *
036 */
037public class LanguagesGenerator extends ServiceableGenerator
038{
039    private LanguagesManager _languagesManager;
040
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        _languagesManager = (LanguagesManager) smanager.lookup(LanguagesManager.ROLE);
046    }
047
048    @Override
049    public void generate() throws IOException, SAXException, ProcessingException
050    {
051        contentHandler.startDocument();
052        XMLUtils.startElement(contentHandler, "languages");
053        
054        Map<String, Language> availableLanguages = _languagesManager.getAvailableLanguages();
055        
056        for (Language language : availableLanguages.values())
057        {
058            saxLanguage(language);
059        }
060
061        XMLUtils.endElement(contentHandler, "languages");
062        contentHandler.endDocument();
063    }
064    
065    /**
066     * Sax the language informations
067     * @param language The language to sax
068     * @throws SAXException if an error occurred
069     */
070    protected void saxLanguage (Language language) throws SAXException
071    {
072        AttributesImpl attrs = new AttributesImpl();
073        attrs.addAttribute("", "name", "name", "CDATA", language.getCode());
074        XMLUtils.startElement(contentHandler, "language", attrs);
075        
076        language.getLabel().toSAX(contentHandler, "label");
077        new I18nizableText(language.getSmallIcon()).toSAX(contentHandler, "small-icon");
078        new I18nizableText(language.getMediumIcon()).toSAX(contentHandler, "medium-icon");
079        new I18nizableText(language.getLargeIcon()).toSAX(contentHandler, "large-icon");
080        
081        XMLUtils.endElement(contentHandler, "language");
082    }
083
084}