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.workspaces.repository.jcr;
017
018import java.io.IOException;
019
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022import javax.jcr.Workspace;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.cocoon.ProcessingException;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.plugins.repositoryapp.RepositoryProvider;
033
034/**
035 * SAX the accessible workspaces
036 */
037public class WorkspacesGenerator extends ServiceableGenerator
038{
039    
040    /** The repository provider. */
041    protected RepositoryProvider _repositoryProvider;
042    
043    @Override
044    public void service(ServiceManager serviceManager) throws ServiceException
045    {
046        super.service(serviceManager);
047        _repositoryProvider = (RepositoryProvider) serviceManager.lookup(RepositoryProvider.ROLE);
048    }
049    
050    @Override
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        try
054        {
055            Session session = _repositoryProvider.getSession("default");
056            
057            Workspace workspace = session.getWorkspace();
058            
059            contentHandler.startDocument();
060            XMLUtils.startElement(contentHandler, "workspaces");
061            
062            for (String workspaceName : workspace.getAccessibleWorkspaceNames())
063            {
064                AttributesImpl attrs = new AttributesImpl();
065                attrs.addCDATAAttribute("name", workspaceName);
066                XMLUtils.createElement(contentHandler, "workspace", attrs);
067            }
068            
069            XMLUtils.endElement(contentHandler, "workspaces");
070            contentHandler.endDocument();
071        }
072        catch (RepositoryException e)
073        {
074            throw new ProcessingException("Unable to get accessible workspaces", e);
075        }
076    }
077}