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> _htmleditor2docbook2Handlers;
043    
044    /**
045     * Get the content of the XSLT file
046     * @return A valid XSLT file content
047     */
048    public String getDocbook2htmleditorXSLT()
049    {
050        return _docbook2htmleditorXSLT;
051    }
052    
053    /**
054     * Get the content of the XSLT file
055     * @return A valid XSLT file content
056     */
057    public String getDocbook2htmlXSLT()
058    {
059        return _docbook2htmlXSLT;
060    }
061    
062    /**
063     * Get the content of the XSLT file
064     * @return A valid XSLT file content
065     */
066    public String getDocbook2outgoingReferencesXSLT()
067    {
068        return _docbook2outgoingReferencesXSLT;
069    }
070    
071    /**
072     * Get the content of the XSLT file
073     * @return A valid XSLT file content
074     */
075    public String getHTMLEditor2docbookXSLT()
076    {
077        return _htmleditor2docbookXSLT;
078    }
079    
080    /**
081     * Get the list of available handlers
082     * @return a non null set of roles
083     */
084    public Set<String> getDocbook2HTMLHandlers()
085    {
086        return _docbook2htmlHandlers;
087    }
088    
089    /**
090     * Get the list of available handlers
091     * @return a non null set of roles
092     */
093    public Set<String> getHTMLEditor2DocbookHandlers()
094    {
095        return _htmleditor2docbook2Handlers;
096    }
097
098    @Override
099    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
100    {
101        String htmleditor2DocbookXSLT = configuration.getChild("htmleditor2docbook").getChild("xslt").getValue(null);
102        String docbook2HTMLEditorXSLT = configuration.getChild("docbook2htmleditor").getChild("xslt").getValue(null);
103        String docbook2HTMLXSLT = configuration.getChild("docbook2html").getChild("xslt").getValue(null);
104        String docbook2outgoingReferencesXSLT = configuration.getChild("docbook2outgoingreferences").getChild("xslt").getValue(null);
105        String docbook2HTMLHandler = configuration.getChild("docbook2html").getChild("handler").getValue(null);
106        String htmlEditor2docbookHandler = configuration.getChild("htmleditor2docbook").getChild("handler").getValue(null);
107        
108        DocbookEnhancer de = new DocbookEnhancer(pluginName, htmleditor2DocbookXSLT, docbook2HTMLEditorXSLT, docbook2HTMLXSLT, docbook2outgoingReferencesXSLT, docbook2HTMLHandler, htmlEditor2docbookHandler);
109        _extensions.put(id, de);
110    }
111    
112    @Override
113    public void initializeExtensions() throws Exception
114    {
115        String template = _readTemplate();
116        
117        Set<String> docbook2htmleditorXSLTs = _getDocbook2htmleditorXSLTs();
118        String docbook2htmleditorXSLTsIncludes = _createIncludes(docbook2htmleditorXSLTs);
119        _docbook2htmleditorXSLT = template.replace("@INCLUDE@", docbook2htmleditorXSLTsIncludes);
120
121        Set<String> docbook2htmlXSLTs = _getDocbook2htmlXSLTs();
122        String docbook2htmlXSLTsIncludes = _createIncludes(docbook2htmlXSLTs);
123        _docbook2htmlXSLT = template.replace("@INCLUDE@", docbook2htmlXSLTsIncludes);
124
125        Set<String> docbook2outgoingReferencesXSLTs = _getDocbook2outgoingReferencesXSLTs();
126        String docbook2outgoingReferencesXSLTsIncludes = _createIncludes(docbook2outgoingReferencesXSLTs);
127        _docbook2outgoingReferencesXSLT = template.replace("@INCLUDE@", docbook2outgoingReferencesXSLTsIncludes);
128
129        Set<String> htmleditor2docbookXSLTs = _getHTMLEditor2docbookXSLTs();
130        String htmleditor2docbookXSLTsIncludes = _createIncludes(htmleditor2docbookXSLTs);
131        _htmleditor2docbookXSLT = template.replace("@INCLUDE@", htmleditor2docbookXSLTsIncludes);
132        
133        _docbook2htmlHandlers = new HashSet<>();
134        for (String key : _extensions.keySet())
135        {
136            DocbookEnhancer de = _extensions.get(key);
137         
138            String handlerRole = de.getDocbook2HTMLHandler();
139            if (handlerRole != null)
140            {
141                _docbook2htmlHandlers.add(handlerRole);
142            }
143        }
144        
145        _htmleditor2docbook2Handlers = new HashSet<>();
146        for (String key : _extensions.keySet())
147        {
148            DocbookEnhancer de = _extensions.get(key);
149         
150            String handlerRole = de.getHTMLEditor2DocbookHandler();
151            if (handlerRole != null)
152            {
153                _htmleditor2docbook2Handlers.add(handlerRole);
154            }
155        }
156    }
157
158    private String _createIncludes(Set<String> includes)
159    {
160        StringBuffer sb = new StringBuffer("");
161        
162        for (String file : includes)
163        {
164            sb.append("<xsl:include href=\"");
165            sb.append(file);
166            sb.append("\"/>");
167        }
168        
169        return sb.toString();
170    }
171    
172    private String _readTemplate() throws IOException
173    {
174        try (InputStream is = this.getClass().getResourceAsStream("template.xsl"))
175        {
176            return IOUtils.toString(is, "UTF-8");
177        }
178    }
179
180    private Set<String> _getDocbook2htmlXSLTs()
181    {
182        Set<String> docbook2htmlXSLTs = new HashSet<>();
183
184        for (String key : _extensions.keySet())
185        {
186            DocbookEnhancer de = _extensions.get(key);
187            
188            String docbook2html = de.getDocbook2HTMLXSLT();
189            if (docbook2html != null)
190            {
191                docbook2htmlXSLTs.add(docbook2html);
192            }
193        }
194        
195        return docbook2htmlXSLTs;
196    }
197    
198    private Set<String> _getDocbook2htmleditorXSLTs()
199    {
200        Set<String> docbook2htmleditorXSLTs = new HashSet<>();
201
202        for (String key : _extensions.keySet())
203        {
204            DocbookEnhancer de = _extensions.get(key);
205            
206            String docbook2htmleditor = de.getDocbook2HTMLEditorXSLT();
207            if (docbook2htmleditor != null)
208            {
209                docbook2htmleditorXSLTs.add(docbook2htmleditor);
210            }
211        }
212        
213        return docbook2htmleditorXSLTs;
214    }
215    
216    private Set<String> _getDocbook2outgoingReferencesXSLTs()
217    {
218        Set<String> docbook2outgoingReferencesXSLTs = new HashSet<>();
219
220        for (String key : _extensions.keySet())
221        {
222            DocbookEnhancer de = _extensions.get(key);
223            
224            String docbook2outgoingReferences = de.getDocbook2outgoingReferencesXSLT();
225            if (docbook2outgoingReferences != null)
226            {
227                docbook2outgoingReferencesXSLTs.add(docbook2outgoingReferences);
228            }
229        }
230        
231        return docbook2outgoingReferencesXSLTs;
232    }
233    
234    private Set<String> _getHTMLEditor2docbookXSLTs()
235    {
236        Set<String> htmleditor2docbookXSLTs = new HashSet<>();
237
238        for (String key : _extensions.keySet())
239        {
240            DocbookEnhancer de = _extensions.get(key);
241            
242            String htmleditor2docbook = de.getHTMLEditor2DocbookXSLT();
243            if (htmleditor2docbook != null)
244            {
245                htmleditor2docbookXSLTs.add(htmleditor2docbook);
246            }
247        }
248        
249        return htmleditor2docbookXSLTs;
250    }
251}