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

<channel>
	<title>[MeIsProgrammer] &#187; Programming</title>
	<atom:link href="http://just-thor.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://just-thor.com</link>
	<description>all play and no work makes me a dull boy</description>
	<lastBuildDate>Sat, 03 Jul 2010 18:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>HTML5 Geolocation &#8211; Google Map with Modernizr</title>
		<link>http://just-thor.com/2010/07/04/html5-geolocation-google-map-with-modernizr/</link>
		<comments>http://just-thor.com/2010/07/04/html5-geolocation-google-map-with-modernizr/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 18:28:21 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=147</guid>
		<description><![CDATA[HTML5 defines a very handy function, which is geolocation. Basically, this function will try to locate user&#8217;s location. But at the moment, not every browser supports this function. We need [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5 defines a very handy function, which is geolocation. Basically, this function will try to locate user&#8217;s location. But at the moment, not every browser supports this function. We need to implement some tedious conditions to check whether the browser supports Geolocation or not. Luckily, there is an open source javascript library, Modernizr. Using modernizr is simple, just import a script and use it. For example, if I want to check the browser whether Geolocation is supported or not, I just write the following code</p>
<p><pre><code>
if (Modernizr.geolocation){
//...Your logic here
}
</code></pre></p>
<p>Here, I wrote a simple html page that locate your location using Geolocation and show it on the Google map.</p>
<p><a href="http://just-thor.com/sample-works/geolocation.html">http://just-thor.com/sample-works/geolocation.html</a></p>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/07/04/html5-geolocation-google-map-with-modernizr/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Obtaining Google Android Map API Key</title>
		<link>http://just-thor.com/2010/06/30/obtaining-google-android-map-api-key/</link>
		<comments>http://just-thor.com/2010/06/30/obtaining-google-android-map-api-key/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 08:53:32 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[google]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=152</guid>
		<description><![CDATA[As a beginner on Android platform, I was trying to embed Google map in my application. I have everything right, except the map is not loading, and no exception was [...]]]></description>
			<content:encoded><![CDATA[<p>As a beginner on Android platform, I was trying to embed Google map in my application. I have everything right, except the map is not loading, and no exception was thrown at the console.</p>
<p>After doing some searching on the internet, my problem seems like caused by incorrect API key. Obtaining Android Map API key is different from normal Google Map API key. I assume you are running Android emulator and also a beginner(like me <img src='http://just-thor.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ), first step is to find out the location of your keystore. In Eclipse, fire up the  &#8220;Preferences&#8221; window, and locate the Android-&gt;Build settings, you should see the location of your keystore file in the &#8220;Default debug keystore&#8221; field.</p>
<p>Type the following command in your command line. By the way, I am using Mac, I think *Nix user should have no problem running the following command, but for Windows Users, I think you need to find a way to do it.</p>
<p><em>keytool -list -keystore ~/.android/debug.keystore</em></p>
<p>and when it prompts you for the password, just hit the Enter key, no need to enter any password.<br /> And you will get something like this in the output</p>
<p>Certificate fingerprint (MD5): 8E:6F:5D:3F:A9:94:96:69:42:D2: <em>and more</em></p>
<p>copy the value of it, and go to this URL<br /> http://code.google.com/intl/sv-SE/android/maps-api-signup.html</p>
<p>Paste your value into the textbox and submit, and you should be able to get your API key. By using this Android Map API key, I was able to get the map working in my Android apps. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/06/30/obtaining-google-android-map-api-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A JSON Util</title>
		<link>http://just-thor.com/2010/03/17/a-json-util/</link>
		<comments>http://just-thor.com/2010/03/17/a-json-util/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 12:00:29 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=140</guid>
		<description><![CDATA[Nowadays JSON(json.org) is a very common data-interchange format other than XML, and its much more lightweights. I was trying to write a JSON util class today and came across many [...]]]></description>
			<content:encoded><![CDATA[<p>Nowadays JSON(json.org) is a very common data-interchange format other than XML, and its much more lightweights. I was trying to write a JSON util class today and came across many JSON lbraries. Its hard to determine which library is better, and I am not planning to do it here in this post neither. I wrote a simple wrapper and wish to share it out with you guys. I used json-simple(http://code.google.com/p/json-simple/), a simple yet useful JSON library. Here&#8217;s my code :<br />
<pre><pre class="brush: java">
package com.justthor;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;

public final class JsonUtil {
&nbsp;&nbsp;private static final LoggingUtil logger = LoggingUtil.getInstance(JsonUtil.class);
&nbsp;&nbsp;
&nbsp;&nbsp;private static final JSONParser jsonParser = new JSONParser();

&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;private static final Map EMPTY_MAP = Collections.EMPTY_MAP;
&nbsp;&nbsp;
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;private static final List EMPTY_LIST = Collections.EMPTY_LIST;
&nbsp;&nbsp;
&nbsp;&nbsp;private final JSONObject obj;
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Private constructor
&nbsp;&nbsp; */
&nbsp;&nbsp;private JsonUtil() {
&nbsp;&nbsp;&nbsp;&nbsp;obj = new JSONObject();
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Encode object to JSON string. Object can be String, Integer etc.
&nbsp;&nbsp; * @param Object 
&nbsp;&nbsp; * @return String
&nbsp;&nbsp; */
&nbsp;&nbsp;public static String encode(final Object obj) {
&nbsp;&nbsp;&nbsp;&nbsp;return JSONValue.toJSONString(obj);
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Decode string to raw object
&nbsp;&nbsp; * @param String 
&nbsp;&nbsp; * @return Object
&nbsp;&nbsp; */
&nbsp;&nbsp;public static Object decode(final String string) {
&nbsp;&nbsp;&nbsp;&nbsp;Object result;
&nbsp;&nbsp;&nbsp;&nbsp;try{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = jsonParser.parse(string);
&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = new Object();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.error(e.getMessage(), e);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return result;
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Decode string to List
&nbsp;&nbsp; * @param String 
&nbsp;&nbsp; * @return List
&nbsp;&nbsp; */
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;public static List decodeAsList(final String string) {
&nbsp;&nbsp;&nbsp;&nbsp;List result = EMPTY_LIST;
&nbsp;&nbsp;&nbsp;&nbsp;try{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = (List)jsonParser.parse(string);
&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.error(e.getMessage(), e);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return result;
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Decode string to Map
&nbsp;&nbsp; * @param String 
&nbsp;&nbsp; * @return Map
&nbsp;&nbsp; */
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;public static Map decodeAsMap(final String string) {
&nbsp;&nbsp;&nbsp;&nbsp;Map result = EMPTY_MAP;
&nbsp;&nbsp;&nbsp;&nbsp;try{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result = (Map)jsonParser.parse(string);
&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logger.error(e.getMessage(), e);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;return result;
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Get an instance of JsonUtil.
&nbsp;&nbsp; * @return JsonUtil
&nbsp;&nbsp; */
&nbsp;&nbsp;public static JsonUtil getInstance() {
&nbsp;&nbsp;&nbsp;&nbsp;return new JsonUtil();
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Put a name and value. 
&nbsp;&nbsp; * Example : 
&nbsp;&nbsp; * JsonUtil.getInstance().put(&quot;name&quot;, &quot;thor&quot;).put(&quot;vehicle&quot;, &quot;none&quot;).put(&quot;height&quot;, 165).toJsonString();
&nbsp;&nbsp; * @param Object
&nbsp;&nbsp; * @param Object
&nbsp;&nbsp; * @return JsonUtil
&nbsp;&nbsp; */
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;public JsonUtil put(final Object name, final Object value) {
&nbsp;&nbsp;&nbsp;&nbsp;obj.put(name, value);
&nbsp;&nbsp;&nbsp;&nbsp;return this;
&nbsp;&nbsp;}
&nbsp;&nbsp;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * Convert stored name and value into Json string.
&nbsp;&nbsp; * @return String
&nbsp;&nbsp; */
&nbsp;&nbsp;public String toJsonString(){
&nbsp;&nbsp;&nbsp;&nbsp;return obj.toString();
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>You can use it as a static class, such as<br />
<pre><pre class="brush: java">
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
... your code to populate the list here...

// Encode!
String jsonEncodedString = JsonUtil.encode(list);
</pre></pre></p>
<p>To decode, simply type the following<br />
<pre><pre class="brush: java">
List decodedList = (List)JsonUtil.decode(jsonEncodedString);
// Or even easier, I wrote another method to decode JSON String as List
List decodedList = JsonUtil.decodeList(jsonEncodedString);
</pre></pre></p>
<p>If you don&#8217;t feel like using Map, you can do this<br />
<pre><pre  class="brush: java">
String jsonEncodedString = JsonUtil.getInstance()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.put(&quot;name&quot;, &quot;Thor&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.put(&quot;url&quot;, &quot;www.just-thor.com&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toJsonString();
</pre></pre></p>
<p>A wrapper is important to encapsulate how things actually works. When you using the wrapper I wrote, you don&#8217;t have to import or even know that I am using json-simple or whatsoever. I hope this example helps you to understand the importance of writing a wrapper class.</p>
<p>Happy programming <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/03/17/a-json-util/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Ajax Libraries Gotcha!</title>
		<link>http://just-thor.com/2010/02/23/google-ajax-libraries-gotch/</link>
		<comments>http://just-thor.com/2010/02/23/google-ajax-libraries-gotch/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 09:37:58 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=129</guid>
		<description><![CDATA[I was working on Google Map for the past few days, and was using jQuery to retrieve markers information from another URL using ajax. I came across the Google Ajax [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on Google Map for the past few days, and was using jQuery to retrieve markers information from another URL using ajax. I came across the Google Ajax Libraries (http://code.google.com/apis/ajaxlibs/) which helps to load Javascript from CDN.</p>
<p>Here&#8217;s part of the code<br />
<pre><pre class="brush: javascript">
//sample code taken from http://code.google.com/apis/ajaxlibs/
&lt;script src=&quot;http://www.google.com/jsapi&quot;&gt;&lt;/script&gt;
&lt;script&gt;
&nbsp;&nbsp;// Load jQuery
&nbsp;&nbsp;google.load(&quot;jquery&quot;, &quot;1&quot;);

&nbsp;&nbsp;// on page load complete, fire off a jQuery json-p query
&nbsp;&nbsp;// against Google web search
&nbsp;&nbsp;google.setOnLoadCallback(function() {
&nbsp;&nbsp;&nbsp;&nbsp;$.getJSON(&quot;http://ajax.googleapis.com/ajax/services/search/web?q=google&amp;v=1.0&amp;callback=?&quot;,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// on search completion, process the results
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function (data) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (data.responseData.results &amp;&amp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.responseData.results.length &gt; 0) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var results = data.responseData.results;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var i=0; i &lt; results.length; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Display each result however you wish
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(results[i].titleNoFormatting);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;});
&lt;/script&gt;
</pre></pre><br />
By using this code, we actually load jQuery 1 from the CDN. However, there is a gotcha. If you follow the example I attached just now, it probably won&#8217;t work, well, at least not on my FF3.5 and IE8. In fact, you will have to do something like the following instead<br />
<pre><pre class="brush: javascript">
//sample code taken from http://code.google.com/apis/ajaxlibs/
&lt;script src=&quot;http://www.google.com/jsapi&quot;&gt;&lt;/script&gt;
&lt;script&gt;
&nbsp;&nbsp;// Load jQuery
&nbsp;&nbsp;google.load(&quot;jquery&quot;, &quot;1&quot;);
&lt;/script&gt;
&lt;script&gt;
&nbsp;&nbsp;// on page load complete, fire off a jQuery json-p query
&nbsp;&nbsp;// against Google web search
&nbsp;&nbsp;google.setOnLoadCallback(function() {
&nbsp;&nbsp;&nbsp;&nbsp;$.getJSON(&quot;http://ajax.googleapis.com/ajax/services/search/web?q=google&amp;v=1.0&amp;callback=?&quot;,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// on search completion, process the results
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function (data) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (data.responseData.results &amp;&amp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.responseData.results.length &gt; 0) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var results = data.responseData.results;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var i=0; i &lt; results.length; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Display each result however you wish
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(results[i].titleNoFormatting);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;});
&lt;/script&gt;
</pre></pre><br />
You will have to close the script tag that load external script from CDN before you can use it. Else you will get javascript error, in IE8, it shows &#8216;Object expected&#8217;.I really hope you will be able to read my article before you pull all your hair off. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Happy Coding</p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/02/23/google-ajax-libraries-gotch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A circular linked list in Java</title>
		<link>http://just-thor.com/2009/12/03/a-circular-linked-list-in-java/</link>
		<comments>http://just-thor.com/2009/12/03/a-circular-linked-list-in-java/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 10:26:21 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=117</guid>
		<description><![CDATA[I was helping a friend of mine with her work today, and we came across with a requirement that there are few players in the room we need to know [...]]]></description>
			<content:encoded><![CDATA[<p>I was helping a friend of mine with her work today, and we came across with a requirement that there are few players in the room we need to know whose turn is it after this. Yes, we can definitely use a LinkedList or an ArrayList, but its not so convenient as we have to check whether this player is at the last position of the list. If so,we will need to start from the beginning of the list again. And its also quite cumbersome to retrieve the next player info after this. Therefore, I wrote a simple yet functional Circular Linked List. You can download the source code and the compiled class <a href="http://just-thor.com/sample-works/CircularLinkedList.jar">here</a> . I will port this over to Scala whenever I have the time to do so.</p>
<p>Happy coding <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/12/03/a-circular-linked-list-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala reading content from the Web</title>
		<link>http://just-thor.com/2009/10/16/scala-reading-content-from-the-web/</link>
		<comments>http://just-thor.com/2009/10/16/scala-reading-content-from-the-web/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 10:54:03 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=84</guid>
		<description><![CDATA[I use scala.io.Source in my previous post, and now I am going to show another usage of this util. Let&#8217;s write a simple RSS feed parser that reads feed from [...]]]></description>
			<content:encoded><![CDATA[<p>I use scala.io.Source in my previous <a href="http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/">post</a>, and now I am going to show another usage of this util. Let&#8217;s write a simple RSS feed parser that reads feed from my website and parse it.</p>
<p>Here&#8217;s the code of it.</p>
<p><pre><pre class="brush: scala">
package com.justthor.reader

import scala.io.Source
import scala.xml._

object SimpleFeedReader {
&nbsp;&nbsp;def main(args : Array[String]) : Unit = {
&nbsp;&nbsp;&nbsp;&nbsp;val url = &quot;http://just-thor.com/feed/&quot;
&nbsp;&nbsp;&nbsp;&nbsp;val data = XML.loadString( Source.fromURL(url).mkString )
&nbsp;&nbsp;&nbsp;&nbsp;data \ &quot;channel&quot; \ &quot;item&quot; foreach(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a =&gt; println((a \ &quot;title&quot; text) + &quot; == &quot;&nbsp;&nbsp;+ (a \ &quot;link&quot; text))
&nbsp;&nbsp;&nbsp;&nbsp;)
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>Ok,let&#8217;s go thru the code line by line.</p>
<p>At line 09, again we use Source to load content from url,by calling the method <strong>fromURL</strong>(quite obvious,right?).The <strong>mkString </strong>will create a String out of the content. Then, we use <strong>XML.loadString</strong> to parse the String into an XML object. </p>
<p>At line 10, we traverse the XML object with something like XQuery.<br />
The RSS xml format is something like this<br />
<pre><pre class="brush: xml">
&lt;channel&gt;
&lt;link&gt;...&lt;/link&gt;
&lt;description&gt;...&lt;/description&gt;
&lt;!-- item 1--&gt;
&lt;item&gt;
&nbsp;&nbsp;&lt;title&gt;&lt;/title&gt;
&nbsp;&nbsp;&lt;link&gt;&lt;/link&gt;
&lt;/item&gt;
&lt;!-- item 2--&gt;
&lt;item&gt;
&nbsp;&nbsp;&lt;title&gt;&lt;/title&gt;
&nbsp;&nbsp;&lt;link&gt;&lt;/link&gt;
&lt;/item&gt;
&lt;!-- and the rest goes here --&gt;
&lt;/channel&gt;
</pre></pre></p>
<p>So when we write <pre class="brush: scala">data \ &quot;channel&quot; \ &quot;item&quot;</pre>, we mean traverse to channel node, then item node of it.It will return a collection of item nodes, and then we loop it. At line 11, we will print the text within the title node and the link node.</p>
<p>Here&#8217;s the output of the result<br />
<pre><pre class="brush: text">
File Reading in Java and Scala == http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/
PHP communicate with Java using JSON – Unicode Version == http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/
Scala And JDBC == http://just-thor.com/2009/09/27/scala-and-jdbc/
A mistake or fate? == http://just-thor.com/2009/09/17/a-mistake-or-fate/
Women are weird == http://just-thor.com/2009/09/13/women-are-weird/
20 must visit sites for Java developer == http://just-thor.com/2009/09/12/20-must-visit-sites-for-java-developer/
Why do I practice pair programming? == http://just-thor.com/2009/09/08/why-do-i-practice-pair-programming/
Installing Scala on Windows == http://just-thor.com/2009/09/02/installing-scala-on-window/
Logging for different requested URI in Apache HTTP Server == http://just-thor.com/2009/09/01/logging-for-different-requested-uri-in-apache-http-server/
Say hello to Java and Scala == http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/
</pre></pre></p>
<p>And now we have a very simple yet functional RSS parser.</p>
<p>Happy coding, <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/10/16/scala-reading-content-from-the-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Reading in Java and Scala</title>
		<link>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/</link>
		<comments>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 16:46:47 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=75</guid>
		<description><![CDATA[Today we are going to look at reading files in Java and how to do the same thing in Scala. This is normally how we read files in Java.First we [...]]]></description>
			<content:encoded><![CDATA[<p>Today we are going to look at reading files in Java and how to do the same thing in Scala.</p>
<p>This is normally how we read files in Java.First we open a reading stream, then read the content and process it,and at last we close the stream.</p>
<p><pre><pre class="brush: java">
package com.justthor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SampleFileReader {

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param args
&nbsp;&nbsp; */
&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader in; 
&nbsp;&nbsp;&nbsp;&nbsp;try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open file
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in = new BufferedReader(new FileReader(&quot;test.txt&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String str;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Start reading
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((str = in.readLine()) != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Print the content
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(str);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Close the stream
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>With some third party libraries, such as <a href="http://commons.apache.org/io/index.html">Apache Common Io</a>, we can simplify the code to something like this.</p>
<p><pre><pre class="brush: java">
package com.justthor;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class SampleFileReader {

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param args
&nbsp;&nbsp; */
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;String&gt; contents = FileUtils.readLines(new File(&quot;test.txt&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( String content : contents ) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(content);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated catch block
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>Well, it does look a bit simple now compare to the previous version. But we only manage to type 1 or 2 lines less than the previous version. This is due to we are force to handle the checked IOException. What if we try to achieve the same thing using Scala?</p>
<p><pre><pre class="brush: scala">
package com.justthor

import scala.io.Source

object SampleFileReader {
 def main(args : Array[String]) = {
&nbsp;&nbsp;Source fromFile(&quot;test.txt&quot;) getLines foreach { println }
 }
}
</pre></pre></p>
<p>Its much simple, right? Scala provides an util for IO named <strong>Source</strong>. This util read the file and get the contents by calling <strong>getLines</strong> method. This <strong>getLines</strong> method returns a <strong>Iterator[String]</strong>. And from there we can process the content by looping the returned iterator. We can use <strong>Source</strong> util to do more than reading files. I will provide more examples in the next few posts.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP communicate with Java using JSON &#8211; Unicode Version</title>
		<link>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/</link>
		<comments>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 12:30:13 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=71</guid>
		<description><![CDATA[JSON is one of the most popular message exchange format nowadays used in web application.Most of the time,we used JSON as a communication message format between Javascript and Server side [...]]]></description>
			<content:encoded><![CDATA[<p>JSON is one of the most popular message exchange format nowadays used in web application.Most of the time,we used JSON as a communication message format between Javascript and Server side language,such as PHP.We can also use it to exchange messages between different languages.</p>
<p>For instance,we want to store a PHP array in database and later to be processed by Java,we can do something like</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;&quot;value1&quot;,&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue)
//we will get something like {&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}
//and then, we store the value to db
</pre></pre></p>
<p>On the other hand,we use Java  to read it and decode it.</p>
<p><pre><pre class="brush: java">
//read from db and got the resultset,assuming we are using JDBC
while (rs.next()){
String rawValue = rs.getString(&quot;encodedValue&quot;);
// since we encoded an associative array,it will be translated to a Java Map object instance.
// we are using JSON-lib for JSON parsing the following example
JSONObject json = JSONObject.fromObject(jsonString);
Map decodedValue = (Map) JSONObject.toBean(json, Map.class);
String value1 = decodedValue.get(&quot;key1&quot;);
}
</pre></pre></p>
<p>What if we are trying to store unicode value using JSON format in PHP?<br />
For example,</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;&quot;你好吗&quot;,&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue);
//This is what we get after encoded
//{&quot;key1&quot;:&quot;\u4f60\u597d\u5417&quot;,&quot;key2&quot;:&quot;value2&quot;}
</pre></pre></p>
<p>If you decoded it back to Java,key1 value will return &#8220;u4f60u597du5417&#8243; instead.This is so wrong.</p>
<p>How to solve it?<br />
We can use <b>mb_convert_encoding</b> function in PHP.<br />
Let&#8217;s make some changes to our code.</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;mb_convert_encoding(&quot;你好吗&quot;, &quot;UTF-8&quot;),&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue);
</pre></pre></p>
<p>By doing so,values are stored in database in unicode format,and you can decode it back in Java.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Scala And JDBC</title>
		<link>http://just-thor.com/2009/09/27/scala-and-jdbc/</link>
		<comments>http://just-thor.com/2009/09/27/scala-and-jdbc/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 10:00:24 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=68</guid>
		<description><![CDATA[I was writing some small programs yesterday trying to read data from one database and input to another database.Hence,I tried to use Scala to do that.I&#8217;ve never write scala to [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing some small programs yesterday trying to read data from one database and input to another database.Hence,I tried to use Scala to do that.I&#8217;ve never write scala to connect to a database using JDBC before,but I am quite surprise with the simplicity of the code.It merely took me 10 minutes to finish writing the code.</p>
<p>Here&#8217;s the code<br />
<pre><pre class="brush: scala">
// Import JDBC package from standard Java SDK
import java.sql.{Connection, DriverManager, ResultSet};

class DataReader {
&nbsp;&nbsp;// Load the driver
&nbsp;&nbsp;Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance;

&nbsp;&nbsp;val connection = DriverManager getConnection &quot;jdbc:mysql://localhost:3306/mail?user=root&amp;amp;password=&amp;amp;characterEncoding=UTF8&quot;

&nbsp;&nbsp;def startReadingData(){
&nbsp;&nbsp;&nbsp;&nbsp;try{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Create statement for readonly
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val statement = conn createStatement (ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val rs = statement executeQuery &quot;select * from mail_queues order by id&quot;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Looping resultset
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (rs next) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;println (rs getLong &quot;id&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statement close
&nbsp;&nbsp;&nbsp;&nbsp;}catch {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case e =&amp;gt; e printStackTrace
&nbsp;&nbsp;&nbsp;&nbsp;}finally {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection close
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}

}
</pre></pre></p>
<p>Ok, you maybe wondering,why are so many dots(.) and parentheses missing?In Scala,when you are invoking one function with no or one parameter, you can omit the dot and parentheses. And Scala doesn&#8217;t have checked exception, which means you can write the above code without enclosed it with try&#8230;catch clause.I included it to make sure the DB connection is properly closed after use.</p>
<p>Happy coding.:)</p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/27/scala-and-jdbc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>20 must visit sites for Java developer</title>
		<link>http://just-thor.com/2009/09/12/20-must-visit-sites-for-java-developer/</link>
		<comments>http://just-thor.com/2009/09/12/20-must-visit-sites-for-java-developer/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 03:06:56 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=39</guid>
		<description><![CDATA[Here is a list of urls I visit to get information on Java. 1.http://www.javaalmanac.com 2.http://www.onjava.com 3.http://java.sun.com 4.http://www.developer.com/java 5.http://www.java.net 6.http://www.builder.com 7.http://www.ibm.com/developerworks/java 8.http://www.javaworld.com 9.http://www.devx.com/java 10.http://www.fawcette.com/javapro 11.http://www.sys-con.com/java 12.http://www.javadesktop.org 13.http://www.theserverside.com 14.http://www.jars.com 15.http://www.jguru.com 16.http://www.javaranch.com 17.http://www.ibiblio.org/javafaq/javafaq.html [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a list of urls I visit to get information on Java.</p>
<p>1.http://www.javaalmanac.com</p>
<p>2.http://www.onjava.com</p>
<p>3.http://java.sun.com</p>
<p>4.http://www.developer.com/java</p>
<p>5.http://www.java.net</p>
<p>6.http://www.builder.com</p>
<p>7.http://www.ibm.com/developerworks/java</p>
<p>8.http://www.javaworld.com</p>
<p>9.http://www.devx.com/java</p>
<p>10.http://www.fawcette.com/javapro</p>
<p>11.http://www.sys-con.com/java</p>
<p>12.http://www.javadesktop.org</p>
<p>13.http://www.theserverside.com</p>
<p>14.http://www.jars.com</p>
<p>15.http://www.jguru.com</p>
<p>16.http://www.javaranch.com</p>
<p>17.http://www.ibiblio.org/javafaq/javafaq.html</p>
<p>18.http://java.sun.com/docs/books/tutorial/</p>
<p>19.http://www.javablogs.com</p>
<p>20.http://java.about.com/</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/12/20-must-visit-sites-for-java-developer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
