Buca Bay - Always nice

Dua tiko noqu toa loaloa, na yacana ko… laga mai…

Client Side Application development for Web Developers

January31

Coming from a web development background, you would probably find using either Adobe AIR, or XUL the simplest to understand and require the least development. Both are developed to mimic browser based development, which is very high level. You work mostly with XML, and would use EcmaScript implementations you are familiar with like Javascript in XUL, or Actionscript in Air.

XUL is XML markup for UIs and is the platform on which Firefox is built. You just need XULRunner, from Mozilla, which is the runtime. XUL allows you to write the UI in XML, then use JavaScript for scripting. XULRunner acts like a mini firefox, providing the same environment you would see in Firefox Extensions or the actual Firefox runtime environment. The JavaScript has an interface to Java, C++, Python, ActiveX, etc. Storage can be XML files or SQLite.
You can embed HTML in XUL, as well as the Flash Player.

So basically you could set up your desktop application to work like a webpage, and just edit HTML and create links, forms etc. (links are relative and internal to your app, no server needed unless you add a http protocol based link) Do some JavaScript when you need a bit of UI animation or storage access etc. If you wanted flashy animations, just use the Flash Object in HTML etc.

With AIR, you can work in a HTML, Flex, or Flash environment. The HTML environment allows you to use JS as it known in browser scripting, and act as if you are working on a browser based application. HTML environment built around Webkit, which is the underlying platform for Safari and Chrome browsers and provides the Ecmascript runtime. The storage is embedded SQLite.

The Flex environment is similar to XUL, as it uses MXML, which is Adobe’s XML markup/standard for UI development. MXML uses Actionscript3.0 which is the latest Ecma specifications. It has some really nice scripting features, classes, libraries, namespaces, E4X etc. and great bindings with the XML UI (MXML).

I don’t have any experience with the Flash environment. Should be the same as FLEX but with the Flash IDE so you code less and do more visual development.

Silverlight I believe provides a similar environment, but I haven’t tried it yet.

Both XUL and AIR are very high level, so you don’t have to worry much about the environment and can focus more on building your App. It is great if you develop by yourself, as you can achieve more faster.

RSS Feeds via cross domain JSON proxy

January22

JavaScript remoting functions are limited to the same domain. For example, XMLHttpRequest can only retrieve URLs on the same domain, and the same applies for the Flash remoting methods.

JavaScript files however, can be hosted on a different domain, and this is the basis of a well known JavaScript remoting method.

To retrieve RSS feeds, you don’t need a proxy on the same domain.
You can actually have a proxy on a different domain, but have that proxy create a JavaScript file of the RSS XML text.

That is, encapsulate the RSS XML text in a JavaScript variable or function.

That way you can include the RSS as a JavaScript file.

For example, the yahoo top stories:
http://rss.news.yahoo.com/rss/topstories

Could be proxied as:

http://json-proxy.appjet.net/?url=http://rss.news.yahoo.com/rss/topstories

And retrieved via a simple <script> tag:

<script src="http://json-proxy.appjet.net/?url=http://rss.news.yahoo.com/rss/topstories"></script>

And recieved in JavaScript as:

function callback(rss) {
// manipular rss here..
}

I’ve written a JavaScript class that will do the heavy lifting, and allow you to retrieve RSS feeds cross-domain from within JavaScript.

You can view the JavaScript source at the project page.

I’m using this JavaScript class to power the images at the top of my blog. They are retrieved from a Flickr RSS feed via JavaScript, with no server side interaction on my domain - just the JSON proxy at Appjet.

Update: 25th Sept, 2009

Appjet has closed down. The JSON/RSS proxy now resides at: http://json-proxy.jgate.de/

So the URL to proxy any webpage would be: http://json-proxy.jgate.de/?url={url}

The JavaScript RSS Proxy library has been updated to reflect this.

Google AJAX Language API with PHP

January20

I had noticed some time ago that Google had released an API for their language translation service. A recent forum discussion made me revisit the API, and since I had a wee bit of time on my hands, I wrote a very rudimentary PHP class implementation of the API.

Google seems to like flaunting “AJAX”, in their APIs at least. So the API is called “Google AJAX Language API” and the main implementation is .. take a guess, AJAX. However, in addition to their pure JavaScript API, they also have a REST interface (of course JavaScript would need such an interface anyway).

The REST interface is just a HTTP endpoint (URL) that returns JSON. You just need to formulate a HTTP GET passing the parameters described in the API documentation, and Google will send you a nicely formated JSON response with the translated text and some other details.

Here is an example request, to translate “Hello World” to Italian.

http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it

The parameters are q=hello world and langpair=en|it

The JSON response looks like:

{"responseData": {"translatedText":"ciao mondo"}, "responseDetails": null, "responseStatus": 200}

So a simple implementation in PHP would be to use file_get_contents() to download the JSON text from the URL over HTTP and here it is in a PHP class.

http://code.google.com/p/php-language-api/source/browse/trunk/google.translator.php

Example Usage:

// example usage
$text = 'Welcome "to my " website.';
$trans_text = Google_Translate_API::translate($text, '', 'it');
if ($trans_text !== false) {
	echo $trans_text;
}

The class uses file_get_contents() which assumes your PHP has allow_url_fopen directive enabled in the PHP configuration (PHP.ini). This is usually the case, however, it can be disabled for security reasons (since it allows include() to use the URL wrapper and thus include remote files - a favourite exploit for attackers it to inject remote files into include() functions)

The class doesn’t use a JSON parser, I think its a bit of an overhead including the JSON libraries in PHP4. Intead it just uses regular expressions. The PCRE regular expression functions are pretty fast. PHP5 has native support for JSON however, so the class could be modified to use the PHP5 native JSON functions if you use PHP5 specifically.

Something I ran into was that Google returns not only UTF-8, but UTF-8 escape sequences. That is, they have characters outside the basic ASCII range escaped with the UTF-8 escape sequence which is \u followed by the character’s hex value. For example, the & symbol becomes:

\u0026

Cools aye. Sucks, because PHP does not understand this. JavaScript, which is the main method of invoking the Google Language API, understands this natively. PHP doesn’t even understand UTF-8 in PHP4. First I resorted to this ugly function to unescape the UTF-8 escape sequences (convert those UTF-8 sequence to actual UTF-8 byte sequences).

/**
         * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes. Old version.
         * @return UTF-8 String
         * @param $str String
         */
        function __unescapeUTF8EscapeSeq($str) {
                return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
        }

The function is ugly because it uses html_entity_decode() to do the transformation for us. We just convert the UTF-8 escape sequence to a HTML escape sequence (HTML entities), then use html_entity_decode() which PHP handles well. I decided on a compatible function that uses bitwise operations instead. Both are included in the source however for reference.

The code is very early development and will be buggy. You can check out the latest sources via SVN:

svn checkout http://php-language-api.googlecode.com/svn/trunk/ php-language-api-read-only

Feel free to let me know on the Google project page if you find any bugs.

http://code.google.com/p/php-language-api/issues/list

Moving my web development blog to Bucabay.com

January19

I’ve decided to move my web development blog here. I’m tired of having to edit more then one blog, there’s my personal blog, then theres my web development blog, and then my Fiji Web Design Blog etc.

So get ready for some boring posts on coding, rants on web standards and the like. I will try and section off my blog later however, so the “boring” stuff will be hidden in the background.

Bad Weather and DVD rips

January10

Been really bad weather here the last two days. I’ve been trying to get to Lautoka but the flooding along the roads has prevented that.

I’ve been following the weather from Nadraki.com and it looks like we’ll have a few more days of this.

Since recovering from my mystery illness - which the doctor named “a virus that is going around” (I had thought it was dengue) - I haven’t had the energy to do any long term coding. Just lil bits of work here and there, and the rest of the time has been occupied with watching DVD rips I’ve gotten off various torrents. Among the movies have been Australia, Gran Torino, Seven Pounds, Slumdog Millionaire, Bedtime Stories and “The Curious Case of Benjamin Button”.

Among all of those movies the best has to be Slumdog Millionaire, or maybe, Australia. Those two are really good movies.

The rest of my time has been spent sleeping, or sitting down on the porch, watching the rain, and there is lots of that to watch.

posted under general | No Comments »

New Year

January8

Happy New Year!

Now you can’t say I forgot.

2009 already, freaky. Seems like the time starts flying by faster the older you get. By the time I’m 40, a year should be just about… 1 day. Can you remember 1 day back in Primary School? Especially the last day of school, endless.

I can actually remember about every step I made from school to home when I was in class 4, on the last day of School. Everyone raced out of Class. I was a bit chubby so I wasn’t the fastest, to put it lightly, so I was really trying hard. In my mind, If I took really long steps, I would get there faster. So there I was, I think it was a Thursday, the end of 3rd term in 1991, with the hugest steps I could make, soaring through the air for an eternity with each stride.

I remember it was a beautiful day. It’s always very beautiful in Tukavesi. The sun was bright, and the ocean gleamed. It was hot, but the light breeze, and flying through the air, makes you forget that.

The only distraction I had that day was that I scored 81 in my “Maths” exam, my first 80. I didn’t blame myself though, I think it was a great trade-off. 81 in Math, for fighting fires in the pine forest that extended from behind our school to an endless land beyond. Climbing up straight rock faces, then jumping off into dense growths of “gasau” reeds (not something I recommend). Crawling into old caves, and burial caves. Hurling gasau reeds from the top of the cliffs and watching them glide like paper aeroplanes to the miniture school buildings below (we got punished for that). Tying our shirts over our head like Ninjas and picking plate sized hornets nests off the trees, to get our nicknames of course.

It was a by far, a worthy exchange.

posted under updates | No Comments »
Tag Cloud