XML for application planning???

I am in the process of converting a Fusebox 5.1 application to an MVC implementation.

This is a relatively complex task, as I am also wanting to split the application into 2 - in order to allow shared functionality to be implemented once, and create another instance of the application with a different controller, and some unique functionality.

I was a little stumped at first to know how to visualise the folder structure for the application - I started laying it out within Visio - but quickly realised that this was not sustainable - Visio is great for visualising many aspects of an application design - but its not ideal for representing a large tree structure.

Then I thought - what about using XML - XML is great for representing hierarchical structures - and using the design view within the Eclipse WTP XML editor, its very quick to add, edit, move, and duplicate elements.

So fairly quickly I created a folder structure I was happy with - about 50 odd folders. I was about to create the folders, then I thought - hang on, this is in XML, it shouldn't be too hard to create a script to do this for me.

Coldfusion Code:

<cffunction name="writeTree" output="false" hint="I recursively loop through an XML object, and create a folder for each element contained therin if not already there.">
   <cfargument name="node">
   <cfargument name="froot" type="string">
   <cfset var thispath = "">
   <cfset var nonExistentFolders = "">
   <cfset var tempOutput = "">
   <cfsavecontent variable="nonExistentFolders">
      <cfloop collection="#arguments.node#" item="current_node">
         <cfif current_node neq "XmlText">
            <cfset thispath = "#arguments.froot#\#current_node#">
            <cfif not directoryexists(thispath)>
               <cfdirectory action="create" directory="#thispath#">
               <cfoutput>#thispath#</cfoutput><br/>
            </cfif>
            <cfif not structisempty(arguments.node[current_node])>
               <cfset tempOutput = writeTree(arguments.node[current_node],thispath)>
               <cfoutput>#tempOutput#</cfoutput>
            </cfif>
         <cfelse>
            <cfset structdelete(arguments.node,current_node)>
         </cfif>
      </cfloop>
   </cfsavecontent>
   <cfreturn trim(nonExistentFolders)>
</cffunction>

<cffunction name="dirToXml" output="true" hint="I recursively loop through a folder, and generate a heierarchical XML document with an element representing each folder">
   <cfargument name="froot" type="string">
   <cfargument name="currentfolder" type="string">
   <cfargument name="additionalFolders" type="struct">
   <cfset var s_XML = "">
   <cfset var temp_XML = "">
   
   <cfdirectory directory="#arguments.froot#\#arguments.currentfolder#" action="list" name="dir">
   <cfsavecontent variable="s_XML">
      <#arguments.currentfolder#>
      <cfloop query="dir">
         <cfif type eq "Dir">
            <cfset temp_XML = DirToXml("#arguments.froot#\#arguments.currentfolder#",Name)>
            <cfoutput>#temp_XML#</cfoutput>
         </cfif>
      </cfloop>
      </#arguments.currentfolder#>
   </cfsavecontent>
   <cfreturn s_XML>
</cffunction>

<cfscript>
   request.froot = "c:\sites\cf7";
   request.app = "APP-Folder";
   xmlobj = xmlparse("#request.froot#\#request.app#\Map.xml");
   st_additionalFolders = structnew();
   nonExistentFolders = writeTree(xmlobj,request.froot);
   
   dir_xml = dirToXml(request.froot,request.app);
   dir_XMLobj = xmlparse(dir_xml);
   dir_xml = tostring(dir_XMLobj);
</cfscript>

<cffile action="write" file="#request.froot#\#request.app#\Map_Generated.xml" output="#dir_xml#">
<cfif len(nonExistentFolders)>
<h2>Folders Created</h2>
<cfoutput>#nonExistentFolders#</cfoutput>
</cfif>

Sample XML Code

<?xml version="1.0" encoding="utf-8"?>
<AppFramework>
   <App1>
      <layouts />
      <mvc>
         <controller />
         <model />
         <view />
      </mvc>
      <objects>
         <mailgeneration />
      </objects>
      <web>
         <admin>
            <css />
            <downloads />
            <images />
            <scripts />
         </admin>
         <super_admin>
            <!--This is identical to existing super_admin-->
         </super_admin>
         <filestore />
         <templates />
      </web>
   </App1>
   <App2>
      <!--Contents identical to App1-->
      <layouts />
      <mvc>
         <controller />
         <model />
         <view />
      </mvc>
      <objects>
         <mailgeneration />
      </objects>
      <web>
         <admin>
            <css />
            <downloads />
            <images />
            <scripts />
         </admin>
         <super_admin>
            <!--This is identical to existing super_admin-->
         </super_admin>
         <filestore />
         <templates />
      </web>
   </App2>
   <Shared>
      <client>
         <!--virtual directory within web/admin and web/super_admin for each site-->
         <texteditor />
         <yuicolorpicker />
      </client>
      <custom />
      
      <lexicon />
      <mvc>
         <model />
         <view />
      </mvc>
      <objects>
         <mailgeneration />
      </objects>
   </Shared>
</AppFramework>

This is fairly dirty, code - fair amount of hard coded stuff in there - P.O.C. only ;)

I've created a function to loop through an XML object, and given a base folder to work from, it will create folders hierarchically within it to match the elements within the XML file (providing they do not exist already). It will also return an HTML string to report on what folders it has created.

I've created another function to generate XML to show the current folders within the Application folder. I can then use an XMLDiff tool to show me the difference between my original XML file, and the current state of the folders.

Definitely needs cleaning up - there are several limitations there at the moment - restrictions on folder names etc. and the fact the original XML file needs to be in alphabetical order if it is to be compared to the generated XML file.

I am definately warming to the idea of using the original XML file as part of the architectural documentation - especially adding comments etc. to give more information as to the responsibilities of each area...

I also thought that with the use of XML attributes, I could extend the generation tool to actually create files within some of the folders - for instance circuit.xml files - with correct visibilities set for the circuits, and all the circuits created in the fusebox.xml file...

Something to thing about :)

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.5.1, hosted by TalkWebSolutions.