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 */
016package org.ametys.cms.transformation.html;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.commons.io.IOUtils;
026
027import org.ametys.runtime.plugin.AbstractExtensionPoint;
028
029/**
030 * This extension point handle the new tags you want to add to the default implementation of the docbook 
031 */
032public class HTMLEnhancementExtensionPoint extends AbstractExtensionPoint<HTMLEnhancer>
033{
034    /** The avalon role */
035    public static final String ROLE = HTMLEnhancementExtensionPoint.class.getName();
036    
037    private String _html2htmleditorXSLT;
038    private String _html2htmlXSLT;
039    private String _html2outgoingReferencesXSLT;
040    private String _htmleditor2htmlXSLT;
041    private Set<String> _html2htmlHandlers;
042    private Set<String> _htmleditor2htmlHandlers;
043    private Set<String> _html2htmleditorHandlers;
044    
045    /**
046     * Get the content of the XSLT file
047     * @return A valid XSLT file content
048     */
049    public String getHtml2htmleditorXSLT()
050    {
051        return _html2htmleditorXSLT;
052    }
053    
054    /**
055     * Get the content of the XSLT file
056     * @return A valid XSLT file content
057     */
058    public String getHtml2htmlXSLT()
059    {
060        return _html2htmlXSLT;
061    }
062    
063    /**
064     * Get the content of the XSLT file
065     * @return A valid XSLT file content
066     */
067    public String getHtml2outgoingReferencesXSLT()
068    {
069        return _html2outgoingReferencesXSLT;
070    }
071    
072    /**
073     * Get the content of the XSLT file
074     * @return A valid XSLT file content
075     */
076    public String getHtmlEditor2HTMLXSLT()
077    {
078        return _htmleditor2htmlXSLT;
079    }
080    
081    /**
082     * Get the list of available handlers
083     * @return a non null set of roles
084     */
085    public Set<String> getHtml2HTMLHandlers()
086    {
087        return _html2htmlHandlers;
088    }
089    
090    /**
091     * Get the list of available handlers
092     * @return a non null set of roles
093     */
094    public Set<String> getHtmleditor2HTMLHandlers()
095    {
096        return _htmleditor2htmlHandlers;
097    }
098    
099    /**
100     * Get the list of available handlers
101     * @return a non null set of roles
102     */
103    public Set<String> getHtml2HTMLEditorHandlers()
104    {
105        return _html2htmleditorHandlers;
106    }
107    
108    @Override
109    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
110    {
111        String htmleditor2HtmlXSLT = configuration.getChild("htmleditor2html").getChild("xslt").getValue(null);
112        String html2HTMLEditorXSLT = configuration.getChild("html2htmleditor").getChild("xslt").getValue(null);
113        String html2HTMLXSLT = configuration.getChild("html2html").getChild("xslt").getValue(null);
114        String html2outgoingReferencesXSLT = configuration.getChild("html2outgoingreferences").getChild("xslt").getValue(null);
115        String html2HTMLHandler = configuration.getChild("html2html").getChild("handler").getValue(null);
116        String htmleditor2HTMLHandler = configuration.getChild("htmleditor2html").getChild("handler").getValue(null);
117        String html2HTMLEditorHandler = configuration.getChild("html2htmleditor").getChild("handler").getValue(null);
118        
119        HTMLEnhancer de = new HTMLEnhancer(pluginName, htmleditor2HtmlXSLT, html2HTMLEditorXSLT, html2HTMLXSLT, html2outgoingReferencesXSLT, html2HTMLHandler, htmleditor2HTMLHandler, html2HTMLEditorHandler);
120        _extensions.put(id, de);
121    }
122    
123    @Override
124    public void initializeExtensions() throws Exception
125    {
126        String template = _readTemplate();
127        
128        Set<String> html2htmleditorXSLTs = _getHtml2htmleditorXSLTs();
129        String html2htmleditorXSLTsIncludes = _createIncludes(html2htmleditorXSLTs);
130        _html2htmleditorXSLT = template.replace("@INCLUDE@", html2htmleditorXSLTsIncludes);
131
132        Set<String> html2htmlXSLTs = _getHtml2htmlXSLTs();
133        String html2htmlXSLTsIncludes = _createIncludes(html2htmlXSLTs);
134        _html2htmlXSLT = template.replace("@INCLUDE@", html2htmlXSLTsIncludes);
135
136        Set<String> htmleditor2htmlXSLTs = _getHTMLEditor2htmlXSLTs();
137        String htmleditor2htmlXSLTsIncludes = _createIncludes(htmleditor2htmlXSLTs);
138        _htmleditor2htmlXSLT = template.replace("@INCLUDE@", htmleditor2htmlXSLTsIncludes);
139        
140        Set<String> html2outgoingReferencesXSLTs = _getHtml2outgoingReferencesXSLTs();
141        String html2outgoingReferencesXSLTsIncludes = _createIncludes(html2outgoingReferencesXSLTs);
142        _html2outgoingReferencesXSLT = template.replace("@INCLUDE@", html2outgoingReferencesXSLTsIncludes);
143        
144        _html2htmlHandlers = new HashSet<>();
145        for (String key : _extensions.keySet())
146        {
147            HTMLEnhancer de = _extensions.get(key);
148         
149            String handlerRole = de.getHtml2HTMLHandler();
150            if (handlerRole != null)
151            {
152                _html2htmlHandlers.add(handlerRole);
153            }
154        }
155        
156        _htmleditor2htmlHandlers = new HashSet<>();
157        for (String key : _extensions.keySet())
158        {
159            HTMLEnhancer de = _extensions.get(key);
160         
161            String handlerRole = de.getHtmleditor2HTMLHandler();
162            if (handlerRole != null)
163            {
164                _htmleditor2htmlHandlers.add(handlerRole);
165            }
166        }
167        
168        _html2htmleditorHandlers = new HashSet<>();
169        for (String key : _extensions.keySet())
170        {
171            HTMLEnhancer de = _extensions.get(key);
172         
173            String handlerRole = de.getHtml2HTMLEditorHandler();
174            if (handlerRole != null)
175            {
176                _html2htmleditorHandlers.add(handlerRole);
177            }
178        }
179    }
180
181    private String _createIncludes(Set<String> includes)
182    {
183        StringBuffer sb = new StringBuffer("");
184        
185        for (String file : includes)
186        {
187            sb.append("<xsl:include href=\"");
188            sb.append(file);
189            sb.append("\"/>");
190        }
191        
192        return sb.toString();
193    }
194    
195    private String _readTemplate() throws IOException
196    {
197        try (InputStream is = this.getClass().getResourceAsStream("template.xsl"))
198        {
199            return IOUtils.toString(is, "UTF-8");
200        }
201    }
202
203    private Set<String> _getHtml2htmlXSLTs()
204    {
205        Set<String> html2htmlXSLTs = new HashSet<>();
206
207        for (String key : _extensions.keySet())
208        {
209            HTMLEnhancer de = _extensions.get(key);
210            
211            String html2html = de.getHtml2HTMLXSLT();
212            if (html2html != null)
213            {
214                html2htmlXSLTs.add(html2html);
215            }
216        }
217        
218        return html2htmlXSLTs;
219    }
220    
221    private Set<String> _getHtml2htmleditorXSLTs()
222    {
223        Set<String> html2htmleditorXSLTs = new HashSet<>();
224
225        for (String key : _extensions.keySet())
226        {
227            HTMLEnhancer de = _extensions.get(key);
228            
229            String html2htmleditor = de.getHtml2HTMLEditorXSLT();
230            if (html2htmleditor != null)
231            {
232                html2htmleditorXSLTs.add(html2htmleditor);
233            }
234        }
235        
236        return html2htmleditorXSLTs;
237    }
238    
239    private Set<String> _getHtml2outgoingReferencesXSLTs()
240    {
241        Set<String> html2outgoingReferencesXSLTs = new HashSet<>();
242
243        for (String key : _extensions.keySet())
244        {
245            HTMLEnhancer de = _extensions.get(key);
246            
247            String html2outgoingReferences = de.getHtml2outgoingReferencesXSLT();
248            if (html2outgoingReferences != null)
249            {
250                html2outgoingReferencesXSLTs.add(html2outgoingReferences);
251            }
252        }
253        
254        return html2outgoingReferencesXSLTs;
255    }
256    
257    private Set<String> _getHTMLEditor2htmlXSLTs()
258    {
259        Set<String> htmleditor2htmlXSLTs = new HashSet<>();
260
261        for (String key : _extensions.keySet())
262        {
263            HTMLEnhancer de = _extensions.get(key);
264            
265            String htmleditor2html = de.getHTMLEditor2HtmlX();
266            if (htmleditor2html != null)
267            {
268                htmleditor2htmlXSLTs.add(htmleditor2html);
269            }
270        }
271        
272        return htmleditor2htmlXSLTs;
273    }
274}