001/*
002 *  Copyright 2012 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.odf.oai;
017
018import java.time.ZoneOffset;
019import java.time.ZonedDateTime;
020
021import org.apache.cocoon.xml.AttributesImpl;
022import org.apache.cocoon.xml.XMLUtils;
023import org.xml.sax.ContentHandler;
024import org.xml.sax.SAXException;
025
026import org.ametys.core.util.DateUtils;
027import org.ametys.odf.cdmfr.ExportCDMfrManager;
028import org.ametys.odf.program.Program;
029
030/**
031 * Helper for handling OAI records/
032 */
033public final class RecordHelper
034{
035    private RecordHelper()
036    {
037        // empty
038    }
039   
040    /**
041     * Generates SAX events representing a Program as an OAI record.
042     * @param program the {@link Program}.
043     * @param contentHandler the {@link ContentHandler}.
044     * @param metadataPrefix "cdm" or "oai_dc".
045     * @param exportCDMfrManager the {@link ExportCDMfrManager}.
046     * @throws SAXException if an error occurs.
047     */
048    public static void saxProgram(Program program, ContentHandler contentHandler, String metadataPrefix, ExportCDMfrManager exportCDMfrManager) throws SAXException
049    {
050        XMLUtils.startElement(contentHandler, "record");
051        
052        XMLUtils.startElement(contentHandler, "header");
053        
054        XMLUtils.createElement(contentHandler, "identifier", program.getCDMId());
055        
056        ZonedDateTime date = program.getLastModified();
057        String lastModified = DateUtils.zonedDateTimeToString(date, ZoneOffset.UTC, "yyyy-MM-dd'T'HH:mm:ssXXX");
058        XMLUtils.createElement(contentHandler, "datestamp", lastModified);
059        
060        XMLUtils.endElement(contentHandler, "header");
061        
062        XMLUtils.startElement(contentHandler, "metadata");
063        
064        if (metadataPrefix.equals("cdm"))
065        {
066            // CDM-fr
067            contentHandler.startPrefixMapping("", "http://cdm-fr.fr/2011/CDM");
068            contentHandler.startPrefixMapping("cdmfr", "http://cdm-fr.fr/2011/CDM-frSchema");
069            contentHandler.startPrefixMapping("ametys-cdm", "http://www.ametys.org/cdm/1.0");
070            contentHandler.startPrefixMapping("xhtml", "http://www.w3.org/1999/xhtml");
071            
072            AttributesImpl attrs = new AttributesImpl();
073            attrs.addCDATAAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi:schemaLocation", "http://cdm-fr.fr/2011/CDM http://cdm-fr.fr/2011/schemas/CDMFR.xsd");
074            attrs.addCDATAAttribute("profile", "CDM-fr");
075            attrs.addCDATAAttribute("language", program.getLanguage());
076            XMLUtils.startElement(contentHandler, "CDM", attrs);
077
078            exportCDMfrManager.saxHabilitation(contentHandler, program);
079            exportCDMfrManager.program2CDM(contentHandler, program);
080            
081            XMLUtils.endElement(contentHandler, "CDM");
082        }
083        else
084        {
085            // Dublin core
086            contentHandler.startPrefixMapping("oai_dc", "http://www.openarchives.org/OAI/2.0/oai_dc/");
087            contentHandler.startPrefixMapping("dc", "http://purl.org/dc/elements/1.1/");
088            
089            AttributesImpl attrs = new AttributesImpl();
090            attrs.addCDATAAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");
091            contentHandler.startElement("http://www.openarchives.org/OAI/2.0/oai_dc/", "dc", "oai_dc:dc", attrs);
092            
093            contentHandler.startElement("http://purl.org/dc/elements/1.1/", "title", "dc:title", new AttributesImpl());
094            String title = program.getTitle();
095            contentHandler.characters(title.toCharArray(), 0, title.length());
096            contentHandler.endElement("http://purl.org/dc/elements/1.1/", "title", "dc:title");
097            
098            contentHandler.startElement("http://purl.org/dc/elements/1.1/", "creator", "dc:creator", new AttributesImpl());
099            String creator = "Ametys";
100            contentHandler.characters(creator.toCharArray(), 0, creator.length());
101            contentHandler.endElement("http://purl.org/dc/elements/1.1/", "creator", "dc:creator");
102            
103            contentHandler.startElement("http://purl.org/dc/elements/1.1/", "date", "dc:date", new AttributesImpl());
104            contentHandler.characters(lastModified.toCharArray(), 0, lastModified.length());
105            contentHandler.endElement("http://purl.org/dc/elements/1.1/", "date", "dc:date");
106            
107            contentHandler.endElement("http://www.openarchives.org/OAI/2.0/oai_dc/", "dc", "oai_dc:dc");
108        }
109        
110        XMLUtils.endElement(contentHandler, "metadata");
111        
112        XMLUtils.endElement(contentHandler, "record");
113    }
114}