Model-Glue Entities

This morning Ray asked a question on the model-glue list about reusing portions of the XML file throughout the document. Basically, since he was typing "cflib2005" a lot, he wanted to replace all of those instances with "@applicationMapping."

I posted a solution using entity parameters, which does exactly what Ray needed. One of the really great things about XML is that it's infinitely extensible, and entities are just one such way in which to do this. Without getting too far into XML generation, you can override anything in a DTD/Schema with your own preferences by including local and external entities, notations, attlists, datatypes, etc. in the DOCTYPE declaration of an XML file.

As an example, I'll walk through using entity references in the stockquote example from the model-glue download.

In the case of model-glue, there's not an "official" URI for the DTD/Schema, so creating the DOCTYPE declaration in the modelglue.xml file consists of

<!DOCTYPE modelglue>

This simply declares that the following XML document had a root element of . Next, we need to add a local entity declaration for our application name, folder name, and server mapping.

<!DOCTYPE modelglue [
<!ENTITY appName "StockQuote">

<!ENTITY mapping "/modelgluesamples">
<!ENTITY folder "stockquote">
]>

To reference this code, we do a little refactoring of the rest of the document. In the controller section, we now have

<config>
   <setting name="defaultEvent" value="&appName;" />
   <setting name="applicationMapping" value="&mapping;/&folder;" />
   <setting name="beanMappings" value="&mapping;/&folder;/config/beans/" />
   <setting name="viewMappings" value="&mapping;/&folder;/views" />
   <setting name="reload" value="false" />
   <setting name="reloadKey" value="init" />
   <setting name="reloadPassword" value="true" />
   <setting name="statePrecedence" value="Form" />
   <setting name="eventValue" value="event" />
   <setting name="modelGlueMapping" value="/ModelGlue" />
   <setting name="defaultExceptionHandler" value="Exception" />
   <setting name="debug" value="true" />
   <setting name="defaultCacheTimeout" value="5" />
</config>

Basically, these entities create variables that can be accessed throughout your XML file. However, there may be instances where you want to share this type of data among applications and use an external entity to handle some of the code.

As an example, we create a new folder under the /modelgluesamples directory called /templates. In templates, we create a folder named /stockquote.

/modelgluesamples
   +
   +--/stockquote
   +--/templates
      +
      +-- stockquote

We'll create three XML files, create config.xml in /modelgluesamples/templates, and controller.xml and event-handler.xml in /modelgluesamples/templates/stockquote.

In config.xml, paste this code:

<?xml version="1.0" encoding="UTF-8"?>
<event-handlers>
   <event-handler name="Home">
      <results>
         <result do="Layout" />
      </results>
   </event-handler>
   
   <event-handler name="&hello;">
      <broadcasts>
         <message name="DoHelloWorld" />
      </broadcasts>
      <views>
         &lt;include name="content" template="dsp.helloworld.cfm">
            <value name="greeting" value="I am the default greeting." />
         </include>
      </views>
      <results>
         <result do="Layout" />
      </results>
   </event-handler>
   
   <event-handler name="&appName;">
      <broadcasts>
         <message name="DoStockQuote">
            <argument name="DefaultSymbol" value="MACR" />
         </message>
      </broadcasts>
      <views>
         &lt;include name="content" template="form.&folder;.cfm" />
      </views>
      <results>
         <result name="BadSymbol" do="BadStockSymbol" />
         <result do="Layout" />
      </results>
   </event-handler>
   
   <event-handler name="BadStockSymbol" access="private">
      <views>
         &lt;include name="content" template="dsp.badStocksymbol.cfm" />
      </views>
   </event-handler>
   
   <event-handler name="Layout" access="private">
      <views>
         &lt;include name="main" template="layout.main.cfm" />
      </views>
   </event-handler>
   
   <event-handler name="Exception">
      <views>
         &lt;include name="body" template="exception.cfm" />
      </views>
   </event-handler>
</event-handlers>

As you see, we've broken up the main application configuration file up into three separate files that are then included through their entity names.

I am not advocating any types of changes to model-glue, just a couple of examples of different approaches that are available.

You can see a live example at http://swem.wm.edu/modelgluesamples/stockquote/ and download the source at http://swem.wm.edu/downloads/mg-entityExample.zip.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Heinz's Gravatar what is wron
<style type="text/css">
a:link { text-decoration:none; font:Verdana; color:#4F4F4F; }
a:visited { text-decoration:none; font:Verdana; color:#4F4F4F; }
a:hover { text-decoration:none; font:Verdana; background-color:#8F8F8F; }
a:active { text-decoration:none; font:Verdana; background-color:#8F8F8F; }
a:focus { text-decoration:none; font:Verdana; background-color:#4F4F4F; }
</style>
# Posted By Heinz | 4/6/07 12:42 AM
Thomas's Gravatar It's seems like the Mirror is down because i don't can download it!
# Posted By Thomas | 4/12/07 3:17 AM
Geld's Gravatar Yes, it seems to be down. Must look at an other place.
# Posted By Geld | 4/18/07 2:12 PM
Vertalen via Vertaal Woordenboek's Gravatar Anyone pls a mirror? Or a saved local copy?
# Posted By Vertalen via Vertaal Woordenboek | 6/22/07 8:27 AM
Wayne Graham's Gravatar Sorry about that...we moved servers and it looks like I didn't grab that particular file since it's so old...

With ColdSpring in MG, you really don't have to do much of this since 1) you can autogenerate most of this stuff with the ANT scripts, and 2) ColdSpring pretty does a pretty good job of not repeating information. Since you really are only handling events and messages in the ModelGlue.xml file, it's really not necessary to do this too much (if at at) there either.

If you still really want to see this, let me know and I can remake the file. However, I would definitely check out the latest from Model-Glue at http://svn.model-glue.com/trunk/
# Posted By Wayne Graham | 6/22/07 5:20 PM
abu's Gravatar hi to all
I have some problem in xml entities used in php project....some xml unicode entities are not supported in linux os...can any one tell me how to access in linux os...pls....mail me to &quot;enum.abu@gmail.com&quot;.....
# Posted By abu | 6/20/08 12:55 AM
Wayne Graham's Gravatar Abu,

Which version of PHP are you using? I suspect its the culprit and not Linux (or XML)...
# Posted By Wayne Graham | 6/20/08 8:43 AM