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.odf.program.generators;
017
018import java.io.IOException;
019
020import org.apache.cocoon.components.source.impl.SitemapSource;
021import org.apache.cocoon.generation.Generator;
022import org.apache.cocoon.xml.AttributesImpl;
023import org.apache.cocoon.xml.XMLUtils;
024import org.xml.sax.SAXException;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.core.util.DateUtils;
028import org.ametys.core.util.IgnoreRootHandler;
029import org.ametys.odf.program.Program;
030import org.ametys.odf.program.SubProgram;
031import org.ametys.plugins.repository.UnknownAmetysObjectException;
032
033/**
034 * {@link Generator} for rendering raw content data for a {@link Program}, to 
035 */
036public class ProgramPdfContentGenerator extends ProgramContentGenerator
037{
038    /**
039     * SAX a sub program
040     * @param subProgram the sub program to SAX
041     * @param parentPath the parent path
042     * @throws SAXException if an error occurs
043     */
044    @Override
045    protected void saxSubProgram (SubProgram subProgram, String parentPath) throws SAXException
046    {
047        try
048        {
049            AttributesImpl attrs = new AttributesImpl();
050            attrs.addCDATAAttribute("title", subProgram.getTitle());
051            attrs.addCDATAAttribute("code", subProgram.getCode());
052            attrs.addCDATAAttribute("id", subProgram.getId());
053            if (parentPath != null)
054            {
055                attrs.addCDATAAttribute("path", parentPath + "/" + subProgram.getName());
056            }
057            XMLUtils.startElement(contentHandler, "subprogram", attrs);
058            saxContent(subProgram, "abstract");  
059            XMLUtils.endElement(contentHandler, "subprogram");
060        }
061        catch (IOException e)
062        {
063            throw new SAXException(e);
064        }
065        catch (UnknownAmetysObjectException e)
066        {
067            throw new SAXException(e);
068        }
069    }
070    
071    
072    @Override
073    protected void saxContent (Content content, String metadataSetName) throws SAXException, IOException
074    {
075        AttributesImpl attrs = new AttributesImpl();
076        attrs.addCDATAAttribute("id", content.getId());
077        attrs.addCDATAAttribute("name", content.getName());
078        attrs.addCDATAAttribute("title", content.getTitle());
079        attrs.addCDATAAttribute("lastModifiedAt", DateUtils.dateToString(content.getLastModified()));
080        
081        XMLUtils.startElement(contentHandler, "content", attrs);
082        
083        String uri = "cocoon://_content.xml?contentId=" + content.getId() + "&output-format=xml";
084        SitemapSource src = null;
085        
086        try
087        {
088            src = (SitemapSource) _srcResolver.resolveURI(uri);
089            src.toSAX(new IgnoreRootHandler(contentHandler));
090        }
091        finally
092        {
093            _srcResolver.release(src);
094        }
095        
096        XMLUtils.endElement(contentHandler, "content");
097    }
098}