Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / XML
Article

A Library for Writing/Building Scripts in C#

Rate me:
Please Sign up or sign in to vote.
4.15/5 (6 votes)
14 Oct 2008CPOL3 min read 54.4K   503   55   12
Designed to make it easier to write scripts such as JavaScript in C#

Introduction

ClockWork Script Builder is a .NET library (2.0) to make the building of scripts more readable and structured.

An extendible architecture lets developers add script building blocks (Script Items) for languages such as the ones provided in the download for JavaScript (Js) and XML (Xs).

Background

Recently I started playing with ExtJs and I ended up needing to build large chunks of JavaScript from .NET code. I've never found a nice way to construct scripts in C# so this time I decided to write a library to help.

Using the Code

(CHM documentation is available from Tony's Wibbles.)

In its simplest form, it is a way to join a bunch of strings together so that each is written on a new line:

C#
Script script = Sb.Script(
    "line 1;",
    "line 2;",
    "line 3;"
);

string result = script.Render();

Console.WriteLine(result);

It results in the following output:

line 1;
line 2;
line 3;

However there's a lot more to it. Scripts don't just understand strings but accept any type of object. In particular, objects based on an IScriptItem are specifically designed to work with scripts and can be written by anyone to extend scripting abilities.

This example demonstrates the use of some core layout items.

C#
Script script = Sb.Script(    
    Sb.Line("1","2","3","4","5",";"),
    Sb.Line("1","2","3","4","5",";"),
    Sb.Indent(
        Sb.Line("indented ","1","2","3","4","5",";"),
        Sb.Line("indented ","1","2","3","4","5",";"),
        Sb.Script(
            Sb.Line("Some objects"),
            Sb.Line("Number = ", 47),
            Sb.Line("True = ", true),
            Sb.Line("Now is = ", DateTime.Now)
        )    
    )
);

string result = script.Render();

Console.WriteLine(result);

This results in the following output:

12345;
12345;
    indented 12345;
    indented 12345;
    Some objects
    Number = 47
    True = True
    Now is = 23/08/2008 11:23:00 AM

You will have noticed all the Script items are created using "Sb.". To make scripts easier to write and read, I have created a helper classes for each set of script items. They provide static methods that will create instances of script items for you.

Script Languages

I have currently developed the following sets of script items to help in writing scripts:

Script SetDescriptionHelper Class
CoreCore Script Items to enable constructing formatted scriptsClockWork.ScriptBuilder.Sb
JavaScriptItems to help construct standard JavaScript elements such as Objects, Arrays, Lists, Functions, Calls, Blocks, StatementsClockWork.ScriptBuilder.JavaScript.Js
XMLItems to construct basic XML nodes of Element, Attribute, Text and CData. Also supports direct writing to an XmlElement or XmlDocumentClockWork.ScriptBuilder.XmlScript.Xs
ExtJsClass and Component items designed to make it easier to write subclasses for ExtJs classesClockWork.ScriptBuilder.JavaScript.ExtJs.ExtJs

Points of Interest

DOM & Rendering

All objects added to a script are stored as-is (like a DOM). The string representation of the script is only created when its Render method is called. Among other things, this lets you build the script in steps letting you structure your builder code.

If an IScriptItem is found while rendering then it will use its own Render method to render itself. Other objects are rendered using ToString with an optional format provider.

Rendering is performed using a ScriptWriter which is passed into the Render methods. This object provides the ability to render to several destinations and also handles indentation.

ScriptLayout

Most items have the option to select a layout mode (Inline, InlineBlock, Block). The effects if the item will render across multiple lines.

ScriptIf

This item will do an if-then-else check at render time and then render the object that won.

A collection based ScriptItem (ScriptSet) will result in false if its collection is empty. Therefore you can decide if something is rendered based on the content of another item (all at render time).

Objects can implement the IScriptIfCondition interface if they wish to dynamically provide results for the if condition.

History

  • 25th August, 2008: Initial version 1.0.0
  • 12th October, 2008: Source file updated (version 1.0.1)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Web Site Advantage
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat job! Pin
Mohammad Dehghan2-Dec-08 17:57
Mohammad Dehghan2-Dec-08 17:57 
GeneralT4 Pin
sam.hill14-Oct-08 15:27
sam.hill14-Oct-08 15:27 
Generalgood Pin
JLKEngine0088-Oct-08 15:44
JLKEngine0088-Oct-08 15:44 
NewsRe: good Pin
Tiggerito14-Oct-08 16:35
professionalTiggerito14-Oct-08 16:35 
GeneralPractical example Pin
Parthasarathy Mandayam26-Aug-08 5:28
Parthasarathy Mandayam26-Aug-08 5:28 
GeneralRe: Practical example Pin
Tiggerito4-Sep-08 19:06
professionalTiggerito4-Sep-08 19:06 
I'm thinking of adding an example that is a json based server for ajax clients. The json data (aka javascript objects) would be generated using the Script Builder. Will get onto it asap.
AnswerRe: Practical example [modified] Pin
Tiggerito29-Sep-08 5:58
professionalTiggerito29-Sep-08 5:58 
GeneralSuggestion for Improvement Pin
Andrew Rissing26-Aug-08 3:51
Andrew Rissing26-Aug-08 3:51 
GeneralRe: Suggestion for Improvement Pin
Tiggerito4-Sep-08 19:14
professionalTiggerito4-Sep-08 19:14 
AnswerRe: Suggestion for Improvement Pin
Tiggerito29-Sep-08 6:10
professionalTiggerito29-Sep-08 6:10 
GeneralRe: Suggestion for Improvement Pin
Andrew Rissing29-Sep-08 7:00
Andrew Rissing29-Sep-08 7:00 
GeneralYou saved me. Pin
gwestwater25-Aug-08 12:00
gwestwater25-Aug-08 12:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.