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;
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.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.plugins.explorer.resources.ResourceCollection;
031import org.ametys.plugins.explorer.resources.generators.ResourcesExplorerGenerator;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.plugins.repository.AmetysObjectIterable;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.ModifiableAmetysObject;
036import org.ametys.plugins.repository.TraversableAmetysObject;
037
038/**
039 * Generates a subtree of the resources explorer.
040 */
041public class ExplorerNodeGenerator extends ServiceableGenerator
042{
043    /** The resolver for ametys object */
044    protected AmetysObjectResolver _resolver;
045    
046    @Override
047    public void service(ServiceManager sManager) throws ServiceException
048    {
049        super.service(sManager);
050        _resolver = (AmetysObjectResolver) sManager.lookup(AmetysObjectResolver.ROLE);
051    }
052    
053    @Override
054    public void generate() throws IOException, SAXException, ProcessingException
055    {
056        Request request = ObjectModelHelper.getRequest(objectModel);
057        
058        String id = request.getParameter("node");
059        ExplorerNode node = _resolver.resolveById(id);
060        
061        _saxExplorerNode(node);
062    }
063    
064    private void _saxExplorerNode (ExplorerNode node) throws SAXException
065    {
066        contentHandler.startDocument();
067        
068        AttributesImpl atts = new AttributesImpl();
069        atts.addCDATAAttribute("id", node.getId());
070        
071        XMLUtils.startElement(contentHandler, "Nodes", atts);
072        
073        if (node instanceof TraversableAmetysObject)
074        {
075            TraversableAmetysObject traversableObject = (TraversableAmetysObject) node;
076            AmetysObjectIterable<AmetysObject> children = traversableObject.getChildren();
077            
078            for (AmetysObject child : children)
079            {
080                if (child instanceof ExplorerNode)
081                {
082                    saxExplorerNode((ExplorerNode) child);
083                }
084            }
085        }
086        
087        XMLUtils.endElement(contentHandler, "Nodes");
088        
089        contentHandler.endDocument();
090    }
091    
092    /**
093     * Sax a {@link ExplorerNode}
094     * @param node The node to sax
095     * @throws SAXException If an error occurred while saxing
096     */
097    protected void saxExplorerNode (ExplorerNode node) throws SAXException
098    {
099        AttributesImpl childAtts = new AttributesImpl();
100        childAtts.addCDATAAttribute("id", node.getId());
101        childAtts.addCDATAAttribute("name", node.getName());
102        childAtts.addCDATAAttribute("iconCls", node.getIconCls());
103        childAtts.addCDATAAttribute("applicationId", node.getApplicationId());
104        
105        String relPath = node.getExplorerPath();
106        childAtts.addCDATAAttribute("path", relPath.startsWith("/") ? relPath.substring(1) : relPath);
107        childAtts.addCDATAAttribute("type", ResourcesExplorerGenerator.RESOURCE_COLLECTION);
108        
109        boolean hasResources = false;
110        if (node instanceof ResourceCollection)
111        {
112            hasResources = ((ResourceCollection) node).hasChildResources();
113        }
114        boolean hasChildNodes = node.hasChildExplorerNodes();
115        
116        if (hasChildNodes)
117        {
118            childAtts.addCDATAAttribute("hasChildNodes", "true");
119        }
120        
121        if (hasResources)
122        {
123            childAtts.addCDATAAttribute("hasResources", "true");
124        }
125        
126        if (node instanceof ModifiableAmetysObject)
127        {
128            childAtts.addCDATAAttribute("isModifiable", "true");
129        }
130        
131        if (node instanceof ModifiableExplorerNode)
132        {
133            childAtts.addCDATAAttribute("canCreateChild", "true");
134        }
135        
136        XMLUtils.createElement(contentHandler, "Node", childAtts);
137    }
138}