String Replacement with XSLT

While working with SharePoint, I found that I sometimes need to replace strings with some characters or strings, for example, replacing "&" with "&", or replacing "_x0020_" with " ". This is a useful template to do so. I got this template from here.

   1:      <!-- here is the template that does the replacement -->
   2:      <xsl:template name="replaceCharsInString">
   3:        <xsl:param name="stringIn"/>
   4:        <xsl:param name="charsIn"/>
   5:        <xsl:param name="charsOut"/>
   6:        <xsl:choose>
   7:          <xsl:when test="contains($stringIn,$charsIn)">
   8:            <xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
   9:            <xsl:call-template name="replaceCharsInString">
  10:              <xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
  11:              <xsl:with-param name="charsIn" select="$charsIn"/>
  12:              <xsl:with-param name="charsOut" select="$charsOut"/>
  13:            </xsl:call-template>
  14:          </xsl:when>
  15:          <xsl:otherwise>
  16:            <xsl:value-of select="$stringIn"/>
  17:          </xsl:otherwise>
  18:        </xsl:choose>
  19:      </xsl:template>

And here's how you'll call it.

   1:  <!-- pretend this is in a template -->
   2:    <xsl:variable name="myString" select="'This%20is%20Test'"/>
   3:    <xsl:variable name="myNewString">
   4:      <xsl:call-template name="replaceCharsInString">
   5:        <xsl:with-param name="stringIn" select="string($myString)"/>
   6:        <xsl:with-param name="charsIn" select="'%20'"/>
   7:        <xsl:with-param name="charsOut" select="' '"/>
   8:      </xsl:call-template>
   9:    </xsl:variable>
  10:    <!-- $myNewString is a result tree fragment, which should be OK. -->
  11:    <!-- If you really need a string object, do this: -->
  12:    <xsl:variable name="myNewRealString" select="string($myNewString)"/>

And there you have it. Very useful indeed.

Channel 5.5: Your Host, Rob WIndsor

After the Toronto Code Camp event on Saturday, the gang went out for a drink at the Spotted Dick. Your host, Rob Windsor was interviewing each of us. There're lots of weird things going on, especially badgers running around. :) Here it is. It's hilarious. Enjoy.


Video: Toronto Code Camp 2008: Interviews at the Pub

Process Lasso

I've just installed this to try it out. Here's a description of what it is from jkOnTheRun.

Process_lasso

Anyone who has been using mobile devices for very long knows too well the hit and miss scenario caused by multi-tasking.  You have too much running in the background and the CPU grinds up to 100% and it's hard to do much of anything.  Today's Freeware of the Moment is a utility that aims to end that problem once and for all.  Process Lasso sets some rules and runs (with very little resources) in the background and prevents bad things from happening when too much gets going at once.  It watches your CPU resources and when they get tapped out it steps the priority down for processes until the system is running at an even keel.  I have heard many people praise this program but none so well as Steven Hughes of BostonPocket PC who has published a very thorough review of Process Lasso.  Check out the review and then give Process Lasso a try and see how well your older system runs with today's programs.  Note that Process Lasso runs under Windows 2000, XP and Vista and there are both 32 bit and 64 bit versions available.  Be sure and get the right one and you'll be hauling buns in short order.  Don't be intimidated, while Process Lasso has all sorts of technical settings to give you total control over your running environment the defaults will pretty much sort your system out with ease.

Presentation: Acropolis and Composite Applications

The presentation I did for the Metro Toronto .NET User Group, "A Look into the Future of Creating Composite Applications", is up on my site.

Download it here.

XNA Game Studio 2.0 Released

Well, the next version of XNA Game Studio is released. Download it here.

What’s New with XNA Game Studio?

  • XNA Game Studio 2.0 works in all versions of Visual Studio 2005. This includes Standard and Professional, as well as many other specific editions.
  • The new and improved interface makes it easier for you to manage your Xbox 360 console.
  • You’ll find that managing and building content is easier and more consistent in XNA Game Studio.
  • We’ve included project templates for content importers and processors.
  • You can configure how content is processed with the new ability to set parameters on Content Processors.

What’s new in the XNA Framework? Now you can:

  • Create rich multiplayer games over Xbox LIVE using the new networking APIs.
  • Create Audio more effectively with the new XACT editor!
  • Host XNA Framework games easily inside a Windows Form.
  • Use the virtualized GraphicsDevice: no more special code to handle device reset and recreate!
  • Take advantage of render targets that are more flexible, consistent, and easier to use. Xbox 360 and Windows now support multiple render targets (MRTs) as well.
  • Easily nest one component inside another thanks to improvements in GameComponent.
  • Enjoy many more enhancements and tweaks!

Recursive Lambda with Fixed Point Generator

Reading Mads Torgersen old blog post on Recursive Lambda Expressions, I decided it was time for me to seriously play with his code and try out the fixed point generator to create recursive lambda expressions (together with the fact that I was sick and felt bored).

Taking lessons from that blog post, I created a reusable fixed point generator which we can use for anything that uses recursion. Here's the code, simply.

   1:  public static Func<T, T> Fix<T>(Func<Func<T, T>, Func<T, T>> F)
   2:  {
   3:     return x => F(Fix<T>(F))(x);
   4:  }
   5:   
   6:  public static Func<T, T, T> Fix<T>(Func<Func<T, T, T>, Func<T, T, T>> F)
   7:  {
   8:     return (x, n) => F(Fix<T>(F))(x, n);
   9:  }
  10:   
  11:  public static Func<T1, T2, T1> Fix<T1, T2>(Func<Func<T1, T2, T1>, Func<T1, T2, T1>> F)
  12:  {
  13:     return (x, n) => F(Fix<T1, T2>(F))(x, n);
  14:  }
  15:   
  16:  public static Func<T, T, T, T> Fix<T>(Func<Func<T, T, T, T>, Func<T, T, T, T>> F)
  17:  {
  18:     return (x, n, k) => F(Fix<T>(F))(x, n, k);
  19:  }

Static Fix methods are the actual Fixed Point Generator as described in Mads' blog. This simplifies the code and makes it reusable. The method at lines 11-14 shows how 2 different input can be used. I will be using this method for the sine and cosine lambda expressions I've written.

   1:  Func<double, double> factorial = Fix<double>(fac => x => x == 0 ? 1 : x * fac(x - 1));
   2:  Func<double, int, double> sine = Fix<double, int>(sin => (x, n) => n == 0 ? x : 
   3:      sin(x, n - 1) + Math.Pow(-1, n) * Math.Pow(x, 2 * n + 1) / factorial(2 * n + 1));
   4:  Func<double, int, double> cosine = Fix<double, int>(cos => (x, n) => n == 0 ? 1 : 
   5:      cos(x, n - 1) + Math.Pow(-1, n) * Math.Pow(x, 2 * n) / factorial(2 * n));

The factorial lambda expression is the same as in Mads' blog. I've created both sine and cosine lambda expressions learning from how the fixed point generator works. I have to keep factorial as double instead of int because it'll make the precision wrong.

I used the Taylor Series in order to generate the sine and cosine function. x is the angle in radians, and n is the number of recursion to do (i.e. the more recursion, the more precision).

The basic recursion is as follows:

So that's that! Plug in your lambda expression into the Fix static method and you're done, you've got recursion.

Further testing out whether if it actually works, here's a simple testing code:

   1:  for (int i = 0; i < 20; i++)
   2:  {
   3:     Console.WriteLine(factorial(i));
   4:  }
   5:   
   6:  Console.WriteLine(sine(Math.PI / 4, 10));
   7:  Console.WriteLine(Math.Sin(Math.PI / 4));
   8:   
   9:  Console.WriteLine(cosine(Math.PI / 3, 10));
  10:  Console.WriteLine(Math.Cos(Math.PI / 3));
  11:  Console.ReadKey();

Here, what I do is I check my sine and cosine output with the actual Math.Sin and Math.Cos methods from the framework. It looks like it works well with n = 10 or higher, achieving the same precision as the framework.

That's that. I will play around with more of this, and create more methods out of this.

I tried generalizing the method itself to make it look better and easier to read, but the limits of Generics in C# stopped me from doing anything further. Here's what I tried to do, but failed of course.

   1:  // I need a constraint operator- to make this work, but Generics does not support this
   2:  public static Func<T, T> Factorial<T>() where T : struct, IEquatable<T>, operator-
   3:  {         
   4:      return Fix<T>(fac => x => x.Equals(0) ? 1 : x * fac(x - 1));
   5:  }

If that was successful, then the code would have been reduced to simply like this.

   1:  Factorial(5)

Doesn't that look better? If only, but there must be come other way to do this.

Microsoft Codename Volta

If Google can do it, so can Microsoft. Volta is the GWT of .NET. Enough said. Here's a description of what it is.

The Volta technology preview is a developer toolset that enables you to build multi-tier web applications by applying familiar techniques and patterns. First, design and build your application as a .NET client application, then assign the portions of the application to run on the server and the client tiers late in the development process. The compiler creates cross-browser JavaScript for the client tier, web services for the server tier, and communication, serialization, synchronization, security, and other boilerplate code to tie the tiers together.

Developers can target either web browsers or the CLR as clients and Volta handles the complexities of tier-splitting for you.  Volta comprises tools such as end-to-end profiling to make architectural refactoring and optimization simple and quick. In effect, Volta offers a best-effort experience in multiple environments without any changes to the application.

Download it here.

You need Visual Studio 2008 and .NET Framework 3.5.

A Detailed look at Overriding the Equality Operator in .Net

InfoQ has a very good article for those who need to override the equality operator. Quote from InfoQ:

In this deep dive article on equal operator overloading Jonathan Allen clears the air on overriding the equality operator. In the article Jonathan provides code samples in both VB and C# to demonstrate the nuances of each .NET language. He also covers usage in both structures and classes.

Areas covered include:

  • The initial Class signature
  • Fields and Properties
  • Type-Safe Equality
  • Hash Codes
  • Overriding the base class Equals method
  • Performance and Testing

Enjoy this well-thought through tutorial on Equality Operator overloading.

New Microsoft Downloads created with SilverLight

There is a new Microsoft Downloads page created entirely with Silverlight. Click on the link to see more. It's just a preview site now.

Line of Code(LOC) Counter For VSTS 2005

Microsoft IT and Microsoft Research released a Line Of Code (LOC) counter that counts the number of lines of code in your software development project to determine the size and status of your project, predicting system defects, providing measurements of productivity and quality, accessing code stability, and using these metrics to measure the success of your project.

LOC Counter can be used as a stand-alone client or as a Visual Studio 2005 add-in. The tool has the following features:

  • It handles many different programming languages.
  • It performs many different kinds of code counts.
  • It handles comments, system-generated code, blank lines, and code churn.
  • It connects to many different repositories.
  • It provides an estimated defect density that is based on code churn.
  • It is customizable. A user can change the kinds of objects that are counted during a counting task.
  • It generates detailed reports. In addition, a user can export the report information to a Microsoft Office Excel® worksheet or to a Portable Document Format (PDF) file.
  • It is fast. The tool can parse 10 million lines of code in less than one hour.

It uses a Defect Density Algorithm developed from Microsoft Research that uses the following version control history of a file to estimate the software defect density with an accuracy of 89 percent:

  • The number of times that the selected files has been modified
  • The time period in which the modifications have occurred
  • The number of files that were actually modified

"Use of Relative Code Churn Measures to Predict System Defect Density" is a PDF document from Microsoft Research that describes this.

An IT Showcase web cast called How Microsoft IT Uses Visual Studio Team System 2005 to Measure Software Code Stability includes a discussion on how code changes, also known as code churn, may affect the code stability of a programming project.