001/*
002 *  Copyright 2014 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.export.indesign;
017
018import java.io.IOException;
019import java.net.MalformedURLException;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.components.source.impl.SitemapSource;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang.StringUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.util.IgnoreRootHandler;
034import org.ametys.core.util.URLEncoder;
035import org.ametys.odf.catalog.Catalog;
036import org.ametys.odf.catalog.CatalogsManager;
037import org.ametys.odf.program.Program;
038import org.ametys.plugins.repository.AmetysObjectIterable;
039import org.ametys.plugins.repository.UnknownAmetysObjectException;
040import org.ametys.runtime.config.Config;
041
042/**
043 * Generate the SAX events corresponding to the transformation of a catalogue to a XML file for InDesign
044 */
045public class IndesignCatalogueGenerator extends ServiceableGenerator
046{
047    /** The catalogs manager */
048    private CatalogsManager _catalogsManager;
049    
050    @Override
051    public void service(ServiceManager sManager) throws ServiceException
052    {
053        super.service(sManager);
054        _catalogsManager = (CatalogsManager) sManager.lookup(CatalogsManager.ROLE);
055    }
056    
057    public void generate() throws IOException, SAXException, ProcessingException
058    {
059        Request request = ObjectModelHelper.getRequest(objectModel);
060        
061        String catalogName = parameters.getParameter("catalog", null);
062        String xslt = request.getParameter("xslt");
063        
064        contentHandler.startDocument();
065        XMLUtils.startElement(contentHandler, "programs");
066        
067        Catalog catalog = _catalogsManager.getCatalog(catalogName);
068        if (catalog == null)
069        {
070            throw new IllegalArgumentException ("Failed to generate the XML for InDesign of unknown catalog '" + catalogName + "'");
071        }
072        
073        XMLUtils.createElement(contentHandler, "catalog", catalog.getTitle());
074        
075        // Retrieve language
076        String lang = request.getParameter("lang");
077        if (StringUtils.isEmpty(lang))
078        {
079            lang = Config.getInstance().getValueAsString("odf.programs.lang");
080        }
081            
082        _saxPrograms(catalog.getName(), lang, xslt);
083        
084        XMLUtils.endElement(contentHandler, "programs");
085        contentHandler.endDocument();
086    }
087    
088    /**
089     * SAX the programs for the given catalogue and language
090     * @param catalogName the catalog's name
091     * @param lang the programs' language
092     * @param xslt the xslt used to transform a given program
093     * @throws MalformedURLException if the url is malformed
094     * @throws IOException if an in / out type error occurs
095     * @throws SAXException if an exception occurs while saxing 
096     */
097    private void _saxPrograms(String catalogName, String lang, String xslt) throws MalformedURLException, IOException, SAXException
098    {
099        AmetysObjectIterable<Program> programs = _catalogsManager.getPrograms(catalogName, lang);
100        
101        for (Program program : programs)
102        {
103            String programId = program.getId();
104            
105            AttributesImpl attrs = new AttributesImpl();
106            attrs.addCDATAAttribute("id", program.getId());
107            attrs.addCDATAAttribute("name", program.getName());
108            attrs.addCDATAAttribute("title", program.getTitle());
109            
110            XMLUtils.startElement(contentHandler, "program", attrs);
111            
112            SitemapSource src = null;      
113            try
114            {
115                src = (SitemapSource) resolver.resolveURI(_getUri(programId, xslt));
116                src.toSAX(new IgnoreRootHandler(contentHandler));
117            }
118            catch (UnknownAmetysObjectException e)
119            {
120                // The content may be archived
121            }
122            finally
123            {
124                resolver.release(src);
125            }
126            
127            XMLUtils.endElement(contentHandler, "program");
128        }
129    }
130    
131    /**
132     * Get the URI corresponding to the pipeline
133     * @param programId the id of the program
134     * @param xslt the name of the xslt
135     * @return the URI 
136     */
137    protected String _getUri(String programId, String xslt)
138    {
139        return "cocoon://_plugins/odf/_indesign/program.xml?id=" + URLEncoder.encodeParameter(programId) + "&xslt=" + xslt;
140    }
141}