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.cms.dublincore;
017
018import java.io.IOException;
019import java.time.LocalDate;
020import java.time.format.DateTimeFormatter;
021import java.util.Date;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
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.core.util.DateUtils;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.AmetysRepositoryException;
035import org.ametys.plugins.repository.dublincore.DublinCoreAwareAmetysObject;
036
037/**
038 * Generates Dublin Core metadata.
039 */
040public class DublinCoreMetadataGenerator extends ServiceableGenerator
041{
042    /** The Ametys object resolver. */
043    protected AmetysObjectResolver _resolver;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
050    }
051    
052    @Override
053    public void generate() throws IOException, SAXException, ProcessingException
054    {
055        String id = parameters.getParameter("id", "");
056        try
057        {
058            AmetysObject object = _resolver.resolveById(id);
059            if (object instanceof DublinCoreAwareAmetysObject)
060            {
061                DublinCoreAwareAmetysObject dcObject = (DublinCoreAwareAmetysObject) object;
062                
063                contentHandler.startDocument();
064                
065                AttributesImpl atts = new AttributesImpl();
066                atts.addCDATAAttribute("id", id);
067                
068                XMLUtils.startElement(contentHandler, "object", atts);
069                _saxDublinCoreMetadata(dcObject);
070                XMLUtils.endElement(contentHandler, "object");
071                
072                contentHandler.endDocument();
073            }
074            else
075            {
076                throw new IllegalArgumentException("Object of id " + id + " is not Dublin Core aware.");
077            }
078        }
079        catch (AmetysRepositoryException e)
080        {
081            getLogger().error("Error trying to get Dublin Core metadata of content " + id, e);
082            throw new ProcessingException("Error trying to get Dublin Core metadata of content " + id, e);
083        }
084    }
085    
086    /**
087     * SAX content Dublin Core metadata.
088     * @param dcObject the Dublin Core object.
089     * @throws SAXException if an error occurs while SAXing.
090     */
091    protected void _saxDublinCoreMetadata(DublinCoreAwareAmetysObject dcObject) throws SAXException
092    {
093        XMLUtils.startElement(contentHandler, "dublin-core-metadata");
094        _saxIfNotNull("title", dcObject.getDCTitle());
095        _saxIfNotNull("creator", dcObject.getDCCreator());
096        _saxIfNotNull("subject", dcObject.getDCSubject());
097        _saxIfNotNull("description", dcObject.getDCDescription());
098        _saxIfNotNull("publisher", dcObject.getDCPublisher());
099        _saxIfNotNull("contributor", dcObject.getDCContributor());
100        _saxIfNotNull("date", dcObject.getDCDate());
101        _saxIfNotNull("type", dcObject.getDCType());
102        _saxIfNotNull("format", dcObject.getDCFormat());
103        _saxIfNotNull("identifier", dcObject.getDCIdentifier());
104        _saxIfNotNull("source", dcObject.getDCSource());
105        _saxIfNotNull("language", dcObject.getDCLanguage());
106        _saxIfNotNull("relation", dcObject.getDCRelation());
107        _saxIfNotNull("coverage", dcObject.getDCCoverage());
108        _saxIfNotNull("rights", dcObject.getDCRights());
109        XMLUtils.endElement(contentHandler, "dublin-core-metadata");
110    }
111    
112    /**
113     * SAX string Dublin Core metadata.
114     * @param name the metadata name.
115     * @param value the metadata value.
116     * @throws SAXException if an error occurs while SAXing.
117     */
118    protected void _saxIfNotNull(String name, String value) throws SAXException
119    {
120        if (value != null)
121        {
122            XMLUtils.createElement(contentHandler, name, value);
123        }
124    }
125    
126    /**
127     * SAX string Dublin Core metadata.
128     * @param name the metadata name.
129     * @param values the metadata values.
130     * @throws SAXException if an error occurs while SAXing.
131     */
132    protected void _saxIfNotNull(String name, String[] values) throws SAXException
133    {
134        if (values != null)
135        {
136            for (String value : values)
137            {
138                XMLUtils.createElement(contentHandler, name, value);
139            }
140        }
141    }
142    
143    /**
144     * SAX date Dublin Core metadata.
145     * @param name the metadata name.
146     * @param value the metadata value.
147     * @throws SAXException if an error occurs while SAXing.
148     */
149    protected void _saxIfNotNull(String name, Date value) throws SAXException
150    {
151        if (value != null)
152        {
153            LocalDate ld = DateUtils.asLocalDate(value);
154            XMLUtils.createElement(contentHandler, name, ld.format(DateTimeFormatter.ISO_LOCAL_DATE));
155        }
156    }
157
158}