NVelocity XML encode all string variables

1 Nov

Getting started with NVelocity:

The library (choose either or; my preference is the Castle project one):

The basic setup code for NVelocity:

var outputBuffer = new StringBuilder();


var velocityContext = new VelocityContext();

var model = new Model();

velocityContext.Put(“model”, model);

var template = Velocity.GetTemplate(“Template.vm”);

template.Merge(velocityContext, new StringWriter(outputBuffer));

The model:

public class Model


public String MyVariable


get { return “Test & Encode”; }



The template (Template.vm; mark file as copy to Output directory):

<?xml version=”1.0″ encoding=”UTF-8″?>




XML encoding:

The subscription to add a behavior over NVelocity parsing:

velocityContext.EventCartridge.ReferenceInsertion += EventCartridge_ReferenceInsertion_XmlEscapingAllStringContent; // add our custom handler to the ReferenceInsertion event

The behavior to XML escape:

private static void EventCartridge_ReferenceInsertion_XmlEscapingAllStringContent(object sender, ReferenceInsertionEventArgs e)

var originalString = e.OriginalValue as string;

if (originalString == null)

e.NewValue = SecurityElement.Escape(originalString);


Expected Output:

<?xml version=”1.0″ encoding=”UTF-8″?>


Test & Encode



I am using this to generate a 760mb file which takes a few seconds to generate. It seems NVelocity is a good solution for generating files, as it keeps models clean and gives an ASP.NET MVC feel to file creation where the view (in the case an NVelocity template) has the output logic, pulling information from models.

Making terrain based maps/levels for UDK

24 Feb

Recently I started using the UDK, and have been extremely impressed with it. I have managed to essentially make the following level in a matter of hours:


It is a contrived level, and is not complete by any means. There were a number of tutorials and links which are worth watching in order to learn how to make a level like this:

Free textures:

http://nobiax.deviantart.com/ in UDK format already
www.cgtextures.com (requires signup)



The way I found was best to make water was to find an existing “Liquid” and copy that material, and then edit it, and use the kismet style UI to play around with the logic. The logic took me 30 minutes to get to a point where I understood what it did, but if you look at the texture images you will start to get an idea of what is going on. Essentially there are horizontal and vertical height maps that combine to make the height wave form, ie simulating moving water. If you play around with the numbers in them you can slow the water down etc.


http://www.youtube.com/watch?v=VYGG2e9CEBE (Terrain & texturing)

Exporting from Blender 2.65a for the Unreal Development Kit (UDK)

20 Jan

I had followed many tutorials on how to export from blender, but unfortunately the python scripts were out of date and had errors preventing the psk file from being generated.

The tutorials I was following were:

UDK Melee Blender tutorial:

UDK Gun Weapon basic Blender tutorial, with bones:

UDK shotgun weapon tutorial:

I managed to fix up their script by commenting out a few unnessasary lines (I have never done python before):


If you rename that file to .py and replace your existing blender addon, it should work as demonstrated in the youtube videos.

Adding a controller to JQueryMobile

1 Aug

JQuery Mobile highlights the power of HTML5 data attributes. An example div element with such an attribute could be:

<div data-role="header">

This new feature introduced in HTML5 – but backwards compatible with previous versions – is able to provide us with the flexibility to say what we mean rather than impreatively tell the code to do something. Im talking about declarative vs imperative programming.

Why Controllers in javascript?

Controllers are a part of the MVC pattern and are quite common in many areas of programming, however in my experience not so common in javascript development. This is possibly due to the requirement of javascript growing more and more as time goes on, and possibly due to the small scale in which it is usually used. Mobile development with phone gap tends to challenge this perspective of javascript by enabling large javascript applications to be developed in the mainstream.

Proposed approach

The Html

<a data-action="postForm">

The JQuery

$('a[data-action*=""]').live('click', function () {
      Log("Click -> Action");
var pageController = new PageController();

function PageController() {
    this.processAction = function (itemWithAction) {

        var action = itemWithAction.attr('data-action');

        //Log("action = " + action);

        //Do something with the action here!!!

Performance benefits

Subscribing one click event to all necessary elements of the DOM for an application will be quite the performance hit. Instead this solution is subscribing to the root element of the DOM once. For more information see: http://api.jquery.com/live/


The actions are able to be reused across multiple pages. They are fully separated from the page, with a reference to the triggering element.


Html elements that use data attributes to call actions make tracing event-action mappings much easier

Jquery Mobile – Dynamic Pages

9 Apr

Recently, I decided to make a mobile application. I have chosen to use a combination of Jquery Mobile and phone gap in order to achieve this goal.

I have found Jquery Mobile to be a good framework for making mobile applications however it is not long before discovering its abilities to dynamically generate content are not well documented. This is possibly because at the time of writing this, Jquery Mobile is in alpha status.

After googling round for quite a while, I discovered that there is a function called page(). That when applied to the end of an HTML fragment, it will convert the HTML into JQuery mobile appropriate HTML. The concepts of using this that are important are as follows

  1. Jquery mobile does not restrict functionality of Jquery.
  2. Jquery mobile parses an HTML document with the page() function to convert HTML to jquery mobile HTML (for better control/css theming etc).
  3. Jquery mobile will not parse updated html fragments added to the document unless you explicitly call the .page() function on that HTML fragment.
  4. In modern browsers, Jquery/java-script often  treats html blobs as a ‘documentFragment’ type which has virtually no functionality[W3C]. This can cause some confusion when calling the page function as the page function will fail on documentFragment types as discussed by me [Jquery Forum].
  5. Jquery mobile allows us to build phone gap and internet browser applications, allowing us to reach many markets with one solution.
  6. Jquery mobile is built on HTML 5. It uses the new meta data functionality for tags, i.e. <div data-tag=”taginfo” /> is valid in HTML 5 as any attribute on any tag that starts with data- is treated as meta-data (its not executed, but is recognized as valid by HTML 5 browsers).
  7. Jquery mobile has good examples but not always a good discussion of the concepts behind it, maybe I skipped over that part in my enthusiasm to get developing.
  8. Regular Updates. Currently they seem to release a new version of the alpha every 3 months.

An example of how to use the page function:

1) make sure you have a “page” on your html document:

   <div id="MyFirstMobilePage" data-role="page"></div>

2) add the following to the head of the document:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" />
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js"></script>
<script type="text/javascript">
      $('<select><option>test 1</option></select>').appendTo('#MyFirstMobilePage).page();

If you run this code with firebug etc. installed in your browser you will observe that the HTML gets updated, not only inserting the select tag, but also other tags that are required by the mobile framework.

WCF Windows Service Deployment

1 Dec

I recently started work on a personal project to make a WCF Windows Service using the following tutorial: [1]. I soon realised that I would need to write a batch file to not be hugely disadvantaged by manual deployment. I also wanted my deployment batch scripts to be re-useable by other developers on the project.

The batch files I came up with were:



:: Known issues –
:: 1) if services window is open (ie the one when u start->run->services.msc) then the process will not close. You must close this window.
:: 2) if you don’t do a clean build then it will just install old version of service. Make sure you uninstall then do a clean build.


SET projectDirectory=%CD%

cd /d “%WinDir%\Microsoft.NET\Framework\v4.0.*”

SET net4Path=%CD%


%net4Path%\installutil “%PROJECTDIRECTORY%\bin\Debug\OrderServices.exe”

net start OrderServices




SET projectDirectory=%CD%

cd /d “%WinDir%\Microsoft.NET\Framework\v4.0.*”

SET net4Path=%CD%

net stop OrderServices

%net4Path%\installutil /u “%PROJECTDIRECTORY%\bin\Debug\OrderServices.exe”


Reinstall.bat (untested):

@echo off
CALL Unistall.bat
CALL Install.bat

Those batch files assume the following file structure in your service project:



The following link adds reference information as to how to forcefully play with services with the SC command: http://ss64.com/nt/sc.html

Dependency Property Basics

5 Mar

When working with WPF it can be necessary to extend a control to enable data-binding to a property that isn’t bind-able (because it is not a dependency property etc.). The following code demonstrates an example I have made where I override a Text property and give it a dependency property to enable data-binding.


public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TextEditorControl), new FrameworkPropertyMetadata(OnTextPropertyChanged));


private static void OnTextPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)


    //TODO: action handler here



public override string Text




        return (string)GetValue(TextProperty) ?? string.Empty;




        base.Text = value;


        SetValue(TextProperty, value);




The code also shows how to handle the event raised when the binding source (the view model) is updated. I recommend using dependency properties for WPF controls; as for view model classes I recommend implementing the INotifyPropertyChanged interface instead.


Get every new post delivered to your Inbox.