001/*
002 *  Copyright 2018 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.plugins.contenttypeseditor.edition;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.components.source.SourceUtil;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.excalibur.source.Source;
029import org.apache.excalibur.source.SourceNotFoundException;
030import org.apache.excalibur.source.SourceResolver;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.util.IgnoreRootHandler;
034
035/**
036 * This generator provide to sax content type right and add a new right
037 */
038public class ContentTypeRightGenerator extends ServiceableGenerator
039{
040    /** The right file path */
041    public static final String __RIGHT_FILE_PATH = "file-path";
042    /** The right id */
043    public static final String __RIGHT_ID = "id";
044    /** The right label */
045    public static final String __RIGHT_LABEL = "label";
046    /** The right description */
047    public static final String __RIGHT_DESCRIPTION = "description";
048    /** The right category */
049    public static final String __RIGHT_CATEGORY = "category";
050    
051    private SourceResolver _sourceResolver;
052
053    @Override
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        _sourceResolver = (SourceResolver) smanager.lookup(SourceResolver.ROLE);
057    }
058    
059    public void generate() throws IOException, SAXException, ProcessingException
060    {
061        Request request = ObjectModelHelper.getRequest(objectModel);
062
063        String filePath = request.getParameter(__RIGHT_FILE_PATH);
064
065        String id = request.getParameter(__RIGHT_ID);
066        String label = request.getParameter(__RIGHT_LABEL);
067        String description = request.getParameter(__RIGHT_DESCRIPTION);
068        String category = request.getParameter(__RIGHT_CATEGORY);
069
070
071        contentHandler.startDocument();
072        XMLUtils.startElement(contentHandler, "root");
073
074        // Add the content of rights file
075        Source right = null;
076        try
077        {
078            right = _sourceResolver.resolveURI(filePath);
079            if (right.exists())
080            {
081                SourceUtil.toSAX(right, new IgnoreRootHandler(contentHandler));
082            }
083        }
084        catch (SourceNotFoundException e)
085        {
086            // Silently ignore
087        }
088        finally
089        {
090            _sourceResolver.release(right);
091        }
092        
093        // Add the new right
094        XMLUtils.startElement(contentHandler, "add");
095        XMLUtils.createElement(contentHandler, "id", id);
096        XMLUtils.createElement(contentHandler, "label", label);
097        XMLUtils.createElement(contentHandler, "description", description);
098        XMLUtils.createElement(contentHandler, "category", category);
099        XMLUtils.endElement(contentHandler, "add");
100        
101        XMLUtils.endElement(contentHandler, "root");
102        contentHandler.endDocument();
103    }
104
105}