<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8988177719810230455</id><updated>2012-02-15T22:34:46.968-08:00</updated><category term='ruby'/><category term='yahoo'/><category term='jsf tag custom'/><category term='javascript'/><category term='perl'/><category term='boost'/><category term='gwt'/><category term='web development'/><category term='web service client'/><category term='HTTP'/><category term='memcache'/><category term='c#'/><category term='www'/><category term='war card game'/><category term='css'/><category term='python'/><category term='shell'/><category term='Examples-HelloWorld'/><category term='canvas'/><category term='JSON'/><category term='c++'/><category term='stl'/><category term='javafx'/><category term='hibernate'/><category term='directx'/><category term='cuda'/><category term='java'/><category term='mysql'/><category term='cygwin'/><category term='php'/><category term='parse'/><category term='jstl'/><category term='web stack'/><category term='URL'/><category term='games'/><category term='algorithm'/><category term='google web toolkit'/><category term='netbeans'/><category term='random number'/><category term='vb'/><category term='visual studio'/><category term='regex'/><category term='dns'/><category term='ntlm'/><category term='sql'/><category term='jpa'/><category term='wxwidgets'/><category term='html'/><category term='ccr'/><category term='twitter'/><category term='microsoft'/><category term='reading list'/><category term='detours'/><category term='Protocol Buffers'/><category term='command line'/><title type='text'>Developer Resource</title><subtitle type='html'>Topics for professional software engineers</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default?start-index=101&amp;max-results=100'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>107</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5914584214413645255</id><published>2011-01-19T17:16:00.001-08:00</published><updated>2011-01-19T17:16:37.804-08:00</updated><title type='text'>Visual studio resolve usings.</title><content type='html'>I had the absolute joy of hanging out at the Tampa .NET Developer Group Meeting last night. Here is a tip I mentioned briefly at the meeting around resolving and optmizing namespaces.&lt;br /&gt;&lt;br /&gt;I know a lot of us are hardcore ReSharper users and can do all kinds of jedi-like moves in the Visual Studio 2008 IDE. However, get us without ReSharper at times and we just stare at the keyboard in bewilderment wondering why namespaces aren't resolving, usings are being optimized, and refactorings are not taking place at blinding speeds, etc.&lt;br /&gt;&lt;br /&gt;It turns out, Visual Studio 2008 actually has good support for resolving namespaces and optmizing using statements that can get you the functionality if you are not using ReSharper.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Resolving Namespaces&lt;br /&gt;&lt;br /&gt;When you are writing code and Visual Studio places a small red notification rectangle at the end of the class,&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Pressing Ctrl + . will bring up a context-sensitive menu that allows you to add a using statement or optionally fully qualify the path to the class.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Clicking the Enter Key will automatically add using System.Collections.Generic; with the other using statements, requiring no touch of the mouse.&lt;br /&gt;&lt;br /&gt;This also works with attributes as well. Adding an attribute that needs a using statement to qualify its namespace will cause the same red notification rectangle to appear:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;and pressing Ctrl + . will allow you to add the using statement, etc.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Optimizing, Removing, and Sorting Unused Using Statements&lt;br /&gt;&lt;br /&gt;The other nice thing that ReSharper does is remove unused using statements using Ctrl+Alt+O.&lt;br /&gt;&lt;br /&gt;We can get that using Visual Studio 2008, because you may have noticed the cool context-sensitive Organize Usings Option:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Very, very cool, but this needs to have a shortcut because we will be using it often. I want to use the familar Ctrl+Alt+O shortcut that I get from ReSharper ( you can choose your own ) so I need to map the shortcut to the Edit.RemoveAndSort Command in the keyboard options:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Now when I type Ctrl+Alt+O in the code editor it will remove all unused using statements in the current file as well as sort those using statements that are being used.&lt;br /&gt;&lt;br /&gt;Very cool! Hope this helps.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5914584214413645255?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5914584214413645255/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5914584214413645255' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5914584214413645255'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5914584214413645255'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2011/01/visual-studio-resolve-usings.html' title='Visual studio resolve usings.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-203737309267236039</id><published>2011-01-03T22:56:00.000-08:00</published><updated>2011-01-03T22:58:13.579-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='www'/><category scheme='http://www.blogger.com/atom/ns#' term='memcache'/><title type='text'>A simple distributed lock with memcached</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Lucida Grande', 'Lucida Sans Unicode', helvetica, verdana, arial, sans-serif; font-size: 12px; color: rgb(51, 51, 51); line-height: 17px; "&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;When you have a cluster of web application servers, you often need to coordinate the activity of your servers to avoid the same expensive work being done at the same time when a condition triggers it.&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;Most people use &lt;a href="http://code.google.com/p/memcached/" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: rgb(0, 90, 140); text-decoration: underline; "&gt;memcached&lt;/a&gt; as a simple key/value store but it can also be used as a simple distributed lock manager: along with the put(key, value) operation, it also has an add(key, value) operation that succeeds only if the cache wasn't already holding a value for the key.&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;Locking then becomes easy:&lt;/p&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;if (cache.add("lock:xyz", "1", System.currentTimeMillis() + 60000)) &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;{   &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;try &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{&lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;     doSomeExpensiveStuff();  &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;} &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;finally &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;{  &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;   cache.delete("lock:xyz");   &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;} &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;} else {&lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;   // someone else is doing the expensive stuff &lt;/pre&gt;&lt;pre style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&gt;} &lt;/pre&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;The code above tries to get the lock by adding a dumb value for our lock's idenfitier, with an expiration of one minute. This is the lock lease time, and should be more than the estimated maximum time for the lengthy operation. This avoids the lock being held forever if ever things go really bad such as your server crashing.&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;Once the operation is completed, we delete the lock, et voilà.&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;If you want the system to be rock-solid, you should check that you still own the lock before deleting it (in case the lease time expired), but in most cases this simple approach works nicely.&lt;/p&gt;&lt;p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; "&gt;And if the expensive operation resets in the database the condition that triggered it, the lock should be released once the transaction has been committed to prevent a race condition in the time interval between the end of the expensive operation and the actual commit that would allow other servers to restart the same work. &lt;a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/transaction/support/TransactionSynchronization.html" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: rgb(0, 90, 140); text-decoration: underline; "&gt;Spring's transaction synchronization&lt;/a&gt; helps doing that.&lt;/p&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-203737309267236039?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/203737309267236039/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=203737309267236039' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/203737309267236039'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/203737309267236039'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2011/01/simple-distributed-lock-with-memcached.html' title='A simple distributed lock with memcached'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-2073453642920111118</id><published>2011-01-03T22:55:00.000-08:00</published><updated>2011-01-03T22:56:16.886-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='HTTP'/><category scheme='http://www.blogger.com/atom/ns#' term='URL'/><title type='text'>Registering an Application to a URL protocol</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 13px; "&gt;&lt;h1 class="title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; font-family: 'Segoe UI', Verdana, Arial; font-size: 1.769em; color: rgb(63, 82, 156); font-weight: bold; font-style: normal; font-variant: normal; line-height: normal; "&gt;Registering an Application to a URL Protocol&lt;/h1&gt;&lt;div class="clsDocBody"&gt;&lt;p&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/aa767916(v=VS.85).aspx" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;About Asynchronous Pluggable Protocols&lt;/a&gt; article describes how to develop handlers for URL protocols. In some cases, it may be desirable to invoke another application to handle a custom protocol. To do so, register the existing application as a URL Protocol handler. Once the application has successfully launched, it can use command-line parameters to retrieve the URL that launched it. These settings apply to protocol handlers launched from within Windows Internet Explorer and from Windows Explorer using the &lt;strong&gt;Run...&lt;/strong&gt; command (Windows logo key+R).&lt;/p&gt;&lt;div class="clsNote" style="background-color: rgb(238, 238, 238); margin-bottom: 4px; padding-top: 2px; padding-right: 2px; padding-bottom: 2px; padding-left: 2px; "&gt;&lt;strong&gt;&lt;img id="UI_security_bang_16" alt="security note" src="http://i.msdn.microsoft.com/dynimg/IC105029.gif" title="security note" xmlns="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /&gt; Security Alert&lt;/strong&gt;  Applications that handle URL protocols must consider how to respond to malicious data. Because handler applications can receive data from untrusted sources, the URL and other parameter values passed to the application may contain malicious data that attempts to exploit the handling application.&lt;/div&gt;&lt;p&gt;This topic contains the following sections:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx#app_reg" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;Registering the Application Handling the Custom Protocol&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx#url_inv" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;Launching the Handler&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx#prot_sec" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;Security Issues&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx#app_ex" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;Example Protocol Handler&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx#related_topics" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;Related Topics&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2 style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: rgb(63, 82, 156); font-weight: bold; "&gt;&lt;a id="app_reg"&gt;&lt;/a&gt;Registering the Application Handling the Custom Protocol&lt;/h2&gt;&lt;p&gt;To register an application to handle a particular URL protocol, add a new key, along with the appropriate subkeys and values, to HKEY_CLASSES_ROOT. The root key must match the protocol scheme that is being added. For instance, to add an "alert:" protocol, add an &lt;strong&gt;alert&lt;/strong&gt; key to HKEY_CLASSES_ROOT, as follows:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div&gt;&lt;strong&gt;HKEY_CLASSES_ROOT&lt;/strong&gt;&lt;br /&gt;     &lt;strong&gt;&lt;em&gt;alert&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;          URL Protocol&lt;strong&gt; = &lt;/strong&gt;""&lt;/div&gt;&lt;p&gt;Under this new key, the &lt;strong&gt;URL Protocol&lt;/strong&gt; string value indicates that this key declares a custom protocol handler. Without this key, the handler application will not launch. The value should be an empty string.&lt;/p&gt;&lt;p&gt;Keys should also be added for &lt;strong&gt;DefaultIcon&lt;/strong&gt; and &lt;strong&gt;shell&lt;/strong&gt;. The Default string value of the &lt;strong&gt;DefaultIcon&lt;/strong&gt; key must be the file name to use as an icon for this new URL protocol. The string takes the form "path, iconindex" with a maximum length of MAX_PATH. The name of the first key under the &lt;strong&gt;shell&lt;/strong&gt; key should be an action verb, such as &lt;strong&gt;open&lt;/strong&gt;. Under this key, a &lt;strong&gt;command&lt;/strong&gt; key or a&lt;strong&gt;DDEEXEC&lt;/strong&gt; key indicate how the handler should be invoked. The values under the &lt;strong&gt;command&lt;/strong&gt; and &lt;strong&gt;DDEEXEC&lt;/strong&gt; keys describe how to launch the application handling the new protocol.&lt;/p&gt;&lt;p&gt;Finally, the &lt;strong&gt;Default&lt;/strong&gt; string value should contain the display name of the new protocol. The following example shows how to register an application, alert.exe in this case, to handle the &lt;strong&gt;alert&lt;/strong&gt;protocol.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div&gt;&lt;strong&gt;HKEY_CLASSES_ROOT&lt;/strong&gt;&lt;br /&gt;     &lt;strong&gt;&lt;em&gt;alert&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;          (Default) = "URL:Alert Protocol"&lt;br /&gt;          URL Protocol&lt;strong&gt; = &lt;/strong&gt;""&lt;br /&gt;          &lt;strong&gt;DefaultIcon&lt;/strong&gt;&lt;br /&gt;               (Default) = "alert.exe,1"&lt;br /&gt;          &lt;strong&gt;shell&lt;/strong&gt;&lt;br /&gt;               &lt;strong&gt;open&lt;/strong&gt;&lt;br /&gt;                    &lt;strong&gt;command&lt;/strong&gt;&lt;br /&gt;                         (Default) = "C:\Program Files\Alert\alert.exe" "%1"&lt;/div&gt;&lt;p&gt;When a user clicks a link registered to your custom URL protocol, Internet Explorer launches the registered URL protocol handler. If the specified &lt;strong&gt;open&lt;/strong&gt; command specified in the registry contains a &lt;strong&gt;%1&lt;/strong&gt; parameter, Internet Explorer passes the URL to the registered protocol handler application.&lt;/p&gt;&lt;h2 style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: rgb(63, 82, 156); font-weight: bold; "&gt;&lt;a id="url_inv"&gt;&lt;/a&gt;Launching the Handler&lt;/h2&gt;&lt;p&gt;By adding the above settings to the registry, navigating to URLs such as &lt;code&gt;alert:Hello%20World&lt;/code&gt; would cause an attempt to launch alert.exe with the complete URL on the command line. Internet Explorer decodes the URL, but the Windows &lt;strong&gt;Run...&lt;/strong&gt; command does not. If a URL contains spaces, it may be split across more than one argument on the command line.&lt;/p&gt;&lt;p&gt;For example, if the link above is followed through Internet Explorer, the command line would be:&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns="" style="min-width: 260px; "&gt;&lt;a name="CodeSpippet0"&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: rgb(229, 229, 229); border-right-style: solid; border-right-width: 1px; border-right-color: rgb(229, 229, 229); clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: rgb(229, 229, 229); border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 3px; border-right-color: rgb(229, 229, 229); "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: rgb(255, 255, 255); width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="Copy to clipboard." href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx" style="color: rgb(19, 100, 196); text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;Copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode0" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;&lt;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; overflow-x: auto; overflow-y: auto; "&gt;"C:\Program Files\Alert\alert.exe" "alert:Hello World" &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;If this link is followed through Windows Explorer, the Windows &lt;strong&gt;Run&lt;/strong&gt; command, or some other application, the command line would be:&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns="" style="min-width: 260px; "&gt;&lt;a name="CodeSpippet1"&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: rgb(229, 229, 229); border-right-style: solid; border-right-width: 1px; border-right-color: rgb(229, 229, 229); clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: rgb(229, 229, 229); border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 3px; border-right-color: rgb(229, 229, 229); "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: rgb(255, 255, 255); width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="Copy to clipboard." href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx" style="color: rgb(19, 100, 196); text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;Copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode1" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;&lt;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; overflow-x: auto; overflow-y: auto; "&gt;"C:\Program Files\Alert\alert.exe" "alert:Hello%20World" &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;Because Internet Explorer will decode all percent-encoded octets in the URL before passing the URL to &lt;a href="http://msdn.microsoft.com/en-us/library/cc422072.aspx" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;ShellExecute&lt;/a&gt;, URLs such as &lt;code&gt;alert:%3F?&lt;/code&gt; will be given to the alert application protocol handler as &lt;code&gt;alert:??&lt;/code&gt;. The handler won't know that the first question mark was percent-encoded. To avoid this issue, application protocol handlers and their associated URL scheme must not rely on encoding. If encoding is necessary, protocol handlers should use another type of encoding that is compatible with URL syntax, such as Base64 encoding. Double percent-encoding is not a perfect solution either; if the application protocol URL isn't processed by Internet Explorer, it will not be decoded.&lt;/p&gt;&lt;p&gt;When &lt;strong&gt;ShellExecute&lt;/strong&gt; executes the application protocol handler with the URL on the command line, any non-encoded spaces, quotes, and slashes in the URL will be interpreted as part of the command line. This means that if you use C/C++'s &lt;a href="http://msdn.microsoft.com/en-us/library/88w63h9k(v=VS.85).aspx" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;argc and argv&lt;/a&gt; to determine the arguments passed to your application, the URL may be broken across multiple parameters. To mitigate this issue:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Avoid spaces, quotes, or backslashes in your URL&lt;/li&gt;&lt;li&gt;Quote the %1 in the registration ("%1" as written in the 'alert' example registration)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;However, avoidance doesn't completely solve the problem of quotes in the URL or a backslash at the end of the URL.&lt;/p&gt;&lt;h2 style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: rgb(63, 82, 156); font-weight: bold; "&gt;&lt;a id="prot_sec"&gt;&lt;/a&gt;Security Issues&lt;/h2&gt;&lt;p&gt;As noted above, the URL that is passed to an application protocol handler might be broken across multiple parameters. Malicious parties could use additional quote or backslash characters to pass additional command line parameters. For this reason, application protocol handlers should assume that any parameters on the command line could come from malicious parties, and carefully validate them. Applications that could initiate dangerous actions based on external data must first confirm those actions with the user. In addition, handling applications should be tested with URLs that are overly long or contain unexpected (or undesirable) character sequences.&lt;/p&gt;&lt;p&gt;For more information, please see &lt;a href="http://go.microsoft.com/fwlink/?linkid=203695" target="_top" style="color: rgb(19, 100, 196); text-decoration: none; "&gt;Writing Secure Code&lt;/a&gt;.&lt;/p&gt;&lt;h2 style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: rgb(63, 82, 156); font-weight: bold; "&gt;&lt;a id="app_ex"&gt;&lt;/a&gt;Example Protocol Handler&lt;/h2&gt;&lt;p&gt;The following sample code contains a simple C# console application demonstrating one way to implement a protocol handler for the &lt;strong&gt;alert&lt;/strong&gt; protocol.&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns="" style="min-width: 260px; "&gt;&lt;a name="CodeSpippet2"&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: rgb(229, 229, 229); border-right-style: solid; border-right-width: 1px; border-right-color: rgb(229, 229, 229); clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: rgb(229, 229, 229); border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 3px; border-right-color: rgb(229, 229, 229); "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: rgb(255, 255, 255); width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="Copy to clipboard." href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx" style="color: rgb(19, 100, 196); text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;Copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode2" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;&lt;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; overflow-x: auto; overflow-y: auto; "&gt;using System; using System.Collections.Generic; using System.Text;  namespace Alert {   class Program   {     static string ProcessInput(string s)     {        // TODO Verify and validate the input         // string as appropriate for your application.        return s;     }      static void Main(string[] args)     {       Console.WriteLine("Alert.exe invoked with the following parameters.\r\n");       Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);        Console.WriteLine("\n\nArguments:\n");       foreach (string s in args)       {         Console.WriteLine("\t" + ProcessInput(s));       }       Console.WriteLine("\nPress any key to continue...");       Console.ReadKey();     }   } } &lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt;When invoked with the URL &lt;code&gt;alert:"Hello%20World"&lt;/code&gt; (note extra quotes) from Internet Explorer, the program responds with:&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns="" style="min-width: 260px; "&gt;&lt;a name="CodeSpippet3"&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: rgb(229, 229, 229); border-right-style: solid; border-right-width: 1px; border-right-color: rgb(229, 229, 229); clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: rgb(229, 229, 229); border-left-style: solid; border-left-width: 1px; border-left-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 3px; border-right-color: rgb(229, 229, 229); "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: rgb(255, 255, 255); width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="Copy to clipboard." href="http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx" style="color: rgb(19, 100, 196); text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;Copy&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode3" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;&lt;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; overflow-x: auto; overflow-y: auto; "&gt;Alert.exe invoked with the following parameters.  Raw command-line:         "C:\Program Files\Alert\alert.exe" "alert:"Hello World""   Arguments:          alert:Hello         World  Press any key to continue... &lt;/pre&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-2073453642920111118?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/2073453642920111118/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=2073453642920111118' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2073453642920111118'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2073453642920111118'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2011/01/registering-application-to-url-protocol.html' title='Registering an Application to a URL protocol'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5218771301184522574</id><published>2010-12-29T17:35:00.001-08:00</published><updated>2010-12-29T17:35:33.497-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>How to make your ASP.Net label multiline (how to wrap text in your label)</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; line-height: 19px; "&gt;&lt;div class="entry-title" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 1.9em; font-weight: bold; letter-spacing: -1px; color: rgb(34, 34, 34); "&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="body" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: rgb(34, 34, 34); font-size: 12px; "&gt;First off, sorry for the absence. Things got a little busy in life as well as work. Not to mention having to get up to speed on 2.0 in a big hurry. All good things, but it left less time for fun stuff.&lt;br /&gt;&lt;br /&gt;With that said, I want to offer a little tip to help with formatting your ASP.Net pages. Getting a label to be multiline within a set width. Now many of you know this trick, but I always have to re-remember the trick every time I need it, so this post can be a reference for you as well.&lt;br /&gt;&lt;br /&gt;The trick is actually this - Don't use a label. Use a textbox instead. Since all the controls inherit from the same base class, the text box and the label are very similar anyway. But only the textbox has the multiline capability.&lt;br /&gt;&lt;br /&gt;Once you have the textbox on your page the trick is to make it look like a label for the end user. To do this you need to set the following properties (This is the part I can never remember):&lt;br /&gt;&lt;br /&gt;Wrap = true; (obviously)&lt;br /&gt;rows = {some number greater than 0} (I use the rows property rather than the height property as it tends to be earier to get the formatting right)&lt;br /&gt;ReadOnly = true (makes sense, right)&lt;br /&gt;TextMode = multiline (or there no real reason to wrap the text...)&lt;br /&gt;BorderStyle = None &lt;br /&gt;BorderWidth = 0&lt;br /&gt;&lt;br /&gt;Those last two are actually VERY important, and here is why. They get rid of that pesky border that you see around text boxes. That border is a common visual signal to the end user that says "ENTER INFO HERE!". If you leave the border up expect a lot of phone calls from folks saying "The web page is broken. It won't let me enter text."&lt;br /&gt;&lt;br /&gt;So use this in good faith. More controls soon. Even my first 2.0 controls.&lt;/div&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5218771301184522574?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5218771301184522574/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5218771301184522574' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5218771301184522574'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5218771301184522574'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/how-to-make-your-aspnet-label-multiline.html' title='How to make your ASP.Net label multiline (how to wrap text in your label)'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-528231818893008658</id><published>2010-12-21T17:33:00.000-08:00</published><updated>2010-12-21T17:34:39.892-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>C# Caching</title><content type='html'>&lt;div&gt;For asp.net apps there are many different kinds of caching available. This includes browser caching, partial page caching, data caching, and others.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The first and foremost is browser caching. Browsers will automatically cache references to your js, css, and images. Although you might need to add the following to your web.config to give it a hint about the js files:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;staticcontent&gt;&lt;/div&gt;&lt;div&gt;    &lt;remove fileextension=".js"&gt;&lt;/div&gt;&lt;div&gt;    &lt;mimemap mimetype="text/javascript" fileextension=".js"&gt;&lt;/div&gt;&lt;div&gt;&lt;/staticcontent&gt;&lt;/div&gt;&lt;div&gt;The can also have the following to give it a hint on how long to cache the static content:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;staticcontent&gt;&lt;/div&gt;&lt;div&gt;      &lt;clientcache cachecontrolmode="UseMaxAge" cachecontrolmaxage="00:00:15"&gt;&lt;/div&gt;&lt;div&gt;&lt;/staticcontent&gt;&lt;/div&gt;&lt;div&gt;For the actual pages, see one of the links above. You have pretty good control over what is and isn't cached in the app itself.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-528231818893008658?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/528231818893008658/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=528231818893008658' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/528231818893008658'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/528231818893008658'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/for-asp.html' title='C# Caching'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-2163846948620610382</id><published>2010-12-17T12:22:00.001-08:00</published><updated>2010-12-17T12:22:30.340-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='gwt'/><title type='text'>Hello World RPC Server</title><content type='html'>package org.mortbay.gwt.example.client;&lt;br /&gt;&lt;br /&gt;import com.google.gwt.user.client.rpc.RemoteService;&lt;br /&gt;&lt;br /&gt;public interface HelloWorldService extends RemoteService&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    public String sayHello(String sender);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;package org.mortbay.gwt.example.client;&lt;br /&gt;&lt;br /&gt;import com.google.gwt.user.client.rpc.RemoteService;&lt;br /&gt;&lt;br /&gt;public interface HelloWorldService extends RemoteService&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    public String sayHello(String sender);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;package org.mortbay.gwt.example.client;&lt;br /&gt;&lt;br /&gt;import com.google.gwt.user.client.rpc.AsyncCallback;&lt;br /&gt;&lt;br /&gt;public interface HelloWorldServiceAsync&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    public void sayHello(String sender, AsyncCallback callback);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;Using Continuations (RetryRequest) | jetty6&lt;br /&gt;&lt;br /&gt;package org.mortbay.gwt.example.server;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;&lt;br /&gt;import org.mortbay.gwt.AsyncRemoteServiceServlet;&lt;br /&gt;import org.mortbay.gwt.example.client.HelloWorldService;&lt;br /&gt;import org.mortbay.util.ajax.Continuation;&lt;br /&gt;import org.mortbay.util.ajax.ContinuationSupport;&lt;br /&gt;&lt;br /&gt;public class HelloWorldServiceImpl extends AsyncRemoteServiceServlet implements HelloWorldService&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    private static final long serialVersionUID = 1L;&lt;br /&gt;&lt;br /&gt;    public String sayHello(String sender)&lt;br /&gt;    {&lt;br /&gt;        HttpServletRequest request = getThreadLocalRequest();&lt;br /&gt;        Continuation continuation = ContinuationSupport.getContinuation(request, null);&lt;br /&gt;        if(continuation.isNew() || !continuation.isPending())&lt;br /&gt;        {&lt;br /&gt;            request.setAttribute("ts", Long.valueOf(System.currentTimeMillis()));&lt;br /&gt;            continuation.suspend(5000);&lt;br /&gt;        }&lt;br /&gt;        long elapsed = System.currentTimeMillis() - ((Long)request.getAttribute("ts")).longValue();&lt;br /&gt;        return "Hello world *" + sender + "* resumed after " + elapsed + " ms";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;Using Servlet-3.0 suspend/resume api | jetty7&lt;br /&gt;&lt;br /&gt;package org.mortbay.gwt.example.server;&lt;br /&gt;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import org.mortbay.gwt.AsyncRemoteServiceServlet;&lt;br /&gt;import org.mortbay.gwt.example.client.HelloWorldService;&lt;br /&gt;&lt;br /&gt;public class HelloWorldServiceImpl extends AsyncRemoteServiceServlet implements HelloWorldService&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    private static final long serialVersionUID = 1L;&lt;br /&gt;&lt;br /&gt;    public String sayHello(String sender)&lt;br /&gt;    {&lt;br /&gt;&lt;br /&gt;        HttpServletRequest request = getThreadLocalRequest();&lt;br /&gt;        HttpServletResponse response = getThreadLocalResponse();&lt;br /&gt;        System.err.println("HANDLING: " + request + " | " + request.hashCode() + " | " + response.hashCode());&lt;br /&gt;&lt;br /&gt;        if(request.isInitial())&lt;br /&gt;        {&lt;br /&gt;            request.setAttribute("ts", Long.valueOf(System.currentTimeMillis()));&lt;br /&gt;            request.suspend(5000);&lt;br /&gt;            return null;&lt;br /&gt;        }&lt;br /&gt;        // timed-out or resumed&lt;br /&gt;        System.err.println("RESPONDING");&lt;br /&gt;        long elapsed = System.currentTimeMillis() - ((Long)request.getAttribute("ts")).longValue();&lt;br /&gt;        return "Hello world *" + sender + "* resumed after " + elapsed + " ms";&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-2163846948620610382?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/2163846948620610382/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=2163846948620610382' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2163846948620610382'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2163846948620610382'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/hello-world-rpc-server.html' title='Hello World RPC Server'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6985725291359512517</id><published>2010-12-17T12:21:00.001-08:00</published><updated>2010-12-17T12:21:29.790-08:00</updated><title type='text'>SetWindowsHookEx</title><content type='html'>Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.&lt;br /&gt;&lt;br /&gt;Syntax&lt;br /&gt;&lt;br /&gt;Copy&lt;br /&gt;HHOOK WINAPI SetWindowsHookEx(&lt;br /&gt;  __in  int idHook,&lt;br /&gt;  __in  HOOKPROC lpfn,&lt;br /&gt;  __in  HINSTANCE hMod,&lt;br /&gt;  __in  DWORD dwThreadId&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;Parameters&lt;br /&gt;&lt;br /&gt;idHook [in]&lt;br /&gt;Type: int&lt;br /&gt;&lt;br /&gt;The type of hook procedure to be installed. This parameter can be one of the following values.&lt;br /&gt;&lt;br /&gt;Value Meaning&lt;br /&gt;WH_CALLWNDPROC&lt;br /&gt;4&lt;br /&gt;Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_CALLWNDPROCRET&lt;br /&gt;12&lt;br /&gt;Installs a hook procedure that monitors messages after they have been processed by the destination window procedure. For more information, see the CallWndRetProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_CBT&lt;br /&gt;5&lt;br /&gt;Installs a hook procedure that receives notifications useful to a CBT application. For more information, see the CBTProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_DEBUG&lt;br /&gt;9&lt;br /&gt;Installs a hook procedure useful for debugging other hook procedures. For more information, see the DebugProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_FOREGROUNDIDLE&lt;br /&gt;11&lt;br /&gt;Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time. For more information, see the ForegroundIdleProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_GETMESSAGE&lt;br /&gt;3&lt;br /&gt;Installs a hook procedure that monitors messages posted to a message queue. For more information, see the GetMsgProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_JOURNALPLAYBACK&lt;br /&gt;1&lt;br /&gt;Installs a hook procedure that posts messages previously recorded by a WH_JOURNALRECORD hook procedure. For more information, see the JournalPlaybackProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_JOURNALRECORD&lt;br /&gt;0&lt;br /&gt;Installs a hook procedure that records input messages posted to the system message queue. This hook is useful for recording macros. For more information, see the JournalRecordProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_KEYBOARD&lt;br /&gt;2&lt;br /&gt;Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_KEYBOARD_LL&lt;br /&gt;13&lt;br /&gt;Installs a hook procedure that monitors low-level keyboard input events. For more information, see the LowLevelKeyboardProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_MOUSE&lt;br /&gt;7&lt;br /&gt;Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_MOUSE_LL&lt;br /&gt;14&lt;br /&gt;Installs a hook procedure that monitors low-level mouse input events. For more information, see the LowLevelMouseProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_MSGFILTER&lt;br /&gt;-1&lt;br /&gt;Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. For more information, see the MessageProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_SHELL&lt;br /&gt;10&lt;br /&gt;Installs a hook procedure that receives notifications useful to shell applications. For more information, see the ShellProc hook procedure.&lt;br /&gt;&lt;br /&gt;WH_SYSMSGFILTER&lt;br /&gt;6&lt;br /&gt;Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. The hook procedure monitors these messages for all applications in the same desktop as the calling thread. For more information, see the SysMsgProc hook procedure.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;lpfn [in]&lt;br /&gt;Type: HOOKPROC&lt;br /&gt;&lt;br /&gt;A pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.&lt;br /&gt;&lt;br /&gt;hMod [in]&lt;br /&gt;Type: HINSTANCE&lt;br /&gt;&lt;br /&gt;A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.&lt;br /&gt;&lt;br /&gt;dwThreadId [in]&lt;br /&gt;Type: DWORD&lt;br /&gt;&lt;br /&gt;The identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.&lt;br /&gt;&lt;br /&gt;Return Value&lt;br /&gt;&lt;br /&gt;Type: HHOOK&lt;br /&gt;&lt;br /&gt;If the function succeeds, the return value is the handle to the hook procedure.&lt;br /&gt;&lt;br /&gt;If the function fails, the return value is NULL. To get extended error information, call GetLastError.&lt;br /&gt;&lt;br /&gt;Remarks&lt;br /&gt;&lt;br /&gt;SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.&lt;br /&gt;&lt;br /&gt;An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process.&lt;br /&gt;&lt;br /&gt;Calling the CallNextHookEx function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.&lt;br /&gt;&lt;br /&gt;Before terminating, an application must call the UnhookWindowsHookEx function to free system resources associated with the hook.&lt;br /&gt;&lt;br /&gt;The scope of a hook depends on the hook type. Some hooks can be set only with global scope; others can also be set for only a specific thread, as shown in the following table.&lt;br /&gt;&lt;br /&gt;Hook Scope&lt;br /&gt;WH_CALLWNDPROC Thread or global&lt;br /&gt;WH_CALLWNDPROCRET Thread or global&lt;br /&gt;WH_CBT Thread or global&lt;br /&gt;WH_DEBUG Thread or global&lt;br /&gt;WH_FOREGROUNDIDLE Thread or global&lt;br /&gt;WH_GETMESSAGE Thread or global&lt;br /&gt;WH_JOURNALPLAYBACK Global only&lt;br /&gt;WH_JOURNALRECORD Global only&lt;br /&gt;WH_KEYBOARD Thread or global&lt;br /&gt;WH_KEYBOARD_LL Global only&lt;br /&gt;WH_MOUSE Thread or global&lt;br /&gt;WH_MOUSE_LL Global only&lt;br /&gt;WH_MSGFILTER Thread or global&lt;br /&gt;WH_SHELL Thread or global&lt;br /&gt;WH_SYSMSGFILTER Global only&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;For a specified hook type, thread hooks are called first, then global hooks.&lt;br /&gt;&lt;br /&gt;The global hooks are a shared resource, and installing one affects all applications in the same desktop as the calling thread. All global hook functions must be in libraries. Global hooks should be restricted to special-purpose applications or to use as a development aid during application debugging. Libraries that no longer need a hook should remove its hook procedure.&lt;br /&gt;&lt;br /&gt;Examples&lt;br /&gt;&lt;br /&gt;For an example, see Installing and Releasing Hook Procedures.&lt;br /&gt;&lt;br /&gt;Requirements&lt;br /&gt;&lt;br /&gt;Minimum supported client&lt;br /&gt;&lt;br /&gt;Windows 2000 Professional&lt;br /&gt;Minimum supported server&lt;br /&gt;&lt;br /&gt;Windows 2000 Server&lt;br /&gt;Header&lt;br /&gt;&lt;br /&gt;Winuser.h (include Windows.h)&lt;br /&gt;Library&lt;br /&gt;&lt;br /&gt;User32.lib&lt;br /&gt;DLL&lt;br /&gt;&lt;br /&gt;User32.dll&lt;br /&gt;Unicode and ANSI names&lt;br /&gt;&lt;br /&gt;SetWindowsHookExW (Unicode) and SetWindowsHookExA (ANSI)&lt;br /&gt;See Also&lt;br /&gt;&lt;br /&gt;Reference&lt;br /&gt;CallNextHookEx&lt;br /&gt;CallWndProc&lt;br /&gt;CallWndRetProc&lt;br /&gt;CBTProc&lt;br /&gt;DebugProc&lt;br /&gt;ForegroundIdleProc&lt;br /&gt;GetMsgProc&lt;br /&gt;JournalPlaybackProc&lt;br /&gt;JournalRecordProc&lt;br /&gt;LowLevelKeyboardProc&lt;br /&gt;LowLevelMouseProc&lt;br /&gt;KeyboardProc&lt;br /&gt;MouseProc&lt;br /&gt;MessageProc&lt;br /&gt;ShellProc&lt;br /&gt;SysMsgProc&lt;br /&gt;UnhookWindowsHookEx&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Conceptual&lt;br /&gt;Hooks&lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Send comments about this topic to Microsoft&lt;br /&gt;&lt;br /&gt;Build date: 12/1/2010&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6985725291359512517?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6985725291359512517/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6985725291359512517' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6985725291359512517'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6985725291359512517'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/setwindowshookex.html' title='SetWindowsHookEx'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-1743523657640935817</id><published>2010-12-17T12:20:00.001-08:00</published><updated>2010-12-17T12:20:50.905-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ccr'/><category scheme='http://www.blogger.com/atom/ns#' term='microsoft'/><title type='text'>CCR Introduction</title><content type='html'>Microsoft Robotics&lt;br /&gt;Concurrency and Coordination Runtime (CCR) is a managed code library, a Dynamically Linked Library (DLL), accessible from any language targeting the .NET Common Language Runtime (CLR).&lt;br /&gt;The CCR addresses the need of service-oriented applications to manage asynchronous operations, deal with concurrency, exploit parallel hardware and deal with partial failure. It enables the user to design applications so that the software modules or components can be loosely coupled; meaning they can be developed independently and make minimal assumptions about their runtime environment and other components. This approach changes how the user can think of programs from the start of the design process and deals with concurrency, failure and isolation in a consistent way.&lt;br /&gt;&lt;br /&gt;Problem Areas&lt;br /&gt;&lt;br /&gt;Asynchrony - When communicating between loosely coupled software components, like programs running across the network, or User Interface (UI) code communicating with the user input and the file I/O subsystem, asynchronous operations enable the code to scale better, be more responsive, and deal with failure across multiple operations. Asynchronous programming however, considerably reduces the readability of user code, since logic is often split between callbacks and the code that originates the operation. In addition, it is an almost impossible task to correctly handle failure across multiple outstanding operations.&lt;br /&gt;Concurrency - Code that needs to better utilize multiple execution resources, must be split into independent logical segments, that can run in parallel, and communicate when necessary to produce results from the combined execution. Often, that logical segment is captured by the thread OS primitive, that is nothing more than a long lived iteration. Because of thread performance implications on thread startup, the thread stays active for long periods of time. This forces a particular pattern.  Code is structured as long sequences that use blocking or synchronous calls, and only deals with one thing at a time. Further, threads assume that the primary communication between them is shared memory, forcing the programmer to use very explicit, error-prone methods to synchronize access to that shared memory.&lt;br /&gt;Coordination and Failure Handling - Coordinating between components is where most of the complexity in large software programs lies. A mismatch of interaction patterns, such as calling methods on objects versus using OS signaling primitives versus using queues plus signaling, leads to unreadable code, where the runtime behavior changes drastically between coordination approaches. More importantly, the error handling approaches are ill-defined and again vary drastically.&lt;br /&gt;Application Model&lt;br /&gt;&lt;br /&gt;CCR is appropriate for an application model that separates components into pieces that can interact only through messages. Components in this model need means to coordinate between messages, deal with complex failure scenarios, and effectively deal with asynchronous programming. This application model is also very similar to how heterogeneous hardware is integrated and how network applications are built. Most software programs have the same needs, from traditional client PC programs to server applications, to applets running in the Web browser. The software needs to coordinate user input, storage input/output and UI presentation. Although disguised in layers of mostly synchronous application code interfaces, asynchrony is inevitable since the devices operate at different speeds, have large differences in resources available, and we in general know how to use queues to isolate them.&lt;br /&gt;&lt;br /&gt;The following sections introduce the CCR programming model and its implementation that addresses the above areas in an efficient, robust and extensible way.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;© 2010 Microsoft Corporation. All Rights Reserved.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-1743523657640935817?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/1743523657640935817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=1743523657640935817' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1743523657640935817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1743523657640935817'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/ccr-introduction.html' title='CCR Introduction'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-9183123165812155499</id><published>2010-12-17T12:19:00.001-08:00</published><updated>2010-12-17T12:19:29.227-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>Array of Tuples</title><content type='html'>Q: How would use tuples in C# 4.0 to create an array of tuples containing various types of data (i.e. Employee Name, Employee ID#)?&lt;br /&gt;&lt;br /&gt;A: Listed below is a sample code snippet to implement an array of tuples.&lt;br /&gt;&lt;br /&gt;//Array of Tuples (i.e. EmpName, EmpIDNum)&lt;br /&gt;Tuple[] EmpRecs =&lt;br /&gt;{ &lt;br /&gt;Tuple.Create("Sam Nasr", 891),&lt;br /&gt;Tuple.Create("Jim Smith", 358),&lt;br /&gt;Tuple.Create("Lisa Jones", 962) &lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;string FirstEmpName = EmpRecs[0].Item1;&lt;br /&gt;&lt;br /&gt;string SecondEmpName = EmpRecs[1].Item1;&lt;br /&gt;int SecondEmpIDNum = EmpRecs[1].Item2;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;More information can be found at http://msdn.microsoft.com/en-us/library/dd413854(VS.95).aspx&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-9183123165812155499?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/9183123165812155499/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=9183123165812155499' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/9183123165812155499'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/9183123165812155499'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/array-of-tuples.html' title='Array of Tuples'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5729447031147442259</id><published>2010-12-17T12:17:00.000-08:00</published><updated>2010-12-17T12:18:07.778-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='ntlm'/><title type='text'>NTLM Authentication for Java http clients</title><content type='html'>I was working to provide NTLM authentication to a RSS aggregator plugin http://confluence.atlassian.com/display/CONFEXT/RSS+aggregator+macro+plugin , in our implementation the rssaggregator needed to access rss feed from an IIS installed application within an AD intergrated setup, so our http request needed to go through NTLM authentication.&lt;br /&gt;What I found out during this exercise is that if one is on the same AD domain as the server hosting the secure contents then JDK 1.5 and 1.6 does a transparent authentication at the backend, automatically transferring the login details from the http client to the server , however if the request is being made from a client outside the domain then the login details have to be provided using the username and password for the client accessing the protected resource, for that I found the Jave Authenticator class to be the best option, since you do not have to alter your existing code and just call the Authenticator before the http request and populate the authentication credentials in it. Worked really well for me.&lt;br /&gt;References&lt;br /&gt;Two exhaustive definitions of how ntlm works, and the interactions&lt;br /&gt;http://www.innovation.ch/personal/ronald/ntlm.html&lt;br /&gt;http://davenport.sourceforge.net/ntlm.html&lt;br /&gt;Useful link to valid Java Implementation&lt;br /&gt;http://oaklandsoftware.com/papers/ntlm.html&lt;br /&gt;Ads by Google&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5729447031147442259?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5729447031147442259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5729447031147442259' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5729447031147442259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5729447031147442259'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/ntlm-authentication-for-java-http.html' title='NTLM Authentication for Java http clients'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6911199023924647245</id><published>2010-12-17T12:16:00.001-08:00</published><updated>2010-12-17T12:16:21.639-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='yahoo'/><title type='text'>Oh Yahoo!</title><content type='html'>Yahoo has hit rock bottom. They’ve now, finally, had their layoffs. Those that are left are keeping their resumes fresh don’t expect to stay there over the long term. Everything we hear from employees boils down to this – the company is in “absolute disarray.”&lt;br /&gt;&lt;br /&gt;Take yesterday as an example. They botch news about closing down products like Delicious. The Upcoming team is apparently wiped out, but an apparently timestamped blog post appeared on wednesday, after the team was gone, asking for feedback on a new design for the site. Except the blog post doesn’t have a link to the new design, and still doesn’t as of today. Probably because whoever wrote it is gone, along with the rest of the team.&lt;br /&gt;&lt;br /&gt;And today Yahoo realized that people really care about sites like Delicious and put up a blog post saying that they’re going to sell it, not shut it down. Which is great except the Delicious blog is now offline and returns an error (we reprinted it here).&lt;br /&gt;&lt;br /&gt;And finally, yesterday Yahoo announced internally that they would be shutting down an instant messaging product called MyM. Have you heard of it? Neither had we. Oh wait, we did – in 2008. It turns out is was an internal project that was never launched and formally shut down nearly three years ago. Apparently the executive team didn’t know that.&lt;br /&gt;&lt;br /&gt;In May I spoke with CEO Carol Bartz on stage at TechCrunch Disrupt. The headline was the last few seconds of the talk. But that wasn’t really what was interesting about the interview. What really riled Yahoo up was when I asked if they were really even a technology company any more. I think it’s now clear to the world now that they aren’t. They’re just a nightmarish Dilbert-cartoon version of the old Yahoo, where employees fear for their jobs and stumble around the office trying to protect themselves, not build anything new and ambitious.&lt;br /&gt;&lt;br /&gt;There is only one way a company recovers from this. They must have new leadership. And soon. Because at this point they are little more than a holding company for some lucrative Asian Internet assets. This can’t possibly be what the board of directors hoped for when they hired Bartz less than a year ago. So much has changed, so fast.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6911199023924647245?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6911199023924647245/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6911199023924647245' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6911199023924647245'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6911199023924647245'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/oh-yahoo.html' title='Oh Yahoo!'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3417777691513125516</id><published>2010-12-14T21:33:00.000-08:00</published><updated>2010-12-14T21:42:02.886-08:00</updated><title type='text'>Zombie Farm</title><content type='html'>A few days ago Apple released its top 10 list of iPhone apps for 2010 and only one freemium app was listed as top grossing, Zombie Farm. Zombie Farm combines all the fun of Farmville with zombies (!), allowing you to plant either crops or corpses (seriously). You can then use the zombies you’ve planted to attack your neighbors or pirates or aliens (no, I am not making this up).&lt;br /&gt;&lt;br /&gt;Apparently this is a winning combination, as the game hit 7.3 million downloads in November, up from 6 million in October and growing about 25% a month. Currently it boasts about 2 million monthly active users, a number which has been doubling every couple of months since the app’s launch this February. Users have spent 2.76 billion minutes playing the game, which have resulted in 61.5 million Zombie Farm invasions. And despite complaints about the most recent updates’ slowness in the iTunes reviews, the developers are obviously doing something right.&lt;br /&gt;&lt;br /&gt;To the tune of millions of dollars. Playforge developer Vince McDonnell and his wife were in danger of  losing their house when he built the app, and now not so much. While McDonnell will not reveal how much total revenue the app has made thus far (people pay for in-game currency like $0.99 brains and tombstones) in comparison, Doodle Jump, the #10 app on the top grossing list sold about 3 million units in five months at $0.99 each which would project to about ($) 7 million sold for the year. Zombie Farm was at number #6 on the list, which means McDonnell has made at least a few million thus far.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3417777691513125516?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3417777691513125516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3417777691513125516' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3417777691513125516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3417777691513125516'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/12/zombie-farm.html' title='Zombie Farm'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8119318319705085382</id><published>2010-02-08T22:58:00.000-08:00</published><updated>2010-02-08T23:28:39.942-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='gwt'/><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>GWT games</title><content type='html'>Many years ago I wrote javascript application that took an image, broke it up into many pieces, then shuffled them into a puzzle for the user. It only worked in I.E. and was horribly written and hard to maintain.&lt;br /&gt;&lt;br /&gt;I wanted to revisit this application and was trying to figure out what the best language/framework to use.&lt;br /&gt;&lt;br /&gt;I decided that GWT (Google Web Toolkit) would be an excellent choice for this application.&lt;br /&gt;&lt;br /&gt;To see the application go here: &lt;a href="http://imageoverflow.appspot.com/puzzle.html?imageUrl=http://imageoverflow.appspot.com/get/3b20793dfc300d058ad04d411da152eb?f=t"&gt;http://imageoverflow.appspot.com/puzzle.html?imageUrl=http://imageoverflow.appspot.com/get/3b20793dfc300d058ad04d411da152eb?f=t&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now to the code...&lt;br /&gt;&lt;br /&gt;The code consists of the following main classes.&lt;br /&gt;&lt;br /&gt;ImagePuzzleWidget&lt;br /&gt;PuzzleGrid&lt;br /&gt;ClickableImage&lt;br /&gt;&lt;br /&gt;ImagePuzzleWidget&lt;br /&gt;&lt;br /&gt;The ImagePuzzleWidget is the main widget that encapsulates the game. It takes no arguments and is added directly to the Root Panel in the GWT entry point.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;public void onModuleLoad() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    RootPanel.get().add(new ImagePuzzleWidget());&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;PuzzleGrid&lt;br /&gt;&lt;br /&gt;PuzzleGrid extends the Grid GWT component. Its constructor looks like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;public PuzzleGrid(final String url,final int cols,final int rows)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This class takes the url of the image you want to scramble as well as how many rows and cols you want.&lt;br /&gt;&lt;br /&gt;We add an onLoadHandler that will get executed after the image has been loaded:&lt;br /&gt;&lt;br /&gt; &lt;span style="font-family:courier new;"&gt; image.addLoadHandler(new LoadHandler() {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            public void onLoad(LoadEvent event) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                setWidth(image.getWidth() + "px");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                setHeight(image.getHeight() + "px");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                int blockWidth = image.getWidth() / cols;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                int blockHeight = image.getHeight() / rows;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                for (int col = 0; col &lt;&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    for (int row = 0; row &lt;&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                        setWidget(row, col,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                                new ClickableImage(url, blockWidth * col, blockHeight * row, blockWidth, blockHeight, row, col));&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                    }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                shuffle();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                setVisible(true);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        });&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It is this class that does most of the work by splitting the image into sub parts and making ClickableImages out of them.&lt;br /&gt;&lt;br /&gt;The class then shuffles the images and sets it as visible.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So now we have an image displayed broken into sub parts and displayed in a grid fashion.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ClickableImage&lt;br /&gt;&lt;br /&gt;ClickableImage class, as its name implies embodies a section of the image that can be clicked.&lt;br /&gt;&lt;br /&gt;The class basically just extends Image and adds a ClickHandler. I add some extra convenience methods like select and unselect which I won't show for the sake of brevity.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;public ClickableImage(String url, int left, int top, int width, int height, int row, int col) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        super(url, left, top, width, height);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        this.row = row;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        this.col = col;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        this.addStyleName("unselected");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        this.addClickHandler(new ClickHandler() {&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            public void onClick(ClickEvent event) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                toggleSelected();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        });&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And that is really all there is to it. By using GWT we can focus on writing our OO style code and not worry about the browser.&lt;br /&gt;&lt;br /&gt;Make sure you check out the app at: &lt;a href="http://imageoverflow.appspot.com/puzzle.html?imageUrl=http://imageoverflow.appspot.com/get/3b20793dfc300d058ad04d411da152eb?f=t"&gt;http://imageoverflow.appspot.com/puzzle.html?imageUrl=http://imageoverflow.appspot.com/get/3b20793dfc300d058ad04d411da152eb?f=t&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This post was written hastily, if there is any interest or question leave a comment and I'll ake the time to expand.&lt;br /&gt;&lt;br /&gt;HTH&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8119318319705085382?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8119318319705085382/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8119318319705085382' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8119318319705085382'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8119318319705085382'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2010/02/gwt-games.html' title='GWT games'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-4661046876941967480</id><published>2009-10-05T17:01:00.000-07:00</published><updated>2009-10-05T17:16:05.834-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='jsf tag custom'/><title type='text'>Extend JSF Command Link</title><content type='html'>I found myself adding a Remove link throughout my JSF application. It looked like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&amp;lt;h:commandlink value="Remove" actionlistener="#{MyBean.remove}"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I wanted to add a confirm dialog. So I added this.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;h:commandlink value="Remove" actionlistener="#{MyBean.remove}" onclick="javascript return confirm('Are you sure')"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But as I copied and pasted this through 5 or 6 different pages, I got to thinking, there has to be a better way.&lt;br /&gt;&lt;br /&gt;So I created a custom jsf component by extending Command Link.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;Step 1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;First step was to create my new tag. It simply wraps the default command link.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;package com.acme;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import com.sun.faces.taglib.html_basic.CommandLinkTag;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import javax.el.MethodExpression;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import javax.el.ValueExpression;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import javax.faces.component.UIComponent;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import javax.faces.component.html.HtmlCommandLink;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import javax.faces.event.MethodExpressionActionListener;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;import javax.faces.webapp.UIComponentELTag;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;* @author cmcintosh&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;public class RemoveCommandLinkTag extends UIComponentELTag { // Declare a bean property for the hellomsg attribute.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;CommandLinkTag commandLink = new CommandLinkTag();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;ValueExpression value;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;MethodExpression actionListener;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;// Associate the renderer and component type.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;public String getComponentType() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  return commandLink.getComponentType();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;public String getRendererType() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  return commandLink.getRendererType();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;@Override&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;protected void setProperties(UIComponent component) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  super.setProperties(component);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  component.setValueExpression("value", value);         &lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  ((HtmlCommandLink)component).addActionListener(new MethodExpressionActionListener(actionListener));&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  ((HtmlCommandLink)component).setOnclick("javascript:return confirm('Are you sure you want to remove?')");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;@Override&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;public void release() {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  super.release();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  commandLink.release();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;public void setValue(ValueExpression value) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  this.value = value;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;public void setActionListener(MethodExpression actionListener) {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt; this.actionListener = actionListener;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;Step 2&lt;/span&gt;&lt;br /&gt;Next I needed to create a tld.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;tlib-version&amp;gt;1.0&amp;lt;/tlib-version&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;short-name&amp;gt;datastore&amp;lt;/short-name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;uri&amp;gt;/WEB-INF/tlds/datastore&amp;lt;/uri&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;tag&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  &amp;lt;name&amp;gt;RemoveCommandLink&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  &amp;lt;tag-class&amp;gt;com.acme.datastore.web.components.tags.RemoveCommandLinkTag&amp;lt;/tag-class&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  &amp;lt;attribute&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;name&amp;gt;value&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;deferred-value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;          &amp;lt;type&amp;gt;java.lang.String&amp;lt;/type&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;/deferred-value&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  &amp;lt;/attribute&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  &amp;lt;attribute&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;name&amp;gt;actionListener&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;required&amp;gt;false&amp;lt;/required&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;deferred-method&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;          &amp;lt;method-signature&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;          actionListener(javax.faces.event.ActionEvent)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;          &amp;lt;/method-signature&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;/deferred-method&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;      &amp;lt;type&amp;gt;String&amp;lt;/type&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;  &amp;lt;/attribute&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;/tag&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;/taglib&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This was one of the hardest parts note the deferred-method and deferredvalue. I've never encountered those before.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;Step 3&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Add it to the web.xml:&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;jsp-config&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;        &amp;lt;taglib&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;            &amp;lt;taglib-uri&amp;gt;http://datastore.acme.com/datastore&amp;lt;/taglib-uri&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;            &amp;lt;taglib-location&amp;gt;/WEB-INF/tlds/datastore.tld&amp;lt;/taglib-location&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;        &amp;lt;/taglib&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;    &amp;lt;/jsp-config&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:180%;"&gt;Step 4 &lt;/span&gt;&lt;br /&gt;Finally to use it:&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;%@taglib prefix="ds" uri="http://datastore.acme.com/datastore"%&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;&amp;lt;ds:removecommandlink value="Remove" actionlistener="#{ViewModifyTable.removeColumn}"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;                      &amp;lt;f:param name="applicationId" value="#{ViewModifyTable.applicationId}"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;                      &amp;lt;f:param name="tableName" value="#{ViewModifyTable.tableName}"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;                  &amp;lt;/f:param&amp;gt;&amp;lt;/f:param&amp;gt;&amp;lt;/ds:removecommandlink&amp;gt;&amp;lt;/h:commandlink&amp;gt;&amp;lt;/h:commandlink&amp;gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-4661046876941967480?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/4661046876941967480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=4661046876941967480' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4661046876941967480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4661046876941967480'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/10/extend-jsf-command-link.html' title='Extend JSF Command Link'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-2023505596991301115</id><published>2009-09-18T12:10:00.003-07:00</published><updated>2009-09-18T12:10:56.187-07:00</updated><title type='text'>Configure logging level</title><content type='html'>Here is how I disabled hibernate JPA logging level in code.&lt;br /&gt;&lt;br /&gt;Logger.getLogger("org.hibernate").setLevel(Level.SEVERE);&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-2023505596991301115?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/2023505596991301115/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=2023505596991301115' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2023505596991301115'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2023505596991301115'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/09/configure-logging-level.html' title='Configure logging level'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-369355760803159325</id><published>2009-08-18T16:09:00.000-07:00</published><updated>2009-08-18T16:10:31.429-07:00</updated><title type='text'>Readling List</title><content type='html'>XtraDb:&lt;br /&gt;&lt;br /&gt;http://www.mysqlperformanceblog.com/2009/08/13/xtradb-the-top-10-enhancements/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Mysql performance blog&lt;br /&gt;&lt;br /&gt;http://www.amazon.com/gp/product/0596101716?ie=UTF8&amp;tag=perinc-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596101716&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-369355760803159325?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/369355760803159325/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=369355760803159325' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/369355760803159325'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/369355760803159325'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/08/readling-list.html' title='Readling List'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8128779721622832231</id><published>2009-08-17T03:57:00.002-07:00</published><updated>2009-08-17T04:05:06.124-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reading list'/><title type='text'>Pro JSF and Ajax by Jonas Jacobi and John R. Fallows</title><content type='html'>&lt;span style="font-size:130%;"&gt;&lt;span style="font-size:100%;"&gt;Read On: 8/17/09&lt;br /&gt;Rating: 7 /10&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Pros:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt; I liked this summary of other applications and which component of MVC they target: &lt;span style="font-style: italic;"&gt;Examples of frameworks are Struts (an open source controller framework);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;TopLink and Hibernate (model frameworks); and Tiles, Tapestry, XUL, and ADF UIX (so-&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;called view frameworks).&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;Cons:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Can be hard to read&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-size:130%;"&gt;Questions:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Why do so many frameworks model navigation these days?&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8128779721622832231?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8128779721622832231/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8128779721622832231' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8128779721622832231'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8128779721622832231'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/08/pro-jsf-and-ajax-by-jonas-jacobi-and.html' title='Pro JSF and Ajax by Jonas Jacobi and John R. Fallows'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3496272771827477075</id><published>2009-08-17T03:57:00.001-07:00</published><updated>2009-08-17T03:57:28.411-07:00</updated><title type='text'></title><content type='html'>&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3496272771827477075?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3496272771827477075/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3496272771827477075' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3496272771827477075'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3496272771827477075'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/08/blog-post.html' title=''/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-1724418442163558215</id><published>2009-08-17T03:45:00.000-07:00</published><updated>2009-08-17T03:47:30.941-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reading list'/><title type='text'>Comparing Web Frameworks: Struts, Spring MVC, Webwork, Tapestry &amp; JSF</title><content type='html'>Read on: 08/17/2009&lt;br /&gt;Rating: 2/10&lt;br /&gt;&lt;br /&gt;Way outdated, by the time I got my hands on it. No fault of the authors it was made in 2005.&lt;br /&gt;&lt;br /&gt;Pros:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Concise&lt;/li&gt;&lt;/ul&gt;Cons:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Its just a bunch of slides&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-1724418442163558215?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/1724418442163558215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=1724418442163558215' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1724418442163558215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1724418442163558215'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/08/comparing-web-frameworks-struts-spring.html' title='Comparing Web Frameworks: Struts, Spring MVC, Webwork, Tapestry &amp; JSF'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-1733772894953064186</id><published>2009-08-17T03:22:00.000-07:00</published><updated>2009-08-17T03:30:46.979-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reading list'/><title type='text'>Beginning Spring 2 by Dave Minter</title><content type='html'>Read On: 8/17/09&lt;br /&gt;Rating: 8/10&lt;br /&gt;&lt;br /&gt;Pros:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Very good introduction to Spring&lt;/li&gt;&lt;li&gt;Clear explanation of IoC and dependency injection&lt;/li&gt;&lt;li&gt;Made a strong case for using Spring for all applications&lt;/li&gt;&lt;/ul&gt;Cons&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Would of liked more Spring annotation examples.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;The database example could of been expanded. In particular I wasn't sure what the purpose of the DAO's were.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;I would of liked a more detail on Spring MVC&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-1733772894953064186?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/1733772894953064186/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=1733772894953064186' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1733772894953064186'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1733772894953064186'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/08/beginning-spring-2-by-dave-minter.html' title='Beginning Spring 2 by Dave Minter'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-252029697567372875</id><published>2009-08-07T18:32:00.001-07:00</published><updated>2009-08-16T23:41:38.053-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='jpa'/><category scheme='http://www.blogger.com/atom/ns#' term='web stack'/><category scheme='http://www.blogger.com/atom/ns#' term='mysql'/><title type='text'>My desired Java technology stack.</title><content type='html'>This blogpost will be continually updated to list my favorite application stack for various projects. I welcome any comments to help me choose the best tools that will make me most productive.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Web Container&lt;/span&gt;&lt;br /&gt;Currently I'm biased for tomcat its easy to setup and administer and I haven't had any problems with it.&lt;br /&gt;&lt;br /&gt;I'm having good success with also using apache with mod_proxy_balancer as a load balancer mechanism. Still need to investigate how to make the apache instance not be my single point of failure.&lt;br /&gt;&lt;br /&gt;I &lt;span style="font-weight: bold;"&gt;do not&lt;/span&gt; like glassfish. For a project a couple years ago we went with that and the app is still buggy and crashing a lot. Granted its probably more of the apps problem EJB 2.0 without a lot of other kruft but glassfish didn't impress me.&lt;br /&gt;&lt;br /&gt;To consider:&lt;br /&gt;I want to investigate JBOSS again. I used it in college and I wonder if features of JBOSS would be compelling enough for me to move off of Tomcat.&lt;br /&gt;&lt;br /&gt;Possible reasons to consider JBOSS&lt;br /&gt;&lt;ul&gt;&lt;li&gt;uses tomcat, and you can configure it to run with almost no features but tomcat, but still have the ability to use enterprise class features at a later date.&lt;/li&gt;&lt;li&gt;Heard about their shard memory cache, this could be useful for some of my applications&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Display Technologies&lt;/span&gt;&lt;br /&gt;I don't know. I've tried GWT, JSF, JavaFX, JSP/JSTL. Nothing seems like the best technology here. I seem to spend most of my time writing the front end of the application which is the part i least enjoy.&lt;br /&gt;&lt;br /&gt;What I'm looking for ideally is an easy to use set of components to layout without much fuss. Any suggestions? Note I'm not looking for something like struts, which as far as I understand it, helps with navigation.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Configuration&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Spring, looks like a good standard way of handling application configuration. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:130%;"&gt;Javascript Libraries&lt;/span&gt;&lt;br /&gt;Need to investigate and choose between:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;tablekit&lt;/li&gt;&lt;li&gt;jquery&lt;/li&gt;&lt;li&gt;fabtabulous&lt;/li&gt;&lt;li&gt;anything else?&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Database&lt;/span&gt;&lt;br /&gt;Mysql enough said, although I would use MSSQL in a heartbeat again but you can't beat the price of Mysql.&lt;br /&gt;&lt;br /&gt;Need to investigate scaling, and replication of Mysql that is the next big task I need to tackle at work.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;ORM&lt;/span&gt;&lt;br /&gt;After EJB 2.0 hell I swore off ORM's, but using the Google Appengine I've grown found of JPA. Just started looking at Hibernate JPA. Early impressions: I like it!&lt;br /&gt;&lt;br /&gt;Anyone else want to recommend something?&lt;br /&gt;&lt;br /&gt;Current ordering of choices:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Hibernate JPA- using javax.persitence classes to not tie into a vendor.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Straight JDBC&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;XML Parsing&lt;/span&gt;&lt;br /&gt;SAX/DOM/JAXB? I don't really know yet&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-252029697567372875?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/252029697567372875/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=252029697567372875' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/252029697567372875'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/252029697567372875'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/08/my-desired-technology-stack.html' title='My desired Java technology stack.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6774998562591942545</id><published>2009-07-26T23:22:00.001-07:00</published><updated>2009-07-26T23:32:58.050-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javafx'/><title type='text'>JavaFX - Peg Game.</title><content type='html'>JavaFX is a technology from SUN ( recently taken over by Oracle ) that competes against Adobe Flash and Microsoft Silverlight.&lt;br /&gt;&lt;br /&gt;This article shows how to build a simple JavaFX program to play the 'Peg Game'. &lt;br /&gt;&lt;br /&gt;The Peg game is played with a triangular piece of board with 15 holes in it. One hole (usually the top) is left open and golf tees are placed into the other 14. The objective is to hop over the pegs, removing the hopped peg, and in doing so remove all but one peg from the game.&lt;br /&gt;&lt;br /&gt;Here it is in action: &lt;a href="http://turnbasedgames.appspot.com/lastblock/Last_Block_Standing.html"&gt;http://turnbasedgames.appspot.com/lastblock/Last_Block_Standing.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To create this game I have four files: &lt;br /&gt;&lt;br /&gt;Main.fx&lt;br /&gt;TriangleBase.fx&lt;br /&gt;GameSquare.fx&lt;br /&gt;Util.java&lt;br /&gt;&lt;br /&gt;Main.fx consists of: &lt;br /&gt;&lt;br /&gt;package javafxapplication1;&lt;br /&gt;&lt;br /&gt;import javafx.scene.paint.Color;&lt;br /&gt;import javafx.scene.paint.LinearGradient;&lt;br /&gt;import javafx.scene.paint.Stop;&lt;br /&gt;import javafx.scene.Scene;&lt;br /&gt;import javafx.scene.shape.Circle;&lt;br /&gt;import javafx.scene.shape.Rectangle;&lt;br /&gt;import javafx.scene.text.Font;&lt;br /&gt;import javafx.scene.text.Text;&lt;br /&gt;import javafx.stage.Stage;&lt;br /&gt;import javafxapplication1.TriangleBase;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @author Lars Vogel&lt;br /&gt; */&lt;br /&gt;Stage {&lt;br /&gt;    var rect: Rectangle;&lt;br /&gt;    var circ: Circle;&lt;br /&gt;    title: "Last Block Standing"&lt;br /&gt;    scene: Scene {&lt;br /&gt;        width: 500&lt;br /&gt;        height: 500&lt;br /&gt;        var myString = "Last Block Standing"&lt;br /&gt;        var gameInstance: TriangleBase = new TriangleBase;        &lt;br /&gt;        content: [&lt;br /&gt;            gameInstance,&lt;br /&gt;            Rectangle {&lt;br /&gt;                x: 50,&lt;br /&gt;                y: 300&lt;br /&gt;                width: 50,&lt;br /&gt;                height: 15&lt;br /&gt;                arcHeight: 2&lt;br /&gt;                arcWidth: 2&lt;br /&gt;                stroke: Color.BLACK&lt;br /&gt;                strokeWidth: 2&lt;br /&gt;                fill: LinearGradient {&lt;br /&gt;                    startX: 0.0&lt;br /&gt;                    startY: 0.0&lt;br /&gt;                    endX: 0.0&lt;br /&gt;                    endY: 1.0&lt;br /&gt;                    stops: [&lt;br /&gt;                        Stop {&lt;br /&gt;                            color: Color.ORANGE&lt;br /&gt;                            offset: 0.0&lt;br /&gt;                        },&lt;br /&gt;                        Stop {&lt;br /&gt;                            color: Color.DARKRED&lt;br /&gt;                            offset: 0.3&lt;br /&gt;                        },&lt;br /&gt;                        Stop {&lt;br /&gt;                            color: Color.ORANGE&lt;br /&gt;                            offset: 1.0&lt;br /&gt;                        },&lt;br /&gt;                    ]&lt;br /&gt;                }&lt;br /&gt;                onMouseClicked: function(me) {&lt;br /&gt;                    gameInstance.restart();&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            Text {&lt;br /&gt;                fill: Color.WHITE&lt;br /&gt;                font: Font {&lt;br /&gt;                    name: "Arial Bold"&lt;br /&gt;                    size: 12&lt;br /&gt;                }&lt;br /&gt;                x: 55,&lt;br /&gt;                y: 315&lt;br /&gt;                content: "Restart"&lt;br /&gt;            }        &lt;br /&gt;        ]&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Triangle Base: &lt;br /&gt;/*&lt;br /&gt;    * TriangleBase.fx&lt;br /&gt;     *&lt;br /&gt;     * Created on Jul 23, 2009, 10:55:02 PM&lt;br /&gt;     */&lt;br /&gt;&lt;br /&gt;    package javafxapplication1;&lt;br /&gt;&lt;br /&gt;import javafx.scene.control.Control;&lt;br /&gt;import javafx.scene.Group;&lt;br /&gt;import javafx.scene.Node;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @author avalanche&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;public class TriangleBase extends Control {&lt;br /&gt;    public override function create(): Node {&lt;br /&gt;        return group;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    var group: Group =&lt;br /&gt;    Group {&lt;br /&gt;        content: [    &lt;br /&gt;        ]&lt;br /&gt;    };&lt;br /&gt;    public function buildContent() {                &lt;br /&gt;        insert GameSquare{ SquareId:1 x1:50 y1:0 hasTee:false} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:2 x1:40 y1:20} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:3 x1:60 y1:20} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:4 x1:30 y1:40} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:5 x1:50 y1:40} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:6 x1:70 y1:40} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:7 x1:20 y1:60} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:8 x1:40 y1:60} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:9 x1:60 y1:60} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:10 x1:80 y1:60} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:11 x1:10 y1:80} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:12 x1:30 y1:80} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:13 x1:50 y1:80} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:14 x1:70 y1:80} into group.content;&lt;br /&gt;        insert GameSquare{ SquareId:15 x1:90 y1:80} into group.content;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public function restart() {&lt;br /&gt;        delete group.content;&lt;br /&gt;        buildContent();&lt;br /&gt;        println("performing restart");&lt;br /&gt;    }&lt;br /&gt;    postinit{&lt;br /&gt;        buildContent();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;GameSquare:&lt;br /&gt;/*&lt;br /&gt; * Node.fx&lt;br /&gt; *&lt;br /&gt; * Created on Jul 23, 2009, 11:12:07 PM&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;package javafxapplication1;&lt;br /&gt;&lt;br /&gt;import javafx.scene.control.Control;&lt;br /&gt;import javafx.scene.paint.Color;&lt;br /&gt;import javafx.scene.shape.Rectangle;&lt;br /&gt;import javafxapplication1.Util;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;    &lt;br /&gt;var clickedSquare: GameSquare= null;&lt;br /&gt;var gameSquareMap = new HashMap();&lt;br /&gt;&lt;br /&gt;public class GameSquare extends Control {&lt;br /&gt;    public var SquareId: Integer;&lt;br /&gt;    public var squareColor: Color  = Color.GREEN;&lt;br /&gt;    public var x1: Number;&lt;br /&gt;    public var y1: Number;&lt;br /&gt;    public var hasTee: Boolean = true;&lt;br /&gt;&lt;br /&gt;    var rect: Rectangle =&lt;br /&gt;    Rectangle{&lt;br /&gt;&lt;br /&gt;        fill: bind squareColor;&lt;br /&gt;       &lt;br /&gt;        height: (20 * 2)&lt;br /&gt;        width: (20 * 2 )&lt;br /&gt;        translateX: (x1 * 2 )&lt;br /&gt;        translateY: (y1 * 2)&lt;br /&gt;        stroke: Color.BLACK;&lt;br /&gt;        onMouseClicked: function(me){&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            // first click, and I have a tee&lt;br /&gt;            if (clickedSquare == null and this.hasTee == true)  {&lt;br /&gt;                println("setting clickedsquare");&lt;br /&gt;                clickedSquare = this;&lt;br /&gt;                clickedSquare.setIsClicked(true)&lt;br /&gt;             &lt;br /&gt;&lt;br /&gt;            // second click, and I don't have a tee&lt;br /&gt;            }else if(this.hasTee == false and clickedSquare != null) {&lt;br /&gt;                // Find square we jumped over.&lt;br /&gt;                var hoppedNode = Util.getHoppedNode(clickedSquare.SquareId, this.SquareId);&lt;br /&gt;                if (hoppedNode != -1 ) {&lt;br /&gt;                    var hoppedSquare = gameSquareMap.get(hoppedNode) as GameSquare;&lt;br /&gt;                    if (hoppedSquare.hasTee) {&lt;br /&gt;                        // hopped square is now empty&lt;br /&gt;                        hoppedSquare.setHasTee(false);&lt;br /&gt;                        this.setHasTee(true);&lt;br /&gt;                        clickedSquare.setHasTee(false);&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;                clickedSquare.setIsClicked(false);&lt;br /&gt;                clickedSquare = null;&lt;br /&gt;            }  else {&lt;br /&gt;                //invalid remove marker for first click so user can try again.                &lt;br /&gt;                clickedSquare.setIsClicked(false);&lt;br /&gt;                clickedSquare = null;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    public override function create(): Rectangle {&lt;br /&gt;        return rect;&lt;br /&gt;    }&lt;br /&gt;    function setIsClicked(arg:Boolean) {&lt;br /&gt;        if (arg==true) {&lt;br /&gt;            squareColor = Color.BROWN;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;            setHasTee(hasTee); //set to proper color;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    function setHasTee(arg:Boolean) {&lt;br /&gt;        hasTee = arg;&lt;br /&gt;        if (hasTee == true) {&lt;br /&gt;            squareColor = Color.GREEN;&lt;br /&gt;        } else {&lt;br /&gt;            squareColor = Color.RED;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    postinit {&lt;br /&gt;      this.setHasTee(hasTee);&lt;br /&gt;      gameSquareMap.put(SquareId, this);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Util.java&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * To change this template, choose Tools | Templates&lt;br /&gt; * and open the template in the editor.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;package javafxapplication1;&lt;br /&gt;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;import java.util.Map;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; *&lt;br /&gt; * @author avalanche&lt;br /&gt; */&lt;br /&gt;public class Util {&lt;br /&gt;    static Map&lt;String,Integer&gt; connections = new HashMap&lt;String,Integer&gt;();&lt;br /&gt;    static {&lt;br /&gt;        connections.put("1,4", 2);&lt;br /&gt;        connections.put("1,6",3);&lt;br /&gt;        connections.put("2,7",4);&lt;br /&gt;        connections.put("2,9",5);&lt;br /&gt;        connections.put("3,8",5);&lt;br /&gt;        connections.put("3,10",6);&lt;br /&gt;        connections.put("4,6",5);&lt;br /&gt;        connections.put("4,11",7);&lt;br /&gt;        connections.put("4,13",8);&lt;br /&gt;        connections.put("5,12",8);&lt;br /&gt;        connections.put("5,14",9);&lt;br /&gt;        connections.put("6,13",9);&lt;br /&gt;        connections.put("6,15",10);&lt;br /&gt;        connections.put("7,9",8);&lt;br /&gt;        connections.put("8,10",9);&lt;br /&gt;        connections.put("11,13",12);&lt;br /&gt;        connections.put("12,14",13);&lt;br /&gt;        connections.put("13,15",14);&lt;br /&gt;    }&lt;br /&gt;    static public int getHoppedNode(int x, int y ) {&lt;br /&gt;        int lowest = Math.min(x,y);&lt;br /&gt;        int highest = Math.max(x,y);&lt;br /&gt;        Integer value = connections.get(lowest+","+highest);&lt;br /&gt;        if ( value == null )&lt;br /&gt;            return -1;&lt;br /&gt;        else&lt;br /&gt;            return value;&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(This article is a placeholder, I'm going to come back and comment later)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6774998562591942545?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6774998562591942545/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6774998562591942545' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6774998562591942545'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6774998562591942545'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/07/javafx-peg-game.html' title='JavaFX - Peg Game.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3194778159187263898</id><published>2009-06-18T17:40:00.000-07:00</published><updated>2009-09-21T12:19:19.437-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><title type='text'>Too many open files</title><content type='html'>I owned a web server about a year ago that would crash every other night. I did everything I could to try to track down the exception (Too many open files), but I couldn't figure out what was causing it. I eventually chalked it up to a glassfish bug since they did have that problem before. &lt;br /&gt;&lt;br /&gt;Well today I hit it again, this time in a tomcat instance. I ran across a form posting suggesting I try this command: &lt;br /&gt;&lt;br /&gt;ls -l /proc/[pid]/fd&lt;br /&gt;&lt;br /&gt;Which is an awesome command that I wish I knew about a year ago. This showed all the files that were opened which were indeed my fault. The problem came from a dom4j reader that wasn't being closed which was leaking the file connection. By closing the datastream the problem went away. &lt;br /&gt;&lt;br /&gt;So if you have this problem on linux try the above command and see who is opening that file.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Hope that helps,&lt;br /&gt;Chris&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3194778159187263898?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3194778159187263898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3194778159187263898' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3194778159187263898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3194778159187263898'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/06/too-many-open-files.html' title='Too many open files'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8844682825820580993</id><published>2009-04-22T00:39:00.000-07:00</published><updated>2009-04-22T00:41:08.048-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='twitter'/><title type='text'>Use Python to post to twitter</title><content type='html'>Here is an example of using python to post to twitter.&lt;br /&gt;&lt;br /&gt;def twit(login, password, msg):&lt;br /&gt;    &lt;br /&gt;    payload= {'status' : msg, 'source' : '&lt;a href="http://tinyblurb.appspot.com"&gt;TinyBlurb&lt;/a&gt;'}&lt;br /&gt;    payload= urllib.urlencode(payload)&lt;br /&gt;&lt;br /&gt;    base64string = base64.encodestring('%s:%s' % (login, password))[:-1]&lt;br /&gt;    headers = {'Authorization': "Basic %s" % base64string}&lt;br /&gt;    url = "http://twitter.com/statuses/update.xml"&lt;br /&gt;&lt;br /&gt;    result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers)&lt;br /&gt;&lt;br /&gt;    return result.content&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8844682825820580993?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8844682825820580993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8844682825820580993' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8844682825820580993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8844682825820580993'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/04/use-python-to-post-to-twitter.html' title='Use Python to post to twitter'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-620298756396162675</id><published>2009-04-21T16:12:00.001-07:00</published><updated>2009-04-21T16:12:26.671-07:00</updated><title type='text'>Find all files that aren't in perforce</title><content type='html'>dir /s/b/a-d | p4 -x- have &gt; nul: 2&gt;missing.txt&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-620298756396162675?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/620298756396162675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=620298756396162675' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/620298756396162675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/620298756396162675'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/04/find-all-files-that-arent-in-perforce.html' title='Find all files that aren&apos;t in perforce'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3078376783094149087</id><published>2009-04-10T23:19:00.000-07:00</published><updated>2009-04-10T23:37:30.547-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><title type='text'>Returning immutable collections</title><content type='html'>Many times it is desirable to return an internal collection object to consumers of your class.&lt;br /&gt;&lt;br /&gt;For example:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Foo &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; _internalObjects &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArrayList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; getList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; _internalObjects&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The method above works, but has a draw back. The elements of your internalObjects list are now mutable outside your control. In C++ you might try to return const&amp; instead to get around this problem, but how do you solve this in Java?&lt;br /&gt;&lt;br /&gt;The first approach might be to return a copy. e.g:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Foo &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; _internalObjects &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArrayList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; getList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; copy &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArrayList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    copy&lt;span style='color:#808030; '&gt;.&lt;/span&gt;addAll&lt;span style='color:#808030; '&gt;(&lt;/span&gt;_internalObjects&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; copy&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This has the desired effect but with a couple of disadvantages. First you had to perform the copy which could be a performance bottleneck. Second the consumer may no longer point to the same objects if you modify your internalObject class.&lt;br /&gt;&lt;br /&gt;The best way, that I know of, to do this is as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Foo &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;final&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; _internalObjects &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArrayList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt; getList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; AbstractList&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;@&lt;/span&gt;Override&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;Object&lt;/span&gt; get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#bb7977; '&gt;int&lt;/span&gt; index&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; _internalObjects&lt;span style='color:#808030; '&gt;.&lt;/span&gt;get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;index&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;@&lt;/span&gt;Override&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;int&lt;/span&gt; size&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; _internalObjects&lt;span style='color:#808030; '&gt;.&lt;/span&gt;size&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The returned list still points to internal list, but is read only. Also, just to be pedantic, I've marked the list as final which means the reference will never change which is more correct for this example.&lt;br /&gt;&lt;br /&gt;I hope that helps, leave comments on what you think.&lt;br /&gt;&lt;br /&gt;@developresource&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3078376783094149087?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3078376783094149087/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3078376783094149087' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3078376783094149087'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3078376783094149087'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/04/returning-immutable-collections.html' title='Returning immutable collections'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6598260239900182697</id><published>2009-03-27T18:00:00.000-07:00</published><updated>2009-03-27T18:01:38.212-07:00</updated><title type='text'>Add new line at end of file.</title><content type='html'>Working on a cross platform c++ project I find it annoying that Linux requires a new line at the end of each file. I constantly forget to do this.&lt;br /&gt;&lt;br /&gt;I wrote this simple macro which will add the new line each time you save a file. &lt;br /&gt;&lt;br /&gt;You don't need the whole thing. Most of this is generated in EnvironmentEvents but saved here for posterity. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;Option Strict Off&lt;br /&gt;Option Explicit Off&lt;br /&gt;Imports System&lt;br /&gt;Imports EnvDTE&lt;br /&gt;Imports EnvDTE80&lt;br /&gt;Imports System.Diagnostics&lt;br /&gt;&lt;br /&gt;Public Module EnvironmentEvents&lt;br /&gt;&lt;br /&gt;#Region "Automatically generated code, do not modify"&lt;br /&gt;'Automatically generated code, do not modify&lt;br /&gt;'Event Sources Begin&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents DTEEvents As EnvDTE.DTEEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents DocumentEvents As EnvDTE.DocumentEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents WindowEvents As EnvDTE.WindowEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents TaskListEvents As EnvDTE.TaskListEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents FindEvents As EnvDTE.FindEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents SelectionEvents As EnvDTE.SelectionEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents BuildEvents As EnvDTE.BuildEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents SolutionEvents As EnvDTE.SolutionEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents TextDocumentKeyPressEvents As EnvDTE80.TextDocumentKeyPressEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents CodeModelEvents As EnvDTE80.CodeModelEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents DebuggerProcessEvents As EnvDTE80.DebuggerProcessEvents&lt;br /&gt; &lt;System.ContextStaticAttribute()&gt; Public WithEvents DebuggerExpressionEvaluationEvents As EnvDTE80.DebuggerExpressionEvaluationEvents&lt;br /&gt;'Event Sources End&lt;br /&gt;'End of automatically generated code&lt;br /&gt;#End Region&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Private Sub DocumentEvents_DocumentOpened(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentOpened&lt;br /&gt;&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Private Sub DocumentEvents_DocumentSaved(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved                &lt;br /&gt;&lt;br /&gt;        Dim textSelection As EnvDTE.TextSelection&lt;br /&gt;&lt;br /&gt;        textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)&lt;br /&gt;        textSelection.EndOfDocument(True)&lt;br /&gt;        textSelection.EndOfLine()&lt;br /&gt;        If textSelection.Text &lt;&gt; vbCrLf Then&lt;br /&gt;            textSelection.Text = textSelection.Text &amp; vbCrLf&lt;br /&gt;        End If&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    End Sub&lt;br /&gt;End Module&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6598260239900182697?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6598260239900182697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6598260239900182697' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6598260239900182697'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6598260239900182697'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/03/add-new-line-at-end-of-file.html' title='Add new line at end of file.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8980862624178821592</id><published>2009-03-27T12:20:00.000-07:00</published><updated>2009-03-27T12:22:44.068-07:00</updated><title type='text'>Visual Studio Macros for source control.</title><content type='html'>Visual Studio allows for users to customize the environment by writing macros. With Visual Studio 2008 the macros can be written in VB.Net. VB.Net is a very powerful language as it can use any of the standard .NET object, and in addition it has access to Visual Studios ENVDTE object.&lt;br /&gt;&lt;br /&gt;The following set of macros replace the buggy Perforce source control plugin. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Imports System&lt;br /&gt;Imports EnvDTE&lt;br /&gt;Imports EnvDTE80&lt;br /&gt;Imports System.Diagnostics&lt;br /&gt;&lt;br /&gt;Public Module MiscCommands&lt;br /&gt;    Sub P4Add()&lt;br /&gt;        Dim filename As String = DTE.ActiveWindow.Document.FullName&lt;br /&gt;        ChDir("c:\p4")&lt;br /&gt;        ExecuteCommand("p4.exe", "add " + filename)        &lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Sub P4Edit()&lt;br /&gt;        Dim filename As String = DTE.ActiveWindow.Document.FullName&lt;br /&gt;        ChDir("c:\p4")&lt;br /&gt;        ExecuteCommand("p4.exe", "edit " + filename)        &lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Sub P4Diff()&lt;br /&gt;        Dim filename As String = DTE.ActiveWindow.Document.FullName&lt;br /&gt;        ChDir("c:\p4")&lt;br /&gt;        ExecuteCommand("p4.exe", "diff " + filename)        &lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Sub P4Revert()&lt;br /&gt;        Dim filename As String = DTE.ActiveWindow.Document.FullName        &lt;br /&gt;        ChDir("c:\p4")&lt;br /&gt;        ExecuteCommand("p4.exe", "revert " + filename)        &lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Sub P4Opened()        &lt;br /&gt;        ChDir("c:\p4")&lt;br /&gt;        ExecuteCommand("p4.exe", "opened")&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Sub P4History()&lt;br /&gt;        Dim filename As String = DTE.ActiveWindow.Document.FullName        &lt;br /&gt;        ChDir("c:\p4")&lt;br /&gt;        ExecuteCommand("p4.exe", "filelog " + filename)&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Public Sub ExecuteCommand(ByVal filename As String, ByVal arguments As String)        &lt;br /&gt;&lt;br /&gt;        Dim p As New System.Diagnostics.Process&lt;br /&gt;        p.StartInfo.UseShellExecute = False&lt;br /&gt;        p.StartInfo.FileName = filename&lt;br /&gt;        p.StartInfo.Arguments = arguments&lt;br /&gt;        p.StartInfo.RedirectStandardOutput = True&lt;br /&gt;        p.StartInfo.RedirectStandardError = True&lt;br /&gt;        p.Start()&lt;br /&gt;        p.WaitForExit()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        WriteToMyNewPane(filename + " " + arguments, p.StandardOutput.ReadToEnd + p.StandardError.ReadToEnd)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;    Public Sub WriteToMyNewPane(ByVal command As String, ByVal results As String)&lt;br /&gt;        Dim win As Window = _&lt;br /&gt;           DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)&lt;br /&gt;        Dim ow As OutputWindow = win.Object&lt;br /&gt;        Dim owPane As OutputWindowPane&lt;br /&gt;        Dim cnt As Integer = ow.OutputWindowPanes.Count&lt;br /&gt;        owPane = ow.OutputWindowPanes.Item(1)&lt;br /&gt;        owPane.OutputString(command &amp; vbCrLf)&lt;br /&gt;        owPane.OutputString(results &amp; vbCrLf)&lt;br /&gt;        owPane.Activate()&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Sub OpenFile()&lt;br /&gt;        Dim fileName As String = InputBox("Open file:")&lt;br /&gt;        If String.IsNullOrEmpty(fileName) Then&lt;br /&gt;            Return&lt;br /&gt;        End If&lt;br /&gt;        Dim item As EnvDTE.ProjectItem = DTE.Solution.FindProjectItem(fileName)&lt;br /&gt;        If item Is Nothing Then&lt;br /&gt;            MsgBox("File not found", MsgBoxStyle.Exclamation)&lt;br /&gt;            Return&lt;br /&gt;        End If&lt;br /&gt;        item.Open()&lt;br /&gt;        item.Document.Activate()&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;End Module&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8980862624178821592?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8980862624178821592/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8980862624178821592' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8980862624178821592'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8980862624178821592'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/03/visual-studio-macros-for-source-control.html' title='Visual Studio Macros for source control.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-1120916762301970148</id><published>2009-02-05T18:15:00.000-08:00</published><updated>2009-02-05T19:31:41.038-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>C# Key Listener</title><content type='html'>CodeGuru has this &lt;a href="http://www.codeguru.com/forum/showthread.php?t=470040"&gt;post&lt;/a&gt;: &lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Hi All&lt;br /&gt;&lt;br /&gt;Looking for a little help..&lt;br /&gt;&lt;br /&gt;I would like to create a process that runs on both a Windows XP and Vista Machines that loads on startup, what this process will do is, on the "F12" keypress it brings up a simple form that a user fills in some details then clicks on send, then it goes back to sleep and waits on the "F12" key being pressed.&lt;br /&gt;&lt;br /&gt;Never created a process before so any help is appreciated.&lt;br /&gt;&lt;br /&gt;Cheers&lt;br /&gt;&lt;br /&gt;Djbell &lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;This post will describe how to do this in C#. We will approach this by creating an executable that could be loaded at startup. It will listen for the F12 keyboard event and present a message box.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Create a new C# Console project in Visual Studio, call it 'ListenForHotkey.'&lt;br /&gt;&lt;br /&gt;We will use a windows hook to notify our application when a key is pressed. So in the Program.cs file you should define four win32 API's. SetWindowsHookEx, UnhookWindowsHookEx, GetModuleName, CallNextHookEx. We'll also define a delegate for the callback, and some constants.&lt;br /&gt;&lt;br /&gt;Here is the Program.cs with these definitions.&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Runtime&lt;span style='color:#808030; '&gt;.&lt;/span&gt;InteropServices&lt;span style='color:#800080; '&gt;;&lt;/span&gt;   &lt;span style='color:#696969; '&gt;//Used for dll import&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Diagnostics&lt;span style='color:#800080; '&gt;;&lt;/span&gt;               &lt;span style='color:#696969; '&gt;//'Process'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; ListenForHotkey&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Program&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;        &lt;br /&gt;        &lt;span style='color:#808030; '&gt;[&lt;/span&gt;STAThread&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; Main&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;        &lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;        &lt;br /&gt;        &lt;span style='color:#696969; '&gt;// Define win32 api calls.&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;[&lt;/span&gt;DllImport&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;user32.dll&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CharSet &lt;span style='color:#808030; '&gt;=&lt;/span&gt; CharSet&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Auto&lt;span style='color:#808030; '&gt;,&lt;/span&gt; SetLastError &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;extern&lt;/span&gt; IntPtr SetWindowsHookEx&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; idHook&lt;span style='color:#808030; '&gt;,&lt;/span&gt; LowLevelKeyboardProc lpfn&lt;span style='color:#808030; '&gt;,&lt;/span&gt; IntPtr hMod&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;uint&lt;/span&gt; dwThreadId&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;[&lt;/span&gt;DllImport&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;user32.dll&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CharSet &lt;span style='color:#808030; '&gt;=&lt;/span&gt; CharSet&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Auto&lt;span style='color:#808030; '&gt;,&lt;/span&gt; SetLastError &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt; MarshalAs&lt;span style='color:#808030; '&gt;(&lt;/span&gt;UnmanagedType&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Bool&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;extern&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;bool&lt;/span&gt; UnhookWindowsHookEx&lt;span style='color:#808030; '&gt;(&lt;/span&gt;IntPtr hhk&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;[&lt;/span&gt;DllImport&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;kernel32.dll&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CharSet &lt;span style='color:#808030; '&gt;=&lt;/span&gt; CharSet&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Auto&lt;span style='color:#808030; '&gt;,&lt;/span&gt; SetLastError &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;extern&lt;/span&gt; IntPtr GetModuleHandle&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; lpModuleName&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;[&lt;/span&gt;DllImport&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;user32.dll&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CharSet &lt;span style='color:#808030; '&gt;=&lt;/span&gt; CharSet&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Auto&lt;span style='color:#808030; '&gt;,&lt;/span&gt; SetLastError &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;extern&lt;/span&gt; IntPtr CallNextHookEx&lt;span style='color:#808030; '&gt;(&lt;/span&gt;IntPtr hhk&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; nCode&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;            IntPtr wParam&lt;span style='color:#808030; '&gt;,&lt;/span&gt; IntPtr lParam&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;//Define delegate for the callback.&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;delegate&lt;/span&gt; IntPtr LowLevelKeyboardProc&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; nCode&lt;span style='color:#808030; '&gt;,&lt;/span&gt; IntPtr wParam&lt;span style='color:#808030; '&gt;,&lt;/span&gt; IntPtr lParam&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;//Define constants&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;const&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; WH_KEYBOARD_LL &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;13&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the code above we've defined a delegate called LowLevelKeyboardProc that is the signature of the callback. Let's add our callback.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#696969; '&gt;// Callback, executed when hook is triggered. In our case on keydown.            &lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; IntPtr HookCallback&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; nCode&lt;span style='color:#808030; '&gt;,&lt;/span&gt; IntPtr wParam&lt;span style='color:#808030; '&gt;,&lt;/span&gt; IntPtr lParam&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;            &lt;br /&gt;            System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Console&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WriteLine&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;key pressed&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;         &lt;br /&gt;            &lt;span style='color:#696969; '&gt;//Call next hook in chain.&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; CallNextHookEx&lt;span style='color:#808030; '&gt;(&lt;/span&gt;m_hookId&lt;span style='color:#808030; '&gt;,&lt;/span&gt; nCode&lt;span style='color:#808030; '&gt;,&lt;/span&gt; wParam&lt;span style='color:#808030; '&gt;,&lt;/span&gt; lParam&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Our callback simply prints that a key was pressed then calls the next hook if applicable. This is important else another application that register a hook may not be called.&lt;br /&gt;&lt;br /&gt;Lastly we should register our hook, and run the app. This is done in the main method.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#808030; '&gt;[&lt;/span&gt;STAThread&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; Main&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;Process curProcess &lt;span style='color:#808030; '&gt;=&lt;/span&gt; Process&lt;span style='color:#808030; '&gt;.&lt;/span&gt;GetCurrentProcess&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;ProcessModule curModule &lt;span style='color:#808030; '&gt;=&lt;/span&gt; curProcess&lt;span style='color:#808030; '&gt;.&lt;/span&gt;MainModule&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                m_hookId &lt;span style='color:#808030; '&gt;=&lt;/span&gt; SetWindowsHookEx&lt;span style='color:#808030; '&gt;(&lt;/span&gt;WH_KEYBOARD_LL&lt;span style='color:#808030; '&gt;,&lt;/span&gt; HookCallback&lt;span style='color:#808030; '&gt;,&lt;/span&gt; GetModuleHandle&lt;span style='color:#808030; '&gt;(&lt;/span&gt;curModule&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ModuleName&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;            System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Windows&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Forms&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Application&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Run&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            UnhookWindowsHookEx&lt;span style='color:#808030; '&gt;(&lt;/span&gt;m_hookId&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In main we register the callback then call Application.Run() which puts the app in an infinite loop by waiting for any system messages. When the program is ready to exit we unregister our hook.&lt;br /&gt;&lt;br /&gt;That's it. &lt;br /&gt;&lt;br /&gt;Hope it helps.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-1120916762301970148?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/1120916762301970148/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=1120916762301970148' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1120916762301970148'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/1120916762301970148'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/02/c-key-listener.html' title='C# Key Listener'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7056101276776789807</id><published>2009-02-05T17:25:00.000-08:00</published><updated>2009-02-05T17:34:56.074-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='gwt'/><title type='text'>GWT Programming Concepts</title><content type='html'>GWT Programming Concepts&lt;br /&gt;&lt;br /&gt;This tutorial will introduce the architecture of GWT. It is aimed at programmers with little or no Java experience, but with reasonable experience with other programming languages using object oriented programming.&lt;br /&gt;&lt;br /&gt;GWT is a single application. It consists of a series of panels. There may be a single panel onto which other panels are places, or multiple panels. You create and place Widgets onto these panels. Widgets are items like hyperlinks, textbox's, buttons, etc. With each of these widgets you can attach event listeners that will be execute when appropriate. For example you can attach a click listener to a button and when the user clicks the button, the action will be performed.&lt;br /&gt;&lt;br /&gt;The important part of GWT is that panels and widgets are added using Java code.&lt;br /&gt;Each widget (which includes a Panel) is an object that is derived from a number of base classes. For example:&lt;br /&gt;&lt;br /&gt;UIObject : the base class with properties or methods:&lt;br /&gt;&lt;br /&gt;    * Size&lt;br /&gt;    * Width&lt;br /&gt;    * toString&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Widget : derived from UIObject (so has its properties and methods), extended by:&lt;br /&gt;&lt;br /&gt;    * attached&lt;br /&gt;    * parent&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;FocusWidget : derived from Widget (so has its properties and methods), extended by:&lt;br /&gt;&lt;br /&gt;    * ClickListenerCollection&lt;br /&gt;    * addClickListener&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Button : derived from FocusWidget (so has its properties and methods), extended by:&lt;br /&gt;&lt;br /&gt;    * getHTML&lt;br /&gt;    * setHTML&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So through a series of inherited commands, the Panel object is instructed by GWT to deliver its HTML. Before delivering its own html, it instructs its children objects to deliver their html. Before their delivery, they also ask their children. And so on, until there are no unpolled children. In this way, the panel's html incorporates all the required html for all of its children.&lt;br /&gt;&lt;br /&gt;So as a programmer, all you need to do is to slap down some panels, add some widgets containing the text, buttons, menu items, response to button clicks etc that you want and save it.&lt;br /&gt;&lt;br /&gt;The command to "compile" the program takes all the Java objects you have created in your code, pulls out the required html and programmed actions and creates a final html container with a large amount of Javascript that writes the html you instructed through your Java objects.&lt;br /&gt;&lt;br /&gt;far more functional and much more fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7056101276776789807?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7056101276776789807/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7056101276776789807' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7056101276776789807'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7056101276776789807'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/02/gwt-programming-concepts.html' title='GWT Programming Concepts'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-615787836095964611</id><published>2009-01-14T00:41:00.000-08:00</published><updated>2009-01-14T00:43:34.373-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Protocol Buffers'/><title type='text'>Protocol Buffers</title><content type='html'>Protocol buffers is another way to serialize/deserialize data.&lt;br /&gt;&lt;br /&gt;It was written by Google and supports Java, C++, Perl, and Python.&lt;br /&gt;&lt;br /&gt;There website is at: http://code.google.com/p/protobuf/&lt;br /&gt;&lt;br /&gt;I'm planning on trying this out for a c++ project I maintain where we need blindingly fast ( as fast we can get ) serialization, and de-serialization performance.&lt;br /&gt;&lt;br /&gt;Once I've played with it, I'll post more information.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-615787836095964611?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/615787836095964611/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=615787836095964611' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/615787836095964611'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/615787836095964611'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/01/protocol-buffers.html' title='Protocol Buffers'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-2529824111989063993</id><published>2009-01-13T20:39:00.000-08:00</published><updated>2009-01-13T23:20:47.304-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><title type='text'>Flyweight Pattern</title><content type='html'>Flyweight design pattern is a software design pattern used to minimize memory usage by sharing data. It enables use of large number of objects that would typically require a lot of memory.&lt;br /&gt;&lt;br /&gt;A common example of the Flyweight pattern is string pooling. Consider the Java programming language. The String data type is immutable. Because it is guaranteed that the string can never be changed the strings are pooled to ensure that only one instance exists in memory at any given time. &lt;br /&gt;&lt;br /&gt;So if you create two strings s1 and s2 which both point to "foo" you really have two pointers to the same location in memory.&lt;br /&gt;&lt;br /&gt;Java also employs the Flyweight pattern for Integer object. new Integer(0) actually returns a pointer to pre-constructed object. So if you create one-thousand Objects which each contain an Object of type Integer you will only have one Integer(0) which is an excellent way to save memory.&lt;br /&gt;&lt;br /&gt;While your writing your programs consider if you could use the Flyweight pattern to save memory. An example I used was for a Object that contained three Strings. This tuple uniquely identified a configuration of runs that were stored in the database. The Strings were loaded via JDBC so they didn't get the String pooling provided by Java. Instead I made the constructor to the class private and exposed a public static method called 'get'. This method took the three strings and created the object if not already existing and returned a pointer. This cut my memory usage drastically.&lt;br /&gt;&lt;br /&gt;Here is an example of my use of the Flyweight pattern.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class Product {&lt;br /&gt;  private Product() { } // don't let anyone explicitly create this object.&lt;br /&gt;  private static Map&lt;int,Product&gt; products = new HashMap&lt;int,Product&gt;();&lt;br /&gt;  public get(String arch, String chip, String config) { &lt;br /&gt;    Product p = products.get(arch + chip + config).hashCode());&lt;br /&gt;    if (p == null)&lt;br /&gt;      products.put(arch + chip + config).hashCode(),new Product(arch,chip,config));&lt;br /&gt;    return products.get(arch + chip + config).hashCode());&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If your using c++ you should look at the boost flyweight class. It hides the implementation and makes adding a flyweight to your c++ code trivial. http://svn.boost.org/svn/boost/sandbox/flyweight/libs/flyweight/doc/index.html&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-2529824111989063993?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/2529824111989063993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=2529824111989063993' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2529824111989063993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2529824111989063993'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/01/flyweight-pattern.html' title='Flyweight Pattern'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-4769841699936335656</id><published>2009-01-13T13:29:00.001-08:00</published><updated>2009-01-13T13:31:14.968-08:00</updated><title type='text'>Counting code lines.</title><content type='html'>I was curious how many lines of code my Java application had. I found this script very useful in finding out. &lt;br /&gt;&lt;br /&gt;Here it is, in case it is helpful:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;find . -name "*java" -exec wc -l {} \; | grep -v "/test/" | awk '{print sum+=$1} END {print "Sum: "sum}'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;First it finds all files with the extension java (change to cpp, or pl, or py if you need to). It runs wc -l on each file to count the lines.&lt;br /&gt;&lt;br /&gt;Then it removes my test directory, you can use -vE for a regular expression of items to remove or remove that command all together. &lt;br /&gt;&lt;br /&gt;I then use awk to sum up the file line count and print out the increasing amount as well as the sum at the end. &lt;br /&gt;&lt;br /&gt;Hope that helps.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-4769841699936335656?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/4769841699936335656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=4769841699936335656' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4769841699936335656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4769841699936335656'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/01/counting-code-lines.html' title='Counting code lines.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5231030565530122472</id><published>2009-01-12T14:58:00.000-08:00</published><updated>2009-01-12T15:01:01.418-08:00</updated><title type='text'>Google Chrome planned Mac rease middle of 2009</title><content type='html'>The popular Google Chrome browser is planned to be released for Mac in the middle of 2009. This should bring competition for the current default browser, Safari.&lt;br /&gt;&lt;br /&gt;Google Chrome is also planning to add pluggable extensions which Mozilla's firefox has had for sometime. &lt;br /&gt;&lt;br /&gt;These two changes should make Google Chrome an even more likely choice for consumers. In the end more competition is always good and Google's entry into the market will help keep the innovations coming.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5231030565530122472?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5231030565530122472/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5231030565530122472' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5231030565530122472'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5231030565530122472'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/01/google-chrome-planned-mac-rease-middle.html' title='Google Chrome planned Mac rease middle of 2009'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7137382978493220764</id><published>2009-01-12T01:22:00.000-08:00</published><updated>2010-12-14T21:32:19.260-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='boost'/><title type='text'>Boost scoped lock</title><content type='html'>I have been very impressed with the Boost libraries. Each day I'm finding more and more uses of Boost that make C++ programming easier.&lt;br /&gt;&lt;br /&gt;My latest enjoyment comes from boost::mutex::scoped_lock.&lt;br /&gt;&lt;br /&gt;scoped_lock follows the RAII (Resource Allocation Is Initialization) pattern. When the lock is constructed it holds onto the mutex until it is destructed.&lt;br /&gt;&lt;br /&gt;Here is a simple example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class Foo &lt;br /&gt;{&lt;br /&gt;   public:&lt;br /&gt;   void bar() &lt;br /&gt;   {&lt;br /&gt;       // Will grab the resource or wait until free&lt;br /&gt;       ::boost::mutex::scoped_lock lock(m_mutex); &lt;br /&gt;       //Critical section&lt;br /&gt;&lt;br /&gt;       // No need to unlock the lock will do that itself.&lt;br /&gt;   }&lt;br /&gt;private:&lt;br /&gt;boost::mutex m_mutex;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;One word of caution. Up to Boost 1.36 the behavior of the mutex was different on Windows vs Linux. On Windows the lock would be recursive, that is you could call another method and enter it your thread already held that lock. Whereas on Linux that would cause a deadlock. The 1.37 version has fixed this issue and the both behave like the Linux version.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7137382978493220764?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7137382978493220764/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7137382978493220764' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7137382978493220764'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7137382978493220764'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/01/boost-scoped-lock.html' title='Boost scoped lock'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-86802179559799867</id><published>2009-01-07T22:27:00.000-08:00</published><updated>2009-01-09T02:19:38.861-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>Pro's and Con's of returing references</title><content type='html'>I've recently began working on a moderate size C++ application. The previous developer loved the benefits of returning const&amp;.&lt;br /&gt;&lt;br /&gt;For those who don't know returning const&amp; can avoid an extraneous copy. Example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;class Foo {&lt;br /&gt;  Foo(std::string s) { m_name = s; }&lt;br /&gt;  std::string m_name;&lt;br /&gt;public:&lt;br /&gt;  std::string const&amp; name() { return m_name; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The above code avoids the copy from m_name to the function return value that would have resulted with the following client code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;  Foo f("bar"); &lt;br /&gt;  std::string name = f.name(); &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;After working in this code base for a while now I believe that returning references are evil and should be treated just like returning a pointer, which is avoid it.&lt;br /&gt;&lt;br /&gt;For example the problem that arose that took a week to debug was the following: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;  class Foo {&lt;br /&gt;    std::vector&lt; Bar &gt; m_vec;&lt;br /&gt;  public:&lt;br /&gt;    void insert(Bar&amp; b) { m_vec.push_back(b); }&lt;br /&gt;    Bar const&amp; getById(int id) { return m_vec[id]; }&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The problem in this example is clients are calling and getting references that are stored in the vector. Now what happens after clients insert a bunch of new elements? The vector needs to resize internally and guess what happens to all those references? That's right there invalid. This caused a very hard to find bug that was simply fixed by removing the &amp;.&lt;br /&gt;&lt;br /&gt;The conclusion I've arrived at is that returning a &amp; is a pre-mature optimization. If your profiling shows you your code is very slow at this point than maybe consider it. But you should also be aware that most modern compilers optimize away this extra copy anyways. &lt;br /&gt;&lt;br /&gt;Here is a contrived example that shows problems you can get into by blindly returning references. My advice avoid them until your profiler tells you that you have a problem.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include &lt;string&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;sstream&gt;&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;class foo {&lt;br /&gt;    std::string m_string;&lt;br /&gt;    public:&lt;br /&gt;    std::string const  getName(){&lt;br /&gt;       static int count = 0;&lt;br /&gt;       count++;&lt;br /&gt;       std::stringstream ss;&lt;br /&gt;       ss &lt;&lt; count ;&lt;br /&gt;       m_string = ss.str();&lt;br /&gt;       return m_string;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;foo f;&lt;br /&gt;void fun(std::string const&amp;s) {&lt;br /&gt;    f.getName();&lt;br /&gt;    std::cout &lt;&lt; s;&lt;br /&gt;}&lt;br /&gt;int main() {&lt;br /&gt;    std::cout &lt;&lt; " print version " &lt;&lt; std::endl;&lt;br /&gt;    fun(f.getName());&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Hope that Helps&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-86802179559799867?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/86802179559799867/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=86802179559799867' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/86802179559799867'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/86802179559799867'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2009/01/pros-and-cons-of-returing-references.html' title='Pro&apos;s and Con&apos;s of returing references'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6045405900833609014</id><published>2008-11-19T12:42:00.000-08:00</published><updated>2008-11-19T12:44:38.557-08:00</updated><title type='text'>VIM formatting</title><content type='html'>To auto format a section of code in vim use '=' followed by a movement command.&lt;br /&gt;&lt;br /&gt;For example if you want to format &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;void foo() {&lt;br /&gt;  int blah;&lt;br /&gt;      int foo;&lt;br /&gt;          int bar;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Navigate the the first '{' and press '=%' = will tell it format, where '%' will tell it to go to the corresponding block.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6045405900833609014?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6045405900833609014/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6045405900833609014' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6045405900833609014'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6045405900833609014'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/11/vim-formatting.html' title='VIM formatting'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3169065029938520379</id><published>2008-09-25T01:19:00.000-07:00</published><updated>2008-09-25T01:21:33.527-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='php'/><title type='text'>PHP Sample Command Line Scrip</title><content type='html'>Some co-workers of mine didn't believe that PHP could be used from the command line. I was rather suprised by this, and thought others might be under the same impression. To help spread the word, here's a very simple example of a php script meant to be run from the command line. &lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;&amp;lt;?php&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$argc&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;!&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#008c00; background:#ffffe8; '&gt;2&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;|&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;|&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;in_array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$argv&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; background:#ffffe8; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;,&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'--help'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;,&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'-help'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;,&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'-h'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;,&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'-?'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;?&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;This is a command line PHP script with one option&lt;/span&gt;&lt;span style='color:#008c00; background:#ffffff; '&gt;.&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;  Usage:&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;  &lt;/span&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;&amp;lt;?php&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;echo&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$argv&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; background:#ffffe8; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;?&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt; &lt;/span&gt;&lt;span style='color:#a65700; background:#ffffff; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; background:#ffffff; font-weight:bold; '&gt;option&lt;/span&gt;&lt;span style='color:#a65700; background:#ffffff; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;  &lt;/span&gt;&lt;span style='color:#a65700; background:#ffffff; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; background:#ffffff; font-weight:bold; '&gt;option&lt;/span&gt;&lt;span style='color:#a65700; background:#ffffff; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt; can be some word you would like&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;  to print out&lt;/span&gt;&lt;span style='color:#008c00; background:#ffffff; '&gt;.&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt; With the --help, -help, -h,&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;  or -? options, you can get this help&lt;/span&gt;&lt;span style='color:#008c00; background:#ffffff; '&gt;.&lt;/span&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffff; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;&amp;lt;?php&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;else&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;    &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;echo&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$argv&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; background:#ffffe8; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;?&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3169065029938520379?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3169065029938520379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3169065029938520379' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3169065029938520379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3169065029938520379'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/php-sample-command-line-scrip.html' title='PHP Sample Command Line Scrip'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-2701972261365120180</id><published>2008-09-25T01:01:00.000-07:00</published><updated>2008-09-25T01:14:56.430-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='web service client'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><title type='text'>PHP Web Service example.</title><content type='html'>Its been a while since I used PHP. The last time I used it was PHP 4. I felt it was time to revisit the language. I have good memories of PHP. It was better than perl and it worked reasonably well for a mediawiki installation I maintained. &lt;br /&gt;&lt;br /&gt;If you view the other post I've made, you'll see that a first test of a language that I like to do is to see if it supports a simple SOAP call. This tutorial will be the same test for PHP.&lt;br /&gt;&lt;br /&gt;First lets install it. If you use any *nix system you'll probably already have it installed. If not or if you want the latest copy then go to &lt;a href="http://www.php.net/downloads.php"&gt;http://www.php.net/downloads.php&lt;/a&gt; and download the version for you install. I chose the windows installer. During the setup process it asked which extensions I would like installed I opted for the SOAP library which will be needed for the rest of the tutorial. &lt;br /&gt;&lt;br /&gt;I installed PHP in the default location of c:\program files\php, so I opened a command prompt and browsed to that location.&lt;br /&gt;&lt;br /&gt;Now for our simple web service test. We'll be accessing 'http://webservices.daehosting.com/services/isbnservice.wso?WSDL' and asking if a given ISBN is valid or not. &lt;br /&gt;&lt;br /&gt;The first task is create a new SOAPClient object.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$client&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;new&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;SOAPClient&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'http://webservices.daehosting.com/services/isbnservice.wso?WSDL'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now we call our method which is named '&gt;IsValidISBN13'. Like perl's SOAP::Lite we need to give a hash as our argument so the web service knows which variable were passing so we first create a parameter list, then call the function.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$params&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'sISBN'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;"9781565926103"&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$result&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$client&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;-&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;IsValidISBN13&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$params&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So far so good. Now the only tricky part I found of the process. The PHP Soap libraries return a stdClass. We need to unravel this into something meaningful to us. The documentation uses this function so I did as well.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;function&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$obj&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;foreach&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$obj&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;as&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;    &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;switch&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;        &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;case&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;is_object&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;:&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;break&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;      &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;case&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;is_array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;:&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;break&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;      &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;default&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;:&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;        &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;    &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;return&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Then lastly I just print out the array which contains our result:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;print_r&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$result&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can now experiment by passing different ISBN's ones that are both valid and invalid and you'll see the result is different. You can also call the method that checks for validity of a 10 character length ISBN. &lt;br /&gt;&lt;br /&gt;Here is the entire code listing.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;&amp;lt;?php&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$client&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;new&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;SOAPClient&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'http://webservices.daehosting.com/services/isbnservice.wso?WSDL'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$params&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;'sISBN'&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#0000e6; background:#ffffe8; '&gt;"9781565926103"&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$result&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$client&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;-&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;IsValidISBN13&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$params&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;function&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$obj&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;foreach&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$obj&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;as&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;    &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;switch&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;{&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;        &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;case&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;is_object&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;:&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;break&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;      &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;case&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;is_array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;:&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;         &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;break&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;      &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;default&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;:&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;        &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;[&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$key&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;]&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;=&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$val&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;    &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;  &lt;/span&gt;&lt;span style='color:#800000; background:#ffffe8; font-weight:bold; '&gt;return&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt; &lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$out&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;}&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#400000; background:#ffffe8; '&gt;print_r&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;obj2array&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;(&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;$result&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; background:#ffffe8; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; background:#ffffe8; '&gt;;&lt;/span&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#000000; background:#ffffe8; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#a65700; background:#ffffe8; '&gt;?&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-2701972261365120180?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/2701972261365120180/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=2701972261365120180' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2701972261365120180'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2701972261365120180'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/php-web-service-example.html' title='PHP Web Service example.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5814705620941046141</id><published>2008-09-22T22:11:00.001-07:00</published><updated>2008-09-22T22:27:12.403-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='boost'/><title type='text'>Boost Python - Abstract Classes</title><content type='html'>In the &lt;a href="http://developer-resource.blogspot.com/2008/09/boost-python.html"&gt;previous article&lt;/a&gt; I covered setting up Boost Python to work in Visual Studio. &lt;br /&gt;&lt;br /&gt;In this article were going to get into a bit more detail of describing the bindings of classes. Specifically were going to deal with abstract classes and polymorphism.&lt;br /&gt;&lt;br /&gt;Consider the following:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;struct&lt;/span&gt; AbstractClass &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    virtual std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;string speak&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;const&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;struct&lt;/span&gt; DerivedClassA &lt;span style='color:#800080; '&gt;:&lt;/span&gt; AbstractClass &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    virtual std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;string speak&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;const&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Derived Class A&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;struct&lt;/span&gt; DerivedClassB &lt;span style='color:#800080; '&gt;:&lt;/span&gt; AbstractClass &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    virtual std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;string speak&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;const&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Derived Class B&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We have an Abstract Class and two derived class that implement the speak method. How do we model this in Boost Python such that in Python we can get a pointer to one of these objects and call speak on the proper derived class. &lt;br /&gt;&lt;br /&gt;To do this we have to build a wrapper class:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;struct&lt;/span&gt; WrapperClass &lt;span style='color:#800080; '&gt;:&lt;/span&gt; AbstractClass &lt;span style='color:#808030; '&gt;,&lt;/span&gt; boost&lt;span style='color:#800080; '&gt;::&lt;/span&gt;python&lt;span style='color:#800080; '&gt;::&lt;/span&gt;wrapper&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;AbstractClass&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt; &lt;br /&gt;    virtual std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;string speak&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;const&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;get_override&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;speak&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This wrapper class extends the AbstractClass, and extends a special boost::python::wrapper&lt;AbstractClass&gt;. For each virtual method you wrap it and call it with this-&gt;get_override(&lt;method name&gt;).&lt;br /&gt;&lt;br /&gt;Lastly you need to define the bindings for Python, this is similar to the Hello World example in article one:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;BOOST_PYTHON_MODULE&lt;span style='color:#808030; '&gt;(&lt;/span&gt;PyHelloWorld&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; namespace boost&lt;span style='color:#800080; '&gt;::&lt;/span&gt;python&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    def&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;getDerivedClassA&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;getDerivedClassA&lt;span style='color:#808030; '&gt;,&lt;/span&gt;return_value_policy&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; manage_new_object &lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    def&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;getDerivedClassB&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;getDerivedClassB&lt;span style='color:#808030; '&gt;,&lt;/span&gt;return_value_policy&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; manage_new_object &lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    class_&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;WrapperClass&lt;span style='color:#808030; '&gt;,&lt;/span&gt; boost&lt;span style='color:#800080; '&gt;::&lt;/span&gt;noncopyable &lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;DerivedClass&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;br /&gt;        &lt;span style='color:#808030; '&gt;.&lt;/span&gt;def&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;speak&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;boost&lt;span style='color:#800080; '&gt;::&lt;/span&gt;python&lt;span style='color:#800080; '&gt;::&lt;/span&gt;pure_virtual&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;AbstractClass&lt;span style='color:#800080; '&gt;::&lt;/span&gt;speak&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here we added two test methods called getDerivedClassA and getDerivedClassB these will be in the package PyHelloWorld as dictated by 'BOOST_PYTHON_MODULE(PyHelloWorld)'&lt;br /&gt;&lt;br /&gt;Lastly we want to be able to execute python in the same process as our executable. If you followed the instructions in part 1 then you simply need to add this method:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; start&lt;span style='color:#808030; '&gt;(&lt;/span&gt; &lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;        &lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; namespace boost&lt;span style='color:#800080; '&gt;::&lt;/span&gt;python&lt;span style='color:#800080; '&gt;;&lt;/span&gt;            &lt;br /&gt;    Py_Initialize&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;    &lt;br /&gt;    &lt;span style='color:#696969; '&gt;// Explicitly call the method that the BOOST_PYTHON_MODULE macro created.&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#696969; '&gt;// This sets up our DLL to be used by python. &lt;/span&gt;&lt;br /&gt;    initPyHelloWorld&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;string pythonCommand&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    try &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        PyRun_SimpleString&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;import PyHelloWorld&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;        &lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;while&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;cout &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;&gt;&gt;&gt; &lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            getline&lt;span style='color:#808030; '&gt;(&lt;/span&gt;std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;cin&lt;span style='color:#808030; '&gt;,&lt;/span&gt;pythonCommand&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;                           &lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;pythonCommand &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;quit&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;break&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;else&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                std&lt;span style='color:#800080; '&gt;::&lt;/span&gt;cout &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; PyRun_SimpleString&lt;span style='color:#808030; '&gt;(&lt;/span&gt;pythonCommand&lt;span style='color:#808030; '&gt;.&lt;/span&gt;c_str&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt; catch &lt;span style='color:#808030; '&gt;(&lt;/span&gt; error_already_set &lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        PyErr_Print&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    Py_Finalize&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;                    &lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So now lets try it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; import PyHelloWorld&lt;br /&gt;0&gt;&gt;&gt; a = PyHelloWorld.getDerivedClassA()&lt;br /&gt;0&gt;&gt;&gt; b = PyHelloWorld.getDerivedClassB()&lt;br /&gt;0&gt;&gt;&gt; print a.speak()&lt;br /&gt;Derived Class A&lt;br /&gt;0&gt;&gt;&gt; print b.speak()&lt;br /&gt;Derived Class B&lt;br /&gt;0&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5814705620941046141?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5814705620941046141/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5814705620941046141' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5814705620941046141'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5814705620941046141'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/boost-python-abstract-classes.html' title='Boost Python - Abstract Classes'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5691262909027642370</id><published>2008-09-18T22:20:00.000-07:00</published><updated>2008-09-18T23:52:16.856-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='boost'/><title type='text'>Boost Python</title><content type='html'>Boost Python allows seamless interoperability between C++ and Python. In this article we'll setup Boost and build a simple hello world program that will be able to run in Python.&lt;br /&gt;&lt;br /&gt;Boost, in case your unfamiliar, is a powerful set of libraries for C++. There website is at &lt;a href="http://www.boost.org/"&gt;www.boost.org&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;For this post we will be using &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=7586&amp;amp;package_id=8041&amp;amp;release_id=619445"&gt;Boost 1.36.0&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Boost can be tricky to setup. Their documentation recommends using their own build tool called bjam, but I typically like to build it myself. For this article we'll walk through setting up Boost Python for Visual Studio 2005.&lt;br /&gt;&lt;br /&gt;If you haven't already, download boost 1.36.0 library to your local machine. I've expanded the library to c:\download\boost\boost-1.36.0 on my machine.&lt;br /&gt;&lt;br /&gt;Now open up Visual Studio 2005. New Project, Visual C++ -&gt; Win32 -&gt; Win32 Console Applicaion.&lt;br /&gt;&lt;br /&gt;At the bottom of the screen enter the name for your project: PyHelloWorld, and the location, in my case c:\download\PyHelloWord. The solution name should be filled out for you (PyHelloWorld), and the check mark for 'Create directory for solution' should be checked.&lt;br /&gt;&lt;br /&gt;Click Ok.&lt;br /&gt;&lt;br /&gt;In the wizard click next. Select 'Dll' as application type, and Empty Project from additional options.&lt;br /&gt;&lt;br /&gt;Click finish.&lt;br /&gt;&lt;br /&gt;The project we just created will hold our client code, and our python bindings code. Before we can do that though we need to build Boost Python. To do this we'll create another project.&lt;br /&gt;&lt;br /&gt;Right click on your solution 'PyHelloWorld', Click Add-&gt;Add New Project.&lt;br /&gt;&lt;br /&gt;Like before we'll create a Win32 Console Application but we'll call it BoostPython this time. I've decided to keep the project located in the same solution folder of PyHelloWorld.&lt;br /&gt;&lt;br /&gt;Follow the steps in the wizard again to create an empty, dll project.&lt;br /&gt;&lt;br /&gt;In order to build boost python we need to include all the source files. Visual studio should have created a 'Source Files' filter in the BoostPython project. Right click and select Add-&gt;Existing Item...&lt;br /&gt;&lt;br /&gt;You want to add all the files from&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;%PATH_TO_BOOST%\libs\python\src&lt;/li&gt;&lt;br /&gt;&lt;li&gt;%PATH_TO_BOOST%\libs\python\src\converter&lt;/li&gt;&lt;br /&gt;&lt;li&gt;%PATH_TO_BOOST%\libs\python\src\object&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;You now need to instruct the compiler that these are part of the boost source code. You do this by setting a preprocessor define. Right click on the BoostPython project, select c/c++ from the property pages, select Preprocessor, and append 'BOOST_PYTHON_SOURCE' to the end of the Preprocessor definitions. Its a semi-colon separated list so add a semi-colon then 'BOOST_PYTHON_SOURCE'.&lt;br /&gt;&lt;br /&gt;Next set the include directory to find the Boost install. Right click on the BoostPython project, open the c/c++ -&gt; General tab. Add your path to %PATH_TO_BOOST%, in my case that is C:\download\boost\boost_1_36_0&lt;br /&gt;&lt;br /&gt;We've now set up the boost portion, we need to setup python by telling our project how to find our local python installation. First you'll need to &lt;a href="http://www.python.org/download/"&gt;download Python&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The standard install saved Python under c:\Python25 on my machine. The standard python install comes with c++ headers so we'll point our BoostPython project at those headers.&lt;br /&gt;&lt;br /&gt;Once again open the property page for BoostPython, C/C++ -&gt; General , and add c:\Python25\include (or your python install) to the include path.&lt;br /&gt;&lt;br /&gt;You can now build the BoostPython project, all the files should compile. The link step will fail though because it needs to link to the Python library as well. So one more tweak to the BoostPython project, this time Linker -&gt; Input , Additional Dependencies. Add C:\Python25\libs\Python25.lib.&lt;br /&gt;&lt;br /&gt;Build the project again and it should link successfully.&lt;br /&gt;&lt;br /&gt;Okay now we have BoostPython ready to go. Lets Use it.&lt;br /&gt;&lt;br /&gt;For the PyHelloWorld project we'll need to do the same steps as above to set the Include Path, and the Linker. More explicity you need to set:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;include path to boost&lt;/li&gt;&lt;li&gt;include path to python&lt;/li&gt;&lt;li&gt;linker to python lib&lt;/li&gt;&lt;/ul&gt;If you don't remember how look above to see what we set.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now create a new cpp file by right clicking on the project, add-&gt;New Item. Selecting cpp file. Call the file hello.cpp&lt;br /&gt;&lt;br /&gt;Add the following  to the file.&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#004a43; '&gt;#&lt;/span&gt;&lt;span style='color:#004a43; '&gt;define&lt;/span&gt;&lt;span style='color:#004a43; '&gt; BOOST_ALL_NO_LIB&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#004a43; '&gt;#&lt;/span&gt;&lt;span style='color:#004a43; '&gt;include &lt;/span&gt;&lt;span style='color:#800000; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#40015a; '&gt;boost/python.hpp&lt;/span&gt;&lt;span style='color:#800000; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;char&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;const&lt;/span&gt;&lt;span style='color:#808030; '&gt;*&lt;/span&gt; speak&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Hello World!&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;BOOST_PYTHON_MODULE&lt;span style='color:#808030; '&gt;(&lt;/span&gt;PyHelloWorld&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; namespace boost&lt;span style='color:#800080; '&gt;::&lt;/span&gt;python&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    def&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;speak&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; speak&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The first define instruct boost that we have already build it. The first function is your standard c++ function, the second is our python bindings. &lt;br /&gt;&lt;br /&gt;Before you build you need to tell visual studio that this project is dependent on the BoostPython Project. Right click on PyHelloWorld project and click Project Dependencies. Select BoostPython.&lt;br /&gt;&lt;br /&gt;Now Build. Everything should compile and build succesfully.&lt;br /&gt;&lt;br /&gt;So what have we accomplished? We've build a dll that exposes method that python can understand. Lets test it and see if it works. &lt;br /&gt;&lt;br /&gt;Open a Command shell, and navigate to your output directory. In my case its c:\download\PyHelloWorld\PyHelloWorld\debug. In order to make it easier for Python to find our dll lets rename it to pyd. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;move PyHelloWorld.dll PyHelloWorld.pyd&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now lets load up the interpreter and try it out:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;c:\download\PyHelloWorld\PyHelloWorld\debug&gt;c:\Python25\python.exe&lt;br /&gt;Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32&lt;br /&gt;Type "help", "copyright", "credits" or "license" for more information.&lt;br /&gt;&gt;&gt;&gt; import PyHelloWorld&lt;br /&gt;&gt;&gt;&gt; print PyHelloWorld.speak();&lt;br /&gt;Hello World!&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There you go, python interacting with your c++ file. Now that you have the BoostPython project created it would be easy to do this binding for more complex types. The official docs cover how to use this for classes, virtual functions, etc. For more info I recommend you start there. &lt;a href="http://www.boost.org/doc/libs/1_36_0/libs/python/doc/tutorial/doc/html/index.html"&gt;http://www.boost.org/doc/libs/1_36_0/libs/python/doc/tutorial/doc/html/index.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Hope that helps...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5691262909027642370?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5691262909027642370/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5691262909027642370' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5691262909027642370'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5691262909027642370'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/boost-python.html' title='Boost Python'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8420189611972844320</id><published>2008-09-17T23:33:00.000-07:00</published><updated>2008-09-17T23:49:09.027-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><title type='text'>Enforce Noninstantiability With a Private Constructor</title><content type='html'>Occasionally you'll want to write a class that is just a grouping of static methods and static fields. Such classes have acquired a bad reputation because some people abuse them to avoid thinking in terms of objects, but they do have valid uses. They can be used to group related methods on primitive values or arrays, in the manner of java.lang.Math or java.util.Arrays. They can also be used to group static methods, including factory methods, for objects that implement a particular interface, in the manner of java.util.Collections. Lastly, they can be used to group methods on a final class, instead of extending the class.&lt;br /&gt;&lt;br /&gt;Such utility classes were not designed to be instantiated: an instance would be nonsensical. In the absence of explicit constructors, however, the compiler provides a public, parameterless default constructor. To a user, this constructor is indistinguishable from any other. It is not uncommon to see unintentionally instantiable classes in published APIs.&lt;br /&gt;&lt;br /&gt;Attempting to enforce noninstantiability by making a class abstract does not work. The class can be subclassed and the subclass instantiated. Furthermore, it misleads the user into thinking the class was designed for inheritance (Item 17). There is, however, a simple idiom to ensure noninstantiability. A default constructor is generated only if a class contains no explicit constructors, so a class can be made noninstantiable by including a private constructor:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#696969; '&gt;// Noninstantiable utility class&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; UtilityClass &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#696969; '&gt;// Suppress default constructor for noninstantiability&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; UtilityClass&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;throw&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; AssertionError&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt; &lt;span style='color:#696969; '&gt;// Remainder omitted&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Because the explicit constructor is private, it is inaccessible outside of the class. The AssertionError isn't strictly required, but it provides insurance in case the constructor is accidentally invoked from within the class. It guarantees that the class will never be instantiated under any circumstances. This idiom is mildly counterintuitive, as the constructor is provided expressly so that it cannot be invoked. It is therefore wise to include a comment, as shown above.&lt;br /&gt;&lt;br /&gt;As a side effect, this idiom also prevents the class from being subclassed. All constructors must invoke a superclass constructor, explicitly or implicitly, and a subclass would have no accessible superclass constructor to invoke. &lt;br /&gt;&lt;br /&gt;This pattern also works well when you want the ability to cache an object. For example if you were creating a String class that is immutable. For every String "FOO" created you want one instance. If you make the class notinstantiable and instead add a String.get("foo") method, you can ensure that you return one and only one instance. For example:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; MyString &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; MyString&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; WeakHashMap&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; map &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; WeakHashMap&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt; s&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;map&lt;span style='color:#808030; '&gt;.&lt;/span&gt;get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;s&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; map&lt;span style='color:#808030; '&gt;.&lt;/span&gt;get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;s&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;else&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;      map&lt;span style='color:#808030; '&gt;.&lt;/span&gt;put&lt;span style='color:#808030; '&gt;(&lt;/span&gt;s&lt;span style='color:#808030; '&gt;,&lt;/span&gt;s&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;   &lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; map&lt;span style='color:#808030; '&gt;.&lt;/span&gt;get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;s&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In fact this pattern is used for the Java Integer class. It is recommended you use Integer.valueOf(int i). We can see why by looking at their code:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; IntegerCache &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; IntegerCache&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;final&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;Integer&lt;/span&gt; cache&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;Integer&lt;/span&gt;&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;128&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#808030; '&gt;+&lt;/span&gt; &lt;span style='color:#008c00; '&gt;127&lt;/span&gt; &lt;span style='color:#808030; '&gt;+&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#bb7977; '&gt;int&lt;/span&gt; i &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; i &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; cache&lt;span style='color:#808030; '&gt;.&lt;/span&gt;length&lt;span style='color:#800080; '&gt;;&lt;/span&gt; i&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        cache&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;Integer&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;i &lt;span style='color:#808030; '&gt;-&lt;/span&gt; &lt;span style='color:#008c00; '&gt;128&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#3f5fbf; '&gt;/**&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; Returns a &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;Integer&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;/tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; instance representing the specified&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;int&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;/tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; value.&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; If a new &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;Integer&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;/tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; instance is not required&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;,&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; this method&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; should generally be used in preference to the constructor&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; {&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;@&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;link &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;#&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;Integer(int)}&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;,&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; as this method is likely to yield&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; significantly better space and time performance by caching&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; frequently requested values.&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;@param&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;  i an &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;code&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;int&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;/code&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; value.&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;@return&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; a &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;Integer&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;/tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; instance representing &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;i&lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;&amp;lt;/tt&gt;&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;.&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;*&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt; &lt;/span&gt;&lt;span style='color:#7f9fbf; font-weight:bold; '&gt;@since&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;  &lt;/span&gt;&lt;span style='color:#008c00; '&gt;1.5&lt;/span&gt;&lt;span style='color:#3f5fbf; '&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#3f5fbf; '&gt;     */&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; Integer valueOf(int i) &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;final&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;int&lt;/span&gt; offset &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;128&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;i &lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;128&lt;/span&gt; &lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt; i &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;127&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt; &lt;span style='color:#696969; '&gt;// must cache &lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; IntegerCache&lt;span style='color:#808030; '&gt;.&lt;/span&gt;cache&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i &lt;span style='color:#808030; '&gt;+&lt;/span&gt; offset&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;Integer&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;For any value between -128 and 127 the class has already been constructed and when you call it, you simply get a pointer to that Object.&lt;br /&gt;&lt;br /&gt;So in conclusion when you have a class that you don't always want instantiated you should consider making the constructor private.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8420189611972844320?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8420189611972844320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8420189611972844320' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8420189611972844320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8420189611972844320'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/enforce-noninstantiability-with-private.html' title='Enforce Noninstantiability With a Private Constructor'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-9204433361606712783</id><published>2008-09-16T00:48:00.000-07:00</published><updated>2008-09-16T00:54:00.959-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>QuickSort (c++)</title><content type='html'>Quicksort is a popular sorting algorithm. It runs in O(n log n). The sort is not stable meaning equal values may not appear in the same order. For this post we'll write a simple quick sort implementation. &lt;br /&gt;&lt;br /&gt;Lets start with the entire code listing then we'll break it apart: &lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; swap&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;a&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;b&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; temp &lt;span style='color:#808030; '&gt;=&lt;/span&gt; a &lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    a &lt;span style='color:#808030; '&gt;=&lt;/span&gt; b&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    b &lt;span style='color:#808030; '&gt;=&lt;/span&gt; temp&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; partition&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#808030; '&gt;*&lt;/span&gt; arr&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; left&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; right&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; pivotIndex&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; pivotValue &lt;span style='color:#808030; '&gt;=&lt;/span&gt; arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;pivotIndex&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    swap&lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;right&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;pivotIndex&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; storeIndex &lt;span style='color:#808030; '&gt;=&lt;/span&gt; left&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; i&lt;span style='color:#808030; '&gt;=&lt;/span&gt;left&lt;span style='color:#800080; '&gt;;&lt;/span&gt;i&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;right &lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#808030; '&gt;+&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; pivotValue&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            swap&lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;storeIndex&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            storeIndex&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    swap&lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;storeIndex&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;right&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; storeIndex&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; quicksort&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#808030; '&gt;*&lt;/span&gt; arr&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; left &lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; right&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;  &lt;span style='color:#808030; '&gt;(&lt;/span&gt;right &lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; left&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; pivotIndex &lt;span style='color:#808030; '&gt;=&lt;/span&gt; left&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; pivotNewIndex &lt;span style='color:#808030; '&gt;=&lt;/span&gt; partition&lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;,&lt;/span&gt;left&lt;span style='color:#808030; '&gt;,&lt;/span&gt;right&lt;span style='color:#808030; '&gt;,&lt;/span&gt;pivotIndex&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;        quicksort&lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;,&lt;/span&gt;left&lt;span style='color:#808030; '&gt;,&lt;/span&gt;pivotNewIndex&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        quicksort&lt;span style='color:#808030; '&gt;(&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;,&lt;/span&gt;pivotNewIndex&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;right&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#400000; '&gt;main&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; argc&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;char&lt;/span&gt;&lt;span style='color:#808030; '&gt;*&lt;/span&gt;&lt;span style='color:#808030; '&gt;*&lt;/span&gt; argv&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt; &lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;6&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;6&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;8&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;9&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#008c00; '&gt;6&lt;/span&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    quicksort&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;amp;&lt;/span&gt;arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;9&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; i &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;0&lt;/span&gt; &lt;span style='color:#800080; '&gt;;&lt;/span&gt; i &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; &lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#808030; '&gt;+&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        printf&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0f69ff; '&gt;%d&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#808030; '&gt;,&lt;/span&gt; arr&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We have 3 methods not including main. Swap is a utility method that swaps the values.&lt;br /&gt;&lt;br /&gt;The heart of the algorithm is the partition method. We choose value in the array and do a O(n) walk of the list putting all elements with value less than the chosen value before and all elements greater than the chosen value after the value. Our code is optimized to do it in place without requiring an extra array by moving the pivot to the end and then swapping it back at the end.&lt;br /&gt;&lt;br /&gt;Once we have the partition method we simply recursively call quicksort and the list will be sorted.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-9204433361606712783?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/9204433361606712783/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=9204433361606712783' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/9204433361606712783'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/9204433361606712783'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/quicksort-c.html' title='QuickSort (c++)'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3216324501977590964</id><published>2008-09-13T16:51:00.000-07:00</published><updated>2008-09-13T17:39:14.840-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='canvas'/><title type='text'>Javascript Hangman</title><content type='html'>Hangman is a popular pencil and paper game. One player chooses a word and draws dashes to represent the length of the word.&lt;br /&gt;&lt;br /&gt;The other player guesses letters. If the letter is in the word the dash is replaced with the letter. If the guessed letter is not in the word then a part of the hanging body is drawn (head, body, left leg, right  leg, left arm, right arm) once the body is completely drawn or the entire word has been guessed the game is over.&lt;br /&gt;&lt;br /&gt;I decided to re-create this game using Javascript and the html canvas object. Javascript is used to handle the game while the canvas is used to draw the hangman character when the guesses are incorrect.&lt;br /&gt;&lt;br /&gt;To start lets define a simple html page that includes our canvas and a couple of spans to hold the word and messages for the user.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;html&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;body&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;onload&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"draw();"&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;onkeyup&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"handleKeyUp(event);"&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;table&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;tr&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;td&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;colspan&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;span&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;id&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'alreadyGuessed'&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;style&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;visibility&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt;&lt;span style='color:#074726; '&gt;hidden&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;Letter already guessed&lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;span&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;span&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;id&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'strike'&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;style&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;visibility&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt;&lt;span style='color:#074726; '&gt;hidden&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;Strike!&lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;span&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;td&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;tr&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;tr&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;td&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#5f5035; '&gt;canvas&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;id&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'hangman'&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;width&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;150&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;height&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;150&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;td&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;    &lt;br /&gt;            &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;td&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#a65700; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;span&lt;/span&gt;&lt;span style='color:#274796; '&gt; &lt;/span&gt;&lt;span style='color:#074726; '&gt;id&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'word'&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;span&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;                        &lt;br /&gt;            &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;td&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;tr&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;table&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;body&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#a65700; '&gt;&amp;lt;/&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;html&lt;/span&gt;&lt;span style='color:#a65700; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;When the page is loaded we call 'draw()' and we setup a keyboard listener to listen to keystrokes. We've defined a few simple span's that will be messages to the user, a canvas to draw the victim and a span to hold the word that the user will be guessing.&lt;br /&gt;&lt;br /&gt;The game expects a character from A-Z so our keyboard listener should filter out all other values here is what our listener looks like:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; handleKeyUp&lt;span style='color:#808030; '&gt;(&lt;/span&gt;evt&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; e &lt;span style='color:#808030; '&gt;=&lt;/span&gt; evt &lt;span style='color:#800080; '&gt;?&lt;/span&gt; evt &lt;span style='color:#800080; '&gt;:&lt;/span&gt; event&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;keyCode &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;65&lt;/span&gt; &lt;span style='color:#808030; '&gt;&amp;amp;&amp;amp;&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;keyCode &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;65&lt;/span&gt;&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#008c00; '&gt;26&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    guess&lt;span style='color:#808030; '&gt;(&lt;/span&gt;String&lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;fromCharCode&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;keyCode&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;            &lt;br /&gt;  &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;  draw&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The keyCode value is the ASCII representation we know that the ASCII characters are in sequence with capital 'A' being 65 and capital 'Z' being 65+26 characters away. If the user enters a valid character we enter our 'guess' function else we do nothing. After guessing our draw() function is called to redraw the screen.&lt;br /&gt;&lt;br /&gt;The draw function as you have now seen is called when the game is first loaded and after each keypress lets see what it does.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; draw&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; str&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;''&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;i&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;i&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;word&lt;span style='color:#808030; '&gt;.&lt;/span&gt;length&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;span style='color:#808030; '&gt;++&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;foundLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                str &lt;span style='color:#808030; '&gt;+=&lt;/span&gt; word&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;else&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                str &lt;span style='color:#808030; '&gt;+=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;'_'&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        str &lt;span style='color:#808030; '&gt;+=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;' '&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'word'&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;innerHTML &lt;span style='color:#808030; '&gt;=&lt;/span&gt; str&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The draw function looks at our internal data structures which will explore later and determines if the user has guessed a letter yet or not. If the letter has been guessed the letter is shown, else a '_' is shown instead.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So now to the heart of the game, the guess function.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; guess&lt;span style='color:#808030; '&gt;(&lt;/span&gt;key&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        hideMessages&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;guessedLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;key&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            showAlreadyGuessed&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        guessedLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;key&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0f4d75; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; found &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0f4d75; '&gt;false&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;i&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;i&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;word&lt;span style='color:#808030; '&gt;.&lt;/span&gt;length&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;span style='color:#808030; '&gt;++&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;word&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;==&lt;/span&gt; key&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                foundLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0f4d75; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                found&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0f4d75; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;!found&lt;span style='color:#808030; '&gt;)&lt;/span&gt; showStrike&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; found&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The guess function iterates over the chosen word and determines if the character in the word has been guessed. If it has it marks foundLetters[index] where index is the index of the character that has been revealed. We saw in the 'draw' function how this is used to change the display.&lt;br /&gt;&lt;br /&gt;The last thing to note is how we use the canvas element to draw a very crude hangman as the user guesses incorrectly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; drawHangMan&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; canvas &lt;span style='color:#808030; '&gt;=&lt;/span&gt; document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'hangman'&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getContext&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; ctx  &lt;span style='color:#808030; '&gt;=&lt;/span&gt; canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getContext&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'2d'&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;save&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;translate&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;45&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;45&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;clearRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;width&lt;span style='color:#808030; '&gt;,&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;height&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;beginPath&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;   &lt;span style='color:#696969; '&gt;//head&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#696969; '&gt;//body&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#696969; '&gt;//left arm&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;4&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;30&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;  &lt;span style='color:#696969; '&gt;// right arm&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;30&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;  &lt;span style='color:#696969; '&gt;// left leg&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;6&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;30&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;  &lt;span style='color:#696969; '&gt;// right leg&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fill&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;restore&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This method is pretty self explanatory. Based on the number of strikes more of the hangman's body will be revealed.&lt;br /&gt;&lt;br /&gt;Here is the entire code listing, let me know if you have any questions. Enjoy...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;html&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;head&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;script type&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"application/x-javascript"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; words &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Array&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Ford"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;     words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Chevy"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Mazda"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Volvo"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;4&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Javascript"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Google"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;6&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Microsoft"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;7&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;"Nvidia"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; rand_no &lt;span style='color:#808030; '&gt;=&lt;/span&gt; Math&lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;random&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    rand_no &lt;span style='color:#808030; '&gt;=&lt;/span&gt; Math&lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;ceil&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;rand_no &lt;span style='color:#808030; '&gt;*&lt;/span&gt; words&lt;span style='color:#808030; '&gt;.&lt;/span&gt;length&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    word &lt;span style='color:#808030; '&gt;=&lt;/span&gt; words&lt;span style='color:#808030; '&gt;[&lt;/span&gt;rand_no&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;toUpperCase&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; foundLetters &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Array&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; guessedLetters &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Array&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; hideMessages&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"alreadyGuessed"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;style&lt;span style='color:#808030; '&gt;.&lt;/span&gt;visibility&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'hidden'&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"strike"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;style&lt;span style='color:#808030; '&gt;.&lt;/span&gt;visibility&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'hidden'&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; showAlreadyGuessed&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"alreadyGuessed"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;style&lt;span style='color:#808030; '&gt;.&lt;/span&gt;visibility&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;''&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; drawHangMan&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; canvas &lt;span style='color:#808030; '&gt;=&lt;/span&gt; document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'hangman'&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getContext&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; ctx  &lt;span style='color:#808030; '&gt;=&lt;/span&gt; canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getContext&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'2d'&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;save&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;translate&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;45&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;45&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;clearRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;width&lt;span style='color:#808030; '&gt;,&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;.&lt;/span&gt;height&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;beginPath&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;   &lt;span style='color:#696969; '&gt;//head&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#696969; '&gt;//body&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;20&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#696969; '&gt;//left arm&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;4&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;30&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;  &lt;span style='color:#696969; '&gt;// right arm&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;-&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;30&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;  &lt;span style='color:#696969; '&gt;// left leg&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;6&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fillRect&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;30&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;5&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;10&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;  &lt;span style='color:#696969; '&gt;// right leg&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;fill&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            ctx&lt;span style='color:#808030; '&gt;.&lt;/span&gt;restore&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; numberOfStrikes&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; showStrike&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        numberOfStrikes&lt;span style='color:#808030; '&gt;++&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        drawHangMan&lt;span style='color:#808030; '&gt;(&lt;/span&gt;numberOfStrikes&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"strike"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;style&lt;span style='color:#808030; '&gt;.&lt;/span&gt;visibility&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;''&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; guess&lt;span style='color:#808030; '&gt;(&lt;/span&gt;key&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        hideMessages&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;guessedLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;key&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            showAlreadyGuessed&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        guessedLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;key&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0f4d75; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; found &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0f4d75; '&gt;false&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;i&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;i&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;word&lt;span style='color:#808030; '&gt;.&lt;/span&gt;length&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;span style='color:#808030; '&gt;++&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;word&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;==&lt;/span&gt; key&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                foundLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#0f4d75; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                found&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0f4d75; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;!found&lt;span style='color:#808030; '&gt;)&lt;/span&gt; showStrike&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; found&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; draw&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; str&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;''&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;for&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;i&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;i&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;word&lt;span style='color:#808030; '&gt;.&lt;/span&gt;length&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;span style='color:#808030; '&gt;++&lt;/span&gt;i&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;foundLetters&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                str &lt;span style='color:#808030; '&gt;+=&lt;/span&gt; word&lt;span style='color:#808030; '&gt;[&lt;/span&gt;i&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;else&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                str &lt;span style='color:#808030; '&gt;+=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;'_'&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        str &lt;span style='color:#808030; '&gt;+=&lt;/span&gt; &lt;span style='color:#0000e6; '&gt;' '&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        document&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getElementById&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'word'&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;innerHTML &lt;span style='color:#808030; '&gt;=&lt;/span&gt; str&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; handleKeyUp&lt;span style='color:#808030; '&gt;(&lt;/span&gt;evt&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;var&lt;/span&gt; e &lt;span style='color:#808030; '&gt;=&lt;/span&gt; evt &lt;span style='color:#800080; '&gt;?&lt;/span&gt; evt &lt;span style='color:#800080; '&gt;:&lt;/span&gt; event&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;keyCode &lt;span style='color:#808030; '&gt;&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;65&lt;/span&gt; &lt;span style='color:#808030; '&gt;&amp;amp;&amp;amp;&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;keyCode &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#008c00; '&gt;65&lt;/span&gt;&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#008c00; '&gt;26&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            guess&lt;span style='color:#808030; '&gt;(&lt;/span&gt;String&lt;span style='color:#808030; '&gt;.&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;fromCharCode&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;keyCode&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;            &lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;else&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#696969; '&gt;//alert("You entered " + e.keyCode);&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        draw&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;script&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;head&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;body onload&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"draw();"&lt;/span&gt; onkeyup&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"handleKeyUp(event);"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;table&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td colspan&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;span id&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'alreadyGuessed'&lt;/span&gt; style&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"visibility:hidden"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;Letter already guessed&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;span&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;span id&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'strike'&lt;/span&gt; style&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"visibility:hidden"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;Strike&lt;span style='color:#808030; '&gt;!&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;span&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;canvas id&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'hangman'&lt;/span&gt; width&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;150&lt;/span&gt; height&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#008c00; '&gt;150&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;    &lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;span id&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;'word'&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;span&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;                        &lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;table&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;body&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;html&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3216324501977590964?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3216324501977590964/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3216324501977590964' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3216324501977590964'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3216324501977590964'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/javascript-hangman.html' title='Javascript Hangman'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7637904101985472510</id><published>2008-09-12T21:04:00.000-07:00</published><updated>2008-09-13T01:08:22.595-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='canvas'/><category scheme='http://www.blogger.com/atom/ns#' term='html'/><title type='text'>HTML Canvas Element - Part 3</title><content type='html'>This is Part 3 of the HTML Canvas Tutorial. If you haven't already you should read &lt;a href="http://developer-resource.blogspot.com/2008/09/html-canvas-element.html"&gt;Part 1&lt;/a&gt; and &lt;a href="http://developer-resource.blogspot.com/2008/09/html-canvas-element-part-2.html"&gt;Part 2&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;In this section we will create a key press listener and zoom our triangle in when we press 'z' and out when we press 'x'.&lt;br /&gt;&lt;br /&gt;To do this we add a onKeyUp event to the body tag, and a corresponding handleKeyUp javascript function:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; color: rgb(0, 0, 0); -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; handleKeyUp&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;evt&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; e &lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt; evt &lt;span style="color: rgb(128, 0, 128);"&gt;?&lt;/span&gt; evt &lt;span style="color: rgb(128, 0, 128);"&gt;:&lt;/span&gt; event&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;e&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;keyCode &lt;span style="color: rgb(128, 128, 48);"&gt;==&lt;/span&gt; &lt;span style="color: rgb(0, 140, 0);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;           scale&lt;span style="color: rgb(128, 128, 48);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;.1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;e&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;keyCode &lt;span style="color: rgb(128, 128, 48);"&gt;==&lt;/span&gt; &lt;span style="color: rgb(0, 140, 0);"&gt;88&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;           scale&lt;span style="color: rgb(128, 128, 48);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;.1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;       draw&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;body onload&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"draw();"&lt;/span&gt; onkeyup&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"handleKeyUp(event);"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;canvas id&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"canvas"&lt;/span&gt; width&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"150"&lt;/span&gt; height&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"150"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;canvas&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;body&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The variable scale is defined as a global javascript variable and we'll use it to call 'ctx.scale(x,y)'&lt;br /&gt;&lt;br /&gt;Before we do that though we'll have to call clearRect(x,y,width,height) to clear the image and redraw. The entire script is displayed below:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; color: rgb(0, 0, 0); -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;html&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;head&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;script type&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"application/x-javascript"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; scale&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; draw&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(105, 105, 105);"&gt;// Get Reference to the 'canvas' tag&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; canvas &lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt; document&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;getElementById&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"canvas"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;       &lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;canvas&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;getContext&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(105, 105, 105);"&gt;// Get a context that allows us to draw on.&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; ctx &lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt; canvas&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;getContext&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;'2d'&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;save&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;clearRect&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;canvas&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;width&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;canvas&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;height&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;           &lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;scale&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;scale&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;scale&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style="color: rgb(105, 105, 105);"&gt;// Move cursor and draw the outline of the Triangle&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;beginPath&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;moveTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;lineTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;50&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;lineTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;lineTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style="color: rgb(105, 105, 105);"&gt;// Actually Draw, you could call stroke instead which will draw the outline&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;fill&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;restore&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; handleKeyUp&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;evt&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; e &lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt; evt &lt;span style="color: rgb(128, 0, 128);"&gt;?&lt;/span&gt; evt &lt;span style="color: rgb(128, 0, 128);"&gt;:&lt;/span&gt; event&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;e&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;keyCode &lt;span style="color: rgb(128, 128, 48);"&gt;==&lt;/span&gt; &lt;span style="color: rgb(0, 140, 0);"&gt;90&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;           scale&lt;span style="color: rgb(128, 128, 48);"&gt;+=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;.1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;else&lt;/span&gt; &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;if&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;e&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;keyCode &lt;span style="color: rgb(128, 128, 48);"&gt;==&lt;/span&gt; &lt;span style="color: rgb(0, 140, 0);"&gt;88&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;           scale&lt;span style="color: rgb(128, 128, 48);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;.1&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;       draw&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;script&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;head&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;body onload&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"draw();"&lt;/span&gt; onkeyup&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"handleKeyUp(event);"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;canvas id&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"canvas"&lt;/span&gt; width&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"150"&lt;/span&gt; height&lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"150"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;canvas&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;body&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;/&lt;/span&gt;html&lt;span style="color: rgb(128, 128, 48);"&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That's it! I hope that got you interested in the Canvas element. If you have any questions leave a comment and I would be glad to answer them.&lt;br /&gt;&lt;br /&gt;For more information I recommend the following sites: &lt;br /&gt;&lt;a href="http://developer.mozilla.org/en/Canvas_tutorial"&gt;http://developer.mozilla.org/en/Canvas_tutorial&lt;/a&gt;&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Canvas_(HTML_element)"&gt;http://en.wikipedia.org/wiki/Canvas_(HTML_element)&lt;/a&gt;&lt;br /&gt;&lt;a href="http://blog.vlad1.com/2008/06/04/html-canvas-in-firefox-3/"&gt;http://blog.vlad1.com/2008/06/04/html-canvas-in-firefox-3/&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7637904101985472510?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7637904101985472510/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7637904101985472510' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7637904101985472510'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7637904101985472510'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/html-canvas-element-part-3.html' title='HTML Canvas Element - Part 3'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-4040686656530222221</id><published>2008-09-12T20:03:00.000-07:00</published><updated>2008-09-12T21:13:35.332-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='canvas'/><category scheme='http://www.blogger.com/atom/ns#' term='html'/><title type='text'>HTML Canvas Element - Part 2</title><content type='html'>&lt;a href="http://developer-resource.blogspot.com/2008/09/html-canvas-element.html"&gt;In Part 1&lt;/a&gt; of the series we set up a simple template to build the rest of the example on.&lt;br /&gt;&lt;br /&gt;Our goal is to display a triangle and allow the user to zoom in and zoom out.&lt;br /&gt;&lt;br /&gt;In this part we will display a black triangle on a white background. Replace your draw function with this one and then refresh your browser.&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: rgb(255, 255, 255) none repeat scroll 0% 0%; color: rgb(0, 0, 0); -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"&gt;&lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; draw&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(105, 105, 105);"&gt;// Get Reference to the 'canvas' tag&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; canvas &lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt; document&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;getElementById&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;"canvas"&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;       &lt;br /&gt;       &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;canvas&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;getContext&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(105, 105, 105);"&gt;// Get a context that allows us to draw on.&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(128, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; ctx &lt;span style="color: rgb(128, 128, 48);"&gt;=&lt;/span&gt; canvas&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;getContext&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 230);"&gt;'2d'&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style="color: rgb(105, 105, 105);"&gt;// Move cursor and draw the outline of the Triangle&lt;/span&gt;&lt;br /&gt;           ctx.beginPath();&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;moveTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;lineTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;50&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;lineTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;lineTo&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;,&lt;/span&gt;&lt;span style="color: rgb(0, 140, 0);"&gt;100&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style="color: rgb(105, 105, 105);"&gt;// Actually Draw, you could call stroke instead which will draw the outline&lt;/span&gt;&lt;br /&gt;           ctx&lt;span style="color: rgb(128, 128, 48);"&gt;.&lt;/span&gt;fill&lt;span style="color: rgb(128, 128, 48);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 48);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(128, 0, 128);"&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 0, 128);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You should now see a black triangle on a white background. The code got a drawing context by calling canvas.getContext("2d").&lt;br /&gt;&lt;br /&gt;We then use the moveTo command which picks up the cursor and moves to the location without drawing. Notice the coordinate system starts in the upper left with x extending to the right and y extending down.&lt;br /&gt;&lt;br /&gt;We start our path with beginPath(). This will clear out any other draw commands and now your ready to trace your path with lineTo. One we have traced our triangle with the lineTo commands we call 'ctx.fill()' Which draws the shape and fills it with a solid color. Alternatively we could of called 'ctx.stroke()' which will draw the outline.&lt;br /&gt;&lt;br /&gt;Continue to Part 3 to learn how to Zoom in/out the triangle.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-4040686656530222221?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/4040686656530222221/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=4040686656530222221' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4040686656530222221'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4040686656530222221'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/html-canvas-element-part-2.html' title='HTML Canvas Element - Part 2'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5145842552729379920</id><published>2008-09-12T00:46:00.000-07:00</published><updated>2008-09-12T21:19:50.443-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='canvas'/><category scheme='http://www.blogger.com/atom/ns#' term='html'/><title type='text'>HTML Canvas Element</title><content type='html'>The canvas html tag element allows web developers to draw graphics in the browser through a scripting language, typically JavaScript.&lt;br /&gt;&lt;br /&gt;In this tutorial, we will create a triangle and allow the user to zoom in or zoom out the picture. Before we get started lets set up our environment. All you'll need is your web browser and a text editor.&lt;br /&gt;&lt;br /&gt;Open your text editor (notepad will work just fine) and enter the following:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;html&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;head&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;script type&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"application/x-javascript"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;function&lt;/span&gt; draw&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        alert&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"hi"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;script&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;head&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;body onload&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"draw();"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;canvas id&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"canvas"&lt;/span&gt; width&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"150"&lt;/span&gt; height&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"150"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;canvas&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;body&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;html&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This is our template that we will use for the rest of the tutorial. We've added a canvas element and when the HTML body is loaded our draw() function will be called.&lt;br /&gt;&lt;br /&gt;If you save the text file and load it in your browser it should print a message box saying 'hi'. If not please re-read the above until you have this working. &lt;br /&gt;&lt;br /&gt;&lt;a href="http://developer-resource.blogspot.com/2008/09/html-canvas-element-part-2.html"&gt;Continue to Part 2&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5145842552729379920?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5145842552729379920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5145842552729379920' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5145842552729379920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5145842552729379920'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/html-canvas-element.html' title='HTML Canvas Element'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6699903573237731564</id><published>2008-09-12T00:23:00.000-07:00</published><updated>2008-09-12T00:27:51.921-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>Google Web Toolkit - Monthly Payment Calculator</title><content type='html'>This example displays a simple Loan Calculator. When the user clicks calculate a ClickListener is fired which calculates the monthly payment and dispays it.&lt;br /&gt;&lt;br /&gt;This example illustrates GWT Labels, TextBox, and Button controls.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    /*&lt;br /&gt; * mcintoshEntryPoint.java&lt;br /&gt; *&lt;br /&gt; * Created on June 29, 2008, 1:46 PM&lt;br /&gt; *&lt;br /&gt; * To change this template, choose Tools | Template Manager&lt;br /&gt; * and open the template in the editor.&lt;br /&gt; */&lt;br /&gt;package com.client;&lt;br /&gt;&lt;br /&gt;import com.google.gwt.core.client.EntryPoint;&lt;br /&gt;import com.client.time.*;&lt;br /&gt;import com.google.gwt.user.client.ui.Button;&lt;br /&gt;import com.google.gwt.user.client.ui.ClickListener;&lt;br /&gt;import com.google.gwt.user.client.ui.Label;&lt;br /&gt;import com.google.gwt.user.client.ui.RootPanel;&lt;br /&gt;import com.google.gwt.user.client.ui.TextBox;&lt;br /&gt;import com.google.gwt.user.client.ui.Widget;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; *&lt;br /&gt; * @author avalanche&lt;br /&gt; */&lt;br /&gt;public class mcintoshEntryPoint implements EntryPoint {&lt;br /&gt;&lt;br /&gt;    final Label lblLoanAmount = new Label("Loan Amount:");&lt;br /&gt;    final TextBox txtLoanAmount = new TextBox();&lt;br /&gt;    final Label lblLoanLength = new Label("Loan Length (Month):");&lt;br /&gt;    final TextBox txtLoanLength = new TextBox();&lt;br /&gt;    final Label lblInterestRate = new Label("Interest Rate (Yearly):");&lt;br /&gt;    final TextBox txtInterestRate = new TextBox();&lt;br /&gt;    final Button calculate = new Button("Calculate");&lt;br /&gt;    final Label lblResult = new Label("Payment per month:");&lt;br /&gt;&lt;br /&gt;    public mcintoshEntryPoint() {&lt;br /&gt;        calculate.addClickListener(new ClickListener() {&lt;br /&gt;&lt;br /&gt;            public void onClick(Widget arg0) {&lt;br /&gt;                &lt;br /&gt;                //P = A (1 + r ) ^ N /( 1 + r ) ^ N -1                                                &lt;br /&gt;                int loanAmount = Integer.parseInt(txtLoanAmount.getText());&lt;br /&gt;                int loanLength = Integer.parseInt(txtLoanLength.getText());&lt;br /&gt;                double interestRate = Double.parseDouble(txtInterestRate.getText())/1200;&lt;br /&gt;                double temp = Math.pow(( 1 + interestRate ) , loanLength);&lt;br /&gt;                Double result = new Double((loanAmount * interestRate * temp)   / (temp -1));                                &lt;br /&gt;                lblResult.setText(result.toString());&lt;br /&gt;            }&lt;br /&gt;        });&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * The entry point method, called automatically by loading a module&lt;br /&gt;     * that declares an implementing class as an entry-point&lt;br /&gt;     */&lt;br /&gt;    public void onModuleLoad() {&lt;br /&gt;        RootPanel.get().add(lblLoanAmount); &lt;br /&gt;        RootPanel.get().add(txtLoanAmount);        &lt;br /&gt;        RootPanel.get().add(lblLoanLength);&lt;br /&gt;        RootPanel.get().add(txtLoanLength);&lt;br /&gt;        RootPanel.get().add(lblInterestRate);&lt;br /&gt;        RootPanel.get().add(txtInterestRate);&lt;br /&gt;        RootPanel.get().add(calculate);&lt;br /&gt;        RootPanel.get().add(lblResult);&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6699903573237731564?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6699903573237731564/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6699903573237731564' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6699903573237731564'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6699903573237731564'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/google-web-toolkit-monthly-payment.html' title='Google Web Toolkit - Monthly Payment Calculator'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8377700056809132829</id><published>2008-09-11T02:20:00.000-07:00</published><updated>2008-09-11T23:34:14.865-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>Google Web Toolkit Data Grid Example</title><content type='html'>At work I have a java J2EE application that displays a large table. The report is about 15 MB when rendered. I've been experimenting with GWT and I think it would be a good tool to rewrite this report with.&lt;br /&gt;&lt;br /&gt;The main problem with the page is when someone updates a value it has to post back to the server then re-render everything. Using GWT it seems like it would be trivial to reload a single row at a time. Along those lines here is a simple DataGrid example that lets me add/remove rows.&lt;br /&gt;&lt;br /&gt;See &lt;a href="http://developer-resource.blogspot.com/2008/07/google-web-toolkit-simple-example.html"&gt;GWT Simple Example&lt;/a&gt; for more information on setting up your environment.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;   /*&lt;br /&gt;* mcintoshEntryPoint.java&lt;br /&gt;*&lt;br /&gt;* Created on June 29, 2008, 1:46 PM&lt;br /&gt;*&lt;br /&gt;* To change this template, choose Tools | Template Manager&lt;br /&gt;* and open the template in the editor.&lt;br /&gt;*/&lt;br /&gt;package com.client;&lt;br /&gt;&lt;br /&gt;import com.google.gwt.core.client.EntryPoint;&lt;br /&gt;import com.client.time.*;&lt;br /&gt;import com.google.gwt.user.client.ui.Button;&lt;br /&gt;import com.google.gwt.user.client.ui.ClickListener;&lt;br /&gt;import com.google.gwt.user.client.ui.FlexTable;&lt;br /&gt;import com.google.gwt.user.client.ui.RootPanel;&lt;br /&gt;import com.google.gwt.user.client.ui.SourcesTableEvents;&lt;br /&gt;import com.google.gwt.user.client.ui.TableListener;&lt;br /&gt;import com.google.gwt.user.client.ui.Widget;&lt;br /&gt;import java.util.Arrays;&lt;br /&gt;import java.util.HashSet;&lt;br /&gt;import java.util.Set;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;*&lt;br /&gt;* @author avalanche&lt;br /&gt;*/&lt;br /&gt;public class mcintoshEntryPoint implements EntryPoint {&lt;br /&gt;&lt;br /&gt;   final Button addButton = new Button("Add Row");&lt;br /&gt;   final Button deleteButton = new Button("Delete Row");&lt;br /&gt;   final FlexTable table = new FlexTable();   &lt;br /&gt;   final Set selectedTableRows = new HashSet();           &lt;br /&gt;&lt;br /&gt;   public mcintoshEntryPoint() {&lt;br /&gt;       addButton.addClickListener(new ClickListener() {&lt;br /&gt;&lt;br /&gt;           public void onClick(Widget arg0) {&lt;br /&gt;               table.insertRow(table.getRowCount());&lt;br /&gt;               table.addCell(table.getRowCount() - 1);&lt;br /&gt;               table.addCell(table.getRowCount() - 1);&lt;br /&gt;               table.setText(table.getRowCount() - 1, 1, "foo" + table.getRowCount());               &lt;br /&gt;           }&lt;br /&gt;       });&lt;br /&gt;       deleteButton.addClickListener(new ClickListener() {&lt;br /&gt;&lt;br /&gt;           public void onClick(Widget arg0) {&lt;br /&gt;               Object[] array = selectedTableRows.toArray();&lt;br /&gt;               Arrays.sort(array);&lt;br /&gt;               for(int i=array.length-1;i&gt;-1;i-- ) {&lt;br /&gt;                   int row = ((Integer)array[i]).intValue();&lt;br /&gt;                   table.removeRow(row);&lt;br /&gt;                   selectedTableRows.remove(new Integer(row));&lt;br /&gt;               }&lt;br /&gt;           }&lt;br /&gt;       });&lt;br /&gt;       table.addTableListener(new TableListener() {&lt;br /&gt;&lt;br /&gt;           public void onCellClicked(SourcesTableEvents arg0, int rrow, int col) {&lt;br /&gt;               Integer row = new Integer(rrow);               &lt;br /&gt;               if (selectedTableRows.contains(row)) {&lt;br /&gt;                   selectedTableRows.remove(row);&lt;br /&gt;                   table.setText(rrow, 0, "x");&lt;br /&gt;               } else {&lt;br /&gt;                   selectedTableRows.add(row);&lt;br /&gt;                   table.setText(rrow, 0, "y");&lt;br /&gt;               }&lt;br /&gt;           }&lt;br /&gt;       });&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   /**&lt;br /&gt;    * The entry point method, called automatically by loading a module&lt;br /&gt;    * that declares an implementing class as an entry-point&lt;br /&gt;    */&lt;br /&gt;   public void onModuleLoad() {&lt;br /&gt;       RootPanel.get().add(table);&lt;br /&gt;       RootPanel.get().add(addButton);&lt;br /&gt;       RootPanel.get().add(deleteButton);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8377700056809132829?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8377700056809132829/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8377700056809132829' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8377700056809132829'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8377700056809132829'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/gwt-data-grid-example.html' title='Google Web Toolkit Data Grid Example'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-705854651136409358</id><published>2008-09-10T23:01:00.000-07:00</published><updated>2008-09-10T23:23:28.022-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sql'/><title type='text'>Modified Preorder Tree Traversal</title><content type='html'>How do you represent an organizational structure in a SQL data table? When I ask this question in interviews the answer I always get is with an adjacency list (although no one refers to it as that)&lt;br /&gt;&lt;br /&gt;An adjaceny list structure would look like this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;EmployeeId, EmployeeName,ManagerId&lt;br /&gt;----------------------------------&lt;br /&gt;         1,          Bob,   &lt;null&gt;&lt;br /&gt;         2,        Frank,        1&lt;br /&gt;         3,         Stan,        1&lt;br /&gt;         4,        James,        3&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This structure shoes that Bob is the boss with no manager. Frank and Stan report to Bob, and James reports to Stan. This structure is very common. If you want to list all of Stan's reports, its easy:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;select EmployeeName from Employees where ManagerId = (select EmployeeId from Employees where EmployeeName = 'Stan')&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;But what if you want to list all of Bob's reports? I have yet had anyone answer this question completely to my satisfaction. The typical response is to use a recursive look up. That is all well and good if your tree isn't very deep, but can we do better? Yes!&lt;br /&gt;&lt;br /&gt;We can instead structure our table as follows: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;EmployeeName,LeftId, RightId&lt;br /&gt;----------------------------------&lt;br /&gt;         Bob,     1,       8&lt;br /&gt;       Frank,     2,       3&lt;br /&gt;        Stan,     4,       7&lt;br /&gt;       James,     5,       6&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Huh you ask what did we just do? Let visualize it like this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;  1-bob-8&lt;br /&gt;     /            \&lt;br /&gt; 2-Frank-3      4-Stan-7&lt;br /&gt;                  /&lt;br /&gt;             5 James 6&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We start at the left of Bob and assign the first id 1. We move down his org chart and hit Frank and assign him next id of 2. Frank has no children so we go to his right node and assign next id of 3. The next sibling is Stan with id 4. Stan has a child so we go to James with id 5, then work our way back up the tree assigning James Right 6, Stan 7 and Bob 8.&lt;br /&gt;&lt;br /&gt;So our algorithm is:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;AssignId(Node n)&lt;br /&gt;  n.leftid = nextId()&lt;br /&gt;  foreach(Node child : n.children)&lt;br /&gt;     AssignId(child)&lt;br /&gt;  // No more children assign my right&lt;br /&gt;  n.rightid = nextId()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So what does this accomplish? Lets answer my orignal question. Display all reports of Bob:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;(leftid,rightid) =select LeftId,RightId from Employee where EmployeeName = 'Bob'&lt;br /&gt;select * from Employee where lft between $leftid, $rightid;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That's it. With this tree structure you can easily find all the children of a given node. Of course this method has drawbacks. Inserts are more difficult. But if you have a database that is mostly read only you may consider giving this a try.&lt;br /&gt;&lt;br /&gt;Enjoy...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-705854651136409358?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/705854651136409358/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=705854651136409358' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/705854651136409358'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/705854651136409358'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/modified-preorder-tree-traversal.html' title='Modified Preorder Tree Traversal'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8482131219022083286</id><published>2008-09-10T22:22:00.001-07:00</published><updated>2008-09-10T22:29:56.333-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='cuda'/><title type='text'>Cuda Hello World: Entire Code Listing</title><content type='html'>&lt;pre&gt;&lt;br /&gt;/********************************************************************&lt;br /&gt;*  CUDAWin32App10.cu&lt;br /&gt;*  This is a example of the CUDA program.&lt;br /&gt;*********************************************************************/&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;cuda_runtime.h&gt;&lt;br /&gt;&lt;br /&gt;/************************************************************************/&lt;br /&gt;/* Init CUDA                                                            */&lt;br /&gt;/************************************************************************/&lt;br /&gt;bool InitCUDA(void)&lt;br /&gt;{  &lt;br /&gt;   int count = 0;&lt;br /&gt;   int i = 0;&lt;br /&gt;&lt;br /&gt;   cudaGetDeviceCount(&amp;amp;count);&lt;br /&gt;   if(count == 0) {&lt;br /&gt;       fprintf(stderr, "There is no device.\n");&lt;br /&gt;       return false;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   for(i = 0; i &lt;&gt;= 1) {&lt;br /&gt;               break;&lt;br /&gt;           }&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;   if(i == count) {&lt;br /&gt;       fprintf(stderr, "There is no device supporting CUDA 1.x.\n");&lt;br /&gt;       return false;&lt;br /&gt;   }&lt;br /&gt;   cudaSetDevice(i);&lt;br /&gt;   return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/************************************************************************/&lt;br /&gt;/* Example                                                              */&lt;br /&gt;/************************************************************************/&lt;br /&gt;__global__ static void HelloCUDA(char* result, int num, clock_t* time,int foo)&lt;br /&gt;{&lt;br /&gt;   int i = 0;&lt;br /&gt;   char p_HelloCUDA[] = "Hello CUDA!";&lt;br /&gt;   clock_t start = clock();&lt;br /&gt;   for(i = 0; i &lt; time =" clock()" device_result    =" 0;" time            =" 0;" time_used        =" 0;"&gt;&gt;&gt;(device_result, 11 , time,1);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   cudaMemcpy(&amp;amp;host_result, device_result, sizeof(char) * 11, cudaMemcpyDeviceToHost);&lt;br /&gt;   cudaMemcpy(&amp;amp;time_used, time, sizeof(clock_t), cudaMemcpyDeviceToHost);&lt;br /&gt;   cudaFree(device_result);&lt;br /&gt;   cudaFree(time);&lt;br /&gt;&lt;br /&gt;   printf("%s,%d\n", host_result, time_used);&lt;br /&gt;&lt;br /&gt;   return 0;&lt;br /&gt;}&lt;/cuda_runtime.h&gt;&lt;/stdlib.h&gt;&lt;/stdio.h&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8482131219022083286?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8482131219022083286/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8482131219022083286' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8482131219022083286'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8482131219022083286'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/cuda-hello-world-entire-code-listing.html' title='Cuda Hello World: Entire Code Listing'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-4090072077768492508</id><published>2008-09-10T21:31:00.000-07:00</published><updated>2008-09-10T22:31:10.145-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='cuda'/><title type='text'>Cuda Hello World: part 2</title><content type='html'>In &lt;a href="http://developer-resource.blogspot.com/2008/06/cuda-hello-world.html"&gt;Part 1&lt;/a&gt; we setup a Visual Studio project to run our first cuda program. In this part we will look deeper into the template and explain what it is doing.&lt;br /&gt;&lt;br /&gt;A Cuda program is structured as followed&lt;br /&gt;1) Define a Kernel&lt;br /&gt;2) Copy system memory to GPU memory&lt;br /&gt;3) Execute the Kernel&lt;br /&gt;4) Copy results from GPU memory back to system memory&lt;br /&gt;5) Print our results, and cleanup&lt;br /&gt;&lt;br /&gt;We'll look at each part.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Define a Kernel&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A kernel is a function that is executed. For our example will follow the sample template from the Visual Studio plugin and create a HelloCuda method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;__global__ static void HelloCUDA(char* result, int num, clock_t* time)&lt;br /&gt;{&lt;br /&gt; int i = 0;&lt;br /&gt; char p_HelloCUDA[] = "Hello CUDA!";&lt;br /&gt; clock_t start = clock();&lt;br /&gt; for(i = 0; i &amp;lt; num; i++) {&lt;br /&gt;    result[i] = p_HelloCUDA[i];&lt;br /&gt; }&lt;br /&gt;*time = clock() - start;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The __global__ declaration specifier indicates that the procedure is a kernel entry point. Our function takes in an:&lt;br /&gt;array of characters&lt;br /&gt;size of the array&lt;br /&gt;pointer to clock_t structure&lt;br /&gt;&lt;br /&gt;The kernel doesn't do much it copies into our character array "Hello CUDA!". As you can probably infer from the assignment of time we are planning on only calling this function one time.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Copy system memory to GPU memory&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now that our kernel is defined we need to prep our data structures such that we can execute the kernel. For this example we'll need to allocate a block of memory to hold the "Hello CUDA!" string, and a clock_t structure for the elapsed time.&lt;br /&gt;&lt;br /&gt;To allocate memory on the GPU we use cudaMalloc.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;char *device_result = 0;&lt;br /&gt;clock_t  *time   = 0;&lt;br /&gt;cudaMalloc((void**) &amp;amp;device_result, sizeof(char) * 11);&lt;br /&gt;cudaMalloc((void**) &amp;amp;time, sizeof(clock_t));&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The memory has now been set aside in the GPU and were ready to execute the kernel&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Execute the Kernel&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The following code will execute our defined kernel passing in the arguments we just created.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;HelloCUDA&amp;lt;&amp;lt;&amp;lt;1, 1, 0&amp;rt;&amp;rt;&amp;rt;(device_result, 11 , time,1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;At this stage the GPU will execute our program and will have our results, we need to copy those results back to the system so we can use them.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Copy results from GPU memory back to system memory&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To get our results off of the GPU we use cudaMemcpy and copy the data back into our own storage. To do that we must define a char*, and a clock_t then memcpy the results back.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;char  host_result[12] ={0};&lt;br /&gt;clock_t  time_used  = 0;&lt;br /&gt;cudaMemcpy(&amp;amp;host_result, device_result, sizeof(char) * 11, cudaMemcpyDeviceToHost);&lt;br /&gt;cudaMemcpy(&amp;amp;time_used, time, sizeof(clock_t), cudaMemcpyDeviceToHost);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Print Results and Clean up&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That's it the GPU has ran our program created a string called 'Hello CUDA!' copied that into the char * result that we have a pointer to called device_result. It has also filled in clock_t struct in gpu memory which we have a pointer to called time.&lt;br /&gt;&lt;br /&gt;We've copied the gpu memory into local variables called host_result, and time_used and now can use it like an C program. So lets print it out.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;cudaMemcpy(&amp;amp;host_result, device_result, sizeof(char) * 11, cudaMemcpyDeviceToHost);&lt;br /&gt;cudaMemcpy(&amp;amp;time_used, time, sizeof(clock_t), cudaMemcpyDeviceToHost);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The last thing we need to do is free the memory on the device we use cudaFree.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;cudaFree(device_result);&lt;br /&gt;cudaFree(time);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That's it! If you run the program you should get output similar to the following:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;CUDA initialized.&lt;br /&gt;Hello CUDA!,0&lt;br /&gt;Press any key to continue . . .&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Click &lt;a href="http://developer-resource.blogspot.com/2008/09/cuda-hello-world-entire-code-listing.html"&gt;here&lt;/a&gt; for the entire code listing&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-4090072077768492508?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/4090072077768492508/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=4090072077768492508' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4090072077768492508'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4090072077768492508'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/cuda-hello-world-part-2.html' title='Cuda Hello World: part 2'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-188344267002653430</id><published>2008-09-07T14:35:00.000-07:00</published><updated>2009-01-08T17:13:20.890-08:00</updated><title type='text'>Miscellaneous tools</title><content type='html'>This is a short post to preserve some useful tools that I use from time to time but I always forget their names.&lt;br /&gt;&lt;br /&gt;1) BackTrack: Very useful linux boot disk based off SLAX. It comes preloaded with a lot of cool tools but I like it so I can just boot into linux off of usb key and run firefox.&lt;br /&gt;&lt;br /&gt;2) Driftnet, fun but useless tool. Shows all the images that you access by sniffing your own traffic&lt;br /&gt;&lt;br /&gt;3) how to set up linux to connect to wirless network:&lt;br /&gt;ifconfig &lt;interface&gt; down&lt;br /&gt;ifconfig &lt;interface&gt; up&lt;br /&gt;iwconfig &lt;interface&gt; essid 'name of network'&lt;br /&gt;iwconfig &lt;interface&gt; key 'name of network'&lt;br /&gt;iwconfig &lt;interface&gt; mode managed&lt;br /&gt;dhcpcd&lt;br /&gt;&lt;br /&gt;4) Syntax highlight code and covert it to HTML: http://www.tohtml.com/jScript/&lt;interface&gt;&lt;/interface&gt;&lt;/interface&gt;&lt;/interface&gt;&lt;/interface&gt;&lt;/interface&gt;&lt;/interface&gt;&lt;br /&gt;&lt;br /&gt;5) Visual Assist X claims to to background compilation (crosses fingers) ( didn't work that well)&lt;br /&gt;&lt;br /&gt;6) SQLylog Enterprise addition ( has auto complete for sql tables )&lt;br /&gt;&lt;br /&gt;7) SiSoftware's SANDRA , useful for analyzing your computer and figuring out what components you have. For examle I couldn't get a used audio card to work. SANDRA identified the chipset then with a quick google I had the driver.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-188344267002653430?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/188344267002653430/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=188344267002653430' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/188344267002653430'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/188344267002653430'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/09/miscellaneous-tools.html' title='Miscellaneous tools'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-4990440873655975139</id><published>2008-08-06T00:12:00.001-07:00</published><updated>2008-08-06T00:15:39.949-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='perl'/><title type='text'>Perl CGI cached parameters.</title><content type='html'>An intern at work today ran into a problem modifying a perl cgi script. It looked something like this&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;my $cgi = new CGI;&lt;br /&gt;$cgi-&gt;param('foo');&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The problem was this value appeared to be cached. Once I accesed the page with http://server/page?foo=bar it would remember bar no matter what I changed the value to.&lt;br /&gt;&lt;br /&gt;First I thought it was a cache issue. So we changed the code to output a timestamp. On each page load we would see the time increment but the variable was still cached somehow. &lt;br /&gt;&lt;br /&gt;We finally found out that putting the new CGI call inside a function made sure we got a new one each time.&lt;br /&gt;&lt;br /&gt;So what happened? I'm not really sure, I'm guessing scope of the perl code is global per apache process? Is this configurable somehow? &lt;br /&gt;&lt;br /&gt;Well if anyone knows please add a comment because i this point I'm still baffled by it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-4990440873655975139?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/4990440873655975139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=4990440873655975139' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4990440873655975139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/4990440873655975139'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/08/perl-cgi-cached-parameters.html' title='Perl CGI cached parameters.'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5844949369105727759</id><published>2008-08-02T23:37:00.000-07:00</published><updated>2008-08-02T23:52:34.270-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>c++ Windows Programing - Creating a window</title><content type='html'>This tutorial will cover how to create a window. If you haven't already you should read the Hello World tutorial for Windows Programming first.&lt;br /&gt;&lt;br /&gt;In the last tutorial I described how to create a windows program that displays a message box. We are going to extend on that tutorial and create a window this time.&lt;br /&gt;&lt;br /&gt;This will consists of 4 parts.&lt;br /&gt;&lt;br /&gt;1. Register a new windows class (not to be confused with a c++ class)&lt;br /&gt;2. Create the window&lt;br /&gt;3. Display the window&lt;br /&gt;4. Process Events.&lt;br /&gt;&lt;br /&gt;Part 1. Registering a new window class&lt;br /&gt;&lt;br /&gt;The first step is to fill in a windows structure that describes how your new window will look like. We'll use the WNDCLASSEX structure.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;WNDCLASSEX wc;&lt;br /&gt;&lt;br /&gt;// clear out the window class for use&lt;br /&gt;ZeroMemory(&amp;amp;wc, sizeof(WNDCLASSEX));&lt;br /&gt;&lt;br /&gt;// fill in the struct with the needed information&lt;br /&gt;wc.cbSize = sizeof(WNDCLASSEX);&lt;br /&gt;wc.style = CS_HREDRAW | CS_VREDRAW;&lt;br /&gt;wc.lpfnWndProc = (WNDPROC)WindowProc;&lt;br /&gt;wc.hInstance = hInstance;&lt;br /&gt;wc.hCursor = LoadCursor(NULL, IDC_ARROW);&lt;br /&gt;wc.hbrBackground = (HBRUSH)COLOR_WINDOW;&lt;br /&gt;wc.lpszClassName = L"MyClass";&lt;br /&gt;&lt;br /&gt;RegisterClassEx(&amp;amp;wc);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;We first declare a new instance and zero out the memory of the instance. We then set appropriate parameters to define how the window will look. For full details on the paremeters you should consult msdn.&lt;br /&gt;&lt;br /&gt;The lpfnWndProc is an important property. This defines a function that will be called back whenever this window needs to handle a message. We'll discuss more abot this in Part 4.&lt;br /&gt;&lt;br /&gt;Part 2. Creating the window.&lt;br /&gt;&lt;br /&gt;Now that we've defined the style of how our new window should look, we want to actually create the window. We'll use the CreateWindowEx function which has the following prototype:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;HWND CreateWindowEx(      &lt;br /&gt;    DWORD dwExStyle,&lt;br /&gt;    LPCTSTR lpClassName,&lt;br /&gt;    LPCTSTR lpWindowName,&lt;br /&gt;    DWORD dwStyle,&lt;br /&gt;    int x,&lt;br /&gt;    int y,&lt;br /&gt;    int nWidth,&lt;br /&gt;    int nHeight,&lt;br /&gt;    HWND hWndParent,&lt;br /&gt;    HMENU hMenu,&lt;br /&gt;    HINSTANCE hInstance,&lt;br /&gt;    LPVOID lpParam&lt;br /&gt;);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Part 3: Display the Window&lt;br /&gt;&lt;br /&gt;Displaying the window is accomplished by calling ShowWindow which has the prototype: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; BOOL ShowWindow(      &lt;br /&gt;    HWND hWnd,&lt;br /&gt;    int nCmdShow&lt;br /&gt;);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Part 4: Processing Events&lt;br /&gt;&lt;br /&gt;We now have the parts to describe the style of a window, register it, create it, and display it. But if we don't process the windows events nothing will happen. Remember lpfnWndProc from Part 1?&lt;br /&gt;&lt;br /&gt;We must define that call back method. Anytime a message is received it will need to run through that method. &lt;br /&gt;&lt;br /&gt;So first we need to get message off the messaging queue than define our callback.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// this struct holds Windows event messages&lt;br /&gt;    MSG msg;&lt;br /&gt;&lt;br /&gt;    // wait for the next message in the queue, store the result in 'msg'&lt;br /&gt;    while(GetMessage(&amp;msg, NULL, 0, 0))&lt;br /&gt;    {&lt;br /&gt;        // translate keystroke messages into the right format&lt;br /&gt;        TranslateMessage(&amp;msg);&lt;br /&gt;&lt;br /&gt;        // send the message to the WindowProc function&lt;br /&gt;        DispatchMessage(&amp;msg);&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;// this is the main message handler for the program&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)&lt;br /&gt;{&lt;br /&gt;    // sort through and find what code to run for the message given&lt;br /&gt;    switch(message)&lt;br /&gt;    {&lt;br /&gt;        // this message is read when the window is closed&lt;br /&gt;        case WM_DESTROY:&lt;br /&gt;            {&lt;br /&gt;                // close the application entirely&lt;br /&gt;                PostQuitMessage(0);&lt;br /&gt;                return 0;&lt;br /&gt;            } break;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Handle any messages the switch statement didn't&lt;br /&gt;    return DefWindowProc (hWnd, message, wParam, lParam);&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Conclusion: &lt;br /&gt;&lt;br /&gt;Putting it all together we have a program that looks like this: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;windows.h&gt;&lt;br /&gt;#include &lt;windowsx.h&gt;&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam); &lt;br /&gt;&lt;br /&gt;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {&lt;br /&gt; //Our window structure.&lt;br /&gt; WNDCLASSEX wc;&lt;br /&gt; ZeroMemory(&amp;wc,sizeof(WNDCLASSEX));&lt;br /&gt;&lt;br /&gt; wc.cbSize = sizeof(WNDCLASSEX);&lt;br /&gt;    wc.style = CS_HREDRAW | CS_VREDRAW;&lt;br /&gt;    wc.lpfnWndProc = (WNDPROC)WindowProc;&lt;br /&gt;    wc.hInstance = hInstance;&lt;br /&gt;    wc.hCursor = LoadCursor(NULL, IDC_ARROW);&lt;br /&gt; wc.hbrBackground = (HBRUSH)COLOR_3DSHADOW;&lt;br /&gt;    wc.lpszClassName = L"WindowClass1";&lt;br /&gt;&lt;br /&gt; RegisterClassEx(&amp;wc); &lt;br /&gt;&lt;br /&gt; // create the window and use the result as the handle&lt;br /&gt;    HWND hWnd = CreateWindowEx(NULL,&lt;br /&gt;                          L"WindowClass1",    // name of the window class&lt;br /&gt;                          L"Our First Windowed Program",   // title of the window&lt;br /&gt;                          WS_OVERLAPPEDWINDOW,    // window style&lt;br /&gt;                          300,    // x-position of the window&lt;br /&gt;                          300,    // y-position of the window&lt;br /&gt;                          500,    // width of the window&lt;br /&gt;                          400,    // height of the window&lt;br /&gt;                          NULL,    // we have no parent window, NULL&lt;br /&gt;                          NULL,    // we aren't using menus, NULL&lt;br /&gt;                          hInstance,    // application handle&lt;br /&gt;                          NULL);    // used with multiple windows, NULL&lt;br /&gt;&lt;br /&gt; ShowWindow(hWnd,nCmdShow);&lt;br /&gt;&lt;br /&gt; MSG msg; &lt;br /&gt; while(GetMessage (&amp;msg,NULL,0,0)) {&lt;br /&gt;  TranslateMessage(&amp;msg); &lt;br /&gt;  DispatchMessage(&amp;msg); &lt;br /&gt; }&lt;br /&gt; return msg.wParam;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK WindowProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) &lt;br /&gt;{&lt;br /&gt; switch(message) {&lt;br /&gt;  case WM_DESTROY: {&lt;br /&gt;   PostQuitMessage(0);&lt;br /&gt;   return 0;&lt;br /&gt;  } break;&lt;br /&gt; }&lt;br /&gt; return DefWindowProc(hwnd,message,wParam,lParam); &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5844949369105727759?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5844949369105727759/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5844949369105727759' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5844949369105727759'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5844949369105727759'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/08/c-windows-programing-creating-window.html' title='c++ Windows Programing - Creating a window'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3966203042463147292</id><published>2008-08-02T23:00:00.000-07:00</published><updated>2008-09-12T01:31:31.086-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>Hello World - c++ Windows Application</title><content type='html'>In this post I'm going to explain the details of writing a hello world program for windows. To start we should look at the version for a console application:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;rt;&lt;br /&gt;using namespace std;&lt;br /&gt;void main() {&lt;br /&gt;  cout &lt;&lt; "Hello World" &lt;&lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;For windows it isn't much different. But instead of declaring a main we need to instead declare a WinMain. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &amp;lt;windows.h&amp;rt;&lt;br /&gt;&lt;br /&gt;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShowCmd) {&lt;br /&gt; MessageBox(NULL,L"Hello World",L"MyCaption",MB_ICONINFORMATION | MB_OK);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;1. We include windows.h to give us access to the windows library functions.&lt;br /&gt;&lt;br /&gt;2. The keyword WINAPI instructs the program to reverse the argument list. For our purposes this doesn't matter but under the hood windows needs this.&lt;br /&gt;&lt;br /&gt;3. HINSTANCE is a handle to an instance. The main program receives a instance to itself (hinstance) and a handle to a previous instance of the same program. The previous instance is legacy and is NULL for modern applications. But is still provided for legacy code.&lt;br /&gt;&lt;br /&gt;4. lpCmdLine is the entire command string that was used to execute the program.&lt;br /&gt;&lt;br /&gt;5. nCmdShowCmd are options indicating how the window should open.&lt;br /&gt;&lt;br /&gt;6. Once our method is called we can then call MessageBox to display the message.&lt;br /&gt;&lt;br /&gt;We can now look into the MessaegBox command in more detail. The signature for the command is: &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int MessageBox(HWND hWnd,&lt;br /&gt;               LPCTSTR lptext,&lt;br /&gt;               LPCTSTR lpcaption,&lt;br /&gt;               UINT utype);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The first argument HWND, is a handle to the window the created this message box. In our case we haven't created a window, so we supply NULL which instructs Windows to have the message box originate from the desktop.&lt;br /&gt;&lt;br /&gt;lpText is a pointer to the text to display&lt;br /&gt;&lt;br /&gt;lpcaption is a pointer to the caption of the message box.&lt;br /&gt;&lt;br /&gt;uType is a parameter that specifies what type of message box, exclamation, information, etc. These are set by 'ORing' constant flags together.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3966203042463147292?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3966203042463147292/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3966203042463147292' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3966203042463147292'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3966203042463147292'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/08/hello-world-c-windows-application.html' title='Hello World - c++ Windows Application'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7508097939621992312</id><published>2008-07-24T15:31:00.000-07:00</published><updated>2008-07-24T16:23:27.131-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><title type='text'>Javascript - Adding options to select box</title><content type='html'>Adding options to List Box in client side Javascript&lt;br /&gt;&lt;br /&gt;Options to a drop down list box can be added dynamically using client side JavaScript. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;function addOption(selectbox,text,value )&lt;br /&gt;{&lt;br /&gt;  var optn = document.createElement("OPTION");&lt;br /&gt;  optn.text = text;&lt;br /&gt;  optn.value = value;&lt;br /&gt;  selectbox.options.add(optn);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note that each time the function is called, it adds a new option to the list box. So we can add one option by calling the function once. Like this.&lt;br /&gt;&lt;br /&gt;addOption(document.drop_list.Month_list,”January”, “January”);&lt;br /&gt;&lt;br /&gt;So this way we can create a drop down list box of all the months by calling the function each time for a month. So with this now you can easily create the list box. But we will add another step to this by using one array of months. So from the array of months we will loop through and add each month to the list box. Here is how to create the array&lt;br /&gt;&lt;br /&gt;var month = new Array("January","February","March","April","May","June",&lt;br /&gt;"July","August","September","October","November","December");&lt;br /&gt;&lt;br /&gt;So now once our array is ready with data, we will loop through the array by using for loop and then call the addOption function using the data of the array. Here is the code.&lt;br /&gt;&lt;br /&gt;for (var i=0; i &lt; month.length;++i){&lt;br /&gt;  addOption(document.drop_list.Month_list, month[i], month[i]);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And if you want to clear the options use: &lt;br /&gt;&lt;br /&gt;document.drop_list.Month_list.options.length = 0;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7508097939621992312?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7508097939621992312/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7508097939621992312' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7508097939621992312'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7508097939621992312'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/javascript-adding-options-to-select-box.html' title='Javascript - Adding options to select box'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6591010027358021377</id><published>2008-07-21T16:58:00.000-07:00</published><updated>2008-07-21T16:59:56.279-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='mysql'/><title type='text'>Mysql on update key</title><content type='html'>This is something cool that I found out today:&lt;br /&gt;&lt;br /&gt;INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)&lt;br /&gt;  ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);&lt;br /&gt;&lt;br /&gt;I've known about the on duplicate key but I would typically have to specify the values again. Here you can say values(&lt;column name&gt;) and it will return what would have been inserted. &lt;br /&gt;&lt;br /&gt;Very useful!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6591010027358021377?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6591010027358021377/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6591010027358021377' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6591010027358021377'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6591010027358021377'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/mysql-on-update-key.html' title='Mysql on update key'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-9004332740742642842</id><published>2008-07-21T14:55:00.001-07:00</published><updated>2008-07-21T14:55:28.690-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='jstl'/><title type='text'></title><content type='html'>Iterate over map value in jstl&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;c:forEach var="entry" items="${hashmap}"&gt;&lt;br /&gt;  ${entry.key} = ${entry.value}&lt;br /&gt;&lt;/c:forEach&gt;&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-9004332740742642842?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/9004332740742642842/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=9004332740742642842' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/9004332740742642842'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/9004332740742642842'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/iterate-over-map-value-in-jstl-entry.html' title=''/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-2555643355231423596</id><published>2008-07-18T01:48:00.000-07:00</published><updated>2008-09-12T12:46:46.012-07:00</updated><title type='text'>SetWindowsHookEx Example</title><content type='html'>Here is an example of using SetWindowsHookEx. &lt;br /&gt;&lt;br /&gt;First create a dll project with this cpp file&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include &amp;lt;windows.h&gt;&lt;br /&gt;#include &amp;lt;iostream&gt;&lt;br /&gt;#include &amp;lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;HINSTANCE hinst;&lt;br /&gt;HHOOK hhk;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;LRESULT CALLBACK wireKeyboardProc(int code,WPARAM wParam,LPARAM lParam) {  &lt;br /&gt; FILE * fileLog = fopen("C:\\sknl.txt", "a+");&lt;br /&gt; fprintf(fileLog,"OK");  &lt;br /&gt; CallNextHookEx(hhk,code,wParam,lParam);&lt;br /&gt; fclose(fileLog);&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;extern "C" __declspec(dllexport) void install() { &lt;br /&gt; hhk = SetWindowsHookEx(WH_KEYBOARD, wireKeyboardProc, hinst, NULL);&lt;br /&gt;}&lt;br /&gt;extern "C" __declspec(dllexport) void uninstall() {&lt;br /&gt; UnhookWindowsHookEx(hhk); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;BOOL WINAPI DllMain(  __in  HINSTANCE hinstDLL,&lt;br /&gt;  __in  DWORD fdwReason,&lt;br /&gt;  __in  LPVOID lpvReserved&lt;br /&gt;  ) {&lt;br /&gt;&lt;br /&gt; hinst = hinstDLL;&lt;br /&gt; return TRUE;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This method has a &lt;br /&gt;* dllmain which simply records the HINSTANCE&lt;br /&gt;* has an install and uninstall method&lt;br /&gt;* defines the keyboardProc call back method&lt;br /&gt;&lt;br /&gt;Now in another process ( this is important and is needed as SetWindowsHook will only work if your callback is in a dll and not in your own process) you simply loadlibrary and install. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int _tmain(int argc, _TCHAR* argv[])&lt;br /&gt;{  &lt;br /&gt;&lt;br /&gt; HINSTANCE hinst = LoadLibrary(_T("KeyboardLogger2.dll")); &lt;br /&gt; if (hinst == NULL) &lt;br /&gt; {&lt;br /&gt;  printf("null hinst");&lt;br /&gt; } &lt;br /&gt; typedef void (*Install)();&lt;br /&gt; typedef void (*Uninstall)();&lt;br /&gt;&lt;br /&gt; Install install = (Install) GetProcAddress(hinst, "install");&lt;br /&gt; Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");&lt;br /&gt; &lt;br /&gt;  &lt;br /&gt; install();&lt;br /&gt; int foo;&lt;br /&gt; std::cin &gt;&gt; foo; &lt;br /&gt;&lt;br /&gt; uninstall();&lt;br /&gt; return 0;&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-2555643355231423596?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/2555643355231423596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=2555643355231423596' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2555643355231423596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/2555643355231423596'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/setwindowshookex-example.html' title='SetWindowsHookEx Example'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7372313111063176033</id><published>2008-07-17T01:03:00.000-07:00</published><updated>2008-07-17T01:06:11.458-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='netbeans'/><title type='text'>Netbeans - Shortcuts</title><content type='html'>Netbeans has a lot of neat shortcuts.&lt;br /&gt;&lt;br /&gt;For example if you want to override a base class method, or create a getter, setter, or generate an equals or hashcode method you can do so easily by pressing &lt;b&gt;alt-insert&lt;/b&gt;. This will bring up a dialog for you to choose what you want.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;alt-shift-f&lt;/b&gt; will reformat your code to your liking&lt;br /&gt;&lt;br /&gt;&lt;b&gt;ctrl-click&lt;/b&gt; on an item will go to the definition of that item.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;alt-o&lt;/b&gt; will bring up a dialog to search for by filename.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7372313111063176033?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7372313111063176033/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7372313111063176033' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7372313111063176033'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7372313111063176033'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/netbeans-shortcuts.html' title='Netbeans - Shortcuts'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5081950418467109096</id><published>2008-07-17T00:23:00.001-07:00</published><updated>2008-07-17T00:29:45.457-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='cuda'/><title type='text'>Cuda - Sieve of Eratosthenes</title><content type='html'>Here is an example of how to use cuda to find all prime numbers within a range. &lt;br /&gt;&lt;br /&gt;Its uses the Sieve of Eratosthenes which creates an array initialized to 0 ( indicates prime) then you iterate over the array starting at zero and for each multiple of zero you mark it 1. You then move to the next element and repeat. When your down iterating all the primes are still marked as 0. &lt;br /&gt;&lt;br /&gt;As an example cuda program I wrote the following kernel&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;__global__ static void Sieve(int * sieve,int sieve_size)&lt;br /&gt;{ &lt;br /&gt; int idx = blockIdx.x * blockDim.x + threadIdx.x; &lt;br /&gt; if (idx &gt; 1) {&lt;br /&gt;  for(int i=idx+idx;i &lt; sieve_size;i+=idx) &lt;br /&gt;   sieve[i] = 1;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The index is figured out by the special keywords blockidx, blockDim and threadIdx. I then go over the multiples for that index and mark them as not prime.&lt;br /&gt;&lt;br /&gt;The code to call this looks like this: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/************************************************************************/&lt;br /&gt;/* Init CUDA                                                            */&lt;br /&gt;/************************************************************************/&lt;br /&gt;bool InitCUDA(void)&lt;br /&gt;{&lt;br /&gt; int count = 0;&lt;br /&gt; int i = 0;&lt;br /&gt;&lt;br /&gt; cudaGetDeviceCount(&amp;count);&lt;br /&gt; if(count == 0) {&lt;br /&gt;  fprintf(stderr, "There is no device.\n");&lt;br /&gt;  return false;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; for(i = 0; i &lt; count; i++) {&lt;br /&gt;  cudaDeviceProp prop;&lt;br /&gt;  if(cudaGetDeviceProperties(&amp;prop, i) == cudaSuccess) {&lt;br /&gt;   if(prop.major &gt;= 1) {&lt;br /&gt;    break;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; if(i == count) {&lt;br /&gt;  fprintf(stderr, "There is no device supporting CUDA 1.x.\n");&lt;br /&gt;  return false;&lt;br /&gt; }&lt;br /&gt; cudaSetDevice(i);&lt;br /&gt; return true;&lt;br /&gt;}&lt;br /&gt;int main(int argc, char** argv)&lt;br /&gt;{&lt;br /&gt;  &lt;br /&gt; int  *device_sieve;&lt;br /&gt; int host_sieve[1000]; &lt;br /&gt; double sieve_size = sizeof(host_sieve)/sizeof(int);&lt;br /&gt;&lt;br /&gt; if(!InitCUDA()) {&lt;br /&gt;  return 0;&lt;br /&gt; } &lt;br /&gt; cudaMalloc((void**) &amp;device_sieve, sizeof(int) * sieve_size); &lt;br /&gt;&lt;br /&gt; Sieve&lt;&lt;&lt;1, sqrt(sieve_size), 0&gt;&gt;&gt;(device_sieve, sieve_size);&lt;br /&gt; cudaThreadSynchronize();  &lt;br /&gt; cudaMemcpy(&amp;host_sieve, device_sieve, sizeof(int) * sieve_size, cudaMemcpyDeviceToHost);&lt;br /&gt; &lt;br /&gt; cudaFree(device_sieve);&lt;br /&gt; &lt;br /&gt; for(int i=2;i &lt; sieve_size;++i) &lt;br /&gt;  if (host_sieve[i] == 0)&lt;br /&gt;   printf("%d is prime\n",i);&lt;br /&gt; &lt;br /&gt;&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;For simplicity I simply create a new thread for each multiple. This is not very efficient as I may be using a value that is already found to not be prime.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5081950418467109096?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5081950418467109096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5081950418467109096' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5081950418467109096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5081950418467109096'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/cuda-sieve-of-eratosthenes.html' title='Cuda - Sieve of Eratosthenes'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8251082876723172810</id><published>2008-07-08T16:53:00.000-07:00</published><updated>2008-07-08T16:55:15.344-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='wxwidgets'/><title type='text'>wxWidgets keyboard shortcuts</title><content type='html'>If you create a menu bar with wxWidgets you can add a keyboard shortcut by specifying \t&lt;shortcut&gt;&lt;br /&gt;&lt;br /&gt;For example if you have a zoom in action Then you may add a menu entry like so:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;actionMenu-&gt;Append( kZoomInButtonId  , "ZoomIn\tz" , "Click to zoom in on the current location.\n" );&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now if you press z it will execute this action.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8251082876723172810?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8251082876723172810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8251082876723172810' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8251082876723172810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8251082876723172810'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/wxwidgets-keyboard-shortcuts.html' title='wxWidgets keyboard shortcuts'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5406775127917274449</id><published>2008-07-08T13:44:00.000-07:00</published><updated>2008-07-08T14:16:03.361-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><title type='text'>C++ convert signals into exceptions</title><content type='html'>Here is a technique to convert signals into exceptions: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include "signal.h"&lt;br /&gt;&lt;br /&gt;template &lt;class SignalExceptionClass&gt; class SignalTranslator&lt;br /&gt; {&lt;br /&gt; private:&lt;br /&gt;     class SingleTonTranslator&lt;br /&gt;     {&lt;br /&gt;     public:&lt;br /&gt;         SingleTonTranslator()&lt;br /&gt;         {&lt;br /&gt;             signal(SignalExceptionClass::GetSignalNumber(), SignalHandler);&lt;br /&gt;         }&lt;br /&gt;&lt;br /&gt;         static void SignalHandler(int)&lt;br /&gt;         {&lt;br /&gt;             std::cout &lt;&lt; "Signal handled and logged" &lt;&lt; std::endl;&lt;br /&gt;             throw SignalExceptionClass();&lt;br /&gt;         }&lt;br /&gt;     };&lt;br /&gt;&lt;br /&gt; public:&lt;br /&gt;     SignalTranslator()&lt;br /&gt;     {&lt;br /&gt;         static SingleTonTranslator s_objTranslator;&lt;br /&gt;     }&lt;br /&gt; };&lt;br /&gt;&lt;br /&gt; &lt;br /&gt; class SegmentationFault : public std::exception&lt;br /&gt; {     &lt;br /&gt; public:&lt;br /&gt;     virtual const char* what() const throw() {&lt;br /&gt;         return "Segmentation Fault"; &lt;br /&gt;     }&lt;br /&gt;     static int GetSignalNumber() {return SIGSEGV;}&lt;br /&gt; };&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I put the above in a SignalHandler.hpp. Then when I want to use it I include that file and instantiate the handlers that I want&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;// Set up signal handleing for Seg Fault. When SegFault occurs it will throw a exception&lt;br /&gt;SignalTranslator&lt;SegmentationFault&gt; g_objSegmentationFaultTranslator; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;To test this code you can simply raise that exception ( raise(SIGSEGV); )&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5406775127917274449?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5406775127917274449/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5406775127917274449' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5406775127917274449'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5406775127917274449'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/c-convert-signals-into-exceptions.html' title='C++ convert signals into exceptions'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-8126901454517154216</id><published>2008-07-04T03:33:00.000-07:00</published><updated>2009-04-10T00:54:42.789-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='web development'/><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>Google Web Toolkit - Suggest Box (RPC)</title><content type='html'>The Google Web Toolkit provides many advanced features to make a web page come alive. One feature that is used commonly in AJAX webpages is a select box that has context sensitive options.&lt;br /&gt;&lt;br /&gt;I have already covered the SuggestBox, this article will focus on retrieving your suggestion through an asynchronous Javascript call. You may want to read the following articles as this article builds off some of those concepts.&lt;br /&gt;&lt;br /&gt;* &lt;a href="http://developer-resource.blogspot.com/2008/07/google-web-toolkit-suggest-box.html"&gt;GWT suggest box&lt;/a&gt;&lt;br /&gt;* &lt;a href="http://developer-resource.blogspot.com/2008/07/google-web-toolkit-rpc-call.html"&gt;GWT RPC Call&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To create an RPC Suggest Box you will need to do the following&lt;br /&gt;* Create a Service to serve your suggestions. This service will take an input and return a collection of data.&lt;br /&gt;* Create an oracle that wraps the calls to the Service&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;Create Suggestion RPC Service&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Your client side Suggestion box will fire an onchange event when the user enters a keystroke. When this happens you want to send over the entered text and instruct the control to suggest only items that match the pattern.&lt;br /&gt;&lt;br /&gt;To do this we need to define a Service. As we saw in &lt;/span&gt;&lt;/span&gt; &lt;a href="http://developer-resource.blogspot.com/2008/07/google-web-toolkit-rpc-call.html"&gt;GWT RPC Call&lt;/a&gt; we need to define two interfaces we'll call them:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;IQuoteService&lt;/li&gt;&lt;li&gt;IQuoteServiceAsync&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;interface&lt;/span&gt; IQuoteService &lt;span style='color:#800000; font-weight:bold; '&gt;extends&lt;/span&gt; RemoteService &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Util &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; IQuoteServiceAsync getInstance&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;   IQuoteServiceAsync instance&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;IQuoteServiceAsync&lt;span style='color:#808030; '&gt;)&lt;/span&gt; GWT&lt;span style='color:#808030; '&gt;.&lt;/span&gt;create&lt;span style='color:#808030; '&gt;(&lt;/span&gt;IQuoteService&lt;span style='color:#808030; '&gt;.&lt;/span&gt;class&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;   ServiceDefTarget target &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;ServiceDefTarget&lt;span style='color:#808030; '&gt;)&lt;/span&gt; instance&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;   target&lt;span style='color:#808030; '&gt;.&lt;/span&gt;setServiceEntryPoint&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"/GWT/quote"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; instance&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Response getQuote&lt;span style='color:#808030; '&gt;(&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Request req&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;interface&lt;/span&gt; IQuoteServiceAsync &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;void&lt;/span&gt; getQuote&lt;span style='color:#808030; '&gt;(&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Request req&lt;span style='color:#808030; '&gt;,&lt;/span&gt; AsyncCallback callback&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Your Oracle will need to return a class with a com.google.gwt.user.client.ui.SuggestOracle.Suggestion interface. To faciliate this we create a new class called ItemSuggestion.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;Note: This class must reside on the client side else you will get serialization errors when you run the app.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; ItemSuggestion &lt;span style='color:#800000; font-weight:bold; '&gt;implements&lt;/span&gt; IsSerializable, Suggestion &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt; s&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#696969; '&gt;// Required for IsSerializable to work&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; ItemSuggestion&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style='color:#696969; '&gt;// Convenience method for creation of a suggestion&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; ItemSuggestion&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt; s&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;         &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;s &lt;span style='color:#808030; '&gt;=&lt;/span&gt; s&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt; getDisplayString&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; s&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; font-weight:bold; '&gt;String&lt;/span&gt; getReplacementString&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; s&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800080; '&gt;}&lt;/span&gt; &lt;span style='color:#696969; '&gt;// end inner class ItemSuggestion&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now define the actual service on the server side:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; RandomQuoteService &lt;span style='color:#800000; font-weight:bold; '&gt;extends&lt;/span&gt; RemoteServiceServlet &lt;span style='color:#800000; font-weight:bold; '&gt;implements&lt;/span&gt; IQuoteService &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;   &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Response getQuote&lt;span style='color:#808030; '&gt;(&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Request req&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;br /&gt;       &lt;span style='color:#696969; '&gt;// req has request properties that you can use to perform a db search&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#696969; '&gt;// or some other query. Then populate the suggestions up to req.getLimit() and&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#696969; '&gt;// return in a SuggestOracle.Response object.&lt;/span&gt;&lt;br /&gt;       SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Response resp &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Response&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;br /&gt;       &lt;span style='color:#bb7977; font-weight:bold; '&gt;List&lt;/span&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;suggestion&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; suggestions &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArrayList&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;suggestion&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       suggestions&lt;span style='color:#808030; '&gt;.&lt;/span&gt;add&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ItemSuggestion&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"It is a good day to die"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       suggestions&lt;span style='color:#808030; '&gt;.&lt;/span&gt;add&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ItemSuggestion&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"I shall return"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       suggestions&lt;span style='color:#808030; '&gt;.&lt;/span&gt;add&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ItemSuggestion&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;"There is nothing to fear but fear itself"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;br /&gt;       resp&lt;span style='color:#808030; '&gt;.&lt;/span&gt;setSuggestions&lt;span style='color:#808030; '&gt;(&lt;/span&gt;suggestions&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; resp&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-size:130%;"&gt;Create Suggestion Oracle&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So far we've created our service now we need to create the Oracle.&lt;br /&gt;&lt;br /&gt;You simply extend SuggestOracle and instruct it to use the Service for its suggestions.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; ItemSuggestOracle &lt;span style='color:#800000; font-weight:bold; '&gt;extends&lt;/span&gt; SuggestOracle &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;boolean&lt;/span&gt; isDisplayStringHTML&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;true&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;void&lt;/span&gt; requestSuggestions&lt;span style='color:#808030; '&gt;(&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Request req&lt;span style='color:#808030; '&gt;,&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Callback callback&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;           IQuoteService&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Util&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getInstance&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;getQuote&lt;span style='color:#808030; '&gt;(&lt;/span&gt;req&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ItemSuggestCallback&lt;span style='color:#808030; '&gt;(&lt;/span&gt;req&lt;span style='color:#808030; '&gt;,&lt;/span&gt; callback&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; ItemSuggestCallback &lt;span style='color:#800000; font-weight:bold; '&gt;implements&lt;/span&gt; AsyncCallback &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Request req&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Callback callback&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; ItemSuggestCallback&lt;span style='color:#808030; '&gt;(&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Request _req&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;                   SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Callback _callback&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;               req &lt;span style='color:#808030; '&gt;=&lt;/span&gt; _req&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;               callback &lt;span style='color:#808030; '&gt;=&lt;/span&gt; _callback&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;void&lt;/span&gt; onFailure&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;Throwable&lt;/span&gt; error&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;               callback&lt;span style='color:#808030; '&gt;.&lt;/span&gt;onSuggestionsReady&lt;span style='color:#808030; '&gt;(&lt;/span&gt;req&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Response&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#bb7977; '&gt;void&lt;/span&gt; onSuccess&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#bb7977; font-weight:bold; '&gt;Object&lt;/span&gt; retValue&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;               callback&lt;span style='color:#808030; '&gt;.&lt;/span&gt;onSuggestionsReady&lt;span style='color:#808030; '&gt;(&lt;/span&gt;req&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;                       &lt;span style='color:#808030; '&gt;(&lt;/span&gt;SuggestOracle&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Response&lt;span style='color:#808030; '&gt;)&lt;/span&gt; retValue&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And now the result of all our work, we create a new SuggestBox with the new oracle.&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; void display() &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       ItemSuggestOracle oracle &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ItemSuggestOracle&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;      &lt;br /&gt;       SuggestBox sb &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; SuggestBox&lt;span style='color:#808030; '&gt;(&lt;/span&gt;oracle&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;       &lt;span style='color:#696969; '&gt;// Add it to the root panel.&lt;/span&gt;&lt;br /&gt;       RootPanel&lt;span style='color:#808030; '&gt;.&lt;/span&gt;get&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;add&lt;span style='color:#808030; '&gt;(&lt;/span&gt;sb&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;   &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That's it! It looks like its a lot of work but its mostly plumbing and you should find its pretty easy to do once you get the hang of it.&lt;br /&gt;&lt;br /&gt;Give it a try and leave a comment to let me know how it goes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-8126901454517154216?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/8126901454517154216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=8126901454517154216' title='55 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8126901454517154216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/8126901454517154216'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/google-web-toolkit-suggest-box-rpc.html' title='Google Web Toolkit - Suggest Box (RPC)'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>55</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7324472825702055280</id><published>2008-07-04T01:21:00.000-07:00</published><updated>2008-07-04T01:27:16.061-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='JSON'/><title type='text'>JSON - JavaScript Object notation</title><content type='html'>JavaScript Object Notation is a data interchange format. The JSON format is often used for transmitting structured data over a network connection in a process called serialization. Its main application is in Ajax web application programming, where it serves as an alternative to the traditional use of the XML format.&lt;br /&gt;&lt;br /&gt;A simple example is :&lt;br /&gt;&lt;script&gt;&lt;br /&gt;  var p = eval({ "firstname":"John", "lastname":"Doe" }) ;&lt;br /&gt;  alert(p.firstname);&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;This script will popup an alert dialog box with the text 'John.'&lt;br /&gt;&lt;br /&gt;The JSON block can support nested types as well as arrays. For example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;{&lt;br /&gt;     "firstName": "John",&lt;br /&gt;     "lastName": "Smith",&lt;br /&gt;     "address": {&lt;br /&gt;         "streetAddress": "21 2nd Street",&lt;br /&gt;         "city": "New York",&lt;br /&gt;         "state": "NY",&lt;br /&gt;         "postalCode": 10021&lt;br /&gt;     },&lt;br /&gt;     "phoneNumbers": [&lt;br /&gt;         "212 555-1234",&lt;br /&gt;         "646 555-4567"&lt;br /&gt;     ]&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Typically the JSON request will come from the server, or through a Ajax callback or some other technique. This approach is must easier to work with then returning XML that the JavaScript client then has to parse.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7324472825702055280?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7324472825702055280/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7324472825702055280' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7324472825702055280'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7324472825702055280'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/json-javascript-object-notation.html' title='JSON - JavaScript Object notation'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3332856572163982333</id><published>2008-07-03T13:04:00.000-07:00</published><updated>2008-07-03T13:06:25.290-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='web development'/><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>Google Web Toolkit - Suggest Box</title><content type='html'>There seems to be some confusion on the web about whether GWT supports an auto completion widget. I suspect this is because the first version of GWT didn't support it. &lt;br /&gt;&lt;br /&gt;However 1.4 sure does, its called SuggestBox. SuggestBox takes a SuggestOracle that contains the data you want and is responsible for returning suggestions.&lt;br /&gt;&lt;br /&gt;Here is an example of how to use it: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();&lt;br /&gt;    oracle.add("foo");&lt;br /&gt;    oracle.add("bar");&lt;br /&gt;    oracle.add("baz");&lt;br /&gt;    oracle.add("toto");&lt;br /&gt;    oracle.add("tintin");&lt;br /&gt;&lt;br /&gt;    SuggestBox sb = new SuggestBox(oracle); &lt;br /&gt;&lt;br /&gt;    // Add it to the root panel.&lt;br /&gt;    RootPanel.get().add(sb);&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3332856572163982333?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3332856572163982333/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3332856572163982333' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3332856572163982333'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3332856572163982333'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/google-web-toolkit-suggest-box.html' title='Google Web Toolkit - Suggest Box'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3482833143443210211</id><published>2008-07-03T03:41:00.000-07:00</published><updated>2008-07-04T03:47:20.427-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='web development'/><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>Google Web Toolkit - RPC Call</title><content type='html'>This post will document how to make a RPC call using the Google Web Toolkit. &lt;br /&gt;&lt;br /&gt;For a more detailed walk through of the Google Web Toolkit I recommend this book: &lt;a href="http://www.amazon.com/gp/product/0321501969?ie=UTF8&amp;tag=develoresour-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321501969"&gt;Google Web Toolkit Applications&lt;/a&gt;&lt;img src="http://www.assoc-amazon.com/e/ir?t=develoresour-20&amp;l=as2&amp;o=1&amp;a=0321501969" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;&lt;br /&gt;&lt;br /&gt;To sum it up you must create 1 class and 2 interfaces.&lt;br /&gt;&lt;br /&gt;1) Service Interface that extends RemoteService and defines the methods for your service&lt;br /&gt;2) Asynchrounous interface which is a similar to the the above except:&lt;br /&gt;** doesn't extend RemoteService&lt;br /&gt;** all method return void&lt;br /&gt;** one extra AsyncCallBack parameter to each defined method&lt;br /&gt;3) Your actual service which extends RemoteServiceServlet and implements your interface created in step (1) &lt;br /&gt;** This should be a servlet and mapped properly in your web.xml&lt;br /&gt;&lt;br /&gt;The two interfaces will need to be in the com.ciient package, and the service in the com.server or on someother url on the same host where this will be deployed.&lt;br /&gt;&lt;br /&gt;So on to the code.&lt;br /&gt;&lt;br /&gt;First the interfaces and classes, following by a sample app that calls it:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;package com.client.time;&lt;br /&gt;public interface ITimeService extends RemoteService {&lt;br /&gt;    public Date getDate(); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;package com.client.time;&lt;br /&gt;public interface ITimeServiceAsync {&lt;br /&gt;    public void getDate(AsyncCallback callback); &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;package com.server.time;&lt;br /&gt;public class TimeService extends RemoteServiceServlet implements ITimeService {&lt;br /&gt;&lt;br /&gt;    public Date getDate() {&lt;br /&gt;        return new Date(); &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt; public void loadDate(final Label label) {&lt;br /&gt;        ITimeServiceAsync timeService = (ITimeServiceAsync) GWT.create(ITimeService.class);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        ServiceDefTarget endpoint = (ServiceDefTarget) timeService;        &lt;br /&gt;        endpoint.setServiceEntryPoint("/GWT/TimeService");&lt;br /&gt;&lt;br /&gt;        AsyncCallback callback = new AsyncCallback() {&lt;br /&gt;&lt;br /&gt;            public void onSuccess(Object result) {                &lt;br /&gt;                label.setText(result.toString());&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public void onFailure(Throwable caught) {&lt;br /&gt;                 label.setText("failure" + caught.getMessage());&lt;br /&gt;            }&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        label.setText("pending");&lt;br /&gt;        timeService.getDate(callback);&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3482833143443210211?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3482833143443210211/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3482833143443210211' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3482833143443210211'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3482833143443210211'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/google-web-toolkit-rpc-call.html' title='Google Web Toolkit - RPC Call'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-7799583171766270996</id><published>2008-07-03T02:34:00.000-07:00</published><updated>2008-07-03T02:39:40.613-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='web development'/><category scheme='http://www.blogger.com/atom/ns#' term='google web toolkit'/><title type='text'>Google Web Toolkit - Simple Example</title><content type='html'>The Google Web Toolkit (GWT) is a framework that allows for web development using Java. The framework will convert the Java code into html/javascript. &lt;br /&gt;&lt;br /&gt;This approach makes developing and debugging feature rich clients much easier than writing javascript yourself. &lt;br /&gt;&lt;br /&gt;To get familiar with the framework I wrote a simple getting started app inspired from the GWT docs. It simply creates a button, label and grid.&lt;br /&gt;&lt;br /&gt;It adds an event to the button to hide the label. Also adds a TableListener to the grid. I use this to capture cell clicks and popup a dialog.&lt;br /&gt;&lt;br /&gt;Reading the docs you can then style this using css with the following attributes:     &lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    *  .gwt-DialogBox { the outside of the dialog }&lt;br /&gt;    * .gwt-DialogBox .Caption { the caption }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/*&lt;br /&gt; * mcintoshEntryPoint.java&lt;br /&gt; *&lt;br /&gt; * Created on June 29, 2008, 1:46 PM&lt;br /&gt; *&lt;br /&gt; * To change this template, choose Tools | Template Manager&lt;br /&gt; * and open the template in the editor.&lt;br /&gt; */&lt;br /&gt;package com.client;&lt;br /&gt;&lt;br /&gt;import com.google.gwt.core.client.EntryPoint;&lt;br /&gt;import com.google.gwt.user.client.ui.Button;&lt;br /&gt;import com.google.gwt.user.client.ui.ClickListener;&lt;br /&gt;import com.google.gwt.user.client.ui.DialogBox;&lt;br /&gt;import com.google.gwt.user.client.ui.Grid;&lt;br /&gt;import com.google.gwt.user.client.ui.HTMLTable;&lt;br /&gt;import com.google.gwt.user.client.ui.Label;&lt;br /&gt;import com.google.gwt.user.client.ui.PopupPanel;&lt;br /&gt;import com.google.gwt.user.client.ui.RootPanel;&lt;br /&gt;import com.google.gwt.user.client.ui.SourcesTableEvents;&lt;br /&gt;import com.google.gwt.user.client.ui.TableListener;&lt;br /&gt;import com.google.gwt.user.client.ui.Widget;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; *&lt;br /&gt; * @author avalanche&lt;br /&gt; */&lt;br /&gt;public class mcintoshEntryPoint implements EntryPoint {&lt;br /&gt;&lt;br /&gt;    /** Creates a new instance of mcintoshEntryPoint */&lt;br /&gt;    public mcintoshEntryPoint() {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    private static class MyDialog extends DialogBox {&lt;br /&gt;&lt;br /&gt;        public MyDialog(String text) {&lt;br /&gt;            // Set the dialog box's caption.&lt;br /&gt;            setText(text);&lt;br /&gt;&lt;br /&gt;            // DialogBox is a SimplePanel, so you have to set its widget property to&lt;br /&gt;            // whatever you want its contents to be.&lt;br /&gt;            Button ok = new Button("OK");&lt;br /&gt;            ok.addClickListener(new ClickListener() {&lt;br /&gt;&lt;br /&gt;                public void onClick(Widget sender) {&lt;br /&gt;                    MyDialog.this.hide();&lt;br /&gt;                }&lt;br /&gt;                });&lt;br /&gt;            setWidget(ok);&lt;br /&gt;        }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * The entry point method, called automatically by loading a module&lt;br /&gt;     * that declares an implementing class as an entry-point&lt;br /&gt;     */&lt;br /&gt;    public void onModuleLoad() {&lt;br /&gt;        final Label label = new Label("Hello, Chris!!!");&lt;br /&gt;        final Button button = new Button("Click me!");&lt;br /&gt;        final HTMLTable table = new Grid(5, 5);&lt;br /&gt;        // Put some values in the grid cells.&lt;br /&gt;        for (int row = 0; row &lt; 5; ++row) {&lt;br /&gt;            for (int col = 0; col &lt; 5; ++col) {&lt;br /&gt;                table.setText(row, col, "" + row + ", " + col);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        button.addClickListener(new ClickListener() {&lt;br /&gt;&lt;br /&gt;            public void onClick(Widget w) {&lt;br /&gt;                label.setVisible(!label.isVisible());&lt;br /&gt;            }&lt;br /&gt;            });&lt;br /&gt;&lt;br /&gt;        table.addTableListener(new TableListener() {&lt;br /&gt;&lt;br /&gt;            public void onCellClicked(SourcesTableEvents arg0, int arg1, int arg2) {&lt;br /&gt;                new MyDialog("You clicked" + arg1 + "," + arg2).show();&lt;br /&gt;            }&lt;br /&gt;            });&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        RootPanel.get().add(button);&lt;br /&gt;        RootPanel.get().add(label);&lt;br /&gt;        RootPanel.get().add(table);&lt;br /&gt;    }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-7799583171766270996?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/7799583171766270996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=7799583171766270996' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7799583171766270996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/7799583171766270996'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/07/google-web-toolkit-simple-example.html' title='Google Web Toolkit - Simple Example'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-6654736131219120376</id><published>2008-06-29T03:38:00.000-07:00</published><updated>2008-06-29T03:42:18.720-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='detours'/><title type='text'>Detours</title><content type='html'>Microsoft's Research team maintains a project that allows you to intercept any function call. The library does this by injecting code into the memory of that process. You then have the ability to do whatever you want and if you choose pass the call through. This can be very useful for instrumentation for example. &lt;br /&gt;&lt;br /&gt;Here's a very simple example that isn't of much use since everything is in the same process, but it shows how I can intercept the call to Window's sleep function.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;//////////////////////////////////////////////////////////////////////////////&lt;br /&gt;//&lt;br /&gt;//  Detours Test Program (simple.cpp of simple.dll)&lt;br /&gt;//&lt;br /&gt;//  Microsoft Research Detours Package, Version 2.1.&lt;br /&gt;//&lt;br /&gt;//  Copyright (c) Microsoft Corporation.  All rights reserved.&lt;br /&gt;//&lt;br /&gt;//  This DLL will detour the Windows Sleep API so that TimedSleep function&lt;br /&gt;//  gets called instead.  TimedSleep records the before and after times, and&lt;br /&gt;//  calls the real Sleep API through the TrueSleep function pointer.&lt;br /&gt;//&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;windows.h&gt;&lt;br /&gt;#include "detours.h"&lt;br /&gt;&lt;br /&gt;static LONG dwSlept = 0;&lt;br /&gt;static VOID (WINAPI * TrueSleep)(DWORD dwMilliseconds) = Sleep;&lt;br /&gt;&lt;br /&gt;VOID WINAPI TimedSleep(DWORD dwMilliseconds)&lt;br /&gt;{&lt;br /&gt;    DWORD dwBeg = GetTickCount();&lt;br /&gt;    TrueSleep(dwMilliseconds);&lt;br /&gt;    DWORD dwEnd = GetTickCount();&lt;br /&gt;&lt;br /&gt;    InterlockedExchangeAdd(&amp;dwSlept, 3636);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)&lt;br /&gt;int __cdecl main(int argc, char ** argv)&lt;br /&gt;{&lt;br /&gt;    LONG error;&lt;br /&gt;    &lt;br /&gt;    &lt;br /&gt;     printf("simple.dll: Starting.\n");&lt;br /&gt;    fflush(stdout);&lt;br /&gt;&lt;br /&gt;    DetourRestoreAfterWith();&lt;br /&gt;&lt;br /&gt;    DetourTransactionBegin();&lt;br /&gt;    DetourUpdateThread(GetCurrentThread());&lt;br /&gt;    DetourAttach(&amp;(PVOID&amp;)TrueSleep, TimedSleep);&lt;br /&gt;    error = DetourTransactionCommit();&lt;br /&gt;&lt;br /&gt;    if (error == NO_ERROR) {&lt;br /&gt;        printf("dwslept %d",dwSlept); &lt;br /&gt;  printf("simple.dll: Detoured Sleep().\n");&lt;br /&gt;  printf("sleep5.exe: Starting.\n");&lt;br /&gt;&lt;br /&gt;        Sleep(5000);&lt;br /&gt;&lt;br /&gt;        printf("sleep5.exe: Done sleeping.\n");&lt;br /&gt;  printf("dwslept %d",dwSlept); &lt;br /&gt;    }&lt;br /&gt;    else {&lt;br /&gt;        printf("simple.dll: Error detouring Sleep(): %d\n", error);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    DetourTransactionBegin();&lt;br /&gt;    DetourUpdateThread(GetCurrentThread());&lt;br /&gt;    DetourDetach(&amp;(PVOID&amp;)TrueSleep, TimedSleep);&lt;br /&gt;    error = DetourTransactionCommit();&lt;br /&gt;&lt;br /&gt;    printf("simple.dll: Removed Sleep() (result=%d), slept %d ticks.\n",&lt;br /&gt;           error, dwSlept);&lt;br /&gt;    fflush(stdout);&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//&lt;br /&gt;///////////////////////////////////////////////////////////////// End of File.&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In order for this to work you first need to download the detours package from http://research.microsoft.com/sn/detours/&lt;br /&gt;&lt;br /&gt;Compile the source as a lib and link to it. &lt;br /&gt;&lt;br /&gt;Give it a shot its pretty cool! &lt;br /&gt;&lt;br /&gt;Enjoy...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-6654736131219120376?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/6654736131219120376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=6654736131219120376' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6654736131219120376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/6654736131219120376'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/06/detours.html' title='Detours'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-571598134902120900</id><published>2008-06-29T02:26:00.000-07:00</published><updated>2008-06-29T02:32:18.857-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c#'/><title type='text'>c# using statement</title><content type='html'>The C# 'using' statement provides a convenient syntax that ensures the correct use of IDisposable objects.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;  using (BlackJackForm blackjackform = new BlackJackForm())&lt;br /&gt;  {&lt;br /&gt;    Hide();&lt;br /&gt;    blackjackform.ShowDialog();&lt;br /&gt;    Show();&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The above is the equivalent of the following more verbose code: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;  BlackJackForm blackjackform = new BlackJackForm()&lt;br /&gt;  try {&lt;br /&gt;    Hide();&lt;br /&gt;    blackjackform.ShowDialog();&lt;br /&gt;    Show();&lt;br /&gt;  }&lt;br /&gt;  finally {&lt;br /&gt;    blackjackform.Dispose();&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The using statement only works with items implementing the IDisposable interface.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-571598134902120900?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/571598134902120900/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=571598134902120900' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/571598134902120900'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/571598134902120900'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/06/c-using-statement.html' title='c# using statement'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-5457695330900756754</id><published>2008-06-27T15:56:00.000-07:00</published><updated>2008-09-12T12:33:44.939-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='games'/><category scheme='http://www.blogger.com/atom/ns#' term='war card game'/><title type='text'>C++ War Card Game</title><content type='html'>While reading &lt;a href="http://www.amazon.com/gp/product/0201308797?ie=UTF8&amp;amp;tag=develoresour-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0201308797"&gt;Data Structures in C++: Using the Standard Template Library (STL)&lt;/a&gt;&lt;img src="http://www.assoc-amazon.com/e/ir?t=develoresour-20&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0201308797" alt="" style="border: medium none  ! important; margin: 0px ! important;" width="1" border="0" height="1" /&gt;&lt;br /&gt;&lt;br /&gt;The author brought up the War card game. I thought it would be interesting challenge to see how fast I could create the game. So just for fun here is the source in case anyone is interested.&lt;br /&gt;&lt;br /&gt;The rules are simple. Each player has half the deck they flip over the top card each time. Whichever card is higher (2 being lowest Ace being the highest) that person gets both cards. On tie each player flips over three cards and the fourth determines who wins everything.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;mono&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;queue&amp;gt;&lt;br /&gt;#include &amp;lt;ctime&amp;gt;    // For time()&lt;br /&gt;#include &amp;lt;cstdlib&amp;gt;  // For srand() and rand()&lt;br /&gt;using namespace std;&lt;br /&gt;struct Card {&lt;br /&gt; Card(int value) {&lt;br /&gt;   this-&gt;value = value;&lt;br /&gt; }&lt;br /&gt; int getValue() {&lt;br /&gt;   return value;&lt;br /&gt; }&lt;br /&gt; private:&lt;br /&gt;   int value;&lt;br /&gt;};&lt;br /&gt;struct Player{&lt;br /&gt; queue&lt;card&gt; cards;&lt;br /&gt;};&lt;br /&gt;struct Deck {&lt;br /&gt; vector&lt;card&gt; cards;&lt;br /&gt; Deck() {&lt;br /&gt;   for(int i=1;i&lt;14;++i) j="1;j&lt;5;++j)" j="0;j&lt;10;++j)" i="0;i&lt; cards.size();++i)" int="" r="(rand()" card="" temp="cards[i];" struct="" game="" void="" deck="" player="" cout=""&gt;&lt;&lt; "dealing" &lt;&lt; i="0;i&lt;deck.cards.size();i+="2)" cout=""&gt;&lt;&lt;&gt; undecided;&lt;br /&gt;   int count =0;&lt;br /&gt;   while(a.cards.size() &gt;0 &amp;amp;&amp;amp; b.cards.size() &gt;0) {&lt;br /&gt;     cout &lt;&lt; ++count &lt;&lt; ": A cards: " &lt;&lt; acard =" a.cards.front();" bcard =" b.cards.front();"&gt; bCard.getValue()) {&lt;br /&gt;       int size = undecided.size();&lt;br /&gt;       for(int i=0;i&lt;size;++i) else="" if=""&gt;&lt; size =" undecided.size();" i="0;i&lt; size;++i)" else="" were="" in="" take="" three="" more="" from="" each="" player="" int="" i="0;i&lt;3;++i)" if=""&gt; 0 ) {&lt;br /&gt;           undecided.push(a.cards.front());&lt;br /&gt;           a.cards.pop();&lt;br /&gt;         }&lt;br /&gt;         if (b.cards.size() &gt; 0 ) {&lt;br /&gt;           undecided.push(b.cards.front());&lt;br /&gt;           b.cards.pop();&lt;br /&gt;         }&lt;br /&gt;       }&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt; }&lt;br /&gt;};&lt;br /&gt;int main() {&lt;br /&gt; Game g;&lt;br /&gt; g.start();&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/mono&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-5457695330900756754?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/5457695330900756754/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=5457695330900756754' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5457695330900756754'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/5457695330900756754'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/06/c-war-card-game.html' title='C++ War Card Game'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-3382084439501919456</id><published>2008-06-27T15:34:00.001-07:00</published><updated>2008-06-27T15:34:54.208-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='random number'/><title type='text'>C++ get random number</title><content type='html'>#include &lt;ctime&gt;    // For time()&lt;br /&gt;#include &lt;cstdlib&gt;  // For srand() and rand()&lt;br /&gt;void foo() {&lt;br /&gt;    srand(time(0));  // Initialize random number generator. &lt;br /&gt;    int r = (rand() % 10) + 1;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8988177719810230455-3382084439501919456?l=developer-resource.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://developer-resource.blogspot.com/feeds/3382084439501919456/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8988177719810230455&amp;postID=3382084439501919456' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3382084439501919456'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8988177719810230455/posts/default/3382084439501919456'/><link rel='alternate' type='text/html' href='http://developer-resource.blogspot.com/2008/06/c-get-random-number.html' title='C++ get random number'/><author><name>developer-resource</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8988177719810230455.post-4159627671446307753</id><published>2008-06-27T15:04:00.000-07:00</published><updated>2009-01-13T14:03:10.940-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='stl'/><title type='text'>c++ std::find algorithm example</title><content type='html'>C++ standard library has many useful features. One of which is the std::find algorithm. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is a simple example:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#004a43; '&gt;#&lt;/span&gt;&lt;span style='color:#004a43; '&gt;include &lt;/span&gt;&lt;span style='color:#800000; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#40015a; '&gt;iostream&lt;/span&gt;&lt;span style='color:#800000; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#004a43; '&gt;#&lt;/span&gt;&lt;span style='color:#004a43; '&gt;include &lt;/span&gt;&lt;span style='color:#800000; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#40015a; '&gt;algorithm&lt;/span&gt;&lt;span style='color:#800000; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#400000; '&gt;main&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; data&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;100&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  data&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#008c00; '&gt;89&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;7&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;  &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; &lt;span style='color:#808030; '&gt;*&lt;/span&gt; where &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#666616; '&gt;std&lt;/span&gt;&lt;span style='color:#800080; '&gt;::&lt;/span&gt;&lt;span style='color:#603000; '&gt;find&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;data&lt;span style='color:#808030; '&gt;,&lt;/span&gt;data&lt;span style='color:#808030; '&gt;+&lt;/span&gt;&lt;span style='color:#008c00; '&gt;100&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;span style='color:#008c00; '&gt;7&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;
