| How to create a renderer |
Create a class implementing IRenderer.
Create an IRendererFactory
This class must inherit from System.Configuration.ConfigurationSection so that it can be declared in the configuration file, and implement the IRendererFactory interface by returning an instance of the class created at the first step.
If the renderer class is thread independent, you can use the singleton pattern and have only one instance of the renderer class.
For more information about creating a custom ConfigurationSection, you can read this MSDN how-to: creating a custom ConfigurationSection
Implement Intercepts(Config): in this method, you must determine if this renderer can render the output, and if so, in Binary or Character mode.
If it is in Character mode, the Render(XmlWriter, Config, XslCompiledTransform) or the Render(TextWriter, Config, XslCompiledTransform) will be called depending on the available output device.
If it is in Binary mode, the Render(Stream, Config, XslCompiledTransform) will be called.
Implement the Render methods: renderers are supposed to filter the output from the XSLT transformation with a third party tool, like a custom XmlWriter or a transforming tool of some kind - we use a command line FOP call to transform XSL-FO content to PDF for example.
Thus, you will most probably call the Render(XmlWriter, XslCompiledTransform) method or one of its overloads, and either pass a custom XmlWriter to it or filter its output after.
Declare a section in the configSections part of the configuration file with your class name and assembly
<configuration> <configSections> <section name="myrenderer" type="MyNamespace.MyRendererFactory,MyAssembly"/> </configSection> </configuration>
Use this section name in your handlers renderers list
<activsoft.engine> <handlers> <handler name="myhandler" path="myhandler.ashx" defaultTemplate="1"> ... <renderers> <renderer section="myrenderer"/> </renderers> ... </handler> </handlers> <loader section=""/> </activsoft.engine>
This is a simple renderer zipping the output from the XSLT transformation.
using System.IO; using System.Xml; using System.Xml.Xsl; using Activsoft.Engine; using ICSharpCode.SharpZipLib.Zip; namespace MyNamespace { public class MyRenderer : IRenderer { public RenderMode Intercepts(Config monConfig) { if(monConfig["RETURN_ZIPPED"]=="1") { return RenderMode.Binary; } return RenderMode.None; } public void Render(XmlWriter ser, Config monConfig, XslCompiledTransform transformer) { throw new NotImplementedException(); } public void Render(TextWriter w, Config monConfig, XslCompiledTransform transformer) { throw new NotImplementedException(); } public void Render(Stream s, Config monConfig, XslCompiledTransform transformer) { using (ZipOutputStream zos = new ZipOutputStream(s)) { zos.PutNextEntry(new ZipEntry("output.xml")); monConfig.Render(new XHtmlWriter(XmlWriter.Create(zos,transformer.OutputSettings)),transformer); zos.CloseEntry(); } } } }
using Activsoft.Engine; using System.Configuration; namespace MyNamespace { public class MyRendererFactory : ConfigurationSection, IRendererFactory { public IRenderer CreateRenderer(Config monConfig) { return new MyRenderer(); } } }
<configuration> <configSections> <section name="myrenderer" type="MyNamespace.MyRendererFactory,MyAssembly"/> <section name="activsoft.engine" type="Activsoft.Engine.EngineSection,Activsoft.Engine"/> <section name="secureFactory" type="Activsoft.Engine.Web.SecureDatabaseCookieSessionManagerFactory,Activsoft.Engine.Web"/> <section name="xslfilter" type="Activsoft.Engine.XslFilter.XslFilterSection,Activsoft.Engine"/> <section name="dashboardRenderer" type="Activsoft.Dashboard.DashboardRendererFactory,Activsoft.Dashboard"/> <section name="webLoader" type="Activsoft.Engine.Web.WebConfigLoaderConfigurationSection,Activsoft.Engine.Web"/> </configSection> <myextractor myProperty="world"/> <secureFactory cookieName="mycookie" /> <dashboardRenderer/> <activsoft.engine> <handlers> <handler name="myhandler" path="myhandler.ashx" defaultTemplate="1"> <actions> </actions> <extractors> </extractors> <renderers> <renderer section="myrenderer"/> </renderers> <enginemodule> <session section="secureFactory"/> <params cookie="mycookie" xslpath="..\xsl"/> <database section="db"/> </enginemodule> <transformgenerator section="xslfilter"/> </handler> </handlers> <loader section="webLoader"/> </activsoft.engine> <xslfilter> <filters> <add type="Activsoft.Dashboard.XslFilter,Activsoft.Dashboard"> <javascript outputDir="..\xsljs"/> </add> </filters> </xslfilter> </configuration>