<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Cambiatablog</title>
	<atom:link href="http://cambiatablog.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cambiatablog.wordpress.com</link>
	<description>Programming and music</description>
	<lastBuildDate>Tue, 24 Jan 2012 09:15:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='cambiatablog.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Cambiatablog</title>
		<link>http://cambiatablog.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://cambiatablog.wordpress.com/osd.xml" title="Cambiatablog" />
	<atom:link rel='hub' href='http://cambiatablog.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Algorithm for string similarity &#8211; better than php Levenshtein and similar_text</title>
		<link>http://cambiatablog.wordpress.com/2011/03/25/algorithm-for-string-similarity-better-than-levenshtein-and-similar_text/</link>
		<comments>http://cambiatablog.wordpress.com/2011/03/25/algorithm-for-string-similarity-better-than-levenshtein-and-similar_text/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 20:16:05 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=393</guid>
		<description><![CDATA[In a current pedagogical project, the students are supposed to answer questions/solving tasks using a drag-and-drop interface. This interface has to be general and loadable with different kind of pedagogical stuff. Examples could sorting things in order depending on let&#8217;s say weight, or sorting months in the right order onto a circular board with 12 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=393&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a current pedagogical project, the students are supposed to answer questions/solving tasks using a drag-and-drop interface. This interface has to be general and loadable with different kind of pedagogical stuff. Examples could sorting things in order depending on let&#8217;s say weight, or sorting months in the right order onto a circular board with 12 &#8220;slots&#8221;, one for each month.</p>
<p>For checking and verifying the students answers, I use a solution where each sortable item carries a single character tag. In the months example, january has the tag character &#8220;a&#8221;, february has the tag character &#8220;b&#8221; etc. This means that a correct answer would be &#8220;abcdefghijkl&#8221; &#8211; all the 12 months in the correct order.</p>
<p>My problem is the following: What algorithm to use to get a &#8220;pedagogically usable and fair&#8221; result value when the student&#8217;s answer pattern is partly correct, but differs from the correct answer in a way that php Levenshtein and similar_text algorithms doesn&#8217;t handle well. Example:</p>
<p><pre class="brush: php;">
similar_text('jonas', 'xxjon', $similar); echo $similar; // returns 60
similar_text('jonas', 'asjon', $similar); echo $similar; // returns 60 &amp;lt;- !
echo levenshtein('jonas', 'xxjon'); // returns 4
echo levenshtein('jonas', 'asjon'); // returns 4  &amp;lt;- !
</pre></p>
<p>As seen in these examples, the similar_text and the Levenshtein functions returns the same value for both strings, in spite of the fact that the &#8220;asjon&#8221; string is much closer to the &#8220;jonas&#8221; when it comes to the character content.</p>
<p>I&#8217;ve been lurking around for some more elaborated solution that would suite my needs better, but not yet found one. So, I had to give it a go myself. And here&#8217;s what I&#8217;ve come up with:</p>
<p><pre class="brush: php;">
static public function string_compare($str_a, $str_b)
{
    $length = strlen($str_a);
    $length_b = strlen($str_b);

&lt;code&gt;    $i = 0;
    $segmentcount = 0;
    $segmentsinfo = array();
    $segment = '';
    while ($i &lt; $length)
    {
        $char = substr($str_a, $i, 1);
        if (strpos($str_b, $char) !== FALSE)
        {
            $segment = $segment.$char;
            if (strpos($str_b, $segment) !== FALSE)
            {
                $segmentpos_a = $i - strlen($segment) + 1;
                $segmentpos_b = strpos($str_b, $segment);
                $positiondiff = abs($segmentpos_a - $segmentpos_b);
                $posfactor = ($length - $positiondiff) / $length_b; // &lt;-- ?
                $lengthfactor = strlen($segment)/$length;
                $segmentsinfo[$segmentcount] = array( 'segment' =&gt; $segment, 'score' =&gt; ($posfactor * $lengthfactor));
            }
            else
            {
                 $segment = '';
                 $i--;
                 $segmentcount++;
             }
         }
         else
         {
             $segment = '';
            $segmentcount++;
         }
         $i++;
     }

     // PHP 5.3 lambda in array_map
     $totalscore = array_sum(array_map(function($v) { return $v['score'];  }, $segmentsinfo));
     return $totalscore;
}
</pre></p>
<p>Some results:</p>
<ul>
<li>jonas / jonax : 0.8</li>
<li>jonas / sjona : 0.68</li>
<li>jonas / sjonas : 0.66</li>
<li>jonas / asjon : 0.52</li>
<li>jonas / xxjon : 0.36</li>
</ul>
<p>I&#8217;m sure i isn&#8217;t perfect, and that it could be optimized, but nevertheless it seems to produce the results that I&#8217;m after&#8230; One weak spot is that when strings have different length, it produces different result when the values are swapped. But, will do for a start!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/393/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/393/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/393/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/393/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/393/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/393/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/393/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/393/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=393&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2011/03/25/algorithm-for-string-similarity-better-than-levenshtein-and-similar_text/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>
	</item>
		<item>
		<title>Php: interacting with Google spreadsheet using Zend Gdata</title>
		<link>http://cambiatablog.wordpress.com/2011/02/05/php-interacting-wit-google-spreadsheet-using-zend-gdata/</link>
		<comments>http://cambiatablog.wordpress.com/2011/02/05/php-interacting-wit-google-spreadsheet-using-zend-gdata/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 09:47:05 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=385</guid>
		<description><![CDATA[Here&#8217;s a quick post describing how to interact with a Google spreadsheet using Zend Gdata libraries. Download the libraries from here, grab the Zend directory from the library folder and put it in the root of the script described below. Create a new Google spreadsheet, and grab the &#8220;key&#8221; part from the browser address bar [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=385&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick post describing how to interact with a Google spreadsheet using Zend Gdata libraries. Download the libraries from <a href="http://framework.zend.com/download/webservices" target="_blank">here</a>, grab the Zend directory from the library folder and put it in the root of the script described below.</p>
<p>Create a new Google spreadsheet, and grab the &#8220;key&#8221; part from the browser address bar query string. This is the unique key used by the api to reference your spreadsheet. You also have to set your google account username and password.</p>
<p>The script isn&#8217;t fancy at all, but it does the basics:</p>
<ul>
<li>Get the spreadsheet data content into an array</li>
<li>Get the column names</li>
<li>Dynamically adds some new rows</li>
</ul>
<p>It&#8217;s a good start, I guess. The Zend Gdata Spreadsheet api seems to be well developed. I&#8217;m looking for solutions for doing the same thing with text documents, but I haven&#8217;t come through yet.</p>
<p>Well, here&#8217;s the script. Be my guest!</p>
<p><pre class="brush: php;">
&lt;?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_Docs');

echo &quot;&lt;pre&gt;&quot;;

//-------------------------------------------------------------------------------
// Google user account

$username = 'yourusername'; // Your google account username
$password = 'yourpassword'; // Your google account password

//-------------------------------------------------------------------------------
// Document key - get it from browser addres bar query key for your open spreadsheet

$key = 'tx1LYk4BpIQaglM38cJbTNA';

//---------------------------------------------------------------------------------
// Init Zend Gdata service

$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($username, $password, $service);
$spreadSheetService = new Zend_Gdata_Spreadsheets($client);

//--------------------------------------------------------------------------------
// Example 1: Get cell data

$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query-&gt;setSpreadsheetKey($key);
$feed = $spreadSheetService-&gt;getWorksheetFeed($query);
$entries = $feed-&gt;entries[0]-&gt;getContentsAsRows();
echo &quot;&lt;hr&gt;&lt;h3&gt;Example 1: Get cell data&lt;/h3&gt;&quot;;
echo var_export($entries, true);

//----------------------------------------------------------------------------------
// Example 2: Get column information

$query = new Zend_Gdata_Spreadsheets_CellQuery();
$query-&gt;setSpreadsheetKey($key);
$feed = $spreadSheetService-&gt;getCellFeed($query);
$columnCount = $feed-&gt;getColumnCount()-&gt;getText();
$columns = array();
for ($i = 0; $i &lt; $columnCount; $i++) {
	$columnName = $feed-&gt;entries[$i]-&gt;getCell()-&gt;getText();
	$columns[$i] = strtolower(str_replace(' ', '', $columnName));
}
echo &quot;&lt;hr&gt;&lt;h3&gt;Example 2: Get column information&lt;/h3&gt;&quot;;
echo &quot;Nr of columns: $columnCount&quot;;
echo &quot;&lt;br&gt;Columns: &quot;;
echo var_export($columns, true);

//-------------------------------------------------------------------------------------------------
// Example 3: Add cell data

$testData = array();
foreach ($columns as $col) {
	$testData[$col] = &quot;Dynamically added &quot; . date(&quot;Y-m-d H:i:s&quot;) . &quot; in column &quot; . $col;
}
$ret = $spreadSheetService-&gt;insertRow($testData, $key);
//echo var_export($ret, true);

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/385/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=385&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2011/02/05/php-interacting-wit-google-spreadsheet-using-zend-gdata/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>
	</item>
		<item>
		<title>GWT and Mvp4g tutorial &#8211; Part 1</title>
		<link>http://cambiatablog.wordpress.com/2010/12/04/gwt-and-mvp4g-tutorial-1/</link>
		<comments>http://cambiatablog.wordpress.com/2010/12/04/gwt-and-mvp4g-tutorial-1/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 01:08:12 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Mvp4g]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=369</guid>
		<description><![CDATA[On the Flex side, I&#8217;ve been using RobotLegs for a while, and really started to appreciate the eventBus oriented architecture. Now, when trying out GWT, I want to do the same here. The search for suitable solution has led me to Mvp4g (http://code.google.com/p/mvp4g/). As always, the very best way of really getting the grips is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=369&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On the Flex side, I&#8217;ve been using RobotLegs for a while, and really started to appreciate the eventBus oriented architecture. Now, when trying out GWT, I want to do the same here. The search for suitable solution has led me to Mvp4g (<a href="http://code.google.com/p/mvp4g/">http://code.google.com/p/mvp4g/</a>). As always, the very best way of really getting the grips is trying to explain to others&#8230; So, here&#8217;s the first part of some very basic tutorials on how to use the Mvp4g framework.</p>
<h2>Creating the project Mvp4gEx1</h2>
<p>Create a new GWT Web Application Project in Eclipse, give it the project name Mvp4gEx1, use a package path of your liking (in this tutorial we use <strong>ex</strong><img src="/DOCUME%7E1/ADMINI%7E1.EXP/LOCALS%7E1/Temp/moz-screenshot-4.png" alt="" /> as a minimalistic root package), and make sure to uncheck the Use Google App Engine checkbox.</p>
<p><a href="http://cambiatablog.files.wordpress.com/2010/12/mvp4g1.png"><img class="alignnone size-full wp-image-370" title="mvp4g1" src="http://cambiatablog.files.wordpress.com/2010/12/mvp4g1.png?w=594" alt=""   /></a></p>
<p>Testrun the example (using F11) to make sure that it compiles and runs in hosted mode as expected.</p>
<h2>Mvp4g dependencies</h2>
<p>To get us up and running, we need some library jars.<br />
Here I use the following:</p>
<ul>
<li>commons-configuration-1.6.jar</li>
<li>commons-lang-2.5.jar</li>
<li>aopalliance.jar</li>
<li>gin-1.0.jar</li>
<li>guice-2.0.jar</li>
<li>mvp4g-1.2.0.jar</li>
</ul>
<p>Have a look at <a href="http://code.google.com/p/mvp4g/wiki/HowtoIntegrateMvp4g">http://code.google.com/p/mvp4g/wiki/HowtoIntegrateMvp4g</a> for information on where to download these libraries.</p>
<p>Create a lib folder at the root of your project, and put the above jars in that folder.</p>
<p>Now, we have to make sure that these jars are included in our project build path. Rightclick on your project root, and use the Properties menu alternative to open the project properties dialog box. Select the alternative Java Build Path, and select the tab Libraries.</p>
<ol>
<li>Press the button Add JARs&#8230;</li>
<li>Navigate to the Mvp4gEx1 &gt; lib folder, and select the jars</li>
<li>Press the OK button</li>
</ol>
<p><a href="http://cambiatablog.files.wordpress.com/2010/12/mvp4g2.png"><img class="alignnone size-full wp-image-371" title="mvp4g2" src="http://cambiatablog.files.wordpress.com/2010/12/mvp4g2.png?w=594" alt=""   /></a></p>
<p>So, now we have those jars included in our project.</p>
<p>Before we can use them (or &#8211; actually &#8211; Mvp4g can use them, we don&#8217;t!) we have to make some changes in our gwt module definition xml. Open Mvp4gJson.gwt.xml, located in your package root. Make sure to to the following changes:</p>
<ol>
<li>ADD <strong>&lt;inherits name=&#8217;com.mvp4g.Mvp4gModule&#8217;/&gt;</strong> under the Other module inherits comment.<br />
This gives us access to the Mvp4g classes.</li>
<li>CHANGE the entry point tag to <strong>&lt;entry-point class=&#8217;com.mvp4g.client.Mvp4gEntryPoint&#8217;/&gt;</strong><br />
This will replace the normal GWT entry point class with a mvp4g-specific one that we will create in a minute.</li>
</ol>
<p>The complete Mvp4gEx1.gwt.xml will look something like this:</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;module rename-to='mvp4gex1'&gt;
  &lt;!-- Inherit the core Web Toolkit stuff.                        --&gt;
  &lt;inherits name='com.google.gwt.user.User'/&gt;

  &lt;!-- Inherit the default GWT style sheet.  You can change       --&gt;
  &lt;!-- the theme of your GWT application by uncommenting          --&gt;
  &lt;!-- any one of the following lines.                            --&gt;
  &lt;inherits name='com.google.gwt.user.theme.standard.Standard'/&gt;
  &lt;!-- &lt;inherits name='com.google.gwt.user.theme.chrome.Chrome'/&gt; --&gt;
  &lt;!-- &lt;inherits name='com.google.gwt.user.theme.dark.Dark'/&gt;     --&gt;

  &lt;!-- Other module inherits                                      --&gt;
  &lt;inherits name='com.mvp4g.Mvp4gModule'/&gt;

  &lt;!-- Specify the app entry point class.                         --&gt;
  &lt;entry-point class='com.mvp4g.client.Mvp4gEntryPoint'/&gt;

&lt;/module&gt;


</pre></p>
<p>Please note: There are two ways of implementing the eventbus:</p>
<ol>
<li>as application entry point (the method used here), and</li>
<li>invoked from the GWT standard entry point class. <a href="http://code.google.com/p/mvp4g/wiki/HowtoIntegrateMvp4g">Here&#8217;s more info about this</a>.</li>
</ol>
<h2>Creating the event bus</h2>
<p>The Mvp4g architecture is built around an eventbus. It acts like a command central, that ties together all other parts of the application. The point is, that these other parts doesn&#8217;t have to know of each other, they can act independently in a loose coupled manner. Wich in turn is what we&#8217;re after to build testable, maintainable and scaleable solutions.<br />
The eventbus is so essential that it acts as the entry-point for our application (replacing the GWT-normal EntryPoint-inherited class).</p>
<p>So let&#8217;s create one. In your package.client, create an AppEventBus interface extending the mvp4g EventBus interface:</p>
<p><a href="http://cambiatablog.files.wordpress.com/2010/12/mvp4g3.png"></a><a href="http://cambiatablog.files.wordpress.com/2010/12/mvp4g31.png"><img class="alignnone size-full wp-image-373" title="mvp4g3" src="http://cambiatablog.files.wordpress.com/2010/12/mvp4g31.png?w=594" alt=""   /></a></p>
<p>This event bus is going to serve as our application entrypoint, and since we need a way to connecting link with the browser/html, we are soon going to create a RootView with a corresponding RootPresenter to give us this connection.</p>
<p>Here we can see the code for our minimalistic AppEventBus, with the @Event(startView = RootView.class) annotation telling the Mvp4g system what class to use as our main start view:</p>
<p><pre class="brush: java;">
// ex.client.AppEventBus.java
package ex.client;

import com.mvp4g.client.annotation.Events;
import com.mvp4g.client.event.EventBus;

@Events( startView = RootView.class )
public interface AppEventBus extends EventBus {
	// No events to handle yet...
}
</pre></p>
<h2>RootView and RootPresenter</h2>
<p>Our RootView, wich will hold our basic UI controls, will inherit from Composite:</p>
<p><pre class="brush: java;">
// ex.client.RootView.java
package ex.client;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class RootView extends Composite {
 private Button button = new Button(&quot;Button&quot;);
 private TextBox textbox = new TextBox();
 private Label label = new Label(&quot;Label&quot;);
 private HorizontalPanel horizontalPanel = new HorizontalPanel();

 public RootView () {
 this.horizontalPanel.add(button);
 this.horizontalPanel.add(textbox);
 this.horizontalPanel.add(label);
 initWidget(this.horizontalPanel);
 }
}

</pre></p>
<p>Please note that this view is a stupid one — no button click handling, no connection to the outer world. This is the way it&#8217;s supposed to be in this type of loose coupled architecture.</p>
<p>The RootView needs a RootPresenter that acts like a bridge between the ui (RootView) and the rest of the application (via the AppEventBus). The RootPresenter will inherit from BasePresenter:</p>
<p><pre class="brush: java;">

// ex.client.RootPresenter.java
package ex.client;

import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter( view = RootView.class )
public class RootPresenter extends BasePresenter {

}
</pre></p>
<p>Please note that the BasePresenter class needs two type arguments: view and event bus:<br />
public class RootPresenter extends BasePresenter<strong>&lt;RootView, AppEventBus&gt;</strong> {<br />
Also note the @Presenter annotation that tells the mvp4g system what view this class is serving.</p>
<h2>Time for some action!</h2>
<p>So, now we have a stupid view, a dead presenter and an empty event bus. Great! Now, with our fancy UI (a button, a textbox and a label), we will try to accomplish the following: <em>When the user presses the button, the text written in the textbox will be copied into the label. </em></p>
<p>There are three ways of handling this &#8220;data status change&#8221;:</p>
<ol>
<li><strong>Putting the logic into the view</strong>, by adding a clickhandler to the button.<br />
Completely simple, quick and un-fancy! It works here, but not if 20 views and models are depending on the same data. Then we will soon get dependency spaghetti hell that Mvp4g is built to help us aviod.<br />
(Code example 1 below)</li>
<li><strong>Putting the logic into the presenter</strong>, by registering the clickhandler there and taking care of the logic. This can be done because the mvp4g system has injected the view into the presenter, so we can access it easily.<br />
Only a fraction better than the solution above. At least we are handling data change in a class that&#8217;s created for this kind of things &#8211; wich the view clearly isn&#8217;t. It&#8217;s also an important step towards doing it the &#8220;right way&#8221; below.<br />
(Code example 2 below)</li>
<li><strong>Dispatch our data status change to the eventbus, </strong>and letting anyone depending on the data change taking care of it. This means that we tell the application event bus: &#8220;Hey, some data has changed&#8221;. At the same time we are handling over the responsibility to take this information further to the event bus. It will broadcast it to the presenters (or EventHandler classes &#8211; subject for next tutorial) that are registered to care.</li>
</ol>
<p>So, lets have a look at some code:</p>
<p>Code example 1: Logic in the view. Have a quick look, and forget it:</p>
<p><pre class="brush: java;">

public RootView () {
 this.horizontalPanel.add(button);
 this.horizontalPanel.add(textbox);
 this.horizontalPanel.add(label);
 initWidget(this.horizontalPanel);
 // code below added...
 button.addClickHandler(new ClickHandler() {
 public void onClick(ClickEvent event) {
 label.setText(textbox.getText());

 }
 });
 }
</pre></p>
<p>Code example 2: Logic in the presenter &#8211; possible via dependency injection, as the view (RootView) is injected in the presenter, and accessible via the protected property view.</p>
<p>We create the click handler in the overridden bind() method, wich is invoked when the presenter is activated.</p>
<p><pre class="brush: java;">
@Presenter( view = RootView.class )
public class RootPresenter extends BasePresenter {
 public void bind() {
 view.button.addClickHandler(new ClickHandler() {
 public void onClick(ClickEvent event) {
 view.label.setText(view.textbox.getText());
 }
 });
 }
}
</pre></p>
<p>Code example 3: Using the event bus to notify the application about our data change.</p>
<p>First, let&#8217;s create a event bus method that we can call from our presenter. As we are dealing with textbox text change, we call our eventbus method &#8220;textChange&#8221;:</p>
<p><pre class="brush: java;">@Events(startView = RootView.class)public interface AppEventBus extends EventBus { @Event(handlers = {}) public void textChange(String newText);}
</pre></p>
<p>Now, we can call this eventbus method from our presenter, like this:</p>
<p><pre class="brush: java;">
view.button.addClickHandler(new ClickHandler() {
 public void onClick(ClickEvent event) {
 //view.label.setText(view.textbox.getText()); // &lt;-- Replace this...
 eventBus.textChange(view.textbox.getText());  // &lt;-- with this...
 }
 });
</pre></p>
<p>The eventbus is now recieveing information about our data change, but there it stops because no one&#8217;s listening!</p>
<p>We need to register one or many recepients for the event information. Right now, we just have one actor &#8211; the RootPresenter &#8211; so this guy will now act not only as a broadcaster, but also as a reciever&#8230;</p>
<p>First, let&#8217;s register the RootPresenter as a listener to the textChange event:</p>
<p><pre class="brush: java;">
public interface AppEventBus extends EventBus {

 @Event(handlers = {RootPresenter.class}) // &lt;-- RootPresenter added here!
 public void textChange(String newText);

}
</pre></p>
<p>Next step is to add a handler for our event method. This is done by adding  &#8220;on&#8221; in the beginning of the method name.</p>
<p>Thus, the event bus method <strong>textChange</strong> needs a presenter handler called <strong>onTextChange</strong>. Let&#8217;s say we have a event bus method named <strong>loginStatusChange </strong>— this would need a presenter handler called <strong>onLoginStatusChange</strong> etc.</p>
<p>The RootPresenter with onTextChange handler added, and some code to update the view label, will look like this:</p>
<p><pre class="brush: java;">
@Presenter( view = RootView.class )
public class RootPresenter extends BasePresenter {
 public void bind() {
 view.button.addClickHandler(new ClickHandler() {
 public void onClick(ClickEvent event) {
 //view.label.setText(view.textbox.getText()); // &lt;-- Replace this...
 eventBus.textChange(view.textbox.getText());  // &lt;-- with this...
 }
 });
 }

 public void onTextChange(String newText) {  // this method recieves the eventbus textChange event
 view.label.setText(newText);
 }
}
</pre></p>
<p>So, now we&#8217;ve completed the data roundtrip. No real benefits this far, of course, but let&#8217;s just say that we had three or thirty different views that should be updated instead of one. All we have to do is to create the view/presenter pair, and register the presenter as a listener to the event in focus. We wouldn&#8217;t add any tight coupling but to the event bus itself, other parts would still act independently.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/369/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=369&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/12/04/gwt-and-mvp4g-tutorial-1/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/12/mvp4g1.png" medium="image">
			<media:title type="html">mvp4g1</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/12/mvp4g2.png" medium="image">
			<media:title type="html">mvp4g2</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/12/mvp4g31.png" medium="image">
			<media:title type="html">mvp4g3</media:title>
		</media:content>
	</item>
		<item>
		<title>GWT: Developing in hosted mode with php, json and XAMPP &#8211; Part 3</title>
		<link>http://cambiatablog.wordpress.com/2010/11/23/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-3/</link>
		<comments>http://cambiatablog.wordpress.com/2010/11/23/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-3/#comments</comments>
		<pubDate>Tue, 23 Nov 2010 05:57:23 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=363</guid>
		<description><![CDATA[Dealing with json and objects So, now we have some raw json data back from our server script. How to do something meaningful with it? The json data that our php script produced is an array of users. To break this down to objects, we can use two entities: User, with properties name and age [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=363&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Dealing with json and objects</h2>
<p>So, now we have some raw json data back from our server script. How to do something meaningful with it?</p>
<p>The json data that our php script produced is an array of users. To break this down to objects, we can use two entities:</p>
<ul>
<li> User, with properties name and age</li>
<li> Users, array of User</li>
</ul>
<p>Following the <a href="http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html">GWT Get started Tutorials</a>, we use javascript overlay types for these objects &#8211; they are javascript objects, but we interact with them through a java interface. This means that instead of extending the native java types (Object respectively ArrayList), we will use the javascript overlay counterparts:</p>
<ul>
<li>User extends JavaScriptObject</li>
<li>Users extends JsArray&lt;User&gt;</li>
</ul>
<p>So, let&#8217;s create those two classes. They&#8217;re nothing more than simple value objects, so let&#8217;s put them in se.cambiata.gwtphp.client.vo package.<br />
Here&#8217;s the code for User:<br />
<pre class="brush: java;">
// User
package se.cambiata.gwtphp.client.vo;
import com.google.gwt.core.client.JavaScriptObject;
public class User extends JavaScriptObject {
	protected User() {};
	public final native String getName() /*-{ return this.name; }-*/;
	public final native int getAge() /*-{ return this.age; }-*/;	
	}
}
</pre><br />
And here&#8217;s the code for Users:<br />
<pre class="brush: java;">
// Users
package se.cambiata.gwtphp.client.vo;
import com.google.gwt.core.client.JsArray;
public class Users extends JsArray {
	protected Users() {}
	public static final native Users usersFromJson(String json) /*-{
		return  eval(json);
	}-*/;		
}
</pre></p>
<p>Now, let&#8217;s add some users to our application. In the GwtPhp.java, after the onModuleLoad() method, add the following code:<br />
<pre class="brush: java;">
	private Users users;

	private void setUsersFromJson(String jsonString) {
		this.users = Users.usersFromJson(jsonString);
		//-----------------------------------------
		// accessing a user
		User user = this.users.get(1);
		GWT.log(user.getName());
		GWT.log(Integer.toString(user.getAge()));
	}
</pre></p>
<p>This gives us users as a property of the application, and a setter-style method to invoke those users from the json code.<br />
So, let&#8217;s feed this setter method with our recieved json.<br />
In the HttpRequestCallback onResponseRecieved method, change the code to include a call to setUsersFromJson — something like this:<br />
<pre class="brush: java;">
					public void onResponseRecieved(Request request, Response response) {
						GWT.log(&quot;HttpRequestCallback.onResponseRecieved&quot; + response.getText());
						setUsersFromJson(response.getText());
					}
</pre><br />
The actual conversion from json to javascript is done in the Users static method usersFromJson, by a simple javascript eval command.<br />
When running this application, the recieved json should be converted into application level Users, and the single users are accessable through<br />
<pre class="brush: java;">
		Users user = this.users.get(index);
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/363/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=363&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/11/23/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>
	</item>
		<item>
		<title>GWT: Developing in hosted mode with php, json and XAMPP &#8211; Part 2</title>
		<link>http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-2/</link>
		<comments>http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-2/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 15:05:12 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=347</guid>
		<description><![CDATA[Now we are up and running with a xampp served frontend. Let&#8217;s go for a connection to php backend. Php server script 1. The php server script could be created inside the current GWT project directory, or as a separate project outside. For simplicity, we here create a server folder inside the GwtPhp project, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=347&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Now we are up and running with a xampp served frontend. Let&#8217;s go for a connection to php backend.</p>
<h2>Php server script</h2>
<p>1. The php server script could be created inside the current GWT project directory, or as a separate project outside. For simplicity, we here create a server folder inside the GwtPhp project, and a php file (test.php) inside of that one:</p>
<p><a href="http://cambiatablog.files.wordpress.com/2010/11/phpscript.png"><img class="alignnone size-full wp-image-338" title="PhpScript" src="http://cambiatablog.files.wordpress.com/2010/11/phpscript.png?w=594" alt=""   /></a></p>
<p>2. Populate the test.php file with something that results a valid json, for example this:</p>
<p><pre class="brush: php;">
&lt;?php

$users = array();

$user = new StdClass();
$user-&gt;name = &quot;John&quot;;
$user-&gt;age = 23;
$users[] = $user;

$user = new StdClass();
$user-&gt;name = &quot;Claire&quot;;
$user-&gt;age = 32;
$users[] = $user;

echo json_encode($users);
</pre></p>
<h2>Making the http request</h2>
<p>3. Now we are ready to create a http request from our Gwt frontend to our php server script.</p>
<p>In java, we can&#8217;t simply pass a function as a parameter, so setting up a callback for an asynchronous event is a bit more tricky in java than in, say, as3 or php. But we can pass an object that has the required callback functions. This is best done by using an interface. <a href="http://stackoverflow.com/questions/443708/callback-functions-in-java">Have a look here if you want to know more.</a></p>
<p>To simplify the callback object creation and the setup of an RequestBuilder, we will use a simple utility class. Create the file se.cambiata.gwtphp.client.HttpRequestUtil.java, and populate it like this:</p>
<p><pre class="brush: java;">

// se.cambiata.gwtphp.client.HttpRequestUtil.java
package se.cambiata.gwtphp.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.RequestBuilder.Method;

public class HttpRequestUtil {

	public interface HttpRequestCallback{
		void onResponseRecieved(Request request, Response response);
		void onError(Request request, Throwable exception);
	}

	public static void makeRequest(String url, String jsonData, Method method, final HttpRequestCallback httpRequestCallback) {
		RequestBuilder requestBuilder = new RequestBuilder(method, url);
		requestBuilder.setHeader(&quot;Content-Type&quot;,&quot;application/json&quot;);
		try {
			Request request = requestBuilder.sendRequest(jsonData, new RequestCallback() {
				@Override
				public void onResponseReceived(Request request, Response response) {
					httpRequestCallback.onResponseRecieved(request, response);
				}
				@Override
				public void onError(Request request, Throwable exception) {
					httpRequestCallback.onError(request, exception);
				}
			});
		} catch (RequestException e) {
			e.printStackTrace();
		}
	}
}
</pre></p>
<p>4. Update the GwtPhp.java btnTest.addClickHandler to the following. Make sure that the url to the test.php script is valid:</p>
<p><pre class="brush: java;">
		// se.cambiata.gwtphp.client.GwtPhp.java
		btnTest.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GWT.log(&quot;Hello from GwtPhp&quot;);

				HttpRequestCallback httpRequestCallback = new HttpRequestUtil.HttpRequestCallback() {
					@Override
					public void onResponseRecieved(Request request, Response response) {
						GWT.log(&quot;HttpRequestCallback.onResponseRecieved&quot; + response.getText());
&lt;pre&gt;						Window.alert(&quot;HttpRequestCallback.onResponseRecieved&quot; + response.getText());&lt;/pre&gt;
}
 public void onError(Request request, Throwable exception) {
 GWT.log(&quot;HttpRequestCallback.onError&quot;);
 }
 };
 String url = &quot;http://localhost/GwtPhp/server/test.php&quot;;
 String postJsonData = &quot;{'test':123}&quot;;
 HttpRequestUtil.makeRequest(url, postJsonData, RequestBuilder.POST, httpRequestCallback);
 }
 });
</pre></p>
<p>5. Refresh the GwtPhp app in the browser and press the button. This should call the php script, and an alert dialog box should pop up displaying the json recieved from the server.</p>
<p>In the above example we&#8217;re actually posting some data to the php script, but this isn&#8217;t delt with in this article. Be my guest! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/347/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=347&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/11/phpscript.png" medium="image">
			<media:title type="html">PhpScript</media:title>
		</media:content>
	</item>
		<item>
		<title>GWT: Developing in hosted mode with php, json and XAMPP &#8211; Part 1</title>
		<link>http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-with-hosted-mode-and-phpxampp/</link>
		<comments>http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-with-hosted-mode-and-phpxampp/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 12:59:11 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=328</guid>
		<description><![CDATA[When developing using Eclipse GWT plugin and hosted mode, the html files (and java server resources) are served via the plugin jetty server. This makes it really quick and easy to get going with developing java server solutions, but it doesn&#8217;t play with php out of the box. Thw GWT startup pages suggests deploying the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=328&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When developing using Eclipse GWT plugin and hosted mode, the html files (and java server resources) are served via the plugin jetty server. This makes it really quick and easy to get going with developing java server solutions, but it doesn&#8217;t play with php out of the box.<br />
Thw GWT startup pages suggests deploying the gwt app to localserver, and running it there, but this breaks the beauty of being able to to develop php backend and gwt frontend on the same time.</p>
<p>Here&#8217;s how I got my setup working so that I can develop with GWT hosted mode served through a localhost php/XAMPP setup. In my setup, I&#8217;m using the following:</p>
<ul>
<li> Eclipse 3.5 with PDT</li>
<li> GWT 2.1 SDK and Eclipse plugin</li>
</ul>
<h2>Serving GWT frontend in hosted mode from XAMPP localhost</h2>
<p>In this example we are going to create a GWT project called GwtPhp.</p>
<p>1. Create a new GWT Web Application Project, but instead of using the default Eclipse workspace, make sure that you create it in the Xampp www root (for exampel D:\xampp\htdocs). Use a package path of your liking:</p>
<p><a href="http://cambiatablog.files.wordpress.com/2010/11/createproject.png"><img class="alignnone size-full wp-image-329" title="CreateProject" src="http://cambiatablog.files.wordpress.com/2010/11/createproject.png?w=594" alt=""   /></a></p>
<p>2. Replace the autogenerated application stub code (in this case se.cambiata.gwtphp.GwtPhp.java) with something simple, like this:</p>
<p><pre class="brush: java;">
package se.cambiata.gwtphp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class GwtPhp implements EntryPoint {

	private Button btnTest = new Button(&quot;btnTest&quot;);

	public void onModuleLoad() {
		RootPanel.get().add(btnTest);

		btnTest.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				GWT.log(&quot;Hello from GwtPhp&quot;);
			}
		});
	}
}
</pre></p>
<p>3. Replace the project html ({project}/war/GwtPhp.html) to something of your liking (basically deleting the autogenerated unneeded stuf like divs and tables).</p>
<p>4. Testdebug in hosted mode by pressing debugbutton or using F11, and doubleclicking the address bar in the Development Mode eclipse view (on my machine http://127.0.0.1:8888/GwtPhp.html?gwt.codesvr=127.0.0.1:9997).<br />
This executes the application in hosted mode using jetty server.<br />
Pressing the button should display a &#8220;Hello from GwtPhp&#8221; message in the Development Mode panel log.</p>
<p>5. Now, we&#8217;re going to change the debug configuration for this project. Rightclick the eclipse project name in the package explorer (GwtPhp) and look for the Debug As &gt; Debug Configurations&#8230; alternative. In the dialog box, select the GwtPhp item in the left tree, and open the Arguments tab.<br />
<a href="http://cambiatablog.files.wordpress.com/2010/11/debugconfiguration.png"><img class="alignnone size-full wp-image-335" title="DebugConfiguration" src="http://cambiatablog.files.wordpress.com/2010/11/debugconfiguration.png?w=594" alt=""   /></a><br />
Replace the program arguments with something like the following:</p>
<p><img src="/DOCUME%7E1/ADMINI%7E1.EXP/LOCALS%7E1/Temp/moz-screenshot-3.png" alt="" /></p>
<pre><strong>
-noserver -remoteUI "${gwt_remote_ui_server_port}:${unique_id}"
-startupUrl http://localhost/GwtPhp/war/GwtPhp.html
-logLevel INFO -codeServerPort 9997
-war "D:\xampp\htdocs\GwtPhp\war" se.cambiata.gwtphp.GwtPhp
</strong></pre>
<p>Make sure that the paths</p>
<ul>
<li> on the localhost (http://localhost/GwtPhp/war/GwtPhp.html)</li>
<li> to the war (D:\xampp\htdocs\GwtPhp\war)</li>
<li> to the entry point class (se.cambiata.gwtphp.GwtPhp)</li>
</ul>
<p>correspond to your setup.</p>
<p>6. Save these settings by clicking on Debug button. Please note that the app isn&#8217;t runnable yet!</p>
<p>7. Now, we have to deploy the application once to the new location. (This only has to be done once!) Do this by clicking the red G bag icon (Gwt Compile Project), and then Compile button in the dialog. This builds the needed class files, javascript files etc in the new xampp location.</p>
<p>8. At this point, we should be able to testrun our app served from Xampp. Click the little arrow right of the debug button, and make sure to select the GwtPhp alternative (the one that we configured in point 5 above. This should display an address in the Development Mode panel, this time located on localhost:</p>
<p><a href="http://cambiatablog.files.wordpress.com/2010/11/devmodelocalhost.png"><img class="alignnone size-full wp-image-336" title="DevModeLocalhost" src="http://cambiatablog.files.wordpress.com/2010/11/devmodelocalhost.png?w=594" alt=""   /></a></p>
<p>Doubleclicking this address should open the browser pointing at localhost. and pressing the button should display the log message in the Development Mode panel.</p>
<p>At last, to clean things up a bit, you should be able to delete the redundant classes in the se.cambiata.gwtphp.shared and se.cambiata.gwtphp.server packages, as well as the GreetingService.java and GreetingServiceAsync.java classes in the se.cambiata.gwtphp.client package. Also delete or comment away the &lt;source path=&#8217;shared&#8217;/&gt; tag in the se.cambiata.gwtphp.GwtPhp.gwt.xml file.</p>
<p><a href="http://stackoverflow.com/questions/4172015/gwt-frontend-hosted-mode-php-backend-apache-simultaneously-on-localhost">Thanks to Bogdan for pointing this solution out at StackOverflow!</a></p>
<p>&nbsp;</p>
<p><a href="http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-in-hosted-mode-with-php-json-and-xampp-part-2/">Go to part 2 of this article</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/328/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=328&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/11/22/gwt-developing-with-hosted-mode-and-phpxampp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/11/createproject.png" medium="image">
			<media:title type="html">CreateProject</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/11/debugconfiguration.png" medium="image">
			<media:title type="html">DebugConfiguration</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/11/devmodelocalhost.png" medium="image">
			<media:title type="html">DevModeLocalhost</media:title>
		</media:content>
	</item>
		<item>
		<title>First steps in Java!</title>
		<link>http://cambiatablog.wordpress.com/2010/11/10/first-steps-in-java/</link>
		<comments>http://cambiatablog.wordpress.com/2010/11/10/first-steps-in-java/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 23:38:51 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=316</guid>
		<description><![CDATA[Java is tricky, verbose and ugly. And an excellent programming language! I&#8217;ve always avoided it, with some kind of subconcious thought that I should use it, know it, that real grown-up programmers program in java. I&#8217;we been working on a music notation solution for some months. Started in AS3, while one of the goals is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=316&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Java is tricky, verbose and ugly. And an excellent programming language!</p>
<p>I&#8217;ve always avoided it, with some kind of subconcious thought that I should use it, know it, that real grown-up programmers program in java.</p>
<p>I&#8217;we been working on a music notation solution for some months. Started in AS3, while one of the goals is accomplishing good solutions for browser-based notation display and playback. But I also at the same time want to create desktop editors for the notation stuff. And why not an Eclipse plug-in? That would be really cool&#8230;</p>
<p>So I started porting it to java the other day. It&#8217;s really interesting, and I learn a lot from it. Here are some experiences:</p>
<h3>Eclipse Java IDE is great!</h3>
<p>Flash Builder is ok, but not at all as mature as the java editor. Two highlights:</p>
<ul>
<li>Typecast suggestion, when a value doesn&#8217;t match the current variable type</li>
<li>Great refactoring features &#8211; renaming works like a charm! (Not at all stable in Flash Builder 4)</li>
</ul>
<p>When starting with a new language, a decent IDE helps a lot! Thank you, Eclipse guys!</p>
<h3>Java is verbose!</h3>
<p>Java doesn&#8217;t have an equivalent to php&#8217;s var_export() or Flex ObjectUtil.toString() functions. (At least not natively. There might of course be some implementations out there.) Instead it&#8217;s expected that the programmer overrides the toString() method with his/her own code. This is from a &#8220;best practice&#8221; example that I found:</p>
<p><pre class="brush: java;">
StringBuilder s = new StringBuilder();
String NEW_LINE = System.getProperty(&quot;line.separator&quot;);
s.append(levelString + this.getClass().getName() + &quot; Object {&quot; + NEW_LINE);
s.append(object property 1...) etc.
</pre><br />
Many code characters for some simple string output! Of course, it&#8217;s done in a well structured object-oriented way, but still&#8230;</p>
<h3>Xml in java sucks! (compared to AS3)</h3>
<p>Working with xml is a joy in AS3, thanks to the e4x implementation. The possibility to use xml as a native type makes it so intuitive. On the other hand, in native java, you have to rely on DOM, SAX and other solutions. That hurts!<br />
Luckily thers a third part solution, JDOM (<a href="http://www.jdom.org/">http://www.jdom.org/</a>) that makes it a lot simpler. Highly recommended!</pre>
<p><pre class="brush: java;">
// Create a xml document - Java DOM
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element root = xmlDocument.createElement(&quot;root&quot;);
xmlDocument.appendChild(root);

// Create a xml document - JDOM
Document document = new Document(new Element(&quot;root&quot;));

// Create a xml document - AS3
var document:XML = new XML(&quot;&lt;root&gt;&lt;/root&gt;&quot;);
</pre></p>
<h3>Java enums are great!</h3>
<p>Example: I need an enum to handle how to flip the musical notes. This should have the values UP, AUTO and DOWN. These should correspond to the ordinal values -1, 0 and 1.<br />
In java, a native enum looks like this, simple and clear:<br />
<pre class="brush: java;">
public enum EnumDirectionUAD {
	UP(-1), AUTO(0), DOWN(1);
	public int ordinal;
	private EnumDirectionUAD(int ordinal) {
		this.ordinal = ordinal;
	}
}

// Grabbing the enum value from a string is as simple as this, using built-in enum method .valueOf(String):
EnumDirectionUAD uad = EnumDirectionUAD .valueOf(&quot;AUTO&quot;);
</pre></p>
<p>AS3 doesn't have enum types, so there has to be a pseudo-enum using classes with static constants:<br />
<pre class="brush: java;">
public class EnumDirectionUAD {		
	public static const UP:EnumDirectionUAD = new EnumDirectionUAD(&quot;UP&quot;, -1);
	public static const AUTO:EnumDirectionUAD = new EnumDirectionUAD(&quot;AUTO&quot;, 0);
	public static const DOWN:EnumDirectionUAD = new EnumDirectionUAD(&quot;DOWN&quot;, 1);

	public var value:String;
	public var ordinal:int;
	public function EnumDirectionUAD(value:String, ordinal:int) {
		this.value = value;
		this.ordinal = ordinal;
	}
	// need to create the getByValue() method by myself...
	public static function get list():Array {
		return [UP, AUTO, DOWN];
	}
	public static function getByValue(value:String):EnumDirectionUAD {
		for each (var enumType:EnumDirectionUAD in EnumDirectionUAD.list) {
			if (value == enumType.value)
				return enumType;
		}
		return AUTO;
	}		
}
</pre><br />
Not ideal, but it works well.<br />
In php, on the other hand, there seems to be no way of creating a decent parameterized pseudo-enum. (Haven't found one yet, anyhow.) It's simply not possible to work with objects or arrays as static constants. That sucks!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/316/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=316&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/11/10/first-steps-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting started with PHP  Cairo graphics extension on XP</title>
		<link>http://cambiatablog.wordpress.com/2010/11/06/getting-started-with-cairo-graphics-and-php/</link>
		<comments>http://cambiatablog.wordpress.com/2010/11/06/getting-started-with-cairo-graphics-and-php/#comments</comments>
		<pubDate>Sat, 06 Nov 2010 08:51:11 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=306</guid>
		<description><![CDATA[The Cairo graphics library (http://cairographics.org/) is a widely used solution for rendering graphics to different devices &#8211; bitmaps, pdf, svg etc. It&#8217;s used by Mozilla, Inkscape, Gimp etc. Some time ago, I realized that Cairo is available as a native php extension: http://php.net/manual/en/book.cairo.php. Great! To get it running on XP, I did the following: Download [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=306&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The Cairo graphics library (http://cairographics.org/) is a widely used solution for rendering graphics to different devices &#8211; bitmaps, pdf, svg etc. It&#8217;s used by Mozilla, Inkscape, Gimp etc. Some time ago, I realized that Cairo is available as a native php extension: http://php.net/manual/en/book.cairo.php. Great!</p>
<p>To get it running on XP, I did the following:</p>
<ol>
<li>Download fresh php binaries (<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>). I choose the 5.2.14 zip package.</li>
<li>Unzip the downloaded php zip.</li>
<li>Download and unzip the the cairo-0.1.0-alpha-php-5.2.10.zip, conatining the cairo wrapper extension (php_cairo.dll). It can be found using the PHP 5.2 link available here: http://perisama.net/cairo/</li>
<li>Copying the php_cairo.dll from the downloaded cairo zip to the ext folder located insid the php directory.</li>
<li>Editing the php.ini-recommended  filename (found in the php directory) to php.ini.</li>
<li>Adding the line
<pre>extension=ext/php_cairo.dll</pre>
<p>to the Windows Extensions part of php.ini (around line 664).</li>
<li>This should be it!</li>
</ol>
<p>Now, it should be possible to run php from the command line with cairo commands.<br />
For example, create a file named &#8220;test_cairo.php&#8221; with the following content and put it in the php directory:</p>
<p><pre class="brush: php;">
&lt;?php
// test_cairo.php
$s = new CairoImageSurface(CairoFormat::ARGB32, 400, 400);
$c = new CairoContext($s);
$c-&gt;fill();
$c-&gt;setSourceRGB(1, 0, 0);
$c-&gt;setLineWidth(50);
$c-&gt;arc(200, 200, 100, 0, 2 * M_PI);
$c-&gt;stroke();
$c-&gt;setSourceRGB(0, 0, 0.6);
$c-&gt;rectangle(0, 160, 400, 75);
$c-&gt;fill();

$s-&gt;writeToPng(dirname(__FILE__)  . '/test.png');
</pre></p>
<p>Now, run open a command line window in the php directory and run the testfile like this: &gt;php thest_cairo.php This should produce a test.png looking like this:</p>
<p><a href="http://cambiatablog.files.wordpress.com/2010/11/test.png"><img title="test" src="http://cambiatablog.files.wordpress.com/2010/11/test.png?w=150&#038;h=150" alt="" width="150" height="150" /></a></p>
<p>The example is taken from Micahel Macleans (http://mgdm.net/) talk on <a href="http://mgdm.net/talks/" target="_blank">Graphics with Cairo</a> &#8211; excellent read! Michael is one of the lead developers on the php cairo wrapper. Thank you, Michael!</p>
<p>Thank you, Elisabeth Marie Smith (http://elizabethmariesmith.com/) for developing and hosting the http://perisama.net/cairo/!</p>
<p>UPDATE:</p>
<p>If you want to use the cairo extension with PHP 5.2,  Eclipse PDT and Zend debugging client (http://www.thierryb.net/pdtwiki/index.php?title=Using_PDT_:_Installation_:_Installing_the_Zend_Debugger), make sure to download the Non Thread Safe (PHP 5.2 NTS) from  http://perisama.net/cairo/.</p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/306/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/306/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/306/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=306&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/11/06/getting-started-with-cairo-graphics-and-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>

		<media:content url="http://cambiatablog.files.wordpress.com/2010/11/test.png?w=150" medium="image">
			<media:title type="html">test</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple encrypt/decrypt in AS3 and Php &#8211; Base64 blues</title>
		<link>http://cambiatablog.wordpress.com/2010/08/24/simple-encryptdecrypt-in-as3-and-php-base64-blues/</link>
		<comments>http://cambiatablog.wordpress.com/2010/08/24/simple-encryptdecrypt-in-as3-and-php-base64-blues/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 04:04:45 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=301</guid>
		<description><![CDATA[Needed the possibility to handle simple string encryption/decryption the other day — handling the same data in both php and As3. Just a little bit of key salting, and some Base64 encryption. No problem except for the Base64 part. When trying this out, only one of my Base64 libraries gave the same result as the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=301&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Needed the possibility to handle simple string encryption/decryption the other day — handling the same data in both php and As3. Just a little bit of key salting, and some Base64 encryption.<br />
No problem except for the Base64 part. When trying this out, only one of my Base64 libraries gave the same result as the php base64_encode/base64_decode functions:</p>
<ul>
<li> mx.utils.Base64Encoder — wrong result</li>
<li>com.dynamicflash.util.Base64 — wrong result</li>
<li>com.hurlant.util.Base64 — wrong result</li>
<li>be.boulevart.as3.security.Base64 — works! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
</ul>
<p>Haven&#8217;t had time to dive into it yet, but there seems to be different interpretations of the more obscure character codes, causing the different results.</p>
<p>The be.boulevart.as3.security.Base64 class is developed by Sven Dens (modelled after an <a href="http://www.aardwulf.com/tutor/base64/base64.html" target="_blank">Aardwulf Systems solution</a>) and can be <a href="http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/" target="_blank">found here</a>.</p>
<p>So here are my simple enctyption solutions:</p>
<h3>Simplecrypt.as</h3>
<p><pre class="brush: java;">
package se.cambiata.utils.crypt {
	
	// The Base64 class is developed by Sven Dens, and can be found here: 
	// http://labs.boulevart.be/index.php/2007/05/23/encryption-in-as2-and-as3/
	import be.boulevart.as3.security.Base64;
	
	public class Simplecrypt {
		static public function encrypt(str:String, key:String = '%key&amp;'):String {
			var result:String = '';
			for (var i:int = 0; i &lt; str.length; i++) {
				var char:String = str.substr(i, 1);
				var keychar:String = key.substr((i % key.length) - 1, 1);
				var ordChar:int = char.charCodeAt(0);
				var ordKeychar:int = keychar.charCodeAt(0);
				var sum:int = ordChar + ordKeychar;
				char = String.fromCharCode(sum);
				result = result + char;
			}
			return Base64.encode(result);
		}

		static public function decrypt(str:String, key:String = '%key&amp;'):String {
			var result:String = '';
			var str:String = Base64.decode(str);
			
			for (var i:int = 0; i &lt; str.length; i++) {
				var char:String = str.substr(i, 1);
				var keychar:String = key.substr((i % key.length) - 1, 1);
				var ordChar:int = char.charCodeAt(0);
				var ordKeychar:int = keychar.charCodeAt(0);
				var sum:int = ordChar - ordKeychar;
				char = String.fromCharCode(sum);
				result = result + char;
			}
			return result;
		}
	}
}
</pre></p>
<h3>Simplecrypt.php</h3>
<p><pre class="brush: php;">
&lt;?php
class Simplecrypt {

	function encrypt($string, $key='%key&amp;') {
		$result = '';
		for($i=0; $i&lt;strlen($string); $i++) {
			$char = substr($string, $i, 1);
			$keychar = substr($key, ($i % strlen($key))-1, 1);
			$ordChar = ord($char);
			$ordKeychar = ord($keychar);
			$sum = $ordChar + $ordKeychar;
			$char = chr($sum);
			$result.=$char;
		}
		return base64_encode($result);
	}

	function decrypt($string, $key='%key&amp;') {
		$result = '';
		$string = base64_decode($string);
		for($i=0; $i&lt;strlen($string); $i++) {
			$char = substr($string, $i, 1);
			$keychar = substr($key, ($i % strlen($key))-1, 1);
			$ordChar = ord($char);
			$ordKeychar = ord($keychar);
			$sum = $ordChar - $ordKeychar;
			$char = chr($sum);
			$result.=$char;
		}
		return $result;
	}

}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/301/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=301&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/08/24/simple-encryptdecrypt-in-as3-and-php-base64-blues/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>
	</item>
		<item>
		<title>RESTful PUT/DELETE with AS3 using X-HTTP-Method-Override</title>
		<link>http://cambiatablog.wordpress.com/2010/08/10/287/</link>
		<comments>http://cambiatablog.wordpress.com/2010/08/10/287/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 01:35:52 +0000</pubDate>
		<dc:creator>cambiatablog</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RESTful]]></category>

		<guid isPermaLink="false">http://cambiatablog.wordpress.com/?p=287</guid>
		<description><![CDATA[Flash/Flex doesn&#8217;t support PUT or DELETE requests, because the browsers don&#8217;t — due to limitations in the NPAPI. This is a source of frustration. Workarounds using sockets or prioxies are messy, don&#8217;t work with binary data or some other problem&#8230; But fortunately, there seems to be a good (enough) solution! Keenan, at Automata Studios, blogs [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=287&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Flash/Flex doesn&#8217;t support PUT or DELETE requests, because the browsers don&#8217;t — due to limitations in the NPAPI. This is a source of frustration. Workarounds using sockets or prioxies are messy, don&#8217;t work with binary data or some other problem&#8230;</p>
<p>But fortunately, there seems to be a good (enough) solution!</p>
<p>Keenan, at Automata Studios, <a href="http://automatastudios.com/flash-and-rest/" target="_blank">blogs about using an extra request header parameter</a> (X-HTTP-Method-Override), a method that seems to be accepted by Google among others. Good news!</p>
<p>To submit a DELETE request from flash, all I have to do is setting the requestHeaders X-HTTP-Method-Override parameter with the value &#8220;DELETE&#8221;:</p>
<p><pre class="brush: java;">
	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE, onComplete);
	var request:URLRequest = new URLRequest(&quot;http://url/to/rest/server&quot;);
	request.method = URLRequestMethod.POST;
	request.data = &quot;dummyData&quot;; // Some data needed, otherwise sent as GET request!
	request.requestHeaders = [new URLRequestHeader(&quot;X-HTTP-Method-Override&quot;, &quot;DELETE&quot;)];
	loader.load(request);

</pre></p>
<p>Using php, this can be handled on the server side like this:</p>
<p><pre class="brush: php;">
$method = (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) ? $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] : $_SERVER['REQUEST_METHOD'];
echo $method;
</pre></p>
<p>if the server parameter for HTTP_X_HTTP_METHOD_OVERRIDE is set, it picks the request method from that parameter value, otherwise from the normal REQUEST_METHOD value.</p>
<p>Not a truly genuine RESTful solution, but close enough! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Thank you, Keenan!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cambiatablog.wordpress.com/287/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cambiatablog.wordpress.com/287/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cambiatablog.wordpress.com/287/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cambiatablog.wordpress.com/287/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cambiatablog.wordpress.com/287/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cambiatablog.wordpress.com/287/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cambiatablog.wordpress.com/287/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cambiatablog.wordpress.com/287/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cambiatablog.wordpress.com&amp;blog=8456041&amp;post=287&amp;subd=cambiatablog&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cambiatablog.wordpress.com/2010/08/10/287/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/23a67691db07dbe225862f246fd125f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cambiatablog</media:title>
		</media:content>
	</item>
	</channel>
</rss>
