OWASP O2 Platform Blog

O2 Script: Create XSD and Assembly from XML file

This script sequence shows how I wrote a function that autocreates an XSD and DLL from an original XSD using O2’s ‘Quick Development GUI’.

The original file used was taken from the TeamMentor OWASP editon (which you can get from Git-Hub)

step 1: define folders and check if file exists

var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
return mainFile.fileExists();

step 2: create XSD file from XML file

panel.clear();
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd =  mainFile.xmlCreateXSD();
return xsd;

step 3: save XSD file in target folder

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
return mainFile.xmlCreateXSD().saveAs(xsd_MainFile);

step 4: create CSharp file from XSD, added NUnit references so that we can use Asserts to make sure all it happening as expected

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

return xsd_MainFile.xsdCreateCSharpFile();
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 5: view created CSharp file in a Source Code Editor (using the ‘panel’ reference that is given to us from the ‘Quick Development GUI’)

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

var cs_MainFile = xsd_MainFile.xsdCreateCSharpFile();
panel.add_SourceCodeEditor().open(cs_MainFile);
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 6: inserting dynamic compilation reference “//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll” inside the cs_MainFile (so that it can be compiled)

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

var cs_MainFile = xsd_MainFile.xsdCreateCSharpFile()
       .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
panel.add_SourceCodeEditor().open(cs_MainFile);
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 7: Compile CSharp file into an .NET Dll

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

var cs_MainFile = xsd_MainFile.xsdCreateCSharpFile()
         .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
panel.add_SourceCodeEditor().open(cs_MainFile);
return cs_MainFile.compile().Location;
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 8: create LAMDA function (createAssemblyFromXml) which is now generic and can be used for any XML file

panel.clear();Func<string, string, string,Assembly> createAssemblyFromXml =
 (xmlFile, targetFolder, xsdAndDll_Name )=>{

  var xsd = targetFolder.pathCombine(xsdAndDll_Name + ".xsd");
  xmlFile.xmlCreateXSD().saveAs(xsd);
  Assert.That(xsd.fileExists(), "xsd was not created");
  
  var cs = xsd.xsdCreateCSharpFile()
     .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
  
  Assert.That(cs.fileExists(), "cs was not created");    
  var dll = targetFolder.pathCombine(xsdAndDll_Name + ".dll");
  cs.compile(dll);
  Assert.That(dll.fileExists(), "dll_MainFile was not created");
  var assembly = dll.assembly();
  Assert.That(assembly.notNull(), "assembly was null");
  return assembly;
 };

var workFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";

var mainFile = rootFolder.pathCombine("OWASP.xml");

return createAssemblyFromXml(mainFile, workFolder, "__TEMP__");

return "ok";
//using System.Reflection
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 9: Added a nunber of Asserts to createAssemblyFromXml Function

panel.clear();Func<string, string, string,string> createAssemblyFromXml =
 (xmlFile, targetFolder, xsdAndDll_Name )=>{

  var xsd = targetFolder.pathCombine(xsdAndDll_Name + ".xsd");
  xmlFile.xmlCreateXSD().saveAs(xsd);
  Assert.That(xsd.fileExists(), "xsd was not created");
  "[createAssemblyFromXml] XSD Created: {0}".debug(xsd);
  var cs = xsd.xsdCreateCSharpFile()
     .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
  
  Assert.That(cs.fileExists(), "cs was not created");  
  "[createAssemblyFromXml] CSharp file Created: {0}".debug(cs);
  
  var dll = targetFolder.pathCombine(xsdAndDll_Name + ".dll");
  var tempDll = cs.compile(dll);
  
  Assert.That(dll.fileExists(), "dll_MainFile was not created");
  "[createAssemblyFromXml] dll file Created: {0}".debug(dll);
  
  var assembly = dll.assembly();
  Assert.That(assembly.notNull(), "assembly was null");  
  return dll;
 };

var workFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";

var mainFile = rootFolder.pathCombine("OWASP.xml");

return createAssemblyFromXml(mainFile, workFolder, "__TEMP__");

return "ok";
//using System.Reflection
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

February 20, 2011 - Posted by | .NET

2 Comments »

  1. […] For example, this is how to do this for the script developed here: https://o2platform.wordpress.com/2011/02/20/o2-script-create-xsd-and-assembly-from-xml-file/ […]

    Pingback by Consuming extension method from external file « O2Platform.com for Developers | February 20, 2011 | Reply

  2. […] creating a Lamba method and putting it on a CSharp file the usual next step is to create an API that can wrap the […]

    Pingback by Creating an API to be consumed by an O2 Script « O2Platform.com for Developers | February 20, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: