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 _docbook2mdXSLT;
040    private String _htmleditor2docbookXSLT;
041    private String _docbook2outgoingReferencesXSLT;
042    private Set<String> _docbook2htmlHandlers;
043    private Set<String> _htmleditor2docbookHandlers;
044    private Set<String> _docbook2htmleditorHandlers;
045    private Set<String> _docbook2mdHandlers;
046    
047    /**
048     * Get the content of the XSLT file
049     * @return A valid XSLT file content
050     */
051    public String getDocbook2htmleditorXSLT()
052    {
053        return _docbook2htmleditorXSLT;
054    }
055    
056    /**
057     * Get the content of the XSLT file
058     * @return A valid XSLT file content
059     */
060    public String getDocbook2htmlXSLT()
061    {
062        return _docbook2htmlXSLT;
063    }
064    
065    /**
066     * Get the content of the XSLT file
067     * @return A valid XSLT file content
068     */
069    public String getDocbook2mdXSLT()
070    {
071        return _docbook2mdXSLT;
072    }
073    
074    /**
075     * Get the content of the XSLT file
076     * @return A valid XSLT file content
077     */
078    public String getDocbook2outgoingReferencesXSLT()
079    {
080        return _docbook2outgoingReferencesXSLT;
081    }
082    
083    /**
084     * Get the content of the XSLT file
085     * @return A valid XSLT file content
086     */
087    public String getHTMLEditor2docbookXSLT()
088    {
089        return _htmleditor2docbookXSLT;
090    }
091    
092    /**
093     * Get the list of available handlers
094     * @return a non null set of roles
095     */
096    public Set<String> getDocbook2HTMLHandlers()
097    {
098        return _docbook2htmlHandlers;
099    }
100    
101    /**
102     * Get the list of available handlers
103     * @return a non null set of roles
104     */
105    public Set<String> getDocbook2MDHandlers()
106    {
107        return _docbook2mdHandlers;
108    }
109    
110    /**
111     * Get the list of available handlers
112     * @return a non null set of roles
113     */
114    public Set<String> getHTMLEditor2DocbookHandlers()
115    {
116        return _htmleditor2docbookHandlers;
117    }
118    
119    /**
120     * Get the list of available handlers
121     * @return a non null set of roles
122     */
123    public Set<String> getDocbook2HTMLEditorHandlers()
124    {
125        return _docbook2htmleditorHandlers;
126    }
127
128    @Override
129    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
130    {
131        String htmleditor2DocbookXSLT = configuration.getChild("htmleditor2docbook").getChild("xslt").getValue(null);
132        String docbook2HTMLEditorXSLT = configuration.getChild("docbook2htmleditor").getChild("xslt").getValue(null);
133        String docbook2HTMLXSLT = configuration.getChild("docbook2html").getChild("xslt").getValue(null);
134        String docbook2MDXSLT = configuration.getChild("docbook2md").getChild("xslt").getValue(null);
135        String docbook2outgoingReferencesXSLT = configuration.getChild("docbook2outgoingreferences").getChild("xslt").getValue(null);
136        String docbook2HTMLHandler = configuration.getChild("docbook2html").getChild("handler").getValue(null);
137        String docbook2MDHandler = configuration.getChild("docbook2md").getChild("handler").getValue(null);
138        String htmlEditor2docbookHandler = configuration.getChild("htmleditor2docbook").getChild("handler").getValue(null);
139        String docbook2htmlEditorHandler = configuration.getChild("docbook2htmleditor").getChild("handler").getValue(null);
140
141        DocbookEnhancer de = new DocbookEnhancer(pluginName, htmleditor2DocbookXSLT, docbook2HTMLEditorXSLT, docbook2HTMLXSLT, docbook2MDXSLT, docbook2outgoingReferencesXSLT, docbook2HTMLHandler, docbook2MDHandler, htmlEditor2docbookHandler, docbook2htmlEditorHandler);
142        _extensions.put(id, de);
143    }
144    
145    @Override
146    public void initializeExtensions() throws Exception
147    {
148        String template = _readTemplate();
149        
150        Set<String> docbook2htmleditorXSLTs = _getDocbook2htmleditorXSLTs();
151        String docbook2htmleditorXSLTsIncludes = _createIncludes(docbook2htmleditorXSLTs);
152        _docbook2htmleditorXSLT = template.replace("@INCLUDE@", docbook2htmleditorXSLTsIncludes);
153
154        Set<String> docbook2htmlXSLTs = _getDocbook2htmlXSLTs();
155        String docbook2htmlXSLTsIncludes = _createIncludes(docbook2htmlXSLTs);
156        _docbook2htmlXSLT = template.replace("@INCLUDE@", docbook2htmlXSLTsIncludes);
157
158        Set<String> docbook2mdXSLTs = _getDocbook2mdXSLTs();
159        String docbook2mdXSLTsIncludes = _createIncludes(docbook2mdXSLTs);
160        _docbook2mdXSLT = template.replace("@INCLUDE@", docbook2mdXSLTsIncludes);
161
162        Set<String> docbook2outgoingReferencesXSLTs = _getDocbook2outgoingReferencesXSLTs();
163        String docbook2outgoingReferencesXSLTsIncludes = _createIncludes(docbook2outgoingReferencesXSLTs);
164        _docbook2outgoingReferencesXSLT = template.replace("@INCLUDE@", docbook2outgoingReferencesXSLTsIncludes);
165
166        Set<String> htmleditor2docbookXSLTs = _getHTMLEditor2docbookXSLTs();
167        String htmleditor2docbookXSLTsIncludes = _createIncludes(htmleditor2docbookXSLTs);
168        _htmleditor2docbookXSLT = template.replace("@INCLUDE@", htmleditor2docbookXSLTsIncludes);
169        
170        _docbook2htmlHandlers = new HashSet<>();
171        for (String key : _extensions.keySet())
172        {
173            DocbookEnhancer de = _extensions.get(key);
174         
175            String handlerRole = de.getDocbook2HTMLHandler();
176            if (handlerRole != null)
177            {
178                _docbook2htmlHandlers.add(handlerRole);
179            }
180        }
181        
182        _docbook2mdHandlers = new HashSet<>();
183        for (String key : _extensions.keySet())
184        {
185            DocbookEnhancer de = _extensions.get(key);
186         
187            String handlerRole = de.getDocbook2MDHandler();
188            if (handlerRole != null)
189            {
190                _docbook2mdHandlers.add(handlerRole);
191            }
192        }
193        
194        _htmleditor2docbookHandlers = new HashSet<>();
195        for (String key : _extensions.keySet())
196        {
197            DocbookEnhancer de = _extensions.get(key);
198         
199            String handlerRole = de.getHTMLEditor2DocbookHandler();
200            if (handlerRole != null)
201            {
202                _htmleditor2docbookHandlers.add(handlerRole);
203            }
204        }
205        
206        _docbook2htmleditorHandlers = new HashSet<>();
207        for (String key : _extensions.keySet())
208        {
209            DocbookEnhancer de = _extensions.get(key);
210         
211            String handlerRole = de.getDocbook2HTMLEditorHandler();
212            if (handlerRole != null)
213            {
214                _docbook2htmleditorHandlers.add(handlerRole);
215            }
216        }
217    }
218
219    private String _createIncludes(Set<String> includes)
220    {
221        StringBuffer sb = new StringBuffer("");
222        
223        for (String file : includes)
224        {
225            sb.append("<xsl:include href=\"");
226            sb.append(file);
227            sb.append("\"/>");
228        }
229        
230        return sb.toString();
231    }
232    
233    private String _readTemplate() throws IOException
234    {
235        try (InputStream is = this.getClass().getResourceAsStream("template.xsl"))
236        {
237            return IOUtils.toString(is, "UTF-8");
238        }
239    }
240
241    private Set<String> _getDocbook2htmlXSLTs()
242    {
243        Set<String> docbook2htmlXSLTs = new HashSet<>();
244
245        for (String key : _extensions.keySet())
246        {
247            DocbookEnhancer de = _extensions.get(key);
248            
249            String docbook2html = de.getDocbook2HTMLXSLT();
250            if (docbook2html != null)
251            {
252                docbook2htmlXSLTs.add(docbook2html);
253            }
254        }
255        
256        return docbook2htmlXSLTs;
257    }
258    
259    private Set<String> _getDocbook2mdXSLTs()
260    {
261        Set<String> docbook2mdXSLTs = new HashSet<>();
262
263        for (String key : _extensions.keySet())
264        {
265            DocbookEnhancer de = _extensions.get(key);
266            
267            String docbook2html = de.getDocbook2MDXSLT();
268            if (docbook2html != null)
269            {
270                docbook2mdXSLTs.add(docbook2html);
271            }
272        }
273        
274        return docbook2mdXSLTs;
275    }
276    
277    private Set<String> _getDocbook2htmleditorXSLTs()
278    {
279        Set<String> docbook2htmleditorXSLTs = new HashSet<>();
280
281        for (String key : _extensions.keySet())
282        {
283            DocbookEnhancer de = _extensions.get(key);
284            
285            String docbook2htmleditor = de.getDocbook2HTMLEditorXSLT();
286            if (docbook2htmleditor != null)
287            {
288                docbook2htmleditorXSLTs.add(docbook2htmleditor);
289            }
290        }
291        
292        return docbook2htmleditorXSLTs;
293    }
294    
295    private Set<String> _getDocbook2outgoingReferencesXSLTs()
296    {
297        Set<String> docbook2outgoingReferencesXSLTs = new HashSet<>();
298
299        for (String key : _extensions.keySet())
300        {
301            DocbookEnhancer de = _extensions.get(key);
302            
303            String docbook2outgoingReferences = de.getDocbook2outgoingReferencesXSLT();
304            if (docbook2outgoingReferences != null)
305            {
306                docbook2outgoingReferencesXSLTs.add(docbook2outgoingReferences);
307            }
308        }
309        
310        return docbook2outgoingReferencesXSLTs;
311    }
312    
313    private Set<String> _getHTMLEditor2docbookXSLTs()
314    {
315        Set<String> htmleditor2docbookXSLTs = new HashSet<>();
316
317        for (String key : _extensions.keySet())
318        {
319            DocbookEnhancer de = _extensions.get(key);
320            
321            String htmleditor2docbook = de.getHTMLEditor2DocbookXSLT();
322            if (htmleditor2docbook != null)
323            {
324                htmleditor2docbookXSLTs.add(htmleditor2docbook);
325            }
326        }
327        
328        return htmleditor2docbookXSLTs;
329    }
330}