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.dublincore;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO;
032
033/**
034 * Generates Dublin Core metadata.
035 */
036public class DublinCoreMetadataGenerator extends ServiceableGenerator
037{
038    private ExplorerResourcesDAO _resourceDAO;
039    
040    @Override
041    public void service(ServiceManager serviceManager) throws ServiceException
042    {
043        super.service(serviceManager);
044        _resourceDAO = (ExplorerResourcesDAO) serviceManager.lookup(ExplorerResourcesDAO.ROLE);
045    }
046    
047    @Override
048    public void generate() throws IOException, SAXException, ProcessingException
049    {
050        Request request = ObjectModelHelper.getRequest(objectModel);
051        String id = parameters.getParameter("id", request.getParameter("id"));
052        
053        Map<String, Object> dcMetadata = _resourceDAO.getDCMetadata(id);
054        
055        contentHandler.startDocument();
056        
057        AttributesImpl atts = new AttributesImpl();
058        atts.addCDATAAttribute("id", id);
059        
060        XMLUtils.startElement(contentHandler, "object", atts);
061        
062        XMLUtils.startElement(contentHandler, "dublin-core-metadata");
063        for (String metadataName : dcMetadata.keySet())
064        {
065            _saxIfNotNull(metadataName, dcMetadata.get(metadataName));
066        }
067        XMLUtils.endElement(contentHandler, "dublin-core-metadata");
068        
069        XMLUtils.endElement(contentHandler, "object");
070        
071        contentHandler.endDocument();
072        
073    }
074    
075    
076    /**
077     * SAX string Dublin Core metadata.
078     * @param name the metadata name.
079     * @param value the metadata value.
080     * @throws SAXException if an error occurs while SAXing.
081     */
082    protected void _saxIfNotNull(String name, Object value) throws SAXException
083    {
084        if (value != null)
085        {
086            if (value instanceof String[])
087            {
088                for (String val : (String[]) value)
089                {
090                    XMLUtils.createElement(contentHandler, name, val);
091                }
092            }
093            else if (value instanceof String)
094            {
095                XMLUtils.createElement(contentHandler, name, (String) value);
096            }
097            else
098            {
099                XMLUtils.createElement(contentHandler, name, String.valueOf(value));
100            }
101            
102        }
103    }
104}