Thursday, April 16, 2009

Google XML Pages - HelloWorld app with source code

Last time I blogged was October 20, 2008... Wow ! Time is going so fast ! I'm really appreciate my readers who stayed with me and keep signing up using feedburner. You rock ! :)

So, to be closer to topic... Google XML Pages or GXP - seems to be interesting since it has been used in an army Google products - AdSense, Blogger, and Google Reader. I have some previous Apache Velocity experience, so the questions are what GXP is, what the differences between the two if any, performance ? I'll try to highlight some of this points in my next post, but for now - just hello world app.

There is a lot of information about GXP, almost all of this info - just annonce. No code, no example...
Our GXP template loooks like this :

<gxp:template name="com.google.code.projects.BlogSrc.GxpApp.HelloGxp"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:gxp="http://google.com/2001/gxp"
xmlns:java="http://google.com/2001/gxp/code/java"
xmlns:call="http://google.com/2001/gxp/call"
gxp:ispace="preserve" gxp:espace="collapse"
content-type="text/plain">

<gxp:import class='java.util.List' />
<gxp:param name='list' java:type='List{Integer}' />

<gxp:loop type='Integer' var='listItem' iterable='list'>
Value : <gxp:eval expr='listItem'/>
</gxp:loop>

</gxp:template>

What do we have here ? Template which takes as param List with name "list", iterates through list and print values stored in it. As you can see, template code is XML, actually. But ! After compiling this XML using GXP compiler we'll get Java source file with appropriate API for calling our template. Java method for calling template looks like this:

public static
void write(
final java.lang.Appendable gxp$out,
final com.google.gxp.base.GxpContext gxp_context,
final List<Integer> list)
throws java.io.IOException
{
final java.util.Locale gxp_locale = gxp_context.getLocale();
gxp$out.append("\n");
{
boolean gxp$bool$0 = false;
for (final Integer listItem : list) {
if (gxp$bool$0) {
gxp$out.append(" ");
} else {
gxp$bool$0 = true;
}
gxp$out.append("\nValue : ");
com.google.gxp.text.PlaintextAppender.INSTANCE
.append(gxp$out, gxp_context, (listItem));
gxp$out.append("\n");
}
}
gxp$out.append("\n");
}
Please notice list and listItem variables and their types. The last step is actually put some param and get result. The name of our tempale is com.google.code.projects.BlogSrc.GxpApp.HelloGxp, the name of file is HelloGxp.gxp. Result of template compilation is a java file - HelloGxp.java in package "com.google.code.projects.BlogSrc.GxpApp". Simplest way to run tempale code is just call static write() method from generated class HelloGxp. Something like this :


package com.google.code.projects.BlogSrc.GxpApp;

import com.google.gxp.base.GxpContext;
import com.google.gxp.html.HtmlClosure;
import com.google.gxp.base.GxpClosure;

import java.io.*;
import java.util.*;

public class MyHelloWorld
{
StringWriter out = new StringWriter();
GxpContext gxpContext =
createGxpContext(Locale.US, false);
GxpContext createGxpContext(Locale locale, boolean useXmlSyntax)
{
return new GxpContext(locale, useXmlSyntax);
}

public void testHello() throws Exception
{
final List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

HelloGxp.write(out, gxpContext,list);
}
public static void main(String args[])
{
final MyHelloWorld obj = new MyHelloWorld();
try
{
obj.testHello();
String s = obj.out.getBuffer().toString();
System.out.println(s);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Complete source code you can find here.


GxpHelloWorldMavenBuild - the same project but for use with Maven 2.

1 comment:

Anonymous said...

IE6 and Google Chrome 2.0 say
[RPC Fault faultString="Error #1090" faultCode="Client.CouldNotDecode" faultDetail="null"]