<?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"
	>

<channel>
	<title>Christian Schenk</title>
	<atom:link href="http://www.christianschenk.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<pubDate>Wed, 03 Dec 2008 06:54:19 +0000</pubDate>
	
	<language>en</language>
			<item>
		<title>Dynamic menu for your WordPress theme</title>
		<link>http://www.christianschenk.org/blog/dynamic-navigation-menu-wordpress-theme/</link>
		<comments>http://www.christianschenk.org/blog/dynamic-navigation-menu-wordpress-theme/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 16:00:21 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[HowTo]]></category>

		<category><![CDATA[menu]]></category>

		<category><![CDATA[sidebar]]></category>

		<category><![CDATA[widgets]]></category>

		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=284</guid>
		<description><![CDATA[I have found various descriptions about a dynamic navigation menu in a WordPress theme. What I&#8217;ve seen these solutions grab some pages and display the result either in the header or sidebar of the theme. It&#8217;s called dynamic because you can add pages to your blog and - depending on the implementation - a link [...]]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span> have <a title="Dynamic WordPress menu with animated JavaScript drop-downs" href="http://wordpress.org/support/topic/215815">found</a> <a title="Building a Dynamic WordPress Nav Menu" href="http://green-beast.com/blog/?p=157">various</a> descriptions about a dynamic navigation menu in a WordPress theme. What I&#8217;ve seen these solutions grab some pages and display the result either in the header or sidebar of the theme. It&#8217;s called <em>dynamic</em> because you can add pages to your blog and - depending on the implementation - a link to the appropriate page will appear in the menu.</p>
<p>Here&#8217;s my idea: What about adding another sidebar to your theme that holds some <code>Text</code> widgets, each representing an item in your menu? I think that&#8217;s ultra neat because you can:</p>
<ul>
<li>change your menu just by adding another <code>Text</code> widget</li>
<li>rearrange the order with drag and drop</li>
<li>use <a title="Widget Logic" href="http://wordpress.org/extend/plugins/widget-logic/">Widget Logic</a> to control where your menu items appear.</li>
</ul>
<p>Let&#8217;s implement this.</p>
<h2>Changing the theme</h2>
<p>Since I&#8217;m using a modified version of the great <a title="The Copyblogger Theme for WordPress" href="http://www.copyblogger.com/the-copyblogger-theme-for-wordpress/">Copyblogger Theme</a> I&#8217;ll show you how to change this specific theme. But it&#8217;s no problem to translate the following to other themes.</p>
<p>Basically we&#8217;ll have to change three files: <code>functions.php</code>, <code>sidebar.php</code> and <code>nav_menu.php</code>; I think the latter is a specialty of the Copyblogger theme. First we&#8217;ll register a new sidebar called <em>menu</em> in the file <code>functions.php</code> like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="">'register_sidebar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    register_sidebar<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'name'</span><span style="color: #339933;">=&gt;</span><span style="">'sidebar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
    register_sidebar<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'name'</span><span style="color: #339933;">=&gt;</span><span style="">'menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>More likely than not you had a call to <code>register_sidebar</code> with no parameters: This registered your sidebar. As you can see we have to supply a name for each sidebar; make sure that each sidebar gets a unique name.</p>
<p>If your theme has got only one sidebar you have to change the file <code>sidebar.php</code> to pick up your original sidebar; if you already had more than one sidebar in your theme you can skip this step. Search for a call to <code>dynamic_sidebar</code> somewhere in the file <code>sidebar.php</code> and add the parameter <code>'sidebar'</code> to <code>dynamic_sidebar</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span> ||
    <span style="color: #339933;">!</span>dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="">'sidebar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span></pre></div></div>

<p>This is a line from my file and it might look different in yours. Just make sure that the code uses the <em>right</em> sidebar here.</p>
<p>Finally the users of the Copyblogger theme will change the <code>nav_menu.php</code>. Everybody else may place the following in the <code>header.php</code> or wherever the menu of your theme might be.</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="">'dynamic_sidebar'</span><span style="color: #009900;">&#41;</span> ||
      <span style="color: #339933;">!</span>dynamic_sidebar<span style="color: #009900;">&#40;</span><span style="">'menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;li&gt;&lt;a href=&quot;/&quot;&gt;home&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/archives/&quot;&gt;archives&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/about/&quot;&gt;about&lt;/a&gt;&lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #b1b100;">endif</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This snippet will display the widgets associated with the sidebar called <em>menu</em>. If there&#8217;re no widgets in this specific sidebar the three hardcoded links will show up.</p>
<h2>Customizing the menu</h2>
<p>Now that you&#8217;ve got a new sidebar called <em>menu</em> go to <em>Design - Widgets</em> and under <em>Current Widgets</em> select the menu sidebar in the drop down list and click on <em>Show</em>. Add as many Text widgets as you like and use them to link to sensible pages on your website. It&#8217;s as easy as this.</p>
<p>Since we leverage the power of the cool widget screen you can easily rearrange the order of your menu items by dragging them around. You can add other widgets to your menu as well - maybe that makes sense too.</p>
<h2>Conclusion</h2>
<p>As we&#8217;ve seen the concept of the sidebar is powerful and can be used for different purposes: one of them is a neat navigation menu. The new menu is easy to setup in any theme and I bet you&#8217;ll really like the way to manage it via the widget screen.</p>
<p>If you&#8217;d like to read more about the functions from the <a title=" Widgets API" href="http://codex.wordpress.org/Plugins/WordPress_Widgets_Api">Widgets API</a> used in this post I recommend having a look at <a title="register_sidebar" href="http://codex.wordpress.org/WordPress_Widgets_Api/register_sidebar">this</a> and <a title="dynamic_sidebar" href="http://codex.wordpress.org/WordPress_Widgets_Api/dynamic_sidebar">that</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/dynamic-navigation-menu-wordpress-theme/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Follow meta refresh with Commons HttpClient</title>
		<link>http://www.christianschenk.org/blog/follow-meta-refresh-with-commons-httpclient/</link>
		<comments>http://www.christianschenk.org/blog/follow-meta-refresh-with-commons-httpclient/#comments</comments>
		<pubDate>Thu, 22 May 2008 13:25:14 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[HowTo]]></category>

		<category><![CDATA[commons]]></category>

		<category><![CDATA[httpclient]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[meta refresh]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/follow-meta-refresh-with-commons-httpclient/</guid>
		<description><![CDATA[Want to follow HTTP redirects and meta refresh tags as well? Have a look at the code in this post.]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="H" class="cap"><span>H</span></span>aving to check the <code>title</code> tag from a lot of different websites I needed a tool that would follow HTTP redirects as well as redirects in meta refresh tags. As a fan of <a href="http://www.google.de/search?q=commons+httpclient&#038;btnI">Commons HttpClient</a>, I hacked together some code that does the work.</p>
<p>For the impatient: check out the code <a title="Code reference" href="http://data.christianschenk.org/follow-meta-refresh-with-commons-httpclient/xref/">here</a>.</p>
<h2>The code</h2>
<p>The actual code is very simple:</p>
<ol>
<li>it executes an HTTP GET with a given URL</li>
<li>follows HTTP redirects</li>
<li>finally downloads the contents</li>
<li>checks whether there&#8217;s a meta refresh tag
<ul>
<li>starts over at 1. if it found such a tag</li>
</ul>
</li>
<li>returns the content.</li>
</ol>
<p>Have a look at the class <code>TitleTest</code> <a title="Code reference" href="http://data.christianschenk.org/follow-meta-refresh-with-commons-httpclient/xref/">here</a>: the private method <code>doGet</code> will be most interesting. </p>
<h2>Conclusion</h2>
<p>I was wondering why there aren&#8217;t that many tools out there which support following meta refresh tags. Since a lot of websites use this feature I thought that this would be something a lot of people might want to use when testing their websites.<br />
Hacking a small tool that does the job is very easy though, as demonstrated in this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/follow-meta-refresh-with-commons-httpclient/feed/</wfw:commentRss>
		</item>
		<item>
		<title>German translation for xRecurseDiff</title>
		<link>http://www.christianschenk.org/blog/german-translation-for-xrecursediff/</link>
		<comments>http://www.christianschenk.org/blog/german-translation-for-xrecursediff/#comments</comments>
		<pubDate>Tue, 06 May 2008 07:09:06 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[diff]]></category>

		<category><![CDATA[translation]]></category>

		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/german-translation-for-xrecursediff/</guid>
		<description><![CDATA[If you'd like to compare the files from two directories you may want to try xRecurseDiff that comes with a German translation now]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span> was searching for a simple Windows application that compares the files of two directories. This comes in handy if you&#8217;ve downloaded an update to some Open Source software and want to know what exactly changed in the new version. So I found <a href="http://www.matteolucarelli.net/xrecursediff/index_en.htm">xRecurseDiff</a> and it was perfect for my situation.</p>
<p>Since there wasn&#8217;t a German translation for it I created one and the author, Matteo Lucarelli, integrated it into a new release. If you&#8217;re from Germany and don&#8217;t speak English you may want to give this software a shot.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/german-translation-for-xrecursediff/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Deleting files older than a week under Windows</title>
		<link>http://www.christianschenk.org/blog/deleting-files-older-than-a-week-under-windows/</link>
		<comments>http://www.christianschenk.org/blog/deleting-files-older-than-a-week-under-windows/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 08:15:59 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[HowTo]]></category>

		<category><![CDATA[at]]></category>

		<category><![CDATA[cron]]></category>

		<category><![CDATA[vbs]]></category>

		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/deleting-files-older-than-a-week-under-windows/</guid>
		<description><![CDATA[Shows how to use a Visual Basic script and the at command to delete files older than a week under Windows]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span>f you&#8217;re running a Windows server and want to delete files that are older than, say, a week then Visual Basic comes to the rescue. Once you&#8217;ve got the script running you&#8217;d like Windows to start it every day so you don&#8217;t have to do this manually: <code>at</code> will be your friend here.</p>
<h2>The Visual Basic script</h2>
<p>For everybody who knows how to program in Visual Basic it&#8217;s be pretty easy to write some lines that get the files from a certain directory and deletes them if they haven&#8217;t been changed for some time. For everybody else: just copy the following lines into a file e.g. <code>cleaner.vbs</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="vb vb" style="font-family:monospace;"><span style="color: #b1b100;">Dim</span> fso, f, f1, fc
<span style="color: #b1b100;">Set</span> fso = <span style="color: #b1b100;">CreateObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">Set</span> f = fso.<span style="color: #66cc66;">GetFolder</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;C:\foo\bar&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">Set</span> fc = f.<span style="color: #66cc66;">Files</span>
<span style="color: #b1b100;">For</span> Each f1 in fc
<span style="color: #b1b100;">If</span> <span style="color: #b1b100;">DateDiff</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;d&quot;</span>, f1.<span style="color: #66cc66;">DateLastModified</span>, <span style="color: #b1b100;">Now</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">7</span> <span style="color: #b1b100;">Then</span> f1.<span style="color: #66cc66;">Delete</span>
<span style="color: #b1b100;">Next</span></pre></td></tr></table></div>

<p>All that&#8217;s left to do is to change the folder in line 3 to whatever you want. Have a look at line 6 and adapt the number of days, currently <code>7</code>, to your needs.</p>
<h2>Running the script</h2>
<p>The <code>at</code> command helps you to run programs at a certain time. Let&#8217;s say we want to run our script every day at 22:30 o&#8217;clock (that&#8217;s 10:30PM) we would type this on the command prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="dos dos" style="font-family:monospace;">at <span style="color: #cc66cc;">22</span>:<span style="color: #cc66cc;">30</span> /every:M,T,W,Th,F,S,Su C:\Scripts\cleaner.vbs</pre></div></div>

<p>If we just type <code>at</code> we&#8217;ll see all the tasks that are planned for execution.</p>
<h2>Conclusion</h2>
<p>With a simple Visual Basic script and the <code>at</code> command it&#8217;s pretty easy to run recurring tasks under Windows. If your Windows server is running longer than a week without rebooting this is a really nice solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/deleting-files-older-than-a-week-under-windows/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Remove clutter from WordPress 2.5</title>
		<link>http://www.christianschenk.org/blog/remove-clutter-from-wordpress-25/</link>
		<comments>http://www.christianschenk.org/blog/remove-clutter-from-wordpress-25/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 08:20:11 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[HowTo]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/remove-clutter-from-wordpress-25/</guid>
		<description><![CDATA[This post shows you how to remove some clutter from your WordPress installation. Namely: RSD and WLW links and some JavaScript.]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="A" class="cap"><span>A</span></span>fter upgrading to WordPress 2.5 I noticed some extra tags in the HTML header of my site: an RSD link, a link to the <code>wlwmanifest.xml</code> and a meta tag with the name set to <code>generator</code>.</p>
<p>While the link tags were introduced in an earlier release of WordPress the meta tag came with 2.5. I&#8217;m pretty sure that the latter will cause incompatibilities with a lot of existing themes that have a line like this in their <code>header.php</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;generator&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;WordPress 2.3.2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- leave this for stats please --&gt;</span></pre></div></div>

<p>In this post I&#8217;d like to share my ideas about removing some clutter from your WordPress installation. Namely:</p>
<ul>
<li>RSD and WLW links</li>
<li>generator tag</li>
<li>some JavaScript</li>
</ul>
<h2>Removing RSD and WLW links</h2>
<p>Getting rid of this stuff is as easy as using the <a title="WLW Disabler" href="http://www.planetmike.com/plugins/wlw-disabler/">WLW Disabler</a> plugin. After installing and activating the plugin the links are gone. That&#8217;s great.</p>
<h2>Getting rid of the generator tag</h2>
<p>Maybe it&#8217;s possible to write a nice plugin that removes this tag too but a quick and dirty hack is to edit the file <code>general-template.php</code>; you can find it in <code>wp-includes</code>. On line 1187 set the variable <code>$gen</code> to an empty string:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #000088;">$gen</span> <span style="color: #339933;">=</span> <span style="">''</span>;
<span style="color: #b1b100;">return</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;get_the_generator_{$type}&quot;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// ...</span></pre></div></div>

<p>If you come up with a clean solution to this please <a href="#respond">leave a comment</a>.</p>
<h2>Living without JavaScript</h2>
<p>Although this isn&#8217;t really related to the previous two I&#8217;d like to share this one too. I&#8217;ve disabled most of the JavaScript that comes with WordPress quite some time ago. And it works great for me: especially the admin pages load a lot faster.</p>
<p>Furthermore I got rid of <a href="http://www.prototypejs.org/">Prototype</a> that doesn&#8217;t play well with SmoothGallery. So, if you&#8217;re using the <a href="http://www.christianschenk.org/projects/wordpress-smoothgallery-plugin/">WordPress SmoothGallery plugin</a> this might be something for you.</p>
<p>Just open the file <code>wp-includes/script-loader.php</code> and comment out the line saying:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">default_scripts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>That&#8217;s it.</p>
<h2>Conclusion</h2>
<p>If you don&#8217;t use Windows Live Writer and the like, using WLW Disabler should be of no harm for you. Disabling some of the JavaScript will give you a performance boost but you&#8217;ll lose some of the nifty features at the same time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/remove-clutter-from-wordpress-25/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Analyzing letter frequencies of RFCs</title>
		<link>http://www.christianschenk.org/blog/analyzing-letter-frequencies-of-rfcs/</link>
		<comments>http://www.christianschenk.org/blog/analyzing-letter-frequencies-of-rfcs/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 09:15:17 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[Projects]]></category>

		<category><![CDATA[frequencies]]></category>

		<category><![CDATA[letter]]></category>

		<category><![CDATA[rfc]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/analyzing-letter-frequencies-of-rfcs/</guid>
		<description><![CDATA[I analyzed the letter frequencies of RFCs and related text files and came to reasonable results]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="L" class="cap"><span>L</span></span>ately, I wrote a small <a href="http://www.christianschenk.org/projects/charactercounter/">tool</a> that analyzes letter frequencies of arbitrary text files. So I decided to download all <a href="http://www.rfc-editor.org/">RFC</a>&#8217;s and give the tool a try. I was wondering whether the relative frequencies of letters in the RFC&#8217;s would match those shown on <a href="http://en.wikipedia.org/wiki/Letter_frequencies">Wikipedia</a> or <a href="http://www.google.com/search?q=letter+frequencies">other</a> websites.</p>
<h2>Results</h2>
<p>I ran a case insensitive analysis on 2833 RFC&#8217;s and related text documents from <a href="http://www.rfc-editor.org/">here</a>; all in all 169 MB. This took quite some time but once it was done I had these results, sorted by:</p>
<ul>
<li><a href="http://data.christianschenk.org/analyzing-letter-frequencies-of-rfcs/stat-alph.txt?keepThis=true&#038;TB_iframe=true&#038;height=350&#038;width=200" title="Letter frequencies" class="thickbox">letters</a></li>
<li><a href="http://data.christianschenk.org/analyzing-letter-frequencies-of-rfcs/stat-count.txt?keepThis=true&#038;TB_iframe=true&#038;height=350&#038;width=200" title="Letter frequencies" class="thickbox">occurrence count</a></li>
</ul>
<p>I didn&#8217;t thought that I might come up with nice results so easily.</p>
<p><img src="http://data.christianschenk.org/analyzing-letter-frequencies-of-rfcs/lorfc2.png" alt="Letter frequencies" /></p>
<p>The most frequently used characters make up the word <em>etiansor</em> here.</p>
<h2>Conclusion</h2>
<p>If I seriously wanted to analyze the letter frequencies of a text corpus I would implement some sort of filtering. But without that I got reasonable results too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/analyzing-letter-frequencies-of-rfcs/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Iterating over the characters in a string</title>
		<link>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/</link>
		<comments>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 14:20:19 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[characters]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[performance]]></category>

		<category><![CDATA[string]]></category>

		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/</guid>
		<description><![CDATA[Evaluates the fastest method to iterate over the characters in a string with Java]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span> was wondering what would be the fastest method to iterate over the characters in a string with Java. A small test implements the following things:</p>
<ul>
<li>using an Iterable</li>
<li><code>toCharArray()</code></li>
<li><code>charAt()</code></li>
<li>calling <code>charAt()</code> on a <code>CharSequence</code></li>
</ul>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/StringIterator-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/StringIterator-1.0.zip">zip</a> or browse the code online <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/xref/">here</a>; have a look at the <code>StringIteratorTest</code> class first.</p>
<h2>Results</h2>
<p>Without further ado here are the results:</p>
<table>
<tr>
<th>Variant</th>
<th>Time in ms.</th>
</tr>
<tr>
<td>Iterator</td>
<td>5.5</td>
</tr>
<tr>
<td><code>toCharArray()</code></td>
<td>1.1</td>
</tr>
<tr>
<td><code>charAt()</code></td>
<td>1.6</td>
</tr>
<tr>
<td><code>CharSequence.charAt()</code></td>
<td>2.2</td>
</tr>
</table>
<p><img src="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/stringIterator.jpg" alt="Bar chart of the test results"/></p>
<h2>Conclusion</h2>
<p>If speed is what you want you shouldn&#8217;t use Iterators but one of the other solutions instead. On the other hand if you <em>just like</em> to play with Iterators and classes that implement <code>Iterable</code> you may want to choose the <em>slower</em> solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Caching with dynamic proxy classes</title>
		<link>http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/</link>
		<comments>http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 09:25:10 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[cache]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/</guid>
		<description><![CDATA[Read about a solution to cache objects with a dynamic proxy class]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span>n my <a title="Caching with AspectJ" href="http://www.christianschenk.org/blog/caching-with-aspectj/">last post</a> I used AspectJ to implement a cache that stored the returned result of methods with a special annotation (<code>@Cachable</code>). If you can&#8217;t use AspectJ you may want to use a dynamic proxy class: in this post I&#8217;ll present a solution for this.</p>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/caching-with-dynamic-proxy-classes/CachingWithProxyInstances-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/caching-with-dynamic-proxy-classes/CachingWithProxyInstances-1.0.zip">zip</a> file or view the code online <a href="http://data.christianschenk.org/caching-with-dynamic-proxy-classes/xref/">here</a>.</p>
<h2>Implementation</h2>
<p>If you don&#8217;t know how <a title="Dynamic Proxy Classes" href="http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html">dynamic proxy classes</a> work here&#8217;s a short overview. Let&#8217;s say you want to do some extra work if the methods <code>foo</code> and <code>bar</code> from the class <code>Tee</code> are called. You would extract the methods into an interface and let <code>Tee</code> implement this interface.</p>
<p>Next, you&#8217;d implement a factory that produces a proxy instance for <code>Tee</code> with a custom <code>InvocationHandler</code>. This handler would have a look at the method&#8217;s name and check whether it&#8217;s <code>foo</code> or <code>bar</code>: you can now implement any extra actions in this handler.</p>
<p>You can also examine the annotations of the invoked method and that&#8217;s what I did: if the method has got the <code>@Cachable</code> annotation we&#8217;ll utilize a cache. But how do we know whether we can safely return an object from the cache?</p>
<h3>Constructing a unique method identifier</h3>
<p>This is crucial since we don&#8217;t want to return the same result from the cache if the method was called with different parameters. So we&#8217;ll have to add the values of the parameters to a identifier like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;package-name&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;class-name&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;method-name&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;param1-param2-[...]&quot;</span></pre></div></div>

<p>This way we&#8217;ll create a unique entry in the cache for different method calls.</p>
<h2>How to</h2>
<p>All we have to do is to add <code>@Cachable</code> to some methods:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Foo <span style="color: #009900;">&#123;</span>
  @Cachable
  <span style="color: #000000; font-weight: bold;">public</span> SomeObject foo<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span>;
  @Cachable
  <span style="color: #000000; font-weight: bold;">public</span> AnotherObject bar<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> param1, <span style="color: #000066; font-weight: bold;">long</span> param2<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once we&#8217;ve done that we can use the factory to produce a new proxy instance with our custom <code>InvocationHandler</code>. The handler will use the cache, i.e. the method calls will return faster.</p>
<h2>Conclusion</h2>
<p>In this post I presented a simple solution for a cache that may speed up method calls. Although I recommend using AspectJ for this kind of job you can use dynamic proxy instances if your environment (in most cases read: your project leader) doesn&#8217;t permit you to use AspectJ.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/caching-with-dynamic-proxy-classes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Caching with AspectJ</title>
		<link>http://www.christianschenk.org/blog/caching-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/caching-with-aspectj/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 09:28:59 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[aop]]></category>

		<category><![CDATA[aspectj]]></category>

		<category><![CDATA[cache]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/caching-with-aspectj/</guid>
		<description><![CDATA[Shows how to use AspectJ and annotations to implement a simple cache that speeds up your application]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span>n my <a title="Implementing a simple cache with Java" href="http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/">last post</a> I presented a very simple cache that stores objects. Now I want to use this cache with <a href="http://www.eclipse.org/aspectj/">AspectJ</a> in a small sample application.</p>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/caching-with-aspectj/CachingWithAspectJ-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/caching-with-aspectj/CachingWithAspectJ-1.0.zip">zip</a> file or browse the code online <a href="http://data.christianschenk.org/caching-with-aspectj/xref/">here</a>.</p>
<h2>Implementing the aspect</h2>
<p>I decided to use an annotation and an aspect with a pointcut that matches this annotation. All I had to do was to annotate the methods that should be cached.</p>
<p>Here&#8217;s the very generic version of a pointcut that matches the execution of all methods annotated with <code>@Cachable</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(@Cachable * *.*(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> cache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The logic is this simple:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cache()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> aroundProfileMethods<span style="color: #009900;">&#40;</span>ProceedingJoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// construct genericIdentifier</span>
  <span style="color: #003399;">Object</span> obj <span style="color: #339933;">=</span> cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>genericIdentifier<span style="color: #009900;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    obj <span style="color: #339933;">=</span> thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>genericIdentifier, obj<span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">return</span> obj;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Everytime a cachable method is executed we&#8217;ll first have a look at the cache. If there&#8217;s a cache hit we&#8217;ll immediately return the object. If there&#8217;s no such object in the cache, we&#8217;ll call the cachable method, put the returned object in the cache and finally return the object.</p>
<p>The <code>genericIdentifier</code> is a string consisting of the package-, class- and method-name and the supplied arguments. For a method named <code>foo</code> from a class <code>bar</code> inside a package called <code>baz</code> this might result in:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;baz.bar.foo-Integer-42&quot;</span></pre></div></div>

<p>The method <code>foo</code> takes one argument and at the time of this execution it was <code>42</code>. This way we&#8217;ll only return objects from the cache if this <code>generic identifier</code> matches.</p>
<h2>Using the aspect</h2>
<p>All we have to do is to use the <code>@Cachable</code> annotation. Hava a look at it:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> Cachable <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>It can be used with any method that returns something:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">@Cachable
<span style="color: #000000; font-weight: bold;">public</span> BigObject getObject<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> BigObject<span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first time this method is called we&#8217;ll store the object in the cache. If it&#8217;s called again with the same parameter we&#8217;ll return the object from the cache.</p>
<h2>Conclusion</h2>
<p>Implementing a cache is simple and so is using it with AspectJ. I presented a straightforward way to speed up any application that deals with <em>big</em> objects which take a long time to load. All you have to do is to use the <code>@Cachable</code> annotation along with the aspect. Implementing another cache in the aspect should be simple, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/caching-with-aspectj/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Implementing a simple cache with Java</title>
		<link>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/</link>
		<comments>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 14:38:24 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[cache]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/</guid>
		<description><![CDATA[I present a simple cache written in Java that can be used to store objects of any kind]]></description>
			<content:encoded><![CDATA[<p class="first-child "><span title="I" class="cap"><span>I</span></span>n this post I&#8217;ll present a simple cache written in Java. It can be used to store objects of any kind or specific types only. The objects will expire after a certain time that can be customized too. Since this cache is really simple I didn&#8217;t implement a caching strategy nor an option for a maximum capacity.</p>
<p>You can download the Eclipse project as <a href="http://data.christianschenk.org/implementing-a-simple-cache-with-java/SimpleCache-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/implementing-a-simple-cache-with-java/SimpleCache-1.0.tar.gz">zip</a> or browse the code online <a href="http://data.christianschenk.org/implementing-a-simple-cache-with-java/xref/">here</a>.</p>
<h2>Basic operations</h2>
<p>The cache supports these basic operations: you can <code>put</code> and <code>get</code> objects. If you put an object you can specify a time in seconds - after this time period the object will be remove from the cache.</p>
<p>Getting an object from the cache is simple too and you&#8217;ve also got the possibility to specify the type of the object you&#8217;re retrieving from the cache - this adds some type safety.</p>
<p>Have a look at this sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">SimpleCache<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> intCache <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCache<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
intCache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>, intCache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This creates a <code>SimpleCache</code> that holds objects derived from <code>Integer</code>. Since you parameterized this cache with <code>Integer</code> you&#8217;ll only be able to <code>put</code> and <code>get</code> integers.</p>
<p>If you want to save all different kinds of objects in the cache that&#8217;s no problem at all. If you do this you can use the convenience method <code>get</code> which performs a cast for you. Have a look at this example:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">SimpleCache cache <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
cache.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str&quot;</span>, <span style="color: #0000ff;">&quot;Some string&quot;</span><span style="color: #009900;">&#41;</span>;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>, cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #003399;">Integer</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Some string&quot;</span>, cache.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>However, I think it&#8217;s better to create several instances of <code>SimpleCache</code> that stores objects of a specific type.</p>
<h2>Removing expired objects</h2>
<p>Once an object is expired we want to remove it from the cache. The <code>SimpleCache</code> class supports two ways of removing old objects:</p>
<ol>
<li>if the <code>get</code> method is called we&#8217;ll check whether the object that&#8217;s about to be returned is expired. If that&#8217;s the case we&#8217;ll remove it from the cache.</li>
<li>a thread that runs periodically removes expired objects from the cache.</li>
</ol>
<p>Both features reside in implementations of <code>Runnable</code> so they can be executed in separate threads. This way the basic operations will not be slowed down by this kind of housekeeping.</p>
<p>With Java 5 it&#8217;s easy to schedule recurring tasks with the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executors.html">Executors</a> class. To remove expired objects from the cache we can use this code:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #003399;">Runnable</span> cleaner <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* remove expired objects here */</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;
Executors.<span style="color: #006633;">newScheduledThreadPool</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">scheduleWithFixedDelay</span><span style="color: #009900;">&#40;</span>cleaner, 0, <span style="color: #cc66cc;">30</span>, TimeUnit.<span style="color: #006633;">SECONDS</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h2>Conclusion</h2>
<p>I presented a very simple cache written in Java that stores objects of any kind. It&#8217;s configurable, type safe and easy to use; it should be pretty easy to integrate this cache into an existing project.</p>
<p>I&#8217;ve tried to keep the cache fast: certain tasks will be executed in threads to speed things up. But don&#8217;t worry because the threads are easy to maintain and to understand.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/implementing-a-simple-cache-with-java/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.105 seconds -->
<!-- Cached page served by WP-Super-Cache -->
<!-- Compression = gzip -->