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