<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
 <channel>
	<title>The title of my RSS 2.0 Feed</title>
	<link>http://richard.jp.leguen.ca/</link>
	<description>Richard Jean-Paul Le Guen is a software developer and a recent graduate of Concordia University's Software Engineering program.</description>
	<lastBuildDate>Tue, 30 Nov 1999 00:00:00 GMT</lastBuildDate>
	<language>en-us</language>
	<item>
		<title>Parse and Validate Markup Server Side - Why Not?</title>
		<link>http://richard.jp.leguen.ca/not-blog/parse-and-validate-markup-server-side</link>
		<guid>http://richard.jp.leguen.ca/not-blog/parse-and-validate-markup-server-side</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
I had a fun experience recently at work where I spent the better part of a day trying to figure out what was wrong with an <code>aspx</code> page I was deploying to a SharePoint pages library. My investigation into it has led me to reflect a little on XHTML, and forgiveness/fault-tolerance on the Web.</p>
<p>
Can you spot the mistake I made? Here&#8217;s a variation of the Page Layout where I made the error:</p>
<pre class=code><code>&lt;%@ Page language=&#8220;C#&#8221;
         Inherits=&#8220;Microsoft.SharePoint.Publishing.PublishingLayoutPage, Microsoft.SharePoint.Publishing, Version=12.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c&#8221; %&gt;
&lt;%@ Register Tagprefix=&#8220;RLG&#8221; Namespace=&#8220;Richard.JP.LeGuen&#8221; Assembly=&#8220;Richard.LeGuen, Version=8.0.0.0, Culture=neutral, PublicKeyToken=XXX&#8221; %&gt;
&lt;asp:Content ContentPlaceholderID=&#8220;PlaceHolderMain&#8221; runat=&#8220;server&#8221;&gt;
	&lt;RLG:MyCustomControl /&gt;	
&lt;/asp:Content&gt;</code></pre>
<p>
I was debugging, attached to the <span class="caps">IIS</span> worker process, and no matter where I put a breakpoint in my <code>MyCustomControl</code> class, <em>it wasn&#8217;t loading</em>. Somehow my custom control&#8217;s code was not being executed; not even a constructor.</p>
<p>
For anyone who&#8217;s found the answer, I&#8217;m ashamed to say I needed help to see it and it took the better part of my day.</p>
<p>
For anyone who hasn&#8217;t found it, I&#8217;m missing a <code><a href="http://msdn.microsoft.com/en-us/library/ms529174%28VS.85%29.aspx" >runat=&#8216;server&#8217;</a></code> attribute on my the custom control. Usually you get a compile-time error when you try to write up an aspx and neglect a <code>runat=&#8216;server&#8217;</code>, but deploying to SharePoint meant I wasn&#8217;t the one doing the compiling.</p>
<p>
My first reaction was to wonder &#8220;Why? Why is this attribute required for web controls? Why isn&#8217;t it &#8216;sous-entendu&#8217;?&#8221; which led me to a handful of <a href="http://stackoverflow.com/questions/304290/asp-net-why-runatserver" >useful answers on Stack Overflow</a>.</p>
<p>
Among the answers are that Microsoft was holding out for the possibility of a <code>runat=&#8220;client&#8221;</code>, <a href="http://www.theregister.co.uk/2008/11/27/microsoft_ignored_ajax/" >with in-browser (IE-only?) controls</a>. This didn&#8217;t come together.</p>
<p>
Another answer cited a fellow name <a href="http://mikeschinkel.com/" >Mike Schinkel</a> talking with another fellow named <a href="http://talbottc.spaces.live.com/" >Talbott Crowell</a> about why <code>runat=&#8220;server&#8221;</code> is needed.</p>
<blockquote><p>
 If [<code>runat=client</code>] was required for all client-side tags, the parser would need to parse all tags and strip out the [<code>runat=client</code>] part. Currently, if my guess is correct, the parser simply ignores all text (tags or no tags) unless it is a tag with the [<code>runat=server</code>] attribute or a &quot;<code>&lt;%</code>&quot; prefix or ssi &quot;<code>&lt;!-- #include</code>&#8230;</p></blockquote>
<p>
This is strange, this is confusing, and this is really too bad. <em>The World Wide Web would been a much better/more professional place if Microsoft had decided to parse all tags.</em> Why? Because <em>not</em> parsing all tags allowed developers to write pages like this:</p>
<pre class=code><code>&lt;%@ Page language=&#8220;C#&#8221; %&gt;&lt;!DOCTYPE <span class="caps">HTML</span> <span class="caps">PUBLIC</span> &#8220;-//W3C//DTD <span class="caps">HTML</span> 4.01 Transitional//EN&#8221;
	&#8220;http://www.w3.org/TR/html4/loose.dtd&#8221;&gt;
&lt;html&gt;
 &lt;head&gt;
	&lt;meta http-equiv=&#8220;Content-Type&#8221; content=&#8220;text/html; charset=iso-8859-1&#8221;&gt;
	&lt;title&gt;Something wrong AspX&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;form runat=&#8220;server&#8221;&gt;
	&lt;p&gt;
		What&#8217;s wrong with this page? &lt;br&gt;
		&lt;asp:label id=&#8220;message&#8221; runat=&#8220;server&#8221; Text=&#8220;There&#8217;s something bad about this control&#8221; /&gt;
  &lt;/form&gt;
 &lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>
I don&#8217;t like what I see here, because <em>this document is a mix of <span class="caps">XML</span> syntax and <span class="caps">HTML</span> syntax</em>, which is both a <a href="http://hixie.ch/advocacy/xhtml" >bad idea</a> and the mark of an amateur. The web control has to have a closing <code>/&gt;</code> while the document is <span class="caps">HTML</span> 4, so other empty tags don&#8217;t have to close. Even some non-empty tags &#8211; like the <code>&lt;p&gt;</code> &#8211; don&#8217;t need to close. You might think this is not important, but I disagree; this is encouraging and perpetuating bad habits on the part of web developers. What benefit comes of allowing lax syntax and format like this? Isn&#8217;t that the kind of thing a .NET developer would criticize in some script-kiddie language like Perl?</p>
<p>
This, as well as <a href="http://blogs.msdn.com/b/ie/archive/2005/09/15/467901.aspx" >IE&#8217;s continued lack of support for documents served as XHTML</a>, is a strange irregularity for the all too often XML-obsessed Microsoft. It&#8217;s too bad for the web at large though, because a little stricter behavior server-side could have mitigated the problem of <a href="http://www.codinghorror.com/blog/2007/04/javascript-and-html-forgiveness-by-default.html" >Forgiveness by Default</a> &#8211; at least in the context of markup &#8211; and that would have been the sort of development which could have made <span class="caps">XHTML</span> a success. It might have even helped &quot;<a href="http://www.w3.org/QA/2007/06/fixing_the_web_together.html" >Fix the Web</a>&quot;.</p>
<p>
Most web developers at this point think (often out loud) &quot;Who cares? <span class="caps">XHTML</span> was lame; I hated <a href="http://en.wikipedia.org/wiki/Yellow_Screen_of_Death#Other_screens_of_death" >the yellow screen of death</a>&quot; but there is no doubt in my mind that <span class="caps">XHTML</span> should have succeeded more than it did. <a href="http://www.elharo.com/" >Eliotte Rusty Harold</a> provides <a href="http://cafe.elharo.com/web/refactoring-html/why-xhtml/" >some reasons</a> why we should use it in his book <a href="http://www.amazon.com/exec/obidos/ASIN/0321503635" >Refactoring HTML</a> which I agree with, but I like to spin the message along the lines of my favorite theme that the Web is not the same place it was in the 90s.</p>
<p>
<span class="caps">HTML</span> was a great tool for the Web in the 90s; the Web was relatively new and was still being defined, but back then the publishing of information was more important than the consumption of information. People and businesses were a lot more focused on getting their content onto the Internet than on sifting through the content of the Internet.</p>
<p>
Now times have changed. There is an excess of information, so much so that we&#8217;re apparently at the end of the Information Age and in what some call the Attention Age; the focus isn&#8217;t on publishing but on the consumption of information. Web-surfers subscribe to (consume) <span class="caps">RSS</span> feeds and follow (consume) Twitter Feeds and receive (consume) <span class="caps">SMS</span> reminders from their Google Calendars.</p>
<p>
<span class="caps">XML</span> was specifically built to cater to the need to consume structured data; the <a href="&quot;http://www.w3.org/TR/2004/REC-xml11-20040204/&quot;" >w3c Recommendation</a> on <span class="caps">XML</span> even states that one of its design goals is that &#8220;It shall be easy to write programs which process <span class="caps">XML</span> documents.&#8221; Any developer with a basic understanding of string manipulation and recursion can write a basic <span class="caps">XML</span> parser in a fairly short amount of time. With the Attention Age focusing on consuming data so much more than publishing it, I will always feel that <span class="caps">XHTML</span> should have been the markup of the future.</p>
<p>
Even with HTML5 on the horizon, I find myself tempted to be devoted to XHTML. Soon <a href="http://blogs.msdn.com/b/ie/archive/2010/03/16/html5-hardware-accelerated-first-ie9-platform-preview-available-for-developers.aspx" >Microsoft will support it in IE9</a> and once that hurdle is jumped, if Web developers started ensuring well-formedness before serving content I think we&#8217;d all benefit.</p>
<p>
I know it won&#8217;t happen &#8211; HTML5 is going to take over &#8211; but would it really be so hard to do? I mean, we already take precautions to sanitize our output against (among other things) <a href="http://en.wikipedia.org/wiki/Cross-site_scripting" >XSS attacks</a>&#8230; why not also sanitize output to ensure well-formedness?</p>
<p class=inlineFootnote>
 Actually, as it happens, my concerns are foolish as HTML5 will be compatible with <span class="caps">XML</span> formatting: see <a href="http://www.w3.org/QA/2008/01/html5-is-html-and-xml.html" >HTML 5, one vocabulary, two serializations</a>.</p>
<p>
As a little demo; visit my <a href="/code/xhtml-cleaner" >XHTML Sanitizer</a> and submit some XHTML; it will be rendered in the page, but sanitize to remove any malicious markup (JavaScript, etc) <strong>and</strong> any ill-formed markup (unclosed tags, etc). Sure, the latter functionality can&#8217;t be <em>perfect</em> in the end &#8211; sometimes content has to disappear to ensure well-formedness &#8211; but missing content sure sure beats this:</p>
<p style="text-align:center">
 <img src="/images/YellowScreenOfDeath.png" style=width:448px;height:271px;margin:auto  alt="Parse-error" /></p>
<p>
It should be noted that writing a server-side formatter like this would be harder to do for HTML, as one cannot rely on simple well-formedness, and has to do some validation &#8211; which requires distinct rules and logic for different tags.</p>]]></description>
	</item>
	<item>
		<title>Concurrency and Scalability on the WWW</title>
		<link>http://richard.jp.leguen.ca/not-blog/concurrency-and-scalability</link>
		<guid>http://richard.jp.leguen.ca/not-blog/concurrency-and-scalability</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
People like to make a lot of noise about the scalability of applications written in <span class="caps">PHP</span> or Ruby or other scripting languages. One of the major criticisms I&#8217;ve heard of <span class="caps">PHP</span> is that of how to deal with concurrency.</p>
<p>
Take the following <span class="caps">PHP</span> script as an example:</p>
<pre class=code><code>require_once(&#8220;DB.php&#8221;);
	
$dsn = &#8220;mysql://username:password@host/db&#8221;;
$conn = DB::connect ($dsn);
if (DB::isError ($conn)) {
        die (&#8220;Cannot connect: &quot; . $conn-&gt;getMessage () . &quot;\n&#8221;);
}
	
// read our row out of the database
$result = $conn-&gt;query (&#8220;SELECT id, count <span class="caps">FROM</span> this_is_a_test <span class="caps">WHERE</span> id=1&#8221;);
if (DB::isError ($result)) {
        die (&#8220;SELECT failed: &quot; . $result-&gt;getMessage () . &quot;\n&#8221;);
}
if ($row =&amp; $result-&gt;fetchRow ()) {
        $count = $row[1];
	echo &#8220;ROW <span class="caps">WITH</span> ID &#8216;1&#8217; FOUND. <span class="caps">COUNT</span> IS: &quot;.$count.&#8221;&lt;br /&gt;&quot;;
}
else {
        die(&#8220;No such row!&#8221;);
}
	
$count++;
echo &#8220;UPDATING <span class="caps">COUNT</span> TO &quot;.$count.&#8221;&lt;br /&gt;&quot;;
// increment the field we&#8217;re modifying
$result = $conn-&gt;query (&#8220;UPDATE this_is_a_test <span class="caps">SET</span> count=?  <span class="caps">WHERE</span> id=1&#8221;, array($count));
if (DB::isError ($result)) {
        die (&#8220;UPDATE failed: &quot; . $result-&gt;getMessage () . &quot;\n&#8221;);
}
	
echo &#8220;DONE&#8221;;</code></pre>
<p>
Seems pretty straightforward&#8230; it might even seem stupid. <em>So</em> stupid that to the untrained eye, there is nothing <em>wrong</em> with this script. (other than inanity) There is something wrong, however, and the problem will only become visible when the system experiences a lot of traffic; the problem has to do with concurrency.</p>
<p>
Contrary to the opinions expressed by <a href="http://blogs.msdn.com/b/michaelbraude/archive/2009/08/16/moving-back-to-blogger.aspx" >some</a>, web development &#8211; <em>serious</em> web development; the kind which scales up to people spending <a href="http://www.facebook.com/press/info.php?statistics" >500 billion minutes per month on your web site</a> or making <a href="http://searchengineland.com/by-the-numbers-twitter-vs-facebook-vs-google-buzz-36709" >34000 requests per minute</a> &#8211; requires an understanding of concurrency, else your web application will be vulnerable to your <a href="http://msdn.microsoft.com/en-us/library/aa213029%28SQL.80%29.aspx" >most common concurrency problems</a>.</p>
<p>
To make the case, I&#8217;m going to subject the above script to a little concurrency, and the results should speak for themselves.</p>
<p class=inlineFootnote>
 Note: The update statement I&#8217;m using should probably have a <code>SET</code> clause more along the lines of <code>count=count+1</code> but that would circumvent the problem I&#8217;m going to demo, and using a more relevant script makes the demo harder to follow.</p>
<h3>
 The Experiment: JMeter</h3>
<p>
What if we had 20 people visiting the page which runs the above code 20 times each. Therefore, when the experiment is over <code>count</code> should be 20 &#215; 20=400&#8230; right? The sad reality is that it won&#8217;t be, because this script was written without consideration for the concurrent nature of the web.</p>
<p>
Don&#8217;t believe me? You can run this experiment, using a tool like <a href="http://jakarta.apache.org/jmeter/" >JMeter</a>.</p>
<h4>
 Step 1: Create a Thread Group</h4>
<p>
Open JMeter; in the left explorer-style toolpane you should see a &#8216;Test Plan&#8217; node. Right click on it, and add a Thread Group. According to the <a href="http://jakarta.apache.org/jmeter/usermanual/test_plan.html" >JMeter User Manual</a>&#8230;</p>
<blockquote><p>
 Thread group elements are the beginning points of any test plan. All controllers and samplers must be under a thread group.</p></blockquote>
<p>
Whatever that means, to run the experiment you need to create a Test Group.</p>
<p style="text-align:center;margin:auto;width:650px">
 <img src="/images/JMeter-screenshots/1-NewThreadGroup.jpg" style=margin:auto  alt="Create a Thread Group" /></p>
<h4>
 Step 2: Configure the Thread Group</h4>
<p>
You need the Thread Group to simulate 20 users visiting the page 20 times, so specify 20 threads and 20 loops per thread:</p>
<p style="text-align:center;margin:auto;width:650px">
 <img src="/images/JMeter-screenshots/2-ThreadGroup.jpg" style=margin:auto  alt="Configure the Thread Group" /></p>
<h4>
 Step 3: Create an <span class="caps">HTTP</span> Request in The Thread Group</h4>
<p>
Now that you&#8217;ve configured how many users and how often they&#8217;ll do something, you need to specify what that something they do is. You can do this using a Controller &#8211; or, more specifically, a Sampler which sends an <span class="caps">HTTP</span> Request:</p>
<blockquote><p>
 Samplers tell JMeter to send requests to a server. For example, add an <span class="caps">HTTP</span> Request Sampler if you want JMeter to send an <span class="caps">HTTP</span> request. You can also customize a request by adding one or more Configuration Elements to a Sampler.</p></blockquote>
<p style="text-align:center;margin:auto;width:650px">
 <img src="/images/JMeter-screenshots/3-NewHttpRequest.jpg" style=margin:auto  alt="Create an HTTP Request in The Thread Group" /></p>
<h4>
 Step 4: Configure the <span class="caps">HTTP</span> Request</h4>
<p>
Once the <span class="caps">HTTP</span> Request sampler is created, you still need to specify where the request goes and by what method. Specify the Server Name, and path to the script &#8211; at the very least.</p>
<p style="text-align:center;margin:auto;width:650px">
 <img src="/images/JMeter-screenshots/4-HttpRequest.jpg" style=margin:auto  alt="Configure the HTTP Request" /></p>
<h4>
 Step 5: Run</h4>
<p>
From the &#8216;Run&#8217; menu, select &#8216;Start&#8217;.</p>
<p style="text-align:center;margin:auto;width:650px">
 <img src="/images/JMeter-screenshots/5-Run.jpg" style=margin:auto  alt="Run" /></p>
<p>
Once the test has finished, poke around in your <span class="caps">SQL</span> database to check the <code>count</code> after these 100 visits. Here&#8217;s what I found:</p>
<pre class=code><code>SELECT id, count <span class="caps">FROM</span> this_is_a_test <span class="caps">WHERE</span> id=1
	
+----+------------------+
| id | count            |
+----+------------------+
| 1  | 299              |</code></pre>
<h3>
 The Result: Lost Updates</h3>
<p>
... and here we have a concurrency problem with our &#8220;simple&#8221; script. When you perform the experiment, you will almost certainly get different values, but the final analysis is this: it is very much unlikely that your <code>count</code> will be the expected 20 &#215; 20=400.</p>
<p>
So what happened?</p>
<p>
This is an example of a common concurrency problem &#8211; the lost update. The idea is this: two requests are received by the server at the same time. As one request&#8217;s process reads (<code>SELECT</code>&#8230;); the other&#8217;s then reads (<code>SELECT</code>&#8230;) before the first writes. They now have both read the same value of <code>count</code> and increment it independently. Then the first writes to the DB with an <code>UPDATE</code> and finally the second does the same&#8230; overwriting whatever changes request #1 made to the DB.</p>
<p style="text-align:center;margin:auto;width:421px">
 <img src="/images/lost-update.jpg" style=margin:auto  alt="Sequence diagram - lost update" /></p>
<p>
This is (apparently) a much more frequent problem when programming <span class="caps">PHP</span> or Perl CGI, as each request is handled by its own process in its own context &#8211; every request gets a new instance of the interpreter &#8211; while in a Java Servlet, simply using the <code>synchronized</code> keyword circumvents this problem, as all requests are handled in the same context. The frequency of this sort of problem is one of the main reasons for which technologies such as <span class="caps">PHP</span> and Ruby are said to not scale well.</p>
<p>
Is that true though? There is a solution; database locking, and it may not be as effective as language constructs like Java&#8217;s <code>synchronized</code> keyword, but the shoe fits nonetheless.</p>
<h3>
 A Solution: <code>Lock Tables</code></h3>
<p>
Using the <code>LOCK TABLES</code> command is meant &#8211; according to the <a href="http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html" >MySQL Reference Manual</a> &#8211; &#8220;explicitly for the purpose of cooperating with other sessions&#8221;. This means that <code>LOCK TABLES</code> can be used as a means for inter-process communication in environments which don&#8217;t handle all requests in the same context, such as <span class="caps">PHP</span> or Ruby CGI.</p>
<p>
So the example script can be fixed using <code>LOCK TABLES</code>.</p>
<pre class=code><code>require_once(&#8220;DB.php&#8221;);
	
$dsn = &#8220;mysql://username:password@host/db&#8221;;
$conn = DB::connect ($dsn);
if (DB::isError ($conn)) {
        die (&#8220;Cannot connect: &quot; . $conn-&gt;getMessage () . &quot;\n&#8221;);
}
	
$stmt = &#8220;LOCK <span class="caps">TABLES</span> this_is_a_test WRITE&#8221;;
$result =&amp; $conn-&gt;query ($stmt);
if (DB::isError ($result)) {
        die (&#8220;LOCK <span class="caps">TABLES</span> failed: &quot; . $result-&gt;getMessage () . &quot;\n&#8221;);
}
	
// read our row out of the database
$result = $conn-&gt;query (&#8220;SELECT id, count <span class="caps">FROM</span> this_is_a_test <span class="caps">WHERE</span> id=1&#8221;);
if (DB::isError ($result)) {
        die (&#8220;SELECT failed: &quot; . $result-&gt;getMessage () . &quot;\n&#8221;);
}
if ($row =&amp; $result-&gt;fetchRow ()) {
        $count = $row[1];
	echo &#8220;ROW <span class="caps">WITH</span> ID &#8216;1&#8217; FOUND. <span class="caps">COUNT</span> IS: &quot;.$count.&#8221;&lt;br /&gt;&quot;;
}
else {
        die(&#8220;No such row!&#8221;);
}
	
$count++;
echo &#8220;UPDATING <span class="caps">COUNT</span> TO &quot;.$count.&#8221;&lt;br /&gt;&quot;;
// increment the field we&#8217;re modifying
$result = $conn-&gt;query (&#8220;UPDATE this_is_a_test <span class="caps">SET</span> count=?  <span class="caps">WHERE</span> id=1&#8221;, array($count));
if (DB::isError ($result)) {
        die (&#8220;UPDATE failed: &quot; . $result-&gt;getMessage () . &quot;\n&#8221;);
}
	
$stmt = &#8220;UNLOCK TABLES&#8221;;
$result =&amp; $conn-&gt;query ($stmt);
if (DB::isError ($result)) {
        die (&#8220;UNLOCK failed: &quot; . $result-&gt;getMessage () . &quot;\n&#8221;);
}
	
echo &#8220;DONE&#8221;;</code></pre>
<h3>
 <em>The</em> Solution</h3>
<p>
The real solution, however, is planning. <em>How</em> you deal with concurrency isn&#8217;t as important as simply dealing with it <em>at all</em>. If you don&#8217;t think about what the site will do when 2 people just happen to perform the same action at the same time &#8211; be it withdraw money, bid on an item or just increment the number of people who &#8220;like&#8221; something &#8211; your site won&#8217;t scale well and will suffer buggy behavior under high traffic. Don&#8217;t fool yourself, though; don&#8217;t think you can&#8217;t both plan and code to deal with these issues in any language &#8211; in some languages it&#8217;s a little easier, but it can always be done.</p>]]></description>
	</item>
	<item>
		<title>For YAFGC - htaccess Files</title>
		<link>http://richard.jp.leguen.ca/not-blog/htaccess-for-yafgc</link>
		<guid>http://richard.jp.leguen.ca/not-blog/htaccess-for-yafgc</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p style="text-align:center;margin:auto;width:247px">
<a href=http://yafgc.net/> <img src="/images/BannerYAFGC.gif" style=margin:auto  alt="Yet Another Fantasy Gamer Comic" /></a></p>
<p>
This one goes out to Rich and Hilary of <a href="http://yafgc.net/" >Yet Another Fantasy Gamer Comic</a> who are making some changes to their site. In so doing some of their URLs are gonna change, and like good, responsible web citizens <a href="http://joane.livejournal.com/493186.html" >they are setting up some redirects from the old URLs to the new ones</a>.</p>
<p class=inlineFootnote>
 To understand why this makes them awesome, you should read <a href="http://www.w3.org/Provider/Style/URI" >Hypertext Style: Cool URIs don&#8217;t change.</a> by <a href="http://www.w3.org/People/Berners-Lee/" >Tim &#8216;I invented the Internet&#8217; Berners-Lee</a></p>
<p>
An htaccess file is a granular Apache configuration file for just one (or a few) directories and files. They are often a pain in the ass to work with and understand&#8230; but they&#8217;re worth it, as they provide a mechanism by which to implement what most people call &#8220;clean urls&#8221; using <code>mod_rewrite</code>. I strongly encourage anyone reading this to look into clean urls; they&#8217;re good for <a href="http://en.wikipedia.org/wiki/Search_engine_optimization" >SEO</a> and make it easier for someone to navigate back to your page when they don&#8217;t have bookmarks available (and can&#8217;t find it on Google &#8216;cause your messy URLs result in poor <span class="caps">SEO</span> :P) and as such it&#8217;s no coincidence that most web frameworks offer them as a feature.</p>
<p>
This article/post is for Rich and Hilary though, so I&#8217;ll cut to the point:</p>
<p class=inlineFootnote>
 Apologies, Rich and Hilary, but <a href="http://richard.jp.leguen.ca/tutoring" >I work as an educator</a> and as such am irrationally <strong>compelled</strong> to explain this slowly and as such will &#8211; even why trying to get to the point &#8211; ramble on at lengths about the &#8220;why&#8221; every step of the way.</p>
<p>
They want to use mod_rewrite to match part of the requested <span class="caps">URL</span> (in the query string) and then redirect to another page (with a different query string&#8230; <em>on an entirely different domain!</em>).</p>
<blockquote><p>
 I am trying to remap this url:<br />
yafgc.shipsinker.com/index.php?strip_id=abcd<br />
to this url:<br />
yafgc.net/?id=abcd<br />
where abcd = a variable numeric parameter, from 1 &#8211; 4 digits.</p></blockquote>
<p>
Most people would try the following:</p>
<p style="color:red">
 The following is a fail:</p>
<pre class=code><code>&lt;IfModule mod_rewrite.c&gt;
    RewriteEngine On
    RewriteRule \?strip_id\=(\d{1,4}) http://yafgc.net/?id=$1 [L,R]
&lt;/IfModule&gt;</code></pre>
<p>
In baby steps, this rewrite rule says that, should the requested <span class="caps">URL</span> contain&#8230;</p>
<pre class=code><code>\?			# a question mark &#8220;\?&#8221; (escaped with a &#8216;\&#8217; as &#8216;?&#8217; is a RegEx quantifier)
strip_id		# followed by &#8220;strip_id&#8221;
\=			# an equals character &#8220;=&#8221;
(
 \d{1,4}		# between 1 and 4 digits \d
)			# store the digits &#8211; hence why they are in brackets</code></pre>
<p>
... then we rewrite the <span class="caps">URL</span> to <code>http://yafgc.net/?id=$1</code>, where <code>$1</code> is a backreference to the digits which were in brackets. The &#8220;L&#8221; and &#8220;R&#8221; flags tell us this should be the <strong>L</strong>ast rule checked and that the browser should be <strong>R</strong>edirected.</p>
<p>
This won&#8217;t work because <code>mod_rewrite</code> doesn&#8217;t allow you to match the contents of the query string (the stuff after the question mark &#8216;?&#8217;) in a <code>RewriteRule</code> so the match perpetually fails. Instead, we have to match the query string in a <code>RewriteCond</code>.</p>
<h3>
 The Solution</h3>
<pre class=code><code>&lt;IfModule mod_rewrite.c&gt;
    RewriteEngine On
    RewriteCond %{QUERY_STRING}  ^strip_id=(\d{1,4})$
    RewriteRule .* http://yafgc.net/?id=%1 [L,R]
&lt;/IfModule&gt;</code></pre>
<p>
Here we match the <code>QUERY_STRING</code> and then use a different kind of backreference; unlike other RegEx implementations <code>mod_rewrite</code> provides two dimensions of backreferences; references to the <code>RewriteRule</code> and references to the <code>RewriteCond</code>. References to the rule are prefixed with a dollar sign &#8216;$&#8217; like <code>$1</code> in the earlier example, and references to the conditions are prefixed with a percentage sign &#8216;%&#8217; like <code>%1</code> in this example.</p>
<p>
There is a demo available at <a href="http://richard.jp.leguen.ca/yafgc/index.php?strip_id=100" >http://richard.jp.leguen.ca/yafgc/index.php?strip_id=100</a></p>]]></description>
	</item>
	<item>
		<title>On Using The Web</title>
		<link>http://richard.jp.leguen.ca/not-blog/on-using-the-web</link>
		<guid>http://richard.jp.leguen.ca/not-blog/on-using-the-web</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
So I&#8217;m a Web Dev TA (ok, <em>was</em>) who works with SharePoint. By day I&#8217;d work with <code>&lt;table&gt;</code> elements nested 15-20 levels deep with more <code>&lt;table&gt;</code> elements. By night I&#8217;d vigorously enforce semantic <span class="caps">HTML</span> and unobtrusive JS. I find myself operating at both ends of a very passionate <a href="http://meta.stackoverflow.com/questions/3110/when-did-so-start-using-tables-for-layout" >holy war</a>.</p>
<p>
It&#8217;s given me some chances to reflect a bit on each side of the debate, and it&#8217;s brought me to the conclusion that the most important thing for a web developer to take away from the argument has nothing to do with tables or layout at all.</p>
<h3>
  In The Classroom: &#8220;That&#8217;s Not Using The Web&#8221;</h3>
<p>
While TAing SOEN287 &#8220;Intro To Web Development&#8221; I had a student express discomfort at the idea that <span class="caps">HTML</span> should be semantic and that a particular layout &#8211; which he found would be incredibly easy with a <code>&lt;table&gt;</code> &#8211; would instead <a href="http://giveupandusetables.com/" >require more work and some challenging CSS</a>. When I insisted that <span class="caps">HTML</span> was semantic information (at least in the academic environment) he noted that <a href="http://en.wikipedia.org/wiki/Tableless_web_design" >Tabless Web Design</a> was taking the fun out of his web site. The best retort I came up with was that it&#8217;s not always fun, but I wish instead I&#8217;d told him this little nugget I found on <a href="http://www.w3.org/People/Berners-Lee/FAQ.html#FOAF" >Tim Berners-Lee&#8217;s <span class="caps">FAQ</span> page</a>:</p>
<blockquote><p>
 That&#8217;s not using the web.</p></blockquote>
<p class=inlineFootnote>
 Ok, I&#8217;m taking the quote a little out of context, but I like it anyways.</p>
<p>
Form Tim Berners-Lee, the World Wide Web is an information medium &#8211; he doesn&#8217;t concern himself much with the visual design of web pages, and focuses on the information therein.</p>
<p>
So why is it so hard to get developers to hear any of this, even students working in an academic environment? Is the ideal even relevant anymore? Did the pragmatists &#8220;win&#8221; or were they just right all along?</p>
<h3>
  Outside The Classroom</h3>
<p>
I don&#8217;t know if they won, but the reality is that this idealistic crap often won&#8217;t put food on the table. Web Sites that we&#8217;re paid to develop are &#8211; for some time now &#8211; the spiffy looking tabular ones, which often include a Flash splash page.</p>
<p>
The problem of clients who wanted seemingly tabular layouts is also compounded with (and probably feeds) a decade-old developer mentality which dictates that semantic <span class="caps">HTML</span> accompanied by <span class="caps">CSS</span> wasn&#8217;t worth the effort, or wasn&#8217;t the point of web development.</p>
<p>
As proof of this mentality I&#8217;m going to bring up <a href="http://forthescience.org/blog/about/" >Stefano Borini</a>. <a href="http://forthescience.org/blog/about/" >Stefano</a> <a href="http://forthescience.org/blog/2010/05/12/about-class-attributes-semantics-and-microformats/" >responded</a> to a <a href="http://richard.jp.leguen.ca/not-blog/a-css-class" >post I wrote a few months ago</a>, and wrote me a wonderful email. Turns out he sems like a pretty cool guy, and he will now forever have that warm special place in my heart reserved for one&#8217;s first blog commenter&#8230; despite the fact that my site has no comments&#8230; and as such <a href="http://www.codinghorror.com/blog/2006/04/a-blog-without-comments-is-not-a-blog.html" >is not really a blog.</a></p>
<p>
We exchanged a few emails about the class attribute, semantics and how I really hope I didn&#8217;t offend him, and in one of those emails he mentioned the following:</p>
<blockquote><p>
 I read it, (the w3c recommendation) but I also tend to forget very easily, and I must be honest, this point wasn&#8217;t of my interest when I read it, so I didn&#8217;t care<br />
(&#8230;)<br />
So now the fact is that most people (I assume) believe what I thought, namely that a class attribute is &#8220;just a name to refer in the CSS&#8221;. until now, I chose my attributes quite freely, but my post of yesterday was a warning for me and others as well: classes do have semantic meaning, choose your names wisely.</p></blockquote>
<p>
I asked if I could quote him on it, as it raises some interesting points about where web development has been, and makes me wonder <em>why</em>? Whatever happened that web sites deviated so far from the ideal? What made developers just not care? Is it just that any kind of software development is <a href="http://en.wikipedia.org/wiki/Software_crisis" >doomed to be off the mark</a>? Or did something a little more specific get in the way?</p>
<h3>
  Where Web Development Was And Why It Caused <span class="caps">CSS</span> To (kind of) Fail</h3>
<p>
In 1998 when CSS2 was published as a recommendation, the web-scape was a different place from what it is today. A big part of this reality is the browsers that users were using; even 6 years later, in 2004, <a href="http://en.wikipedia.org/wiki/Internet_Explorer_6#Market_share" >IE6 and other older versions of IE held 95% of market share</a>; FireFox and Safari were new and unpopular. So if you were developing web sites, you were targetting IE6.</p>
<p>
IE6, however, does not (and did not) fully or properly support CSS2, and has (or had) no shortage of <a href="http://www.positioniseverything.net/explorer.html" >shortcomings</a> in any CSS2 it did support. Any web developer can tell you this.</p>
<p class=inlineFootnote>
 My <a href="http://www.satzansatz.de/cssd/pseudocss.html#hoverjump" >favorite IE bug</a> is how sometimes absolutely positioned elements disappear when you <code>:hover</code> over an anchor. A solid runner up (still an issue in IE7&#8230; not IE8) was a <span class="caps">CSS</span> rule I wrote which caused &#8211; for no explicable reason &#8211; a JavaScript error. I&#8217;m surprised I ever found a solution (remove and redo the <span class="caps">CSS</span> rule) &#8211; and that feeling of surprise, which is associated with solving a problem in older versions of IE, is a <em>huge</em> part of what sculpted the web-scape in the early 2000s.</p>
<p>
Learning to write proper, semantically-relevant <span class="caps">HTML</span> with presentational <span class="caps">CSS</span> was almost impossible in those days, and definitly <strong>not</strong> worth the invested time and effort. You could spend all the time you wanted learning about <a href="http://www.w3.org/TR/CSS2/visuren.html#positioning-scheme" >positioning schemes</a> and <a href="http://www.w3.org/TR/CSS2/box.html" >the box model</a> but the research and hard work both never would and never could pay off. It was not what <a href="http://www.gladwell.com/" >Malcom Gladwell</a> would call Meaningful Work:</p>
<blockquote><p>
 Meaningful work is work that is autonomous. Work that is complex, that occupies your mind. And work where there is a relationship between effort and reward &#8211; for everything you put in, you get something out&#8230;</p></blockquote>
<p>
Creating web pages in the 90s, no matter what you put in, you always got the same thing out; irrational/unexpected behavior and buggy software. At the time, it really was the best idea to <a href="http://giveupandusetables.com/" >give up and use a table</a>, because it was unlikely you would <em>ever</em> have a solid grasp of how the tools (HTML-CSS) interacted. It&#8217;s no wonder then that developers like <a href="http://forthescience.org/blog/about/" >Stefano Borini</a> &#8211; who aren&#8217;t stupid people, nor bad developers &#8211; didn&#8217;t take any interest in how <span class="caps">CSS</span> worked or what the standard defined; it really was useless knowledge. Not caring was the smartest thing to do, with the highest <a href="http://www.investopedia.com/terms/r/returnoninvestment.asp" >ROI</a>.</p>
<p>
And so it was, for years and years. Until <a href="http://www.mozilla.com/en-US/firefox/upgrade.html" >Mozilla FireFox</a> came along and shook things up. In order to keep up with the changing demands and needs of web-users, Microsoft had to release a new version of Internet Explorer&#8230; and then another a few years later. These new browsers were more compliant &#8211; though they still had their shortcomings. The victory, however, was that now idealistic web dev had the potential to become Meaningful Work and developers <em>could</em> create semantic web pages.</p>
<p>
This was only half the problem, however, as <span class="caps">CSS</span> still has limitations, and customers <em>still</em> want (need?) visually appealing web pages, so it doesn&#8217;t really make any difference&#8230; or is that changing too?</p>
<h3>
  Where Web Development Is And Why MySpace Is Failing</h3>
<p>
There is massive potential and opportunity in steering web development towards the ideal, not only because web browsers are behaving themselves, but because <em>regular every day people are beginning to think of the web as an information medium</em>. This is why websites like <a href="http://hearablog.com/" >Hear a Blog</a> (coincidentally my new favorite way to cyberslack) are popping up, and I also believe this contributes to why <a href="http://web.resource.org/rss/1.0/" >RSS</a> is booming right now but a similar technology, <a href="http://en.wikipedia.org/wiki/Channel_Definition_Format" >Channel Definition Format</a>, faded away in the 90s. Back then people weren&#8217;t used to the idea of the web as an information medium; they were still hooked on visual concepts.</p>
<p>
Now, people are ready to use and perceive the web as an information medium.</p>
<p>
We can see the world is ready by observing the social networking sites of the 2000s; <a href="http://www.facebook.com/" >Facebook</a> and <a href="http://www.myspace.com/" >MySpace</a>. Facebook has remained a monster success while MySpace has faded. Why? <em>Because the intent with Facebook was always to use the web for information</em>. That&#8217;s why from the very beginning user&#8217;s couldn&#8217;t customize the look and feel of their profile. Meanwhile, MySpace is truly the GeoCities of the 21st century; its peronalized, poorly designed, inaccessible and often flat out ugly pages may have been the rage back &#8220;Where Web Development Was&#8221; but now the reality is that&#8230;</p>
<blockquote><p>
 That&#8217;s not using the web.</p></blockquote>
<p>
And people know this now. They may not be able to say it or explain it when you ask, but deep down inside people know that Facebook succeeded and MySpace fails because of Facebook&#8217;s understanding of how to use the web and MySpace&#8217;s dated (obsolete?) outlook on the web.</p>
<p class=inlineFootnote>
 Now would also be a good time to point out it&#8217;s no coincidence that Facebook has some sort of <a href="http://blog.facebook.com/blog.php?post=71852922130" >accessibility help center</a> and <a href="http://en.wikipedia.org/wiki/MySpace#Accessibility_and_reliability" >MySpace doesn&#8217;t</a>. I&#8217;m not saying that Facebook succeeds <em>because</em> they care for accessibility, but rather that there is correlation between their success and their concern for accessibility &#8211; both stem from the same understanding of how to use the Web.</p>
<h3>
  What&#8217;s Your Point? &#8211; Where Web Development Is Going</h3>
<p>
My point is that web developers of all sorts (be they graphic designers, Asp.NET programmers, Rails developers or <span class="caps">PHP</span> scripters) need to stop thinking of the web as a visual medium. My point <em>is not</em> that you shouldn&#8217;t use tables &#8211; that horse is long dead &#8211; but rather that if you find yourself thinking <a href="http://37signals.com/svn/posts/1550-so-im-going-to-give-up-and-use-tables" >I should just give up an use tables</a> chances are <em>you&#8217;re already thinking about your web app incorrectly</em>, not really using the Web, and stifling your product&#8217;s potential. You&#8217;re also taking the risk that you&#8217;ll be left behind, <a href="/images/mc-hammer.jpg" >stuck in the 90s</a> as the web-scape continues to change.</p>
<p>
Don&#8217;t confuse web development&#8217;s past with your career&#8217;s future.</p>
<p>
I see where the web is as being identical to where movies were in 1927&#8230; or more specifically, <a href="http://en.wikipedia.org/wiki/Talkies" >talkies</a>. The tools, technology and theory for sound film was available for some years but wasn&#8217;t <em>perfected</em> until <a href="http://en.wikipedia.org/wiki/The_Jazz_Singer_%281927_film%29" >1927</a> and even once a &#8216;talkie&#8217; had seen mainstream success, it still <a href="http://en.wikipedia.org/wiki/Silent_film#Transition" >took some years for it to become the standard</a>. The industry players didn&#8217;t think it would catch on; they thought it was silly and that theirs was a visual medium. Technological impedemends had made them lose sight of the reality that to the consumers, they were an <em>entertainment</em> medium &#8211; not a visual medium. Once the transition came, <a href="http://en.wikipedia.org/wiki/Sound_film#Labor" >people&#8217;s careers were ruined</a>.</p>
<p>
Personally, I don&#8217;t want to stand around idly while a similar change happens in my field, <a href="http://www.youtube.com/watch?v=-j8GwkniGrU" >commenting on how it&#8217;s a toy, it&#8217;s a scream, it&#8217;s vulgar, and doubting if they&#8217;ll ever really use it</a>.</p>]]></description>
	</item>
	<item>
		<title>Congratulations Marc!</title>
		<link>http://richard.jp.leguen.ca/not-blog/congratulations-marc</link>
		<guid>http://richard.jp.leguen.ca/not-blog/congratulations-marc</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
Time for some cheesy family stuff; as it turns out I have a brother &#8211; <a href="http://ca.linkedin.com/pub/marc-le-guen/1b/217/909" >Marc Le Guen</a> &#8211; who is pursuing an <span class="caps">MBA</span> at the John Molson School of Business.</p>
<p>
It&#8217;s treating him well, and there&#8217;s lots of big news from him; he&#8217;s been elected VP Academic for the Commerce Grad students something something (the title&#8217;s kind of long) and is going to be a key organizer for the next <a href="http://www.mbacasecomp.com/" >JMSB International Case Competition</a>.</p>
<p>
As such, he was selected as one of the <span class="caps">JMSB</span> poster boys for the <a href="http://johnmolson.concordia.ca/images/stories/graduate_prog/mba/docs/john_molson_mba_brochure.pdf" >new batch of brochures this year</a>&#8230; see page 16 (numbered as 13).</p>
<p style="text-align:center;margin:auto;width:475px">
<a href=http://johnmolson.concordia.ca/images/stories/graduate_prog/mba/docs/john_molson_mba_brochure.pdf> <img src="/images/marc-le-guen.jpg" style=margin:auto  alt="Marc Le Guen" /></a></p>]]></description>
	</item>
	<item>
		<title>SOEN287: Parsing XML In Perl</title>
		<link>http://richard.jp.leguen.ca/not-blog/soen287-parse-xml</link>
		<guid>http://richard.jp.leguen.ca/not-blog/soen287-parse-xml</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
I don&#8217;t know if any students in the f2010 SOEN287 class actually read the main page of my web site, but a number of students have shown me their work on writing an <span class="caps">XML</span> file for their final project, and their work has concerned me. The example I saw which concerned me the most was as follows: (it&#8217;s an <span class="caps">XML</span> session file)</p>
<p style="color:red">
 The Following Session File Is Incorrectly Formatted</p>
<pre class=code><code>&lt;hand&gt;Ace of Hearts, 2 of Clubs, 8 of Diamonds, 7 of Hearts, King of Clubs&lt;/hand&gt;
&lt;hand&gt;Ace of Spades, Ace of Clubs, 8 of Clubs, 8 of Spades, 7 of Diamonds&lt;/hand&gt;
&lt;deck&gt;Ace of Hearts, 2 of Hearts, (&#8230;)&lt;/deck&gt;</code></pre>
<p>
This is not correct, and &#8211; in my opinion &#8211; demonstrates a lack of understanding of XML. The three major problems I can identify are:</p>
<ol><li> There is no root node; in <span class="caps">XML</span> you always need a root element. When you were writing <span class="caps">XHTML</span> the root node was <code>&lt;html&gt;</code>.</li><li> There are comma separated values nested in your elements for no good reason; if you&#8217;re using <span class="caps">XML</span> formatting, use <span class="caps">XML</span> formatting. If you&#8217;re using CSV, use CSV; don&#8217;t mix them up like this.</li><li> There is no indicator in the <span class="caps">XML</span> as to which hand belongs to which player.</li></ol>
<p>
Last week, Thursday, I gave an additional tutorial on parsing <span class="caps">XML</span> using Perl. While the technique I demonstrated used RegEx, <a href="http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html" >you cannot parse <span class="caps">XML</span> (or XHTML) using regular expressions</a>. The keyword in regular expressions is &#8216;regular&#8217; and it is there to indicate that regular expressions are a tool to be used with <a href="http://en.wikipedia.org/wiki/Regular_language" >Regular Languages</a> but <span class="caps">XML</span> (and XHTML) are <em>not</em> regular languages, they are <a href="http://en.wikipedia.org/wiki/Context_free_language" >Context Free Languages</a> but not Regular Languages.</p>
<p>
As such you <em>cannot</em> use something like this to extract an element from XML:</p>
<pre class=code><code># This is so wrong!!
if($xml =~ /&lt;something&gt;.+&lt;/something&gt;/) {
	# it won&#8217;t work!
}</code></pre>
<p>
... this is because <span class="caps">XML</span> elements can be nested, and your regular expression cannot ensure that you haven&#8217;t matched the wrong close tag <code>&lt;/something&gt;</code>. </p>
<pre class=code><code>&lt;something&gt;
  &lt;something&gt;
    Valid <span class="caps">XML</span> which will break your regex
  &lt;/something&gt;
&lt;/something&gt;
&lt;something&gt;
  Valid <span class="caps">XML</span> which will break your regex
&lt;/something&gt;</code></pre>
<p>
This is a mathematically proven reality of life; <em>you cannot parse <span class="caps">XML</span> with RegExs</em>.</p>
<p>
The following is the code I demoed in tutorial on Thursday, for those who look at this page. I have included only a screenshot to ensure that you still have to read and understand it a little. I have also omitted the <code>parseCard</code> subroutine so that you will have to generalize what you learned in the first two subroutines to make it work.</p>
<p style="text-align:center;margin:auto;width:741px">
 <img src="/images/soen287-w2010/xml-session-file.jpg"  alt="XML" /></p>
<p style="text-align:center;margin:auto;width:741px">
 <img src="/images/soen287-w2010/parse-xml.jpg"  alt="Parser" /></p>
<p>
Good luck.</p>]]></description>
	</item>
	<item>
		<title>Unicorn!</title>
		<link>http://richard.jp.leguen.ca/not-blog/unicorn</link>
		<guid>http://richard.jp.leguen.ca/not-blog/unicorn</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p style="text-align:center;margin:auto;width:200px">
<a href=http://blog.stackoverflow.com/2010/03/reminder-its-april-1st/> <img src="/images/stackoverflow-unicorn.jpg" style=margin:auto  alt="Unicorns" /></a></p>]]></description>
	</item>
	<item>
		<title>Application Configuration Files</title>
		<link>http://richard.jp.leguen.ca/not-blog/application-config-files</link>
		<guid>http://richard.jp.leguen.ca/not-blog/application-config-files</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
Common in .NET development is the use of the <a href="http://msdn.microsoft.com/en-us/library/ms184658%28VS.80%29.aspx" >Application Configuration Files</a>, more commonly refereed to as the <code>app.config</code> file and the <code>web.config</code> file. I thought it would be nice to learn a little more about them, since a lot of developers I meet know how to use Application Configuration Files without really knowing anything about Application Configuration Files &#8211; they allow Visual Studio to create and populate these config files &#8211; and I don&#8217;t want to be one of them.</p>
<p>
So the first thing I did was write up a C# FizzBuzz program. The program I wrote was inspired by <a href="http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html" >this</a> Coding Horror blog entry. It will:</p>
<ul><li> Print the numbers 1 to 100</li><li> ... But for multiples of some number &#8216;N&#8217; it should &#8211; instead of outputting the number &#8211; output &#8220;FizzBuzz&#8221;.</li><li> I want &#8216;N&#8217; to be configurable in my Application Configuration File</li></ul>
<p>
I&#8217;m going to start with the easy way to provide configuration options using an <code>app.config</code> file; using the <code>&lt;appSettings&gt;</code> config section. I&#8217;m then going to touch on defining custom config sections.</p>
<h3>
 The Easy Way: <code>&lt;appSettings&gt;</code></h3>
<p>
While the Application Configuration File provides a wide range of configuration functionality, I&#8217;m going to start with the simplest means of configuration available; the <code>appSettings</code> configuration section.</p>
<h4>
 Step 1: Writing the (basic) Program</h4>
<p>
This is the program I will be working with, excluding any Application Configuration File related functionality.</p>
<pre class=code><code>using System;
	
public class FizzBuzz {
	
	public static void Main() {
	
		int configurableN = 3;
		for(int index=1;index&lt;=100;index++) {
			if(index%configurableN!=0) {
				System.Console.WriteLine(index);
			}
			else {
				System.Console.WriteLine(&#8220;FizzBuzz!&#8221;);
			}
		}
	
	}
	
}</code></pre>
<p>
I have chosen to name this <code>.cs</code> file <code>FizzBizz.cs</code>. This will be important as the name of the Application Configuration File will depend on the name of the <code>.exe</code> file.</p>
<h4>
 Step 2: Creating the Application Configuration File</h4>
<p>
At its simplest, the basic structure of an <code>app.config</code> file is an xml file with a <code>&lt;configuration&gt;</code> root element.</p>
<pre class=code><code>&lt;?xml version=&#8220;1.0&#8221; encoding=&#8220;utf-8&#8221; ?&gt;
&lt;configuration&gt;
	&lt;!-- This is my first app.config file! --&gt;
&lt;/configuration&gt;</code></pre>
<p>
The name of this file should be the same the <code>.exe</code> file (including the <code>.exe</code> extension) with an added <code>.config</code> extension. So I&#8217;ve named this file <code>FizzBuzz.exe.config</code>.</p>
<h4>
 Step 3: Add an <code>&lt;appSettings&gt;</code> Section to the Application Configuration File</h4>
<p>
An Application Configuration File is divided into sections. In this first example, I&#8217;m using the out of the box <code>&lt;appSeettiongs&gt;</code> so I need to add that section. While I&#8217;m at it, I might as well also add my key-value pair:</p>
<pre class=code><code>&lt;?xml version=&#8220;1.0&#8221; encoding=&#8220;utf-8&#8221; ?&gt;
&lt;configuration&gt;
	&lt;appSettings&gt;
		&lt;add key=&#8220;N&#8221; value=&#8220;5&#8221; /&gt;
	&lt;/appSettings&gt;
&lt;/configuration&gt;</code></pre>
<p>
When I run <code>FizzBuzz.exe</code> I will now be able to programatically access this application setting which I have named &#8220;N&#8221;.</p>
<h4>
 Step 4: Reading the <code>&lt;appSetting&gt;</code> Entry</h4>
<p>
I now need to change our <code>FizzBuzz.cs</code> file to read the application settings we&#8217;ve added to the config file.</p>
<p class=inlineFootnote>
 Technically, I&#8217;m donig this backwards:I should have started with writing code which can be configured and then written the config file, but this is a learning exercise.</p>
<pre class=code><code>using System;
using System.Configuration;
	
public class FizzBuzz {
	
	public static void Main() {
	
		string configurableNSetting = ConfigurationSettings.AppSettings[&#8220;N&#8221;];
		int configurableN;
		if(int.TryParse(configurableNSetting, out configurableN)) {
			for(int index=1;index&lt;=100;index++) {
				if(index%configurableN!=0) {
					System.Console.WriteLine(index);
				}
				else {
					System.Console.WriteLine(&#8220;FizzBuzz!&#8221;);
				}
			}
		}
	
	}
	
}</code></pre>
<p>
... it should be noted that in order to read from the <code>&lt;appSettings&gt;</code> in this way you have to use <code>System.Configuration</code>.</p>
<h4>
 Compile and Run!</h4>
<p>
Since I have the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc&amp;displaylang=en" >Windows SDK</a> installed, my computer has a command line C# compiler. From a command prompt I just type in the following;</p>
<pre class=code><code>&gt; C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe FizzBuzz.cs</code></pre>
<p>
... to compile <code>FizzBuzz.cs</code> file into an executable (<code>.exe</code>) file. I can now run <code>FizzBuzz.exe</code> and I can see that the code uses the value of N in my configuration file. I can change the value in my config file without re-compiling my code.</p>
<h3>
 The More Advanced Way: Custom Config Sections</h3>
<p>
If using the <code>&lt;appSettings&gt;</code> config section runs the risk of bloating that section and &#8211; if the code should be reused &#8211; suffering namespace collisions between the key-names of different settings; what if code you written later uses a setting with the key-name &#8216;N&#8217;, and has to be integrated with this code?</p>
<p>
The solution I&#8217;m trying is to define a custom config section. First, I need to write a new class which extends class <code>System.Configuration.ConfigurationSection</code>:</p>
<pre class=code><code>using System.Configuration;
	
namespace ConfigSectionExample {
	
	public class FizzBuzzConfigSection : ConfigurationSection
	{
		// More to come&#8230;
	}
	
}</code></pre>
<p>
... I now need to add a <code>&lt;configSections&gt;</code> element to my <code>app.config</code>, and in it a <code>&lt;section&gt;</code>:</p>
<pre class=code><code>&lt;configSections&gt;
	&lt;section name=&#8220;richard&#8221; type=&#8220;ConfigSectionExample.FizzBuzzConfigSection,FizzBuzzConfigSection&#8221;/&gt;
&lt;/configSections&gt;</code></pre>
<p>
This <code>&lt;section&gt;</code> element means I can now add a <code>&lt;richard&gt;</code> element to my <code>app.config</code> file, and settings defined in that <code>&lt;richard&gt;</code> element are associated with my class <code>ConfigSectionExample.FizzBuzzConfigSection</code> which will be in an assembly named <code>FizzBuzzConfigSection.dll</code>.</p>
<p>
While there are a few ways I could proceed, the main two options for defining custom config sections would be to either provide my configurable &#8216;N&#8217; as an <span class="caps">XML</span> attribute (or what Microsoft would call a Configuration Property) of this section, or as <span class="caps">XML</span> elements nested within my section.</p>
<h4>
 <span class="caps">XML</span> Attributes for a Custom Section</h4>
<p>
What if I want to use <span class="caps">XML</span> Attributes to define my configurable value of &#8216;N&#8217;? I&#8217;ll start by adding an <span class="caps">XML</span> attribute on this config section, so all together my <code>app.config</code> file now looks like this:</p>
<pre class=code><code>&lt;?xml version=&#8220;1.0&#8221; encoding=&#8220;utf-8&#8221; ?&gt;
&lt;configuration&gt;
	&lt;configSections&gt;
		&lt;section name=&#8220;richard&#8221; type=&#8220;ConfigSectionExample.FizzBuzzConfigSection,FizzBuzzConfigSection&#8221;/&gt;
	&lt;/configSections&gt;
	&lt;richard N=&#8220;5&#8221; /&gt;
&lt;/configuration&gt;</code></pre>
<p>
In order to read and utilize this &#8220;N&#8221; <span class="caps">XML</span> attribute I need to make some code changes to the custom config section class:</p>
<pre class=code><code>using System.Configuration;
	
namespace ConfigSectionExample {
	
	public class FizzBuzzConfigSection : ConfigurationSection
	{
		[ConfigurationProperty( &#8220;N&#8221; )]
		public string N {
			get {
				// Note the ConfigurationProperty( &#8220;N&#8221; ) attribute,
				// and how the &#8220;N&#8221; there matches the &#8220;N&#8221; index here&#8230;
				return this[&#8220;N&#8221;].ToString();
			}
		}
	}
	
}</code></pre>
<p>
What I&#8217;ve had to do is add a property &#8216;N&#8217; to my <code>FizzBuzzConfigSection</code> class. I have applied the <code><a href="http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute.aspx" >ConfigurationProperty</a></code> attribute to this new property, associating it with the &#8216;N&#8217; <span class="caps">XML</span> attribute in my <code>app.config</code> file. Doing this allows me to access this <span class="caps">XML</span> attribute using an indexer:</p>
<pre class=code><code>this[&#8220;N&#8221;]</code></pre>
<p>
... without the <code>ConfigurationProperty</code> attribute, this would generate a runtime exception.</p>
<p>
Accessing this custom config section as opposed to the <code>&lt;appSettings&gt;</code> also requires a change in the main program.</p>
<pre class=code><code>using System;
using System.Configuration;
	
public class FizzBuzz {
	
	public static void Main() {
	
		FizzBuzzConfigSection section = (FizzBuzzConfigSection)ConfigurationManager.GetSection( &#8220;richard&#8221; );
		string configurableNSetting = section.N;
		int configurableN;
		if(int.TryParse(configurableNSetting, out configurableN)) {
			for(int index=1;index&lt;=100;index++) {
				if(index%configurableN!=0) {
					System.Console.WriteLine(index);
				}
				else {
					System.Console.WriteLine(&#8220;FizzBuzz!&#8221;);
				}
			}
		}
	
	}
	
}</code></pre>
<h5>
 Compiling the Custom Confi Section</h5>
<p>
Lastly, I need to compile the two <code>.cs</code> files. To compile <code>FizzBuzzConfigSection.cs</code> into a <span class="caps">DLL</span> (since it has no main it can&#8217;t be a <code>.exe.</code> file) I can use the <code>/t:library</code> switch on the command prompt:</p>
<pre class=code><code>&gt; csc /t:library FizzBuzzConfigSection.cs</code></pre>
<p>
I can then reference the newly creaetd <code>FizzBuzzConfigSection.dll</code> when I compile <code>FizzBuzz.cs</code> into a <code>.exe</code> file, using the <code>/r:</code> switch.</p>
<pre class=code><code>&gt; csc /r:FizzBuzzConfigSection.cs FizzBuzz.cs</code></pre>
<h4>
 Custom Configuration Elements</h4>
<p>
You may have noticed that the original <code>app.config</code> &#8211; when I was using <code>&lt;appSettings&gt;</code> &#8211; was using an <code>&lt;add&gt;</code> element instead of an <span class="caps">XML</span> attribute on the config section. I&#8217;m not going to document my attempts to do this here; maybe in a future news entry. I will, however, link to the tutorial I&#8217;m reading to figure it out: <a href="http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx" >http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx</a></p>]]></description>
	</item>
	<item>
		<title>Don't Look Directly Into Its Eyes</title>
		<link>http://richard.jp.leguen.ca/not-blog/dont-look-directly-into-its-eyes</link>
		<guid>http://richard.jp.leguen.ca/not-blog/dont-look-directly-into-its-eyes</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p style="text-align:center;margin:auto;width:415px">
 <img src="/images/creepyewok.gif" style=margin:auto  alt="Creepy Ewok" /></p>]]></description>
	</item>
	<item>
		<title>Asp.NET Development With Nothin' But a Pair of Sticks</title>
		<link>http://richard.jp.leguen.ca/not-blog/asp.net-development-with-sticks</link>
		<guid>http://richard.jp.leguen.ca/not-blog/asp.net-development-with-sticks</guid>
		<pubDate>Tue, 30 Nov 1999 00:00:00 GMT</pubDate>
		<description><![CDATA[<p>
Given my status as a Microsoft Certified Technical Specialist, I figured I should start playing with .NET and &#8211; since I fancy myself a bit of a web-developer &#8211; Asp.NET in particular. When it comes to Asp.NET though I don&#8217;t really have a good environment in which to do anything cool like <a href="http://en.wikiquote.org/wiki/The_Magic_School_Bus" >take chances, get messy or make mistakes</a> and since my work environment provides me with all the conveniences of Asp.NET development, I thought maybe I&#8217;d like to contrast that a little at home by learning this the way Boy Scouts stereotypically learn to make fire.</p>
<h3>
 Setup</h3>
<p>
With a little bit of what some might call researching, I figured out that I am running &#8220;Windows Vista Home Premium&#8221; (Start&gt;Computer&gt;Help&gt;&#8216;About Windows&#8217;) which means that <a href="http://technet.microsoft.com/en-us/library/cc731911%28WS.10%29.aspx" >IIS and Asp.NET are waiting for me to turn them on</a>. All I have to do is <a href="http://windows.microsoft.com/en-CA/windows-vista/Turn-Windows-features-on-or-off" >activate those Windows features from the Control Panel</a>.</p>
<p style="text-align:center;margin:auto;width:430px">
 <img src="/images/WindowsFeatures.jpg" style=margin:auto  alt="Turn Windows Features On-Off" /></p>
<p class=inlineFootnote>
 If you&#8217;re following along and running Vista, while you&#8217;re in there you might want to consider poking around and choosing if you want to <a href="http://windows.about.com/od/customizingwindows/qt/speed_up_vista.htm" >remove some features which may just be slowing your machine down</a>. Most notably &#8211; since this excercise involces a lot of &#8216;Run as Administrator&#8217; &#8211; you might want to turf off UAC.</p>
<p>
Activating the features took &#8220;several minutes to complete&#8221;, but once I was done I had <span class="caps">IIS</span> and Asp.NET up and running.</p>
<p>
I was surprised to discover that the setup was that easy. Seriously, try it; once you do the above, just type up an <span class="caps">ASPX</span> file&#8230; (I did it in notepad and cleverly named the file &#8220;hello.aspx&#8221;)</p>
<pre class=code><code>&lt;%@ Page language=&#8220;C#&#8221; %&gt;
&lt;!DOCTYPE html <span class="caps">PUBLIC</span> &#8220;-//W3C//DTD <span class="caps">XHTML</span> 1.0 Strict//EN&#8221; &#8220;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&#8221;&gt;
&lt;html xmlns=&#8220;http://www.w3.org/1999/xhtml&#8221;&gt;
 &lt;head&gt;
	&lt;meta http-equiv=&#8220;Content-Type&#8221; content=&#8220;text/html; charset=iso-8859-1&#8221; /&gt;
	&lt;title&gt;Hello World AspX&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;form runat=&#8220;server&#8221;&gt;
	&lt;p&gt;
		Did it work?
		&lt;asp:label id=&#8220;message&#8221; runat=&#8220;server&#8221; Text=&#8220;Yes, it worked!&#8221; /&gt;
	&lt;/p&gt;
  &lt;/form&gt;
 &lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>
... and drop it in the <code>C:\inetpub\wwwroot</code> folder, no different than you&#8217;d toss a <span class="caps">PHP</span> script in an htdocs folder on a <a href="http://en.wikipedia.org/wiki/LAMPP" >LAMPP</a> system. Open up a web browser and visit <code>http://localhost/hello.aspx</code> and you should see the fruits of your labors!</p>
<p class=inlineFootnote>
 Again, if you&#8217;re following along and this is your first aspx file, you should take note of <a href="http://msdn.microsoft.com/en-us/library/k33801s3.aspx" >the characteristics that distinguish ASP.NET Web pages from static <span class="caps">HTML</span> pages</a>:</p>
<ul><li> A file name extension of .aspx instead of .htm, .html, or other file name extensions. The .aspx file name extension causes the page to be processed by ASP.NET.</li><li> An optional <a href="http://msdn.microsoft.com/en-us/library/ydy4%7804a.aspx" >Page directive</a>. In the future, you might use other directives, such as <a href="http://msdn.microsoft.com/en-us/library/d19c0t4b.aspx" >Control</a>, <a href="http://msdn.microsoft.com/en-us/library/eb44kack.aspx" >Import</a>, <a href="http://msdn.microsoft.com/en-us/library/cbsf6k72.aspx" >Implements</a>, <a href="http://msdn.microsoft.com/en-us/library/c76dd5k1%28VS.71%29.aspx" >Register</a>, <a href="http://msdn.microsoft.com/en-us/library/d864zc1k%28VS.71%29.aspx" >Assembly</a>, <a href="http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx" >OutputCache</a> and <a href="http://msdn.microsoft.com/en-us/library/w70c655a.aspx" >Reference</a>.</li><li> A <code>&lt;form&gt;</code> element which includes a <code>runat=&#8216;server&#8217;</code> attribute.</li><li> Web server controls, such as the <code>&lt;asp:Label&gt;</code>. (note that it also has a <code>runat=&#8216;server&#8217;</code> attribute) These are special tags which are processed server-side and are changed into (sometimes complicated and inaccessible) HTML-CSS-JavaScript.</li><li> Server code, though this is not in the example.</li></ul>
<h3>
 Adding Code Behind</h3>
<p>
As exciting as accomplishing this much was, the really useful part of Asp.NET development (contrasted with classic Asp) is the code behind. (this is also where <span class="caps">JSP</span> disappoints a little&#8230;<sup class="footnote"><a href="#fn1">1</a></sup>)</p>
<p>
If you&#8217;re not familiar with what I mean by code behind, <a href="http://support.microsoft.com/kb/303247" >support.miscrosoft.com</a> describes it very effectively as follows:</p>
<blockquote><p>
 Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your <span class="caps">HTML</span> from your presentation logic.</p></blockquote>
<p>
I wanted to write up a code behind, still using no spiffy development environments. This meant three things:</p>
<ol><li> Writing the code behind</li><li> compiling the code behind to a DLL</li><li> deploying the compiled <span class="caps">DLL</span> to my web application</li></ol>
<h4>
 Writing a C# Code Behind</h4>
<p>
Writing a C# code behind the old fashioned way using just notepad and the command line wasn&#8217;t that tough in the end. All I had to do was install the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc&amp;displaylang=en" >Windows SDK</a>, open up notepad and write out a class (in C#) which extends System.Web.UI.Page:</p>
<pre class=code><code>using System;
	
public class HelloWorld : System.Web.UI.Page {
	
	protected System.Web.UI.WebControls.Label message;
	
	public void Page_Load() {
		message.Text = DateTime.UtcNow.ToString();
	}
	
}</code></pre>
<p>
This code will change the <code>Text</code> of a &#8216;message&#8217; <code>Label</code> to the current date-time. I&#8217;m doing this by overriding the <code>Page_Load</code> method of class <code>System.Web.UI.Page</code> and manipulating my server controls in that method. Overloading this method actually seems to be a shorthand for using the <code>Load</code> <a href="http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx" >event</a>:</p>
<pre class=code><code>using System;
	
public class HelloWorld : System.Web.UI.Page {
	
	protected System.Web.UI.WebControls.Label message;
	protected System.Web.UI.WebControls.Button button;
	
	public HelloWorld() {
		this.Load += LoadHelloWorld;	// I like this better than using Page_Load
						// but if you&#8217;re new to .NET Page_Load might be easier to grasp&#8230;
	}
	
	public void LoadHelloWorld(Object sender, EventArgs e) {
		message.Text = DateTime.UtcNow.ToString();
	}
	
}</code></pre>
<p>
... whichever way, the end result is the same.</p>
<p>
Next came the interesting part; I saved the file as &#8216;HelloWorld.cs&#8217; and compiled it into a DLL.</p>
<h4>
 Compiling a .cs File On The Command Line (Look Mom! No IDE!)</h4>
<p>
To use this HelloWorld class in an Asp.NET page, it had to be compiled into a <a href="http://msdn.microsoft.com/en-us/library/ms682589%28VS.85%29.aspx" >DLL</a>. Again, though, the point of this exercise has been to learn how to develop .NET from the ground up using nothing but my wits and a couple of twigs, so I installed the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc&amp;displaylang=en" >Windows SDK</a> on my computer, which includes the C# compiler <code>csc.exe</code>. The compiler itself ends up in the <code>C:\Windows\Microsoft.NET\Framework\v3.5</code> folder, so from the command prompt I used the following command to compile &#8216;HelloWorld.cs&#8217; into a DLL:</p>
<pre class=code><code>&gt; C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:library HelloWorld.cs</code></pre>
<p class=inlineFootnote>
 The <code>/t:library</code> option specifies that the ouput file should be a DLL.</p>
<p>
Since I suspsect I&#8217;ll be using <code>csc.exe</code> pretty often in my Asp.NET adventures, I think I&#8217;ll <a href="http://www.voidspace.org.uk/python/articles/command_line.shtml#path" >modify the <span class="caps">PATH</span> environment variable</a>  on my system to include the <code>C:\Windows\Microsoft.NET\Framework\v3.5\</code> directory, so I don&#8217;t have to type out the full path to <code>csc.exe</code> every time I use it&#8230;</p>
<pre class=code><code>&gt; csc /t:library HelloWorld.cs</code></pre>
<h4>
 Deploying the Code Behind</h4>
<p>
I copy-pasted the resulting &#8216;HelloWorld.dll&#8217; file into the &#8216;bin&#8217; directory of my web application&#8217;s virtual directory, <code>C:\inetpub\wwwroot\bin</code> and added an &#8216;inherits&#8217; attribute to my <a href="http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx" >Page directive</a>:</p>
<pre class=code><code>&lt;%@ Page language=&#8220;C#&#8221; inherits=&#8220;HelloWorld&#8221; %&gt;
&lt;!DOCTYPE html <span class="caps">PUBLIC</span> &#8220;-//W3C//DTD <span class="caps">XHTML</span> 1.0 Strict//EN&#8221; &#8220;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&#8221;&gt;
&lt;html xmlns=&#8220;http://www.w3.org/1999/xhtml&#8221;&gt;
 &lt;head&gt;
	&lt;meta http-equiv=&#8220;Content-Type&#8221; content=&#8220;text/html; charset=iso-8859-1&#8221; /&gt;
	&lt;title&gt;Hello World AspX&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;form runat=&#8220;server&#8221;&gt;
	&lt;p&gt;
		Did it work?
		&lt;asp:label id=&#8220;message&#8221; runat=&#8220;server&#8221; Text=&#8220;Yes, it worked!&#8221; /&gt;
	&lt;/p&gt;
  &lt;/form&gt;
 &lt;/body&gt;
&lt;/html&gt;</code></pre>
<p class=inlineFootnote>
 Since this was my first little adventure with Asp.NET on this machine, the <code>bin</code> folder did not exist for my web application, so I had to create it myself.</p>
<p>
Alternately, I figure I could have deployed to the <a href="http://msdn.microsoft.com/en-us/library/yf1d93sz%28VS.71%29.aspx" >GAC</a>.</p>
<p id="fn1" class="footnote"><sup>1</sup>
 Why am I disappointed? <a href="http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html" >JSP Page directives</a> provide the &#8216;extends&#8217; attribute but no one seems to have ever used it! The only information I&#8217;ve <em>ever</em> found about it is that &#8220;Sun recommends &#8216;to use this attribute cautiously&#8217;&#8221;. </p>]]></description>
	</item>
 </channel>
</rss>
