Page Code Generator

Page code generator is a powerful code generator that generates code which completely separates logic and presentation. Using this tool, each page is represented as an object, when a request to such page arrives the logic part of the web application returns an object in response. This object is serialized with a Formatter class and then can be presented by the client.

Formatters

Currently, there are 3 available formatters (but you can easily write your own) these are:

Example

Lets say that we have a messaging service and we would like to see all that messages sent by a particular person, in addition we want to let the client limit the number of messages in the result. We first describe a message as an object.

Msg{
	string date
	string text
}

Then we declare the function that returns those messages.

list<Msg> msgs/string from ? int limit = 10

This page has a required parameter (from) and an optional parameter (limit) that has a default value (10).

We also need to define in which formats the result can be delivered, lets use them all.

html.Html
json.Json
xmlxsl.XmlXsl

So the overall definition file looks something like this:

html.Html
json.Json
xmlxsl.XmlXsl

Msg{
	string date
	string text
}

list<Msg> msgs/string from ? int limit = 10

Using the generated code this function can be accessed by the following urls:

http://<domain>/<method>/<required parameters>/<optional parameters>

For example:

http://www.mydomain.com/msgs/Alex/
http://www.mydomain.com/msgs/Baba/?f=json&limit=100

The output format is specified as the value of the f (optional) parameter.

The first specified format in the definition file served as the default format. That means that you can access a function without specifying the output format, in the example above the result will be an HTML format.

All we need to do on the server side is implementing the following method:

public static List<Msg> doGet(HttpServletRequest req, HttpServletResponse res, String from, Integer limit) {
	//return a list of messages
}

No need to deal with formating the output at all !!! (take a minute to understand how powerful that is).

Page Code Generator Syntax

The pdl file declare structures and functions.

Formatters Declaration

$formatter_class

Where $formatter_class is the full qualified name of the formatter class.

Structure Declaration

Structure declaration is done as follow:

$structure_name{
	$type $name
	$type $name
	.
	.
	.
}

Where $structure_name is the name of the structure, $type is the type of the field and $name is the name of the field.

Function Declaration

Function declaration is done as follow:

$ret_type $function_name $sep $type $name $sep $type $name ...

Where:

fielddescription
$ret_typethe return value of the function
$function_namethe name of the function
$sepone of the following {/,?,&}, if slash is used then the corresponding parameter is sent as part of the path, otherwise it is sent as part of the query string
$typethe type of the argument
$namethe name of the argument

Optionally, you can supply a default value for the arguments in the query string.

Comments

A '#' sign at the beginning of a line will comment out this line.

Full Syntax

The syntax is given in the EBNF format:

pdl		::=		{spec}
spec		::=		FORMAT							
		|		struct							
		|		function							
struct     	::=     	ID "{" {field} "}"				
field   	::=     	type ID						
basicType	::=		"bool"								
		| 		"int"									
		|		"long"								
		|		"double"								
		|		"string"								
type		::=		basicType							
		|		ID								
		|		"list" "<" type ">"
		|		"map" "<" basicType COMMA type ">"	
rettype		::=		type								
		|		"void"
function	::=		rettype id 					
		|		rettype id {arg} 			
		|		">" ID id						
		|		">" ID id	{arg}				
arg		::=		sep basicType ID				
		|		sep basicType ID "=" val	
sep		::=		"/"								
		|		"?"
		|		"&"
val		::=		VAL_NUM							
		|		VAL_STRING						
		|		VAL_BOOL