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 */
016
017package org.ametys.odf.cdmfr;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.text.DateFormat;
022import java.text.SimpleDateFormat;
023import java.util.Date;
024import java.util.HashMap;
025
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.excalibur.source.Source;
029import org.apache.excalibur.source.SourceResolver;
030import org.apache.excalibur.xml.sax.XMLizable;
031import org.xml.sax.ContentHandler;
032import org.xml.sax.SAXException;
033
034import org.ametys.plugins.repository.metadata.RichText;
035
036/**
037 * Helper for export tasks.
038 */
039public final class CDMHelper
040{
041    /** The CDM date format. */
042    public static final DateFormat CDM_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
043    
044    private CDMHelper()
045    {
046        // empty constructor
047    }
048
049    
050    /**
051     * Transform a docbook metadata in a CDM infoBlock way.
052     * @param contentHandler the handler to SAX CDM.
053     * @param tag the surrounding tag.
054     * @param richText the metadata. If richText is null, nothing is SAXed. 
055     * @param sourceResolver the Cocoon SourceResolver.
056     * @throws SAXException if an error occurs. 
057     */
058    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, SourceResolver sourceResolver) throws SAXException
059    {
060        richText2CDM(contentHandler, tag, richText, sourceResolver, false, new AttributesImpl());
061    }
062    
063    /**
064     * Transform a docbook metadata in a CDM infoBlock way.
065     * @param contentHandler the handler to SAX CDM.
066     * @param tag the surrounding tag.
067     * @param richText the metadata. If richText is null, nothing is SAXed. 
068     * @param sourceResolver the Cocoon SourceResolver.
069     * @param attrs The attributes to SAX
070     * @throws SAXException if an error occurs. 
071     */
072    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, SourceResolver sourceResolver, AttributesImpl attrs) throws SAXException
073    {
074        richText2CDM(contentHandler, tag, richText, sourceResolver, false, attrs);
075    }
076
077    
078    /**
079     * Transform a docbook metadata in a CDM infoBlock way.
080     * @param contentHandler the handler to SAX CDM.
081     * @param tag the surrounding tag.
082     * @param richText the metadata. If richText is null, nothing is SAXed. 
083     * @param sourceResolver the Cocoon SourceResolver.
084     * @param writeIfEmpty true to sax CDM info block even if the rich text is empty
085     * @throws SAXException if an error occurs. 
086     */
087    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, SourceResolver sourceResolver, boolean writeIfEmpty) throws SAXException
088    {
089        richText2CDM(contentHandler, tag, richText, sourceResolver, writeIfEmpty, new AttributesImpl());
090    }
091
092    /**
093     * Transform a docbook metadata in a CDM infoBlock way.
094     * @param contentHandler the handler to SAX CDM.
095     * @param tag the surrounding tag.
096     * @param richText the metadata. If richText is null, nothing is SAXed
097     * @param writeIfEmpty true to sax CDM info block even if the rich text is empty
098     * @param attrs The attributes to SAX
099     * @param sourceResolver the Cocoon SourceResolver.
100     * @throws SAXException if an error occurs. 
101     */
102    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, SourceResolver sourceResolver, boolean writeIfEmpty, AttributesImpl attrs) throws SAXException
103    {
104        if (richText != null)
105        {
106            XMLUtils.startElement(contentHandler, tag, attrs);
107            richText2CDM(contentHandler, richText, sourceResolver);
108            XMLUtils.endElement(contentHandler, tag);
109        }
110        else if (attrs.getLength() > 0)
111        {
112            XMLUtils.createElement(contentHandler, tag, attrs);
113        }
114        else if (writeIfEmpty)
115        {
116            XMLUtils.createElement(contentHandler, tag, attrs);
117        }
118    }
119    
120    /**
121     * Transform a docbook metadata in a CDM infoBlock way.
122     * @param contentHandler the handler to SAX CDM.
123     * @param richText the metadata. If richText is null, nothing is SAXed
124     * @param sourceResolver the Cocoon SourceResolver.
125     * @throws SAXException if an error occurs. 
126     */
127    public static void richText2CDM(ContentHandler contentHandler, RichText richText, SourceResolver sourceResolver) throws SAXException
128    {
129        if (richText != null)
130        {
131            try (InputStream is = richText.getInputStream())
132            {
133                HashMap<String, Object> params = new HashMap<>();
134                params.put("source", is);
135                
136                Source source = sourceResolver.resolveURI("cocoon://_plugins/odf/docbook2cdm", null, params);
137                
138                ((XMLizable) source).toSAX(contentHandler);
139    
140            }
141            catch (IOException ex)
142            {
143                throw new RuntimeException(ex);
144            }
145        }
146    }
147    
148    /**
149     * SAX a date in a CDM document.
150     * @param contentHandler The content handler to SAX into.
151     * @param tagName The element name.
152     * @param value The date value.
153     * @throws SAXException if an error occurs.
154     */
155    public static void date2CDM(ContentHandler contentHandler, String tagName, Date value) throws SAXException
156    {
157        if (value != null)
158        {
159            AttributesImpl attrs = new AttributesImpl();
160            attrs.addCDATAAttribute(CDMFRTagsConstants.ATTRIBUTE_DATE, CDM_DATE_FORMAT.format(value));
161            XMLUtils.createElement(contentHandler, tagName, attrs);
162        }
163    }
164    
165}