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.plugins.explorer.resources.generators;
017
018import java.io.IOException;
019
020import org.apache.cocoon.ProcessingException;
021import org.apache.cocoon.environment.ObjectModelHelper;
022import org.apache.cocoon.environment.Request;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.xml.sax.SAXException;
026
027import org.ametys.plugins.explorer.ExplorerNode;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.AmetysObjectIterable;
031import org.ametys.plugins.repository.TraversableAmetysObject;
032
033/**
034 * Generate the resources of a resources explorer node
035 */
036public class ResourcesGenerator extends ResourcesExplorerGenerator
037{
038    
039    @Override
040    public void generate() throws IOException, SAXException, ProcessingException
041    {
042        Request request = ObjectModelHelper.getRequest(objectModel);
043        
044        String id = request.getParameter("id");
045        ExplorerNode node = (ExplorerNode) _resolver.resolveById(id);
046        
047        contentHandler.startDocument();
048        
049        AttributesImpl atts = new AttributesImpl();
050        atts.addCDATAAttribute("id", node.getId());
051        XMLUtils.startElement(contentHandler, "Node", atts);
052        
053        _saxResources(node);
054        
055        XMLUtils.endElement(contentHandler, "Node");
056        
057        contentHandler.endDocument();
058    }
059    
060    private void _saxResources(ExplorerNode node) throws SAXException
061    {
062        if (node instanceof TraversableAmetysObject)
063        {
064            TraversableAmetysObject traversableObject = (TraversableAmetysObject) node;
065            AmetysObjectIterable<AmetysObject> children = traversableObject.getChildren();
066            
067            for (AmetysObject child : children)
068            {
069                if (child instanceof Resource)
070                {
071                    saxResource((Resource) child);
072                }
073            }
074        }
075    }
076}