Daily Dilbert

Unofficial Content

DailyDilbert01

Daiy Dilbert is one of the samples accompanying the GXextensions SDK. It shows how to use RSS feeds and XSL transformations to create dynamic HTML content (a daily Dilbert cartoon in this case) which is displayed in a tool window.

The form of the tool window (DilbertWindow.cs), which was derived from AbstractToolWindow, contains a System.Windows.Forms.WebBrowser with its Dock property set to Fill (i.e.: covering all the form).

There is an XSLT file (Transform.XSLT), which contains the instructions on how to create the HTML content from a given XML obtained through the RSS feed. This file is also added to the resources and so its content is available as Resources.Transform. The DilbertWindow class, uses a property to create and store a compiled XSL transformation, based on the content of Transform.XSLT:

 

private XslCompiledTransform transform;

public XslCompiledTransform Transform
{
   get
   {
      if (transform == null)
      {
         XmlDocument xmlTransform = new XmlDocument();
         xmlTransform.LoadXml(Resources.Transform);
         transform = new XslCompiledTransform();
         transform.Load(xmlTransform);
      }

      return transform; 
   }
}

In an override of the OnLoad method, it loads the RSS and applies the transformation, using the following code:

protected override void OnLoad(EventArgs e)
{
   string documentText;
   try
   {
      StringWriter textWriter = new StringWriter(CultureInfo.InvariantCulture);
      Transform.Transform("http://feeds.feedburner.com/tapestrydilbert",
                          new XsltArgumentList(),
                          textWriter);
      documentText = textWriter.ToString();
   }
   catch (Exception exception)
   {
      documentText = GetFailPage(exception.Message);
   }
   webBrowser.DocumentText = documentText;
}

In case of an exception, the document text is obtained through the GetFailPage method, which is shown below:

protected string GetFailPage(string errorDescription)
{
   return string.Format(Resources.FailPage, errorDescription);
}

Resources.FailPage comes from an HTML file, into which the error description is inserted using string.Format. Here you can see the content of the FailPage.htm file:

<html>
    <head>
        <title></title>
    </head>
    <body>
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td style="line-height:18pt;background-color:#fbf8cb">
            <div style="text-align:left;" class="txtComun">Sorry, there was an error loading Daily Dilbert</div>
          </td>
        </tr>
        <tr>
        <td>{0}</td>
        </tr>
      </table>
    </body>
</html>

The transformation used is fairly simple. In a nutshell, it creates an HTML page containing only an image with link. The values for the src and href attributes of the respective tags, are taken from an XML node selected using /rss/channel/item/ as its path. There will typically be more than an item node in the XML, so the first one will be selected, which corresponds with the last day. The entire content of the XSLT file is as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" encoding="UTF-8"/>
   <xsl:template match="/">
      <html>
         <body>
            <a target="_blank">
               <xsl:attribute name="href">
                  <xsl:value-of select="/rss/channel/item/link"/>
               </xsl:attribute>
               <img border="0">
                  <xsl:attribute name="src">
                     <xsl:value-of select="/rss/channel/item/enclosure/@url" />
                  </xsl:attribute>
               </img>
            </a>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Project Configuration

Add to your project a new file called Catalog.xml:

<?xml version="1.0" encoding="utf-8"?>
<Projects>
    <Project Name="DailyDilbert" Location=".">
        <File Name="DailyDilbert.dll" Target="Packages" />
        <File Name="DailyDilbert.pdb" Target="Packages" />
    </Project>
</Projects>

Modify the project post-build event command line to:

"$(GX_SDK_DIR)\Tools\Updater" ..\..\Catalog.xml ..\..\ "$(GX_PROGRAM_DIR)"\ $(Configuration)
"$(GX_PROGRAM_DIR)"\Genexus /install

When your project is built, it will be copied to the GeneXus version and installed.