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 044 /** 045 * Get the content of the XSLT file 046 * @return A valid XSLT file content 047 */ 048 public String getHtml2htmleditorXSLT() 049 { 050 return _html2htmleditorXSLT; 051 } 052 053 /** 054 * Get the content of the XSLT file 055 * @return A valid XSLT file content 056 */ 057 public String getHtml2htmlXSLT() 058 { 059 return _html2htmlXSLT; 060 } 061 062 /** 063 * Get the content of the XSLT file 064 * @return A valid XSLT file content 065 */ 066 public String getHtml2outgoingReferencesXSLT() 067 { 068 return _html2outgoingReferencesXSLT; 069 } 070 071 /** 072 * Get the content of the XSLT file 073 * @return A valid XSLT file content 074 */ 075 public String getHtmlEditor2HTMLXSLT() 076 { 077 return _htmleditor2htmlXSLT; 078 } 079 080 /** 081 * Get the list of available handlers 082 * @return a non null set of roles 083 */ 084 public Set<String> getHtml2HTMLHandlers() 085 { 086 return _html2htmlHandlers; 087 } 088 089 /** 090 * Get the list of available handlers 091 * @return a non null set of roles 092 */ 093 public Set<String> getHtmleditor2HTMLHandlers() 094 { 095 return _htmleditor2htmlHandlers; 096 } 097 098 @Override 099 public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException 100 { 101 String htmleditor2HtmlXSLT = configuration.getChild("htmleditor2html").getChild("xslt").getValue(null); 102 String html2HTMLEditorXSLT = configuration.getChild("html2htmleditor").getChild("xslt").getValue(null); 103 String html2HTMLXSLT = configuration.getChild("html2html").getChild("xslt").getValue(null); 104 String html2outgoingReferencesXSLT = configuration.getChild("html2outgoingreferences").getChild("xslt").getValue(null); 105 String html2HTMLHandler = configuration.getChild("html2html").getChild("handler").getValue(null); 106 String htmleditor2HTMLHandler = configuration.getChild("htmleditor2html").getChild("handler").getValue(null); 107 108 HTMLEnhancer de = new HTMLEnhancer(pluginName, htmleditor2HtmlXSLT, html2HTMLEditorXSLT, html2HTMLXSLT, html2outgoingReferencesXSLT, html2HTMLHandler, htmleditor2HTMLHandler); 109 _extensions.put(id, de); 110 } 111 112 @Override 113 public void initializeExtensions() throws Exception 114 { 115 String template = _readTemplate(); 116 117 Set<String> html2htmleditorXSLTs = _getHtml2htmleditorXSLTs(); 118 String html2htmleditorXSLTsIncludes = _createIncludes(html2htmleditorXSLTs); 119 _html2htmleditorXSLT = template.replace("@INCLUDE@", html2htmleditorXSLTsIncludes); 120 121 Set<String> html2htmlXSLTs = _getHtml2htmlXSLTs(); 122 String html2htmlXSLTsIncludes = _createIncludes(html2htmlXSLTs); 123 _html2htmlXSLT = template.replace("@INCLUDE@", html2htmlXSLTsIncludes); 124 125 Set<String> htmleditor2htmlXSLTs = _getHTMLEditor2htmlXSLTs(); 126 String htmleditor2htmlXSLTsIncludes = _createIncludes(htmleditor2htmlXSLTs); 127 _htmleditor2htmlXSLT = template.replace("@INCLUDE@", htmleditor2htmlXSLTsIncludes); 128 129 Set<String> html2outgoingReferencesXSLTs = _getHtml2outgoingReferencesXSLTs(); 130 String html2outgoingReferencesXSLTsIncludes = _createIncludes(html2outgoingReferencesXSLTs); 131 _html2outgoingReferencesXSLT = template.replace("@INCLUDE@", html2outgoingReferencesXSLTsIncludes); 132 133 _html2htmlHandlers = new HashSet<>(); 134 for (String key : _extensions.keySet()) 135 { 136 HTMLEnhancer de = _extensions.get(key); 137 138 String handlerRole = de.getHtml2HTMLHandler(); 139 if (handlerRole != null) 140 { 141 _html2htmlHandlers.add(handlerRole); 142 } 143 } 144 145 _htmleditor2htmlHandlers = new HashSet<>(); 146 for (String key : _extensions.keySet()) 147 { 148 HTMLEnhancer de = _extensions.get(key); 149 150 String handlerRole = de.getHtmleditor2HTMLHandler(); 151 if (handlerRole != null) 152 { 153 _htmleditor2htmlHandlers.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> _getHtml2htmlXSLTs() 181 { 182 Set<String> html2htmlXSLTs = new HashSet<>(); 183 184 for (String key : _extensions.keySet()) 185 { 186 HTMLEnhancer de = _extensions.get(key); 187 188 String html2html = de.getHtml2HTMLXSLT(); 189 if (html2html != null) 190 { 191 html2htmlXSLTs.add(html2html); 192 } 193 } 194 195 return html2htmlXSLTs; 196 } 197 198 private Set<String> _getHtml2htmleditorXSLTs() 199 { 200 Set<String> html2htmleditorXSLTs = new HashSet<>(); 201 202 for (String key : _extensions.keySet()) 203 { 204 HTMLEnhancer de = _extensions.get(key); 205 206 String html2htmleditor = de.getHtml2HTMLEditorXSLT(); 207 if (html2htmleditor != null) 208 { 209 html2htmleditorXSLTs.add(html2htmleditor); 210 } 211 } 212 213 return html2htmleditorXSLTs; 214 } 215 216 private Set<String> _getHtml2outgoingReferencesXSLTs() 217 { 218 Set<String> html2outgoingReferencesXSLTs = new HashSet<>(); 219 220 for (String key : _extensions.keySet()) 221 { 222 HTMLEnhancer de = _extensions.get(key); 223 224 String html2outgoingReferences = de.getHtml2outgoingReferencesXSLT(); 225 if (html2outgoingReferences != null) 226 { 227 html2outgoingReferencesXSLTs.add(html2outgoingReferences); 228 } 229 } 230 231 return html2outgoingReferencesXSLTs; 232 } 233 234 private Set<String> _getHTMLEditor2htmlXSLTs() 235 { 236 Set<String> htmleditor2htmlXSLTs = new HashSet<>(); 237 238 for (String key : _extensions.keySet()) 239 { 240 HTMLEnhancer de = _extensions.get(key); 241 242 String htmleditor2html = de.getHTMLEditor2HtmlX(); 243 if (htmleditor2html != null) 244 { 245 htmleditor2htmlXSLTs.add(htmleditor2html); 246 } 247 } 248 249 return htmleditor2htmlXSLTs; 250 } 251}