It is easy to notice that JSP source text may be formatted in two equally legal styles (this is true also for PHP, Razor and so on):

<!-- some markup ->     <%    /*   some Java   */%> <!-- some markup ->
OR
/*   some Java   */     %>   <!-- some markup -> <% /*   some Java   */

We see that Java and HTML have equal rights in a JSP page and that is what we call a symmetric approach. HybridJava Language goes further and applies that symmetric approach at a deeper level: the grammar-level, where Java and HTML formal grammars are merged. The great difference is that the JSP processor is just a 'template engine' - it performs a blind text substitution while HybridJava is a strictly defined compilable language.

First immediate advantage of HybridJava Language over JSP is a dramatic drop of junk syntax of <% and %> delimiters, which usually make JSP pages barely readable.

Another advantage lies in the more rigorous syntax control of the code. Having a joint formal grammar makes it possible to  discover syntax problems at the earliest possible time. Not only syntax problems in Java and HTML parts of the code separately, but also syntax problems of their intersection, like in {<b>}</b>. Though current implementation of HybridJava compiler does not use this opportunity in full, still it controls many specific cases of erroneous syntax with about 60 different error messages.

HybridJava has a flexible, simple and consistent concept of widget, which by itself does not imply any additional Java programming. Unlike known implementations of user-defined tags, HybridJava widgets may have several named points of insertion (using JSP 2.0 terminology - several bodies). Thus every widget may have its own "Layout". 

Another unique feature is transparency of both HTML and widgets for Java context. A pair of tags <p></p> or <widget1></widget1> is as transparent for Java context as a pair of curly Java brackets { }. HTML and widget tags even create Java blocks.

Syntax

HybridJava Language is defined through a merge of HTML and a subset of Java at the grammar level. HybridJava code contains both Java code and HTML. Tags that belong to HTML represent HTML elements. For elements that may have a closing tag that tag must be present. HTML elements that do not have an end tag may be with or without the end slash. For example, <br/> and <br> mean the same thing. So, HybridJava is XHTML friendly. Other (non-HTML) tags, except for few reserved (<widget, <slot, <attr, <include, <using) represent widget and slot usage points.

Reserved word Hybrid in the attribute list of any opening tag means that the text between this tag and corresponding closing tag is Hybrid code (Java code mixed with HTML elements). Otherwise the text is interpreted as page content:

<p Hybrid>/*here goes hybrid code like:*/ x=y; < br>z=h; </p>
<p> /*text after this comment goes to the HTML page:*/ Text </p>

HTML elements that do not permit text content between tags are always “Hybrid ”.

<table> //Table tag is always "hybrid".
for (int i=1; i<5; i++) {
<tr>
<td>...</td>
<td>...</td>
</tr>
}
</table>

Widgets and Slots

Widgets are defined as below and may contain definitions of attributes and definitions of slots. A slot is a position in the widget body that may be filled with HybridJava code by the user (caller) of this widget. A widget may have either an anonymous slot or named slot(s).

<widget name=widget1 Hybrid> // Example of widget definition, file widget1.widget
<attr name=attr1 type=int default=8/> // Example of attribute definition
... // some HybridJava code; continued over the slot s1 definition
<slot name=s1 Hybrid> // start of a named slot definition; “default” HybridJava code inside
... // some “default” HybridJava code
<p> some text /* some “default” HTML text */ </p>
</slot> // end of a named slot definition
... // some HybridJava code; continued over the slot s2 definition
<slot name=s2> // start of a named slot definition; “default” document text inside
... // some HTML document text
</slot> // end of a named slot definition
... // yet even more HybridJava code
<slot name=s3/> // definition of a slot without default filling (content)
</widget> // end of widget definition

Widget call (usage of a widget) example:

<widget1>
<s1> some text /* some filling document text */ </s1>
<s2 Hybrid> ... /* some filling HybridJava code */ </s2>
</widget1> // slot s3 not used at all but who cares;

To call widget with an anonymous slot simply:

<widget2 Hybrid> /* some HybridJava code */ </widget2>

A variable defined in the widget body may be “exported” into the slot:

<widget name=widget4 Hybrid> // widget definition
String color =”Red“;
<slot color/> // anonymous slot does not have a name, “color” is exported into the slot
</widget> // slot s3 not used at all but who cares;
//------- usage:
<widget4> color=$color </widget4>// on the browser screen: color=Red

In case of recursion at least one of the widgets involved must limit recursion depth. Attribute recursion may be set > 1 in widget definition or in the widget call. Setting recursion in a widget definition to 0 prohibits any recursive use of the given widget.

Variables

HybridJava variables are basically Java variables. They may be declared and used within the Hybrid areas of the code using standard Java syntax. Definition of a widget's attribute also creates a variable visible inside the widget's body. Default type of an attribute is String:

<attr name=attr1 type=int/> // creates a integer variable “attr1” local to the widget
int x = 77;//conventional (Java) way to create a variable
System.err.println(attr1 + x);

Variables may be used in widget call tags:

<MyWidget attr1=x> // variable (attribute) “attr1” of widget is assigned the value of x (77)
    // here goes the filling of an anonymous slot of widget “MyWidget”
</MyWidget>

Also (prefixed by $ sign) in HTML document content and in attributes of HTML tags:

<p > profit is $x dollars </p > // value of x printed into the HTML
<input type=button value=”$x>// value of x printed into the HTML attribute value area

An additional semicolon delimiter may be necessary:

String prefix = flag ? “con” : “contra”;
<p> What a wonderful $prefix;ception!</p>

Restriction: unlike Java in HybridJava the dollar sign ($) may not be used as a part of identifier

Variables defined in a page are accessible in any widget called by that page directly or indirectly. Variables defined inside a widget are accessible only in same instance of the body of the widget. Imported (Javaimport … “) items are accessible anywhere.

Expression Language

Java expression concluded into ${ ... } brackets is evaluated at the runtime and its value is used according to the context of the expression. It will be substituted into rendered HTML document text, into the attribute of HTML element or passed to a widget

Pseudo-variable $#

The framework substitutes this variable with a unique ID of a component.

Other Features

Java comments get into generated Java code. HTML comments appear in the HTML pages.<,> and & signs in the HTML content are converted to & lt; , &gt; and &amp; . $// and $/* escape beginning of comments; $< escapes what should not be recognized as a beginning of HTML or widget tag (also of <widget, <slot, <attr and <!--). $$ escapes $.

In HybridJava import and implements statements are placed among other Java statements. Compiler collects import and implements statements into the generated page Java class source.

Pages and widgets are assumed belonging to packages in the same sense as Java classes. To use widgets from a package different than package of the current page or widget use either attribute use or tag using:

<widget7 use=com.HybridJava.X>
<using com.HybridJava.X/>

Widgets in com.HybridJava.Lib are accessible by default if not shadowed.

A predefined widget <Include with attribute file inserts the content of that file into the generated Java code so that it finally gets into HTML page content. The file may be processed depending on the file extension and other attributes of <Include widget. As of today .html, .page, .widget and .java files are presented in colors.

There are no class and method definitions in HybridJava but you may use classes and methods defined elsewhere.

Identification

While processing an identifier in the .widget file the HybridJava compiler first tries to find its definition in HybridJava code of the widget. Then - among widget's attributes. After that it looks in exports of related slots. Next it examines the members of component state instance. Finally it checks if there is an appropriate definition in Application State (AS). Otherwise (in the current implementation) the identifier is left to be handled by javac. The latter may result in locating a definition in the Hybrid (Java) code of the .page or in the Page State instance. Finally javac tries to resolve identifier in imported classes. Resolving identifier from .page is similar except that it has fewer steps.

Directory Structure

.page files should be placed under directory pages\into package subdirectories (e.g. pages\com\HybridJava\Lib\Page1.page). Likewise files .widget must be located under directory widgets\. So in HybridJava the full names of pages and widgets include the package name.

Command line implementation consumes files .page and .widget from directories pages\and widgets\ and generates .java files under gen\ sub-directory; one .java file corresponding to one page. For instance for page X.page the name of the generated file would be X_P.java.

Support of International Languages

Current implementation of HybridJava compiler accepts source files with non-ASCII characters presented in \uffff format. Such files may be generated from Unicode source files using standard native2ascii utility. So most of Internationalization is completed at build time. However nothing can stop you from using standard Resource Bundles and/or CMS - it will wotk a bit slower but that may not matter for a specific application.

On the so called "separation"

It is quite reasonable to seek separation of presentation layer from business (and other) layers. However many people look for separation of Java from HTML instead, which is just not the same.

This is all about pages with dynamically generated structure, which HTML as such does not provide so we need at least conditional operators and loops to be involved. The presentation layer is doomed to have its own logic and all technologies add that bit, though some take the corresponding constructs from a programming language, while others hide them under tags. The latter rids of Java, but not of conditional statements and loops within presentation layer source code in some form.

Do not confuse possibility and necessity. If you do not want to mix Java and HTML simply do not do that. That is more a matter of your own culture, and not of the language. With HybridJava (same as with any other language) you may program in different ways. In particular in the Sample Application all the pages and most widgets are written in "separation style". Only few low level widgets had to use bits of Java syntax, like <If widget. For that HybridJava (unlike JSP  +  JSTL) has zero predefined tags - you may easily write them all yourself.

© 2012 HybridServerPages Group. All rights reserved.