001/*
002
003 *  Copyright 2024 Anyware Services
004 *
005 *  Licensed under the Apache License, Version 2.0 (the "License");
006 *  you may not use this file except in compliance with the License.
007 *  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.ametys.cms.properties.section.technical;
018
019import java.io.IOException;
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.SourceUtil;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.XMLUtils;
029import org.apache.excalibur.source.Source;
030import org.xml.sax.SAXException;
031
032import org.ametys.core.cocoon.ActionResultGenerator;
033import org.ametys.core.util.IgnoreRootHandler;
034import org.ametys.plugins.repository.AmetysObject;
035
036/**
037 * The generator that getting technical data in {@link TechnicalItemExtensionPoint} and merge it.
038 */
039public class TechnicalSectionGenerator extends ServiceableGenerator
040{
041    private TechnicalItemExtensionPoint _technicalItemEP;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _technicalItemEP = (TechnicalItemExtensionPoint) smanager.lookup(TechnicalItemExtensionPoint.ROLE);
048    }
049    
050    public void generate() throws IOException, SAXException, ProcessingException
051    {
052        Request request = ObjectModelHelper.getRequest(objectModel);
053        AmetysObject ametysObject = (AmetysObject) request.getAttribute(AmetysObject.class.getName());
054        
055        contentHandler.startDocument();
056        
057        XMLUtils.startElement(contentHandler, "items");
058        
059        for (TechnicalItem sectionItem : _technicalItemEP.getSupportingExtensions(ametysObject))
060        {
061            Source src = null;
062            try
063            {
064                // Set request attributes
065                request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, sectionItem.buildData(ametysObject));
066                request.setAttribute("xslt", sectionItem.getXSLT());
067                
068                // Resolve URI
069                src = resolver.resolveURI("cocoon://_plugins/cms/property-section");
070                XMLUtils.startElement(contentHandler, "item");
071                SourceUtil.toSAX(src, new IgnoreRootHandler(contentHandler));
072                XMLUtils.endElement(contentHandler, "item");
073            }
074            catch (Exception e)
075            {
076                throw new ProcessingException("Error while using technical item " + sectionItem.getId() + " on object " + ametysObject.getId(), e);
077            }
078            finally
079            {
080                resolver.release(src);
081            }
082        }
083        
084        XMLUtils.endElement(contentHandler, "items");
085        
086        contentHandler.endDocument();
087    }
088}