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.time.LocalDate;
022import java.time.format.DateTimeFormatter;
023import java.util.HashMap;
024
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.excalibur.source.Source;
028import org.apache.excalibur.source.SourceResolver;
029import org.apache.excalibur.xml.sax.XMLizable;
030import org.xml.sax.ContentHandler;
031import org.xml.sax.SAXException;
032
033import org.ametys.cms.data.RichText;
034import org.ametys.runtime.model.type.DataContext;
035
036/**
037 * Helper for export tasks.
038 */
039public final class CDMHelper
040{
041    /** The CDM date formatter. */
042    public static final DateTimeFormatter CDM_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
043    
044    private CDMHelper()
045    {
046        // empty constructor
047    }
048
049    
050    /**
051     * Transform a docbook in a CDM infoBlock way.
052     * @param contentHandler the handler to SAX CDM.
053     * @param tag the surrounding tag.
054     * @param richText the richText. If <code>null</code>, nothing is SAXed.
055     * @param richTextContext The context of the rich 
056     * @param sourceResolver the Cocoon SourceResolver.
057     * @throws SAXException if an error occurs. 
058     */
059    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, DataContext richTextContext, SourceResolver sourceResolver) throws SAXException
060    {
061        richText2CDM(contentHandler, tag, richText, richTextContext, sourceResolver, false, new AttributesImpl());
062    }
063    
064    /**
065     * Transform a docbook in a CDM infoBlock way.
066     * @param contentHandler the handler to SAX CDM.
067     * @param tag the surrounding tag.
068     * @param richText the richText. If <code>null</code>, nothing is SAXed.
069     * @param richTextContext The context of the rich 
070     * @param sourceResolver the Cocoon SourceResolver.
071     * @param attrs The attributes to SAX
072     * @throws SAXException if an error occurs. 
073     */
074    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, DataContext richTextContext, SourceResolver sourceResolver, AttributesImpl attrs) throws SAXException
075    {
076        richText2CDM(contentHandler, tag, richText, richTextContext, sourceResolver, false, attrs);
077    }
078
079    
080    /**
081     * Transform a docbook in a CDM infoBlock way.
082     * @param contentHandler the handler to SAX CDM.
083     * @param tag the surrounding tag.
084     * @param richText the richText. If <code>null</code>, nothing is SAXed.
085     * @param richTextContext The context of the rich  
086     * @param sourceResolver the Cocoon SourceResolver.
087     * @param writeIfEmpty true to sax CDM info block even if the rich text is empty
088     * @throws SAXException if an error occurs. 
089     */
090    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, DataContext richTextContext, SourceResolver sourceResolver, boolean writeIfEmpty) throws SAXException
091    {
092        richText2CDM(contentHandler, tag, richText, richTextContext, sourceResolver, writeIfEmpty, false, new AttributesImpl());
093    }
094    
095    /**
096     * Transform a docbook in a CDM infoBlock way.
097     * @param contentHandler the handler to SAX CDM.
098     * @param tag the surrounding tag.
099     * @param richText the richText. If <code>null</code>, nothing is SAXed.
100     * @param richTextContext The context of the rich 
101     * @param sourceResolver the Cocoon SourceResolver.
102     * @param writeIfEmpty true to sax CDM info block even if the rich text is empty
103     * @param rawHtmlExpert true to export HTML expert as raw CDATA. If false, parse and resolve HTML expert code.
104     * @throws SAXException if an error occurs. 
105     */
106    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, DataContext richTextContext, SourceResolver sourceResolver, boolean writeIfEmpty, boolean rawHtmlExpert) throws SAXException
107    {
108        richText2CDM(contentHandler, tag, richText, richTextContext, sourceResolver, writeIfEmpty, rawHtmlExpert, new AttributesImpl());
109    }
110
111    /**
112     * Transform a docbook in a CDM infoBlock way.
113     * @param contentHandler the handler to SAX CDM.
114     * @param tag the surrounding tag.
115     * @param richText the richText. If <code>null</code>, nothing is SAXed.
116     * @param richTextContext The context of the rich 
117     * @param writeIfEmpty true to sax CDM info block even if the rich text is empty
118     * @param attrs The attributes to SAX
119     * @param sourceResolver the Cocoon SourceResolver.
120     * @throws SAXException if an error occurs. 
121     */
122    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, DataContext richTextContext, SourceResolver sourceResolver, boolean writeIfEmpty, AttributesImpl attrs) throws SAXException
123    {
124        richText2CDM(contentHandler, tag, richText, richTextContext, sourceResolver, writeIfEmpty, false, attrs);
125    }
126    
127    /**
128     * Transform a docbook in a CDM infoBlock way.
129     * @param contentHandler the handler to SAX CDM.
130     * @param tag the surrounding tag.
131     * @param richText the richText. If <code>null</code>, nothing is SAXed.
132     * @param richTextContext The context of the rich 
133     * @param writeIfEmpty true to sax CDM info block even if the rich text is empty
134     * @param rawHtmlExpert true to export HTML expert as raw CDATA. If false, parse and resolve HTML expert code.
135     * @param attrs The attributes to SAX
136     * @param sourceResolver the Cocoon SourceResolver.
137     * @throws SAXException if an error occurs. 
138     */
139    public static void richText2CDM(ContentHandler contentHandler, String tag, RichText richText, DataContext richTextContext, SourceResolver sourceResolver, boolean writeIfEmpty, boolean rawHtmlExpert, AttributesImpl attrs) throws SAXException
140    {
141        if (richText != null)
142        {
143            XMLUtils.startElement(contentHandler, tag, attrs);
144            richText2CDM(contentHandler, richText, richTextContext, sourceResolver, rawHtmlExpert);
145            XMLUtils.endElement(contentHandler, tag);
146        }
147        else if (attrs.getLength() > 0)
148        {
149            XMLUtils.createElement(contentHandler, tag, attrs);
150        }
151        else if (writeIfEmpty)
152        {
153            XMLUtils.createElement(contentHandler, tag, attrs);
154        }
155    }
156    
157    /**
158     * Transform a docbook in a CDM infoBlock way.
159     * @param contentHandler the handler to SAX CDM.
160     * @param richText the richText. If <code>null</code>, nothing is SAXed
161     * @param richTextContext The context of the rich
162     * @param sourceResolver the Cocoon SourceResolver.
163     * @throws SAXException if an error occurs. 
164     */
165    public static void richText2CDM(ContentHandler contentHandler, RichText richText, DataContext richTextContext, SourceResolver sourceResolver) throws SAXException
166    {
167        richText2CDM(contentHandler, richText, richTextContext, sourceResolver, false);
168    }
169    
170    /**
171     * Transform a docbook in a CDM infoBlock way.
172     * @param contentHandler the handler to SAX CDM.
173     * @param richText the richText. If <code>null</code>, nothing is SAXed.
174     * @param richTextContext The context of the rich 
175     * @param sourceResolver the Cocoon SourceResolver.
176     * @param rawHtmlExpert true to export HTML expert as raw CDATA. If false, parse and resolve HTML expert code.
177     * @throws SAXException if an error occurs. 
178     */
179    public static void richText2CDM(ContentHandler contentHandler, RichText richText, DataContext richTextContext, SourceResolver sourceResolver, boolean rawHtmlExpert) throws SAXException
180    {
181        if (richText != null)
182        {
183            try (InputStream is = richText.getInputStream())
184            {
185                HashMap<String, Object> params = new HashMap<>();
186                params.put("source", is);
187                params.put("rawHtmlExpert", rawHtmlExpert);
188                params.put("dataContext", richTextContext);
189                
190                Source source = sourceResolver.resolveURI("cocoon://_plugins/odf/docbook2cdm", null, params);
191                
192                ((XMLizable) source).toSAX(contentHandler);
193    
194            }
195            catch (IOException ex)
196            {
197                throw new RuntimeException(ex);
198            }
199        }
200    }
201    
202    /**
203     * SAX a date in a CDM document.
204     * @param contentHandler The content handler to SAX into.
205     * @param tagName The element name.
206     * @param value The date value.
207     * @throws SAXException if an error occurs.
208     */
209    public static void date2CDM(ContentHandler contentHandler, String tagName, LocalDate value) throws SAXException
210    {
211        if (value != null)
212        {
213            AttributesImpl attrs = new AttributesImpl();
214            attrs.addCDATAAttribute(CDMFRTagsConstants.ATTRIBUTE_DATE, value.format(CDM_DATE_FORMATTER));
215            XMLUtils.createElement(contentHandler, tagName, attrs);
216        }
217    }
218    
219}