001/*
002 *  Copyright 2011 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.odf.program.generators;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.generation.Generator;
023import org.apache.cocoon.xml.XMLUtils;
024import org.xml.sax.SAXException;
025
026import org.ametys.cms.content.ContentGenerator;
027import org.ametys.cms.repository.Content;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.plugins.repository.UnknownAmetysObjectException;
030
031/**
032 * {@link Generator} for rendering ODF content data potentially synchronized.
033 */
034public class ODFContentGenerator extends ContentGenerator
035{
036    /** The Ametys resolver */
037    protected AmetysObjectResolver _resolver;
038    
039    @Override
040    public void service(ServiceManager serviceManager) throws ServiceException
041    {
042        super.service(serviceManager);
043        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
044    }
045    
046    /**
047     * Sax the existing translations
048     * @param translations The translations
049     * @throws SAXException if an error occurs
050     */
051    protected void saxTranslations (Map<String, String> translations) throws SAXException
052    {
053        XMLUtils.startElement(contentHandler, "translations");
054        for (String lang : translations.keySet())
055        {
056            String contentId = translations.get(lang);
057            try
058            {
059                Content translatedContent = _resolver.resolveById(contentId);
060                XMLUtils.createElement(contentHandler, lang, translatedContent.getId());
061            }
062            catch (UnknownAmetysObjectException e)
063            {
064                // Ignore, the translation just doesn't exist in the live workspace.
065            }
066        }
067        XMLUtils.endElement(contentHandler, "translations");
068    }
069}