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;
017
018import java.io.IOException;
019
020import javax.jcr.Repository;
021import javax.jcr.RepositoryException;
022import javax.jcr.Value;
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 * Generate descriptors of the repository
036 */
037public class DescriptorsGenerator 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        // get current repository
054        Repository repository = _repositoryProvider.getRepository();
055
056        contentHandler.startDocument();
057        
058        XMLUtils.startElement(contentHandler, "repository");
059        
060        String[] descriptorKeys = repository.getDescriptorKeys();
061        for (String key : descriptorKeys)
062        {
063            Value[] values = repository.getDescriptorValues(key);
064            
065            AttributesImpl descAttrs = new AttributesImpl();
066            descAttrs.addCDATAAttribute("key", key);
067            descAttrs.addCDATAAttribute("standard", String.valueOf(repository.isStandardDescriptor(key)));
068            descAttrs.addCDATAAttribute("singleValue", String.valueOf(repository.isSingleValueDescriptor(key)));
069            
070            XMLUtils.startElement(contentHandler, "descriptor", descAttrs);
071            XMLUtils.startElement(contentHandler, "values");
072            
073            if (values != null)
074            {
075                for (Value value : values)
076                {
077                    try
078                    {
079                        AttributesImpl attrs = new AttributesImpl();
080                        attrs.addCDATAAttribute("value", value.getString());
081//                        attrs.addCDATAAttribute("type", PropertyType.nameFromValue(value.getType()));
082//                        attrs.addCDATAAttribute("typeCode", String.valueOf(value.getType()));
083                        XMLUtils.createElement(contentHandler, "value", attrs);
084                    }
085                    catch (RepositoryException e)
086                    {
087                        getLogger().error("Error while retrieving a repository descriptor.", e);
088                    }
089                }
090            }
091            
092            XMLUtils.endElement(contentHandler, "values");
093            XMLUtils.endElement(contentHandler, "descriptor");
094        }
095        
096        XMLUtils.endElement(contentHandler, "repository");
097        
098        contentHandler.endDocument();
099    }
100    
101}