<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>We Heart Code &#187; code generation</title>
	<atom:link href="http://www.weheartcode.com/category/code-generation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Sun, 17 Jan 2010 02:16:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automatically Add JavaDoc Comments in Eclipse</title>
		<link>http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/</link>
		<comments>http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 13:05:44 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[code generation]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/</guid>
		<description><![CDATA[Recently while preparing to contribute some code to Eclipse I had to add a copyright comment to the top of every class. Enter JAutodoc it will go through and add Javadoc comments and other automated comments, the update site is:
http://jautodoc.sourceforge.net/update/
Screenshot:

Bookmark to:
]]></description>
			<content:encoded><![CDATA[<p>Recently while preparing to contribute some code to <a href="http://www.eclipse.org">Eclipse</a> I had to add a copyright comment to the top of every class. Enter <a href="http://jautodoc.sourceforge.net/">JAutodoc</a> it will go through and add Javadoc comments and other automated comments, the update site is:<br />
http://jautodoc.sourceforge.net/update/</p>
<p>Screenshot:<br />
<img src="http://jautodoc.sourceforge.net/images/preferences.gif" alt="JAutodoc Preferences" /></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Del.icio.us" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to digg" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Automatically+Add+JavaDoc+Comments+in+Eclipse&amp;u=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to FURL" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to reddit" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Technorati" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Google Bookmarks" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy XML Generation with Ruby and ERB</title>
		<link>http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/</link>
		<comments>http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 15:42:25 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[code generation]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/</guid>
		<description><![CDATA[Ah, the bracket tax. XML-Situps, whatever you want to call them, if you write any code (especially if you use java) it should be your sworn enemy. The other day I had to create about 40 XML files for some Java Webstart configs. Instead of doing them by hand, I decided to check out the [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, the bracket tax. XML-Situps, whatever you want to call them, if you write any code (especially if you use java) it should be your sworn enemy. The other day I had to create about 40 XML files for some Java Webstart configs. Instead of doing them by hand, I decided to check out the ERB class for easy "templating" that's built into Ruby. It makes doing code generation very easy!</p>
<h3>Our Task</h3>
<p>Create a custom XML file for each .jar file in a directory.</p>
<p>...more after the jump!<br />
<span id="more-31"></span></p>
<h3>Strategy</h3>
<p>With any code generation project we need to identify a few things.</p>
<ol>
<li>Static template: The code/text that will stay the same.</li>
<li>Dynamic variables: These are the pieces that will change in our template.</li>
<li>Input source: The input for our dynamic variables.</li>
</ol>
<p>For me, the static template is a JNLP XML file that looks like this:</p>
<div class="syntax_hilite">
<div id="xml-8">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jnlp</span><br />
&nbsp; <span style="color: #000066;">spec</span>=<span style="color: #ff0000;">"1.0+"</span><br />
&nbsp; <span style="color: #000066;">codebase</span>=<span style="color: #ff0000;">"http://redacted.com/libs/com.redacted.dao_1.3"</span><br />
&nbsp; <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"RcpWebStart.jnlp"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;information<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;title<span style="font-weight: bold; color: black;">&gt;</span></span></span>RedactedLibs<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/title<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;vendor<span style="font-weight: bold; color: black;">&gt;</span></span></span>Redacted<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/vendor<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;strong<span style="font-weight: bold; color: black;">&gt;</span></span></span>com.redacted.dao_1.3.1.jar<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/strong<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/information<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;security<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;all</span>-permissions<span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/security<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;resources<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;j2se</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.4+"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jar</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"com.redacted.dao_1.3.1.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/resources<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;component</span>-desc<span style="font-weight: bold; color: black;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/jnlp<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>The dynamic bits will be anywhere a jar is referenced as well as the directory for the jar.</p>
<h3>The Template</h3>
<p>The template defines our static text, and the variables and/or ruby code that will be substituted into the text by ERB.</p>
<p>We use
<div class="syntax_hilite">
<div id="code-9">
<div class="code">&lt;%= %&gt;</div>
</div>
</div>
<p> blocks to substitute in variables from our ruby script.</p>
<p>Here's what my template looks like, I have four variables that I want to substitute in.</p>
<ul>
<li><strong>basedir</strong>: the base directory of where I'm storing my jar.</li>
<li><strong>shortname</strong>: the name of the jar sans the version.</li>
<li><strong>jarname</strong>: the full name of the jar file.</li>
</ul>
<div class="syntax_hilite">
<div id="xml-10">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jnlp</span><br />
&nbsp; <span style="color: #000066;">spec</span>=<span style="color: #ff0000;">"1.0+"</span><br />
&nbsp; <span style="color: #000066;">codebase</span>=<span style="color: #ff0000;">"http://redacted.net/libs/&lt;%=basedir%&gt;</span>/<span style="color: #009900;">&lt;%=shortname%&gt;</span>_<span style="color: #009900;">&lt;%=version%&gt;</span>&quot;<br />
&nbsp; href=&quot;RcpWebStart.jnlp&quot;&gt;<br />
&nbsp; <span style="color: #009900;">&lt;information&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;title&gt;</span><span style="color: #009900;">&lt;%=basedir%&gt;</span><span style="color: #009900;">&lt;/title&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;vendor&gt;</span>Redacted<span style="color: #009900;">&lt;/vendor&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;description&gt;</span><span style="color: #009900;">&lt;%=jarname%&gt;</span><span style="color: #009900;">&lt;/description&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;/information&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;security&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;all-permissions/&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;/security&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;resources&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;j2se version="</span><span style="color: #cc66cc;">1</span>.<span style="color: #cc66cc;">4</span>+<span style="color: #ff0000;">"/&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;jar href="</span><span style="font-weight: bold; color: black;">&lt;</span>%=jarname%<span style="font-weight: bold; color: black;">&gt;</span></span>&quot;/&gt;<br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/resources<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;component</span>-desc<span style="font-weight: bold; color: black;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/jnlp<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<h3>The Code</h3>
<p>Let's examine some code. The first thing I do is create the template string, i Use the %q{ shortcut to avoid having to worry about quotes.</p>
<p>Next, I need to define my template, that's what this line does.</p>
<div class="syntax_hilite">
<div id="ruby-11">
<div class="ruby">jnlp = ERB.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>template, <span style="color:#006666;">0</span>, <span style="color:#996600;">"%&lt;&gt;"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>It says, create a new ERB object using the string 'template' as the template and the "%&lt;&gt;" as my template escape operators.</p>
<p>Our next step is to loop through all the jar's in a directory and create a new file for each one, here's the juicy bits to accomplish that.</p>
<div class="syntax_hilite">
<div id="ruby-12">
<div class="ruby">b = <span style="color:#CC0066; font-weight:bold;">binding</span><br />
&nbsp; &nbsp; &nbsp;jarname = File.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;basedir = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;shortname = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">1</span>'<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;version = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d\.\d<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">2</span>'<span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp;dirname = shortname + <span style="color:#996600;">"_"</span> + version<br />
&nbsp; &nbsp; &nbsp;ourfile = jnlp.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p></p>
<p>Notice that we create a binding variable called b and then pass that to our ERB object (jnlp), that binding variable tells ERB to use template variables in the same scope as it. It's some ruby black magic that makes our lives easier.<br />
The rest of the script actually creates some directory and writes the created file to a directory based on the name of the jar.</p>
<div class="syntax_hilite">
<div id="ruby-13">
<div class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">"erb"</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">"find"</span></p>
<p>template = %q<span style="color:#006600; font-weight:bold;">&#123;</span><br />
&lt;?xml version=<span style="color:#996600;">"1.0"</span> encoding=<span style="color:#996600;">"UTF-8"</span>?&gt;<br />
&lt;jnlp<br />
&nbsp; spec=<span style="color:#996600;">"1.0+"</span><br />
&nbsp; codebase=<span style="color:#996600;">"http://redacted.net/libs/&lt;%=basedir%&gt;/&lt;%=shortname%&gt;_&lt;%=version%&gt;"</span><br />
&nbsp; href=<span style="color:#996600;">"RcpWebStart.jnlp"</span>&gt;<br />
&nbsp; &lt;information&gt;<br />
&nbsp; &nbsp; &lt;title&gt;&lt;%=basedir%&gt;&lt;/title&gt;<br />
&nbsp; &nbsp; &lt;vendor&gt;Redactedl&lt;/vendor&gt;<br />
&nbsp; &nbsp; &lt;description&gt;&lt;%=jarname%&gt;&lt;/description&gt;<br />
&nbsp; &lt;/information&gt;<br />
&nbsp; &lt;security&gt;<br />
&nbsp; &nbsp; &lt;all-permissions/&gt;<br />
&nbsp; &lt;/security&gt;<br />
&nbsp; &lt;resources&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;j2se version=<span style="color:#996600;">"1.4+"</span>/&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;jar href=<span style="color:#996600;">"&lt;%=jarname%&gt;"</span>/&gt;<br />
&nbsp; &lt;/resources&gt;<br />
&nbsp; &lt;component-desc/&gt;<br />
&lt;/jnlp&gt;<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; jnlp = ERB.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>template, <span style="color:#006666;">0</span>, <span style="color:#996600;">"%&lt;&gt;"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; <span style="color:#008000; font-style:italic;">#Search for jar files here</span><br />
&nbsp; Find.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |path|</p>
<p>&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">if</span> !FileTest.<span style="color:#9900CC;">directory</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;b = <span style="color:#CC0066; font-weight:bold;">binding</span><br />
&nbsp; &nbsp; &nbsp;jarname = File.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;basedir = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;shortname = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">1</span>'<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;version = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d\.\d<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">2</span>'<span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp;dirname = shortname + <span style="color:#996600;">"_"</span> + version</p>
<p>&nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#pass in the binding variable and create a string from the template</span><br />
&nbsp; &nbsp; &nbsp;ourfile = jnlp.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#create a new directory and file, and output the template string to it.</span><br />
&nbsp; &nbsp; &nbsp;Dir.<span style="color:#9900CC;">mkdir</span><span style="color:#006600; font-weight:bold;">&#40;</span>basedir + <span style="color:#996600;">"/"</span> + dirname<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;outfile = File.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>basedir + <span style="color:#996600;">"/"</span> + dirname +&nbsp; <span style="color:#996600;">"/RcpWebStart.jnlp"</span>,<span style="color:#996600;">"w+"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;outfile &lt;&lt;ourfile<br />
&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
</div>
<p></p>
<h3>The fun doesn't stop there</h3>
<p>This is an elementary example, but ERB also allows you to embed real ruby code in the template, this allows you to accomplish looping. </p>
<p>So we could pass in an array of person objects and loop through them with a template like this:</p>
<div class="syntax_hilite">
<div id="xml-14">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;people<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%@people.each do |person| %<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%=person.name%<span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%end%<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/people<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Easy XML Generation with Ruby and ERB' to Del.icio.us" alt="Add 'Easy XML Generation with Ruby and ERB' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Easy XML Generation with Ruby and ERB' to digg" alt="Add 'Easy XML Generation with Ruby and ERB' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Easy+XML+Generation+with+Ruby+and+ERB&amp;u=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/" title="Add 'Easy XML Generation with Ruby and ERB' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Easy XML Generation with Ruby and ERB' to FURL" alt="Add 'Easy XML Generation with Ruby and ERB' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Easy XML Generation with Ruby and ERB' to reddit" alt="Add 'Easy XML Generation with Ruby and ERB' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/" title="Add 'Easy XML Generation with Ruby and ERB' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Easy XML Generation with Ruby and ERB' to Technorati" alt="Add 'Easy XML Generation with Ruby and ERB' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Easy XML Generation with Ruby and ERB' to Google Bookmarks" alt="Add 'Easy XML Generation with Ruby and ERB' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
