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.docbook;
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 DocbookEnhancementExtensionPoint extends AbstractExtensionPoint<DocbookEnhancer>
033{
034    /** The avalon role */
035    public static final String ROLE = DocbookEnhancementExtensionPoint.class.getName();
036    
037    private String _docbook2htmleditorXSLT;
038    private String _docbook2htmlXSLT;
039    private String _htmleditor2docbookXSLT;
040    private String _docbook2outgoingReferencesXSLT;
041    private Set<String> _docbook2htmlHandlers;
042    private Set<String> _htmleditor2docbookHandlers;
043    private Set<String> _docbook2htmleditorHandlers;
044    
045    /**
046     * Get the content of the XSLT file
047     * @return A valid XSLT file content
048     */
049    public String getDocbook2htmleditorXSLT()
050    {
051        return _docbook2htmleditorXSLT;
052    }
053    
054    /**
055     * Get the content of the XSLT file
056     * @return A valid XSLT file content
057     */
058    public String getDocbook2htmlXSLT()
059    {
060        return _docbook2htmlXSLT;
061    }
062    
063    /**
064     * Get the content of the XSLT file
065     * @return A valid XSLT file content
066     */
067    public String getDocbook2outgoingReferencesXSLT()
068    {
069        return _docbook2outgoingReferencesXSLT;
070    }
071    
072    /**
073     * Get the content of the XSLT file
074     * @return A valid XSLT file content
075     */
076    public String getHTMLEditor2docbookXSLT()
077    {
078        return _htmleditor2docbookXSLT;
079    }
080    
081    /**
082     * Get the list of available handlers
083     * @return a non null set of roles
084     */
085    public Set<String> getDocbook2HTMLHandlers()
086    {
087        return _docbook2htmlHandlers;
088    }
089    
090    /**
091     * Get the list of available handlers
092     * @return a non null set of roles
093     */
094    public Set<String> getHTMLEditor2DocbookHandlers()
095    {
096        return _htmleditor2docbookHandlers;
097    }
098    
099    /**
100     * Get the list of available handlers
101     * @return a non null set of roles
102     */
103    public Set<String> getDocbook2HTMLEditorHandlers()
104    {
105        return _docbook2htmleditorHandlers;
106    }
107
108    @Override
109    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
110    {
111        String htmleditor2DocbookXSLT = configuration.getChild("htmleditor2docbook").getChild("xslt").getValue(null);
112        String docbook2HTMLEditorXSLT = configuration.getChild("docbook2htmleditor").getChild("xslt").getValue(null);
113        String docbook2HTMLXSLT = configuration.getChild("docbook2html").getChild("xslt").getValue(null);
114        String docbook2outgoingReferencesXSLT = configuration.getChild("docbook2outgoingreferences").getChild("xslt").getValue(null);
115        String docbook2HTMLHandler = configuration.getChild("docbook2html").getChild("handler").getValue(null);
116        String htmlEditor2docbookHandler = configuration.getChild("htmleditor2docbook").getChild("handler").getValue(null);
117        String docbook2htmlEditorHandler = configuration.getChild("docbook2htmleditor").getChild("handler").getValue(null);
118        
119        DocbookEnhancer de = new DocbookEnhancer(pluginName, htmleditor2DocbookXSLT, docbook2HTMLEditorXSLT, docbook2HTMLXSLT, docbook2outgoingReferencesXSLT, docbook2HTMLHandler, htmlEditor2docbookHandler, docbook2htmlEditorHandler);
120        _extensions.put(id, de);
121    }
122    
123    @Override
124    public void initializeExtensions() throws Exception
125    {
126        String template = _readTemplate();
127        
128        Set<String> docbook2htmleditorXSLTs = _getDocbook2htmleditorXSLTs();
129        String docbook2htmleditorXSLTsIncludes = _createIncludes(docbook2htmleditorXSLTs);
130        _docbook2htmleditorXSLT = template.replace("@INCLUDE@", docbook2htmleditorXSLTsIncludes);
131
132        Set<String> docbook2htmlXSLTs = _getDocbook2htmlXSLTs();
133        String docbook2htmlXSLTsIncludes = _createIncludes(docbook2htmlXSLTs);
134        _docbook2htmlXSLT = template.replace("@INCLUDE@", docbook2htmlXSLTsIncludes);
135
136        Set<String> docbook2outgoingReferencesXSLTs = _getDocbook2outgoingReferencesXSLTs();
137        String docbook2outgoingReferencesXSLTsIncludes = _createIncludes(docbook2outgoingReferencesXSLTs);
138        _docbook2outgoingReferencesXSLT = template.replace("@INCLUDE@", docbook2outgoingReferencesXSLTsIncludes);
139
140        Set<String> htmleditor2docbookXSLTs = _getHTMLEditor2docbookXSLTs();
141        String htmleditor2docbookXSLTsIncludes = _createIncludes(htmleditor2docbookXSLTs);
142        _htmleditor2docbookXSLT = template.replace("@INCLUDE@", htmleditor2docbookXSLTsIncludes);
143        
144        _docbook2htmlHandlers = new HashSet<>();
145        for (String key : _extensions.keySet())
146        {
147            DocbookEnhancer de = _extensions.get(key);
148         
149            String handlerRole = de.getDocbook2HTMLHandler();
150            if (handlerRole != null)
151            {
152                _docbook2htmlHandlers.add(handlerRole);
153            }
154        }
155        
156        _htmleditor2docbookHandlers = new HashSet<>();
157        for (String key : _extensions.keySet())
158        {
159            DocbookEnhancer de = _extensions.get(key);
160         
161            String handlerRole = de.getHTMLEditor2DocbookHandler();
162            if (handlerRole != null)
163            {
164                _htmleditor2docbookHandlers.add(handlerRole);
165            }
166        }
167        
168        _docbook2htmleditorHandlers = new HashSet<>();
169        for (String key : _extensions.keySet())
170        {
171            DocbookEnhancer de = _extensions.get(key);
172         
173            String handlerRole = de.getDocbook2HTMLEditorHandler();
174            if (handlerRole != null)
175            {
176                _docbook2htmleditorHandlers.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> _getDocbook2htmlXSLTs()
204    {
205        Set<String> docbook2htmlXSLTs = new HashSet<>();
206
207        for (String key : _extensions.keySet())
208        {
209            DocbookEnhancer de = _extensions.get(key);
210            
211            String docbook2html = de.getDocbook2HTMLXSLT();
212            if (docbook2html != null)
213            {
214                docbook2htmlXSLTs.add(docbook2html);
215            }
216        }
217        
218        return docbook2htmlXSLTs;
219    }
220    
221    private Set<String> _getDocbook2htmleditorXSLTs()
222    {
223        Set<String> docbook2htmleditorXSLTs = new HashSet<>();
224
225        for (String key : _extensions.keySet())
226        {
227            DocbookEnhancer de = _extensions.get(key);
228            
229            String docbook2htmleditor = de.getDocbook2HTMLEditorXSLT();
230            if (docbook2htmleditor != null)
231            {
232                docbook2htmleditorXSLTs.add(docbook2htmleditor);
233            }
234        }
235        
236        return docbook2htmleditorXSLTs;
237    }
238    
239    private Set<String> _getDocbook2outgoingReferencesXSLTs()
240    {
241        Set<String> docbook2outgoingReferencesXSLTs = new HashSet<>();
242
243        for (String key : _extensions.keySet())
244        {
245            DocbookEnhancer de = _extensions.get(key);
246            
247            String docbook2outgoingReferences = de.getDocbook2outgoingReferencesXSLT();
248            if (docbook2outgoingReferences != null)
249            {
250                docbook2outgoingReferencesXSLTs.add(docbook2outgoingReferences);
251            }
252        }
253        
254        return docbook2outgoingReferencesXSLTs;
255    }
256    
257    private Set<String> _getHTMLEditor2docbookXSLTs()
258    {
259        Set<String> htmleditor2docbookXSLTs = new HashSet<>();
260
261        for (String key : _extensions.keySet())
262        {
263            DocbookEnhancer de = _extensions.get(key);
264            
265            String htmleditor2docbook = de.getHTMLEditor2DocbookXSLT();
266            if (htmleditor2docbook != null)
267            {
268                htmleditor2docbookXSLTs.add(htmleditor2docbook);
269            }
270        }
271        
272        return htmleditor2docbookXSLTs;
273    }
274}