JustTE4J - Java Web Template Engine

JustTE4J is a web template engine written in Java. It's core is based on JSoup HTML Parser and servlets. Therefore requires servlet container (preferably Tomcat). In general in consist of one servlet for handling data processing, JustTE4J library (one file) and dependencies.

Usage

Once installed and configured JustTE4J engine may be used to process web pages. In order to do that you must take the following steps

Step 1:

  • Put template form in the /template/form folder (unless otherwise configure).
  • Template form is just a normal html page. It should be working and well formatted page.
  • It does not have to be accessible from the Internet as servlet loads it as file.
  • Naturally it may me accompanied by css and js folders and files, but they must be located relative to main folder.
  • If you don't want to use /template/form file structure but main folder instead - you must reconfigure web.xml as follows:

<context-param>
<param-name>TE4J_FILE_FOLDER</param-name>
<param-value></param-value>
</context-param>
<context-param>
<param-name>FORM_FILE_FOLDER</param-name>
<param-value></param-value>
</context-param>

Step 2:

  • Create and put data files in the /template/data folder (unless otherwise configure).
  • Data file name should correspond to requested file name. Default extension is .ted but may be configured.
  • Data file should contain commands for template engine as described below.
  • If you don't want to use /template/data file structure but main folder instead - you must reconfigure web.xml as follows

<context-param>
<param-name>TE4J_FILE_FOLDER</param-name>
<param-value></param-value>
</context-param>
<context-param>
<param-name>DATA_FILE_FOLDER</param-name>
<param-value></param-value>
</context-param>

Data file content

Data files (those located in /template/data folder) should contain commands for template engine. Each command is one line and has key-value structure. In general, the key consists of information on how to find tag to be process, what to change and how to change it. The value is data to be process by template engine.

The syntax for every command is as follows:

[TARGET ELEMENT] : [COMMAND] : [SUBCOMMAND] : [PARAMI]:[PARAMII] [KEY-VALUE SEPARATOR] [DATA_TO_HANDLE] [END_SEQUENCE]

Element are separated by colon. Spaces are not required. The parameters may be skipped but colons should remain. Key and value are separated by two characters "!=" (may be configured in source code). Command is finished by three characters ";\r\n" (may be configured in source code) which is ';' and ENTER.

TARGET

TARGET ELEMENT is always one character and indicates how to find tag to be processed. This may be:

-T for TAG - finds and processes tags that has defined tag name. The tag name should be defined as PARAMI,

- C for CLASS - finds and processes tags that has indicated class value. The class value should be defined as PARAMI,

- A for ATTRIBUTE - finds and processes tags that has defined attribute name. The attribute name should be defined as PARAMI,

- I for ID - finds and processes tags that has indicated id value. The id value should be defined as PARAMI,

COMMAND

COMMAND is always one character and indicates what would be processed if the tag is found.

-C for content - it is information that tags content would be changed. In includes content between tags as well as start and end tags and content before and after tag.

- A for attribute - informs that only content of start tag should be changed. Name of this attribute is indicated by PARAMII.

SUBCOMMAND

SUBCOMMAND is always one character and indicates how data would be processed if the tag is found.

R for Replace - indicates that the whole tag (start tag, content and end tag) or attribute name and value should be replaced by defined data.

I for Insert - indicates that the content of the tag (start tag and end tag left untouched) or attribute value should be replaced by defined data.

A for append - indicates that defined data should be placed after existing content of the tag (start tag, content and end tag left untouched) or existing content of attribute value.

P for prepend - indicates that defined data should be placed before existing content of the tag (start tag, content and end tag left untouched) or existing content of attribute value,

N for name change -indicates that tag or attribute should be renamed.

D for delete - indicates that tag or attribute should be deleted.

F for former - indicates that defined data should be placed before the tag ( tag left untouched) or the attribute.

L - for latter - indicates that defined data should be placed after the tag ( tag left untouched) or the attribute.

PARAM

PARAMI is multi characters value. It is parameter data for target. Mainly name of tag, class, attribute or id.

PARAMII is multi characters value. It is parameter data for subcommand. Mainly name attribute.

KEY-VALUE SEPARATOR and END_SEQUENCE

KEY-VALUE SEPARATOR - two characters "!=" (may be configured in source code).

END_SEQUENCE - three characters ";\r\n" (semi colon and ENTER, may be configured in source code).

Commands example

1. T:C:I:title:!=New Page Name;

means find every tag matching tag name - "title", change it's content, replace data between start tag and end tag but don't change start and end tags. Use "New Page Name" text as new content. Command finished with ';' and ENTER. No PARAMII defined.

2. C:C:P:classy:!=<div>new tag</div>;

means find every tag matching class value - "classy", change it's content, put data before existing content. Use "<div>new tag</tag>". Command finished with ';' and ENTER. No PARAMII defined.

3. A:A:I:data-new:width!=344;

means find every tag which has attribute matching name - "data-new", change other attribute - 'width', put data as value, don't change attribute name. Use "344" as value for width attribute. Command finished with ';' and ENTER.

4. I:A:D:tag222:selected!=NA;

means find tag which has id matching value - "tag222", change attribute - selected by deleting it. No value defined but MUST put something (for example - NA) as value. Command finished with ';' and ENTER.

5. T:C:L:div:!=<div>new line</div>;

means find every tag matching tag name - "div", change content - put data after end tag. Use "<div>new line</div>" text as new content. Command finished with ; and ENTER. No PARAMII defined.

6. I:A:I:myId:class:!=selectOption;

means find every tag matching id value - "myId", change attributes - find class attribute and insert 'selectOption' data as value. Command finished with ';' and ENTER. No PARAMII defined.