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.text.SimpleDateFormat;
019import java.util.Date;
020import java.util.TimeZone;
021
022import org.apache.cocoon.xml.AttributesImpl;
023import org.apache.cocoon.xml.XMLUtils;
024import org.xml.sax.ContentHandler;
025import org.xml.sax.SAXException;
026
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        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
057        TimeZone tz = TimeZone.getTimeZone("UTC");
058        formatter.setTimeZone(tz);
059
060        Date date = program.getLastModified();
061        
062        XMLUtils.createElement(contentHandler, "datestamp", formatter.format(date));
063        
064        XMLUtils.endElement(contentHandler, "header");
065        
066        XMLUtils.startElement(contentHandler, "metadata");
067        
068        if (metadataPrefix.equals("cdm"))
069        {
070            // CDM-fr
071            contentHandler.startPrefixMapping("", "http://cdm-fr.fr/2011/CDM");
072            contentHandler.startPrefixMapping("cdmfr", "http://cdm-fr.fr/2011/CDM-frSchema");
073            contentHandler.startPrefixMapping("ametys-cdm", "http://www.ametys.org/cdm/1.0");
074            contentHandler.startPrefixMapping("xhtml", "http://www.w3.org/1999/xhtml");
075            
076            AttributesImpl attrs = new AttributesImpl();
077            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");
078            attrs.addCDATAAttribute("profile", "CDM-fr");
079            attrs.addCDATAAttribute("language", program.getLanguage());
080            XMLUtils.startElement(contentHandler, "CDM", attrs);
081
082            exportCDMfrManager.saxHabilitation(contentHandler, program);
083            exportCDMfrManager.program2CDM(contentHandler, program);
084            
085            XMLUtils.endElement(contentHandler, "CDM");
086        }
087        else
088        {
089            // Dublin core
090            contentHandler.startPrefixMapping("oai_dc", "http://www.openarchives.org/OAI/2.0/oai_dc/");
091            contentHandler.startPrefixMapping("dc", "http://purl.org/dc/elements/1.1/");
092            
093            AttributesImpl attrs = new AttributesImpl();
094            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");
095            contentHandler.startElement("http://www.openarchives.org/OAI/2.0/oai_dc/", "dc", "oai_dc:dc", attrs);
096            
097            contentHandler.startElement("http://purl.org/dc/elements/1.1/", "title", "dc:title", new AttributesImpl());
098            String title = program.getTitle();
099            contentHandler.characters(title.toCharArray(), 0, title.length());
100            contentHandler.endElement("http://purl.org/dc/elements/1.1/", "title", "dc:title");
101            
102            contentHandler.startElement("http://purl.org/dc/elements/1.1/", "creator", "dc:creator", new AttributesImpl());
103            String creator = "Ametys";
104            contentHandler.characters(creator.toCharArray(), 0, creator.length());
105            contentHandler.endElement("http://purl.org/dc/elements/1.1/", "creator", "dc:creator");
106            
107            contentHandler.startElement("http://purl.org/dc/elements/1.1/", "date", "dc:date", new AttributesImpl());
108            String lastModified = formatter.format(date);
109            contentHandler.characters(lastModified.toCharArray(), 0, lastModified.length());
110            contentHandler.endElement("http://purl.org/dc/elements/1.1/", "date", "dc:date");
111            
112            contentHandler.endElement("http://www.openarchives.org/OAI/2.0/oai_dc/", "dc", "oai_dc:dc");
113        }
114        
115        XMLUtils.endElement(contentHandler, "metadata");
116        
117        XMLUtils.endElement(contentHandler, "record");
118    }
119}