Document revision level $Revision: 1.21 $
Date: 28 October 2006
AdaCore
First of all this package is distributed under the GNAT modified GNU GPL.
The templates parser package has been designed to parse files and to replace some specific tags into these files by some specified values.
The main goal was to ease the development of Web servers. In CGI
(Common Gateway Interface) mode you have to write the HTML page in
the program (in Ada or whatever other languages) by using some specific
libraries or by using only basic output functions like Ada Put_Line
for
example. This is of course not mandatory but by lack of a good library
every Web development end up doing just that.
The main problems with this approach are:
With the templates parser package these problems are gone. The code and the design is completely separated. This is a very important point. PHP or JSP have tried this but most of the time you have the script embedded into the Web template. And worst you need to use another language just for your Web development.
In fact, the Ada program now simply computes some values, gets some data from a database or whatever and then calls the templates parser to output a page with the data displayed. To the templates parser you just pass the template filename and an associative table.
It is even more convenient to have different displays with the same set of data. You just have to provide as many templates as you like.
A tag is a string found in the template page and surrounded by a specific set
of characters. The default is @_ at the start and _@ at the
end of the tag. This default can be changed using Set_Tag_Separators
routine, see Templates_Parser API Reference. Note that it must be changed
as the first API call and should not be changed after that.
The tag will be replaced by a value specified in a translation table.
For example with the template file demo.tmplt:
<P>Hello @_NAME_@
Using the following code demo.adb:
with Ada.Text_IO; with Templates_Parser; procedure Demo is Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("NAME", "Ada")); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("demo.tmplt", Translations)); end Demo;
The program will print out :
<P>Hello Ada
This is a very simple example, but you'll see that there is a lot of powerful construct that you can use into a template file.
A variable tag is a specific string to be replaced by the template
parser. There is three kinds of variable tags: discrete, boolean,
composite. All variables tags are built using the Assoc
constructors, see Templates_Parser API Reference.
IF
tag
statement.
TABLE
tag statement
see TABLE tag statement. Outside a table statement it will be
replaced by all values concatenated with a specified separator. See
Set_Separator
routine. Such tag are variables declared on the
Ada program a Templates_Parser.Tag
type.
There is many overloaded constructors to build a composite tags (see "+" operators). The "+" operators are used to build a Tag item from standard types like String, Unbounded_String, Character, Integer and Boolean.
To add items to a Tag many overloaded operators are provided (see "&" operators). The "&" operators add one item at the start or the end of the tag, it is possible to add directly String, Unbounded_String, Character, Integer and Boolean items using one of the overloaded operator.
A tag composed of only boolean values TRUE or FALSE is called a
Boolean composite tag. This tag is to be used with a IF
tag statement
inside a TABLE
tag statement.
It is possible to build a composite tag having any number of nested level. A vector is a composite tag with only one level, a matrix is a composite tag with two level (a Tag with a set of vector tag).
Two aliases exists for composite tags with one or two nested level,
they are named Vector_Tag
and Matrix_Tag
. In the suite
of the document, we call vector tag a tag with a single nested
level and matrix tag a tag with two nested level.
All kinds of variable tag can have one or more function-prefix or
filter. The function prefix is applied to the variable value. The
syntax is
@_[[FILTER_NAME[(parameter)]:]FILTER_NAME[(parameter)]:]SOME_VAR_@
.
Filters are evaluated from right to left.
Composite tags can also have attributes. Attributes are placed
after the tag name and preceded with a simple quote.
@_SOME_VAR['ATTRIBUTE_NAME]_@
. It is possible to use filters
and attributes together. In that case the attribute is first evaluated and
the result is passed-through the filters.
The current supported filters are:
"+"(
N) or ADD(
N)
"-"(
N) or SUB(
N)
"*"(
N) or MULT(
N)
"/"(
N) or DIV(
N)
ABS
ADD_PARAM(
NAME[=VALUE])
BR_2_EOL(
EOL)
<br>
HTML tag by a line terminator
determined by EOL. EOL must be either CR (Carriage-Return), LF (Line-Feed),
LFCR (Line-Feed followed by Carriage-Return) or CRLF (Carriage-Return
followed by Line-Feed).
BR_2_LF
CAPITALIZE
CLEAN_TEXT
COMA_2_POINT
CONTRACT
DEL_PARAM(
NAME)
EXIST
FILE_EXISTS
FORMAT_DATE(
FORMAT)
GNAT.Calendar.Time_IO
.
FORMAT_NUMBER
IS_EMPTY
LF_2_BR
<br>
HTML tag.
LOWER
MATCH(
REGEXP)
MAX(
N)
MIN(
N)
MOD(
N)
NEG
NO_DYNAMIC
NO_DYNAMIC
must
be the first filter. This filter returns the value as-is.
NO_DIGIT
NO_LETTER
NO_SPACE
OUI_NON
POINT_2_COMA
REPEAT(
N)
REPEAT(
STR)
REPEAT
form. In this case STR is repeated
a number of time corresponding to the variable value which must be a number.
REPLACE(
REGEXP[/
STR])
REPLACE_ALL(
REGEXP[/
STR])
REPLACE_PARAM(
NAME[=VALUE])
REVERSE
SIZE
SLICE(
x .. y)
TRIM
UPPER
WEB_ENCODE
WEB_ESCAPE
WEB_NBSP
WRAP(
N)
YES_NO
For example:
If VAR is set to "vector_tag", ONE to "1" and TWO to "2" then: @_VAR_@ -> vector_tag @_UPPER:VAR_@ -> VECTOR_TAG @_CAPITALIZE:VAR_@ -> Vector_Tag @_EXIST:VAR_@ -> TRUE @_UPPER:REVERSE:VAR_@ -> GAT_ROTCEV @_MATCH(VEC.*):UPPER:VAR_@ -> TRUE @_SLICE(1..6):VAR_@ -> vector @_REPLACE(([^_]+)):VAR_@ -> vector @_REPLACE(([a-z]+)_([a-z]+)/\2_\1):VAR_@ -> tag_vector @_"+"(TWO):ONE_@ -> 3 @_"-"(TWO):ONE_@ -> -1
It is also possible to define a new filter by registering a callback routine associated with the filter name. Here is the corresponding API:
type Callback is access function (Value : in String; Parameters : in String; Translations : in Translate_Set) return String; -- User's filter callback type Callback_No_Param is access function (Value : in String; Translations : in Translate_Set) return String; -- User's filter callback procedure Register_Filter (Name : in String; Handler : in Callback); -- Register user's filter Name using the specified Handler procedure Register_Filter (Name : in String; Handler : in Callback_No_Param); -- Register user's filter Name using the specified Handler
Filters with and without parameter are supported.
Current supported attributes are:
V'length
V'Up_Level(n)
'Up_Level
is equivalent
to 'Up_Level(1)
(can be applied only to a composite tag having
a single nested level - a vector).
M'Line
M'Min_Column
M'Max_Column
For example:
If VEC is set to "<1 , 2>" and MAT to "<a, b, c> ; <2, 3, 5, 7>" then: @_VEC'Length_@ -> 2 @_ADD(3):VEC'Length_@ -> 5 @_MAT'Line_@ -> 2 @_MAT'Min_Column_@ -> 3 @_MAT'Max_Column_@ -> 4
There is some specific variables tags that can be used in any templates. Here is a description of them:
NOW
YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
MONTH_NAME
DAY_NAME
Associations between variable tags and the template tag names are
created with one of the Assoc
routines. This set of
associations are used by the parser (Parse
routine). There is
two kinds of associations set:
Note that this difference is only for users, the Templates_Parser
engine uses only Translate_Set
objects internally as it is much more
efficient.
There is three different tag statements. A tag statement is surrounded
by @@
. The tag statements are:
Every line starting with @@– are comments and are completly ignored by the parser. The resulting page will have the exact same format and number of lines with or without the comments.
@@-- This template is used to display the client's data @@-- It uses the following tags: @@-- @@-- @_CID_@ Client ID @@-- @_ITEMS_V_@ List of items (vector tag) <P>Client @_CID_@ ...
This tag is used to include another template file. This is useful if you have the same header and/or footer in all your HTML pages. For example:
@@INCLUDE@@ header.tmplt <P>This is by Web page @@INCLUDE@@ footer.tmplt
It is also possible to pass arguments to the include file. These parameters
are given after the include filename. It is possible to reference these
parameters into the included file with the special variable names
@_$<n>_@
, where n is the include's parameter indice (0 is
the include filename, 1 the first parameter and so on).
@@INCLUDE@@ another.tmplt @_VAR_@ azerty
In file another.tmplt
@_$0_@
@_$1_@
@_$2_@
If an include variable references a non existing include parameter the tag is kept as-is.
Note that it is possible to pass the include parameters using names, a set of positional parameters can be pass first, so all following include commands are identical:
@@INCLUDE@@ another.tmplt one two three four "a text" @@INCLUDE@@ another.tmplt (one, two, 3 => three, 4 => four, 5 => "a text") @@INCLUDE@@ another.tmplt (one, 5 => "a text", 3 => three, 2 => two, 4 => four)
This is the conditional tag statement. The complete form is:
@@IF@@ <expression1> part1 @@ELSIF@@ <expression2> part2 @@ELSE@@ part3 @@END_IF@@
The part1 one will be parsed if expression1 evaluate to "TRUE", part2
will be parsed if expression2 evaluate to "TRUE" and the part3 will
be parse in any other case. The ELSIF
and ELSE
part are
optional.
The expression here is composed of boolean variable (or conditional variable) and/or boolean expression. Recognized operators are:
A = B
A /= B
A > B
A >= B
A < B
A <= B
A and B
A or B
A xor B
not A
The default evaluation order is done from left to right, all operators having the same precedence. To build an expression it is possible to use the parentheses to change the evaluation order. A value with spaces must be quoted as a string. So valid expressions could be:
@@IF@@ (@_VAR1_@ > 3) or (@_COND1_@ and @_COND2_@) @@IF@@ not (@_VAR1_@ > 3) or (@_COND1_@ and @_COND2_@) @@IF@@ (@_VAR1_@ > 3) and not @_COND1_@ @@IF@@ @_VAR1_@ = "a value"
Note also that variables and values can be surrounded by quotes if needed. Quotes are needed if a value contain spaces.
To generate a conditional variable tag it is possible to use the following Templates_Parser function:
function Assoc (Variable : in String; Value : in Boolean) return Association; -- Build an Association (Variable = Value) to be added to a -- Translate_Table. It set the variable to TRUE or FALSE depending on -- Value.
Let's see an example using an IF
tag statement. With the following
template:
@@IF@@ @_USER_@ <P>As a user you have a restricted access to this server. @@ELSE@@ <P>As an administrator you have full access to this server. @@END_IF@@
The following program:
with Ada.Text_IO; with Templates_Parser; procedure User1 is Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("USER", True)); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("user.tmplt", Translations)); end User1;
Will display:
<P>As a user you have a restricted access to this server.
But the following program:
with Ada.Text_IO; with Templates_Parser; procedure User2 is Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("USER", False)); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("user.tmplt", Translations)); end User2;
Will display:
<P>As an administrator you have full access to this server.
Table tags are useful to generate HTML
tables for example.
Basically the code between the @@TABLE@@
and
@@END_TABLE@@
will be repeated as many times as the vector
tag has values. If many vector tags are specified in a table
statement, the code between the table will be repeated a number of
times equal to the maximum length of all vector tags in the
TABLE
tag statement.
A TABLE
tag statement is a kind of implicit iterator. This is a very
important concept to build HTML tables. Using a composite tag variable in
a @@TABLE@@
tag statement it is possible to build very
complex Web pages.
Syntax:
@@TABLE['TERMINATE_SECTIONS]['REVERSE]@@ ... [@@BEGIN@@] ... [@@SECTION@@] ... [@@END@@] ... @@END_TABLE@@
Let's have an example. With the following template:
<P>Here is the ages of some peoples: <TABLE> @@TABLE@@ <TR> <TD>@_NAME_@ <TD>@_AGE_@ @@END_TABLE@@ </TABLE>
And the following program:
with Ada.Text_IO; with Templates_Parser; procedure Table is use type Templates_Parser.Vector_Tag; Names : constant Templates_Parser.Vector_Tag := +"Bob" & "Bill" & "Toto"; Ages : constant Templates_Parser.Vector_Tag := +"10" & "30" & "5"; Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("NAME", Names), 2 => Templates_Parser.Assoc ("AGE", Ages)); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("table.tmplt", Translations)); end Table;
The following output will be generated:
<P>Here is the ages of some peoples: <TABLE> <TR> <TD>Bob <TD>10 <TR> <TD>Bill <TD>30 <TR> <TD>Toto <TD>5 </TABLE>
Note that we use vector tag variables here. A discrete variable tag in a table will be replaced by the same (the only one) value for each row. A vector tag outside a table will be displayed as a list of values, each value being separated by a specified separator. The default is a comma and a space ", ".
The complete prototype for the Tag
Assoc function is:
function Assoc (Variable : in String; Value : in Tag; Separator : in String := Default_Separator) return Association; -- Build an Association (Variable = Value) to be added to Translate_Table. -- This is a tag association. Separator will be used when outputting the -- a flat representation of the Tag (outside a table statement).
A table can contain many sections. The section to use will be selected depending on the current line. For example, a table with two sections will use different data on even and odd lines. This is useful when you want to alternate the line background color for a better readability when working on HTML pages.
A table with sections can have attributes:
TERMINATE_SECTIONS
REVERSE
<P>Here are some available computer devices: <TABLE> @@TABLE@@ <TR BGCOLOR=#FF0000> <TD>@_DEVICES_@ <TD>@_PRICES_@ @@SECTION@@ <TR BGCOLOR=#00000F> <TD>@_DEVICES_@ <TD>@_PRICES_@ @@END_TABLE@@ </TABLE> <TABLE> @@TABLE@@ @@TERMINATE_SECTIONS@@ <TR> <TD BGCOLOR=#00000F WIDTH=10> <TD WIDTH=150>@_DEVICES_@ @@SECTION@@ <TD WIDTH=150>@_DEVICES_@ @@SECTION@@ <TD WIDTH=150>@_DEVICES_@ <TD BGCOLOR=#00000F WIDTH=10> @@END_TABLE@@ </TABLE>
And the following program:
with Ada.Text_IO; with Templates_Parser; procedure Table_Section is use type Templates_Parser.Vector_Tag; Devices : constant Templates_Parser.Vector_Tag := +"Screen" & "Keyboard" & "Mouse" & "Hard Drive"; Prices : constant Templates_Parser.Vector_Tag := +"$500" & "$20" & "$15" & "$140"; Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("DEVICES", Devices), 2 => Templates_Parser.Assoc ("PRICES", Prices)); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("table_section.tmplt", Translations)); end Table_Section;
The following output will be generated:
<P>Here are some available computer devices: <TABLE> <TR BGCOLOR=#FF0000> <TD>Screen <TD>$500 <TR BGCOLOR=#00000F> <TD>Keyboard <TD>$20 <TR BGCOLOR=#FF0000> <TD>Mouse <TD>$15 <TR BGCOLOR=#00000F> <TD>Hard Drive <TD>$140 </TABLE> <TABLE> <TR> <TD BGCOLOR=#00000F WIDTH=10> <TD WIDTH=150>Screen <TD WIDTH=150>Keyboard <TD WIDTH=150>Mouse <TD BGCOLOR=#00000F WIDTH=10> <TR> <TD BGCOLOR=#00000F WIDTH=10> <TD WIDTH=150>Hard Drive <TD WIDTH=150> <TD WIDTH=150> <TD BGCOLOR=#00000F WIDTH=10> </TABLE>
It is important to note that it is possible to avoid code
duplication by using the @@BEGIN@@
and @@END@@
block statements. In this case only the code inside the block is part
of the section, the code outside is common to all sections. Here is
an example to generate an HTML table with different colors for each line:
The template file above can be written this way:
<P>Here are some available computer devices: <TABLE> @@TABLE@@ <TR BGCOLOR= @@BEGIN@@ "#FF0000" @@SECTION@@ "#000000F" @@END@@ > <TD>@_DEVICES_@ <TD>@_PRICES_@ @@END_TABLE@@ </TABLE>
Into a table construct there are some additional variable tags available:
@_UP_TABLE_LINE_@
@_TABLE_LINE_@
@_NUMBER_LINE_@
@_TABLE_LEVEL_@
Let's have a look at a more complex example with mixed IF and TABLE tag statements.
Here is the template:
Hello here is a list of devices: <table> <tr> <th>Device Name <th>Price <th>Order @@TABLE@@ <tr> <td>@_DEVICES_@ <td>@_PRICES_@ <td> @@IF@@ @_AVAILABLE_@ <a href="/order?DEVICE=@_DEVICES_@">Order @@ELSE@@ Sorry, not available @@END_IF@@ @@END_TABLE@@
And the following program:
with Ada.Text_IO; with Templates_Parser; procedure Table_If is use type Templates_Parser.Vector_Tag; function In_Stock (Device : in String) return Boolean; -- Complex function. Does a SQL access to the right database to know if -- the Device is available and thus can be ordered. procedure Add (Device, Price : in String); -- Add the device into the list to be displayed. Devices : Templates_Parser.Tag; Prices : Templates_Parser.Tag; Available : Templates_Parser.Tag; --------- -- Add -- --------- procedure Add (Device, Price : in String) is begin Devices := Devices & Device; Prices := Prices & Price; Available := Available & In_Stock (Device); end Add; -------------- -- In_Stock -- -------------- function In_Stock (Device : in String) return Boolean is begin if Device = "Keyboard" then return True; else return False; end if; end In_Stock; Translations : Templates_Parser.Translate_Table (1 .. 3); begin Add ("Screen", "$500"); Add ("Keyboard", "$15"); Add ("Mouse", "$15"); Add ("Hard Drive", "$140"); Translations := (Templates_Parser.Assoc ("DEVICES", Devices), Templates_Parser.Assoc ("PRICES", Prices), Templates_Parser.Assoc ("AVAILABLE", Available)); Ada.Text_IO.Put_Line (Templates_Parser.Parse ("table_if.tmplt", Translations)); end Table_If;
The following output will be generated:
Hello here is a list of devices: <table> <tr> <th>Device Name <th>Price <th>Order <tr> <td>Screen <td>$500 <td> Sorry, not available <tr> <td>Keyboard <td>$15 <td> <a href="/order?DEVICE=Keyboard">Order <tr> <td>Mouse <td>$15 <td> Sorry, not available <tr> <td>Hard Drive <td>$140 <td> Sorry, not available
Table tag statements can also be used with matrix tag or more nested
tag variables. In this case, for a tag variable with N nested levels,
the Nth closest enclosing TABLE
tag statement will be used for
the corresponding indices. If there is not enough indices, the last
axis are just streamed as a single text value.
Let's see what happens for a matrix tag:
In this case the first TABLE
iterates through the matrix lines.
First iteration will use the first matrix's vector, second
iteration will use the second matrix's vector and so on. And the second
TABLE
will be used to iterate through the vector's values.
In this case the TABLE
iterates through the matrix lines. First
iteration will use the first matrix's vector, second iteration will
use the second matrix's vector and so on. Each vector is then converted to
a string by concatenating all values using the specified separator
(see Assoc constructor for Tag or Set_Separator
routine).
In this case the matrix is converted to a string. Each line represents
a vector converted to a string using the supplied separator (see point
2 above), and each vector is separated by an ASCII.LF character. The
separators to use for each level can be specified using Set_Separator
.
Let's look at an example, with the following template:
A matrix inside a table of level 2: @@TABLE@@ <tr> @@TABLE@@ <td> @_MAT_@ </td> @@END_TABLE@@ </tr> @@END_TABLE@@ The same matrix inside a single table: @@TABLE@@ <tr> <td> @_MAT_@ </tr> @@END_TABLE@@ The same matrix outside a table: @_MAT_@
Using the program:
with Ada.Text_IO; with Templates_Parser; procedure Matrix is package TP renames Templates_Parser; use type TP.Vector_Tag; use type TP.Matrix_Tag; V1 : constant TP.Vector_Tag := +"A1.1" & "A1.2"; V2 : constant TP.Vector_Tag := +"A2.1" & "A2.2"; V3 : constant TP.Vector_Tag := +"A3.1" & "A3.2"; M : constant TP.Matrix_Tag := +V1 & V2 & V3; begin Ada.Text_IO.Put_Line (TP.Parse ("matrix.tmplt", TP.Translate_Table'(1 => TP.Assoc ("MAT", M)))); end Matrix;
We get the following result:
A matrix inside a table of level 2: <tr> <td> A1.1 </td> <td> A1.2 </td> </tr> <tr> <td> A2.1 </td> <td> A2.2 </td> </tr> <tr> <td> A3.1 </td> <td> A3.2 </td> </tr> The same matrix inside a single table: <tr> <td> A1.1, A1.2 </tr> <tr> <td> A2.1, A2.2 </tr> <tr> <td> A3.1, A3.2 </tr> The same matrix outside a table: A1.1, A1.2 A2.1, A2.2 A3.1, A3.2
The SET
command tag can be used to define a constant or an
alias for an include file parameter. This is especially important in
the context of reusable template files. For example, instead of having
many references to the red color in an HTML document, it is better to
define a constant COLOR with the value red and use COLOR
everywhere. It is then easier to change the color afterward.
The first form, to define a simple constant that can be used as any other variable in a template file, is:
@@SET@@ <name> = <value>
The second form, to define an alias for a template file parameter, is:
@@SET@@ <name> = $n [| <default_value>]
In this case <name> is an alias for the Nth include parameter. In this form it is also possible to define a default value that would be used if the Nth include parameter is not specified.
Some examples:
@@SET@@ COLOR = red @@SET@@ SIZE = $1 @@SET@@ COLOR = $4 | green
It is important to note that a variable is set global to a template file. It means that constants set into an include file are visible into the parent template. This is an important feature to be able to have a "theme" like include template file for example.
The INLINE
tag statement can be used to better control the
result's layout. For example it is not possible to have the results of
a vector tag on the same line, also it is not possible to have a
conditional output in the middle of a line. The INLINE
block
tag statement can be used to achieve that.
Elements in an inlined block are separated by a single space by
default. It is possible to specify any string as the separator. The
text layout on an INLINE
block has no meaning (the lines are
trimmed on both side). As part of the inline command it is possible to
specify texts to output before and after the block.
Syntax:
@@INLINE[(<before>)(<separator>)(<after>)]@@ ... @@END_INLINE@@
There is three supported usages:
@@INLINE@@
@@INLINE(<separator>)@@
@@INLINE(<before>)(<separator>)(<after>)@@
Let's look at an example, with the following template:
@@INLINE(colors=")(, )(")@@ @@TABLE@@ @_COLORS_@ @@END_TABLE@@ @@END_INLINE@@
Using the program:
with Ada.Text_IO; with Templates_Parser; procedure Table_Inline is use type Templates_Parser.Vector_Tag; Colors : constant Templates_Parser.Vector_Tag := +"Red" & "Green" & "Blue"; Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("COLORS", Colors)); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("table_inline.tmplt", Translations)); end Table_Inline;
We get the following result:
colors="Red, Green, Blue"
Another example with an IF
tag statement:
@@INLINE@@ A @@IF@@ @_COND_@ big @@ELSE@@ small @@END_IF@@ car. @@END_INLINE@@
Using the program:
with Ada.Text_IO; with Templates_Parser; procedure If_Inline is use type Templates_Parser.Vector_Tag; Translations : constant Templates_Parser.Translate_Table := (1 => Templates_Parser.Assoc ("COND", True)); begin Ada.Text_IO.Put_Line (Templates_Parser.Parse ("if_inline.tmplt", Translations)); end If_Inline;
We get the following result:
A big car.
Dynamic tags are handled through abstract interfaces and give the opportunity to create tags dynamically while the template is being parsed.
The Lazy_Tag
object can be used to dynamically handle tags. Such
object can be passed to the Parse
routines. If a template's tag
is not found in the Translate_Set
the Lazy_Tag
's Value
callback method is called by the parser. The default callback method
does nothing, it is up to the user to define it. The callback
procedure is defined as follow:
procedure Value (Lazy_Tag : access Dynamic.Lazy_Tag; Var_Name : in String; Translations : in out Translate_Set) is abstract; -- Value is called by the Parse routines below if a tag variable was not -- found in the set of translations. This routine must then add the -- association for variable Name. It is possible to add other -- associations in the translation table but a check is done to see if -- the variable Name as been set or not. The default implementation does -- nothing.
One common usage is to handle tag variables that can be shared by
many templates are not always used (because a conditional is False for
example). If computing the corresponding value (or values for a ...)
is somewhat expensive it is better to delay building such tag at the
point it is needed. Using a Lazy_Tag
object it is possible to do
so. The Value
procedure will be called if the tag value is
needed. At this point, one can just add the corresponding association
into the Translate_Set
. Note that it is possible to add more
than one association. If the association for Var_Name
is not
given this tag has no value.
In some cases, data structure on the Ada side can be so complex that it is
difficult to map it into a variable tag. The Cursor_Tag
object has
been designed to work around such problem. Using a Cursor_Tag
it is possible to create an iterator through a data structure without
mapping it into a variable tag. The data stays on the Ada side.
To create a Cursor_Tag
it is necessary to implement the following
abstract routines:
function Dimension (Cursor_Tag : access Dynamic.Cursor_Tag; Var_Name : in String) return Natural is abstract; -- Must return the number of dimensions for the given variable name. For -- a matrix this routine should return 2 for example. type Path is array (Positive range <>) of Natural; -- A Path gives the full position of a given element in the cursor tag function Length (Cursor_Tag : access Dynamic.Cursor_Tag; Var_Name : in String; Path : in Dynamic.Path) return Natural is abstract; -- Must return the number of item for the given path. The first -- dimension is given by the Path (1), for the second column the Path is -- (1, 2). Note that each dimension can have a different length. For -- example a Matrix is not necessary square. function Value (Cursor_Tag : access Dynamic.Cursor_Tag; Var_Name : in String; Path : in Dynamic.Path) return String is abstract; -- Must return the value for the variable at the given Path. Note that -- this routine will be called only for valid items as given by the -- dimension and Length above.
The child package Utils
, see Templates_Parser.Utils
contains a routine to encode a Tag variable into a string and the
inverse routine that build a Tag given it's string
representation. This is useful for example, in the context of AWS to
store a Tag into a session variable. See the AWS project.
The child package XML
, see Templates_Parser.XML contains
routines to save a Translation_Set
into an XML document or to
create a Translation_Set
by loading an XML document. The XML
document must conform to a specific DTD
(see the Ada spec file).
A set of routines to help to debug the Templates_Parser
engine,
see Templates_Parser.Debug. For example, Debug.Print_Tree
will display, to the standard output, a representation of the internal
semantic tree for a template file.
The templates parser has be written to parse HTML
pages but it is usable
with any kind of files. There is nothing hard coded for HTML
. It is then
possible to use it with plain text files, XML
files, SGML
files
or whatever as long as it is not a binary file.
All tag statements can be mixed together. A TABLE
tag statement can be
put in an IF
tag statement. An IF
tag statement can be put
in a TABLE
tag statement. Idem for the INCLUDE
tag statement.
Templates_Parser User's API
------------------------------------------------------------------------------ -- Templates Parser -- -- -- -- Copyright (C) 1999-2006 -- -- AdaCore -- -- -- -- This library is free software; you can redistribute it and/or modify -- -- it under the terms of the GNU General Public License as published by -- -- the Free Software Foundation; either version 2 of the License, or (at -- -- your option) any later version. -- -- -- -- This library is distributed in the hope that it will be useful, but -- -- WITHOUT ANY WARRANTY; without even the implied warranty of -- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- -- General Public License for more details. -- -- -- -- You should have received a copy of the GNU General Public License -- -- along with this library; if not, write to the Free Software Foundation, -- -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -- -- ------------------------------------------------------------------------------ with Ada.Finalization; with Ada.Strings.Unbounded; with Strings_Maps; package Templates_Parser is use Ada.Strings.Unbounded; Template_Error : exception; Default_Begin_Tag : constant String := "@_"; Default_End_Tag : constant String := "_@"; Default_Separator : constant String := ", "; procedure Set_Tag_Separators (Start_With : in String := Default_Begin_Tag; Stop_With : in String := Default_End_Tag); -- Set the tag separators for the whole session. This should be changed as -- the very first API call and should not be changed after. ----------------- -- Generic Tag -- ----------------- type Tag is private; -- A tag is using a by reference semantic function "+" (Value : in String) return Tag; function "+" (Value : in Character) return Tag; function "+" (Value : in Boolean) return Tag; function "+" (Value : in Unbounded_String) return Tag; function "+" (Value : in Integer) return Tag; function "+" (Value : in Tag) return Tag; -- Tag constructors function "&" (T : in Tag; Value : in String) return Tag; function "&" (T : in Tag; Value : in Character) return Tag; function "&" (T : in Tag; Value : in Boolean) return Tag; function "&" (T : in Tag; Value : in Unbounded_String) return Tag; function "&" (T : in Tag; Value : in Integer) return Tag; function "&" (T : in Tag; Value : in Tag) return Tag; -- Add Value at the end of the tag function "&" (Value : in String; T : in Tag) return Tag; function "&" (Value : in Character; T : in Tag) return Tag; function "&" (Value : in Boolean; T : in Tag) return Tag; function "&" (Value : in Unbounded_String; T : in Tag) return Tag; function "&" (Value : in Integer; T : in Tag) return Tag; -- Add Value at the front of the tag procedure Set_Separator (T : in out Tag; Separator : in String); -- Set separator to be used when building a flat representation of -- a composite tag. procedure Clear (T : in out Tag); -- Removes all values in the tag. Current tag T is not released but -- the returned object is separated (not using the same reference) than -- the original one. function Size (T : in Tag) return Natural; -- Returns the number of value into T function Item (T : in Tag; N : in Positive) return String; -- Returns the Nth Tag's item. Raises Constraint_Error if there is -- no such Item in T (i.e. T length < N). function Composite (T : in Tag; N : in Positive) return Tag; -- Returns the Nth Tag's item. Raises Constraint_Error if there is -- no such Item in T (i.e. T length < N). subtype Vector_Tag is Tag; subtype Matrix_Tag is Tag; ------------------ -- Associations -- ------------------ type Association is private; Null_Association : constant Association; type Association_Kind is (Std, Composite); -- The kind of association which is either Std (a simple value), a vector -- tag or a Matrix tag. function Assoc (Variable : in String; Value : in String) return Association; -- Build an Association (Variable = Value) to be added to a -- Translate_Table. This is a standard association, value is a string. function Assoc (Variable : in String; Value : in Unbounded_String) return Association; -- Build an Association (Variable = Value) to be added to a -- Translate_Table. This is a standard association, value is an -- Unbounded_String. function Assoc (Variable : in String; Value : in Integer) return Association; -- Build an Association (Variable = Value) to be added to a -- Translate_Table. This is a standard association, value is an Integer. -- It will be displayed without leading space if positive. function Assoc (Variable : in String; Value : in Boolean) return Association; -- Build an Association (Variable = Value) to be added to a -- Translate_Table. It set the variable to TRUE or FALSE depending on -- value. function Assoc (Variable : in String; Value : in Tag; Separator : in String := Default_Separator) return Association; -- Build an Association (Variable = Value) to be added to Translate_Table. -- This is a tag association. Separator will be used when outputting the -- a flat representation of the Tag (outside a table statement). function Get (Assoc : in Association) return Tag; -- Returns the Tag in Assoc, raise Constraint_Error if Assoc is not -- containing a Tag (Association_Kind is Std). function Get (Assoc : in Association) return String; -- Returns the value in Assoc, raise Constraint_Error if Assoc is not -- containing a simple value (Association_Kind is Composite). --------------------------- -- Association table/set -- --------------------------- type Translate_Table is array (Positive range <>) of Association; -- A table with a set of associations, note that it is better to use -- Translate_Set below as it is more efficient. No_Translation : constant Translate_Table; type Translate_Set is private; -- This is a set of association like Translate_Table but it is possible to -- insert item into this set more easily, furthermore there is no need to -- know the number of item before hand. This is the object used internally -- by the templates engine as it is far more efficient to retrieve a -- specific item from it. Null_Set : constant Translate_Set; procedure Insert (Set : in out Translate_Set; Item : in Association); -- Add Item into the translate set. If an association for this variable -- already exists it just replaces it by the new item. procedure Insert (Set : in out Translate_Set; Items : in Translate_Set); -- Add Items into the translate set. If an association for variables in -- Items already exists it just replaces it by the new one. procedure Remove (Set : in out Translate_Set; Name : in String); -- Removes association named Name from the Set. Does nothing if there is -- not such association in the set. function Get (Set : in Translate_Set; Name : in String) return Association; -- Returns the association named Name in the Set. Returns Null_Association -- is no such association if found in Set. function Exists (Set : in Translate_Set; Variable : in String) return Boolean; -- Returns True if an association for Variable exists into the Set generic with procedure Action (Item : in Association; Quit : in out Boolean); procedure For_Every_Association (Set : in Translate_Set); -- Iterates through all associations in the set, call Action for each one. -- Set Quit to True to stop the iteration. function To_Set (Table : in Translate_Table) return Translate_Set; -- Convert a Translate_Table into a Translate_Set ------------- -- Dynamic -- ------------- package Dynamic is -------------- -- Lazy_Tag -- -------------- type Lazy_Tag is abstract tagged private; type Lazy_Tag_Access is access all Lazy_Tag'Class; procedure Value (Lazy_Tag : access Dynamic.Lazy_Tag; Var_Name : in String; Translations : in out Translate_Set) is abstract; -- Value is called by the Parse routines below if a tag variable was not -- found in the set of translations. This routine must then add the -- association for variable Name. It is possible to add other -- associations in the translation table but a check is done to see if -- the variable Name as been set or not. The default implementation does -- nothing. Null_Lazy_Tag : constant Lazy_Tag_Access; ---------------- -- Cursor_Tag -- ---------------- type Cursor_Tag is abstract tagged private; type Cursor_Tag_Access is access all Cursor_Tag'Class; -- In some cases it is difficult and not efficient to have to map all -- Ada data into a template Tag. A Cursor_Tag object gives the ability -- to iterate through a data structure which is living on the Ada side -- only. function Dimension (Cursor_Tag : access Dynamic.Cursor_Tag; Var_Name : in String) return Natural is abstract; -- Must return the number of dimensions for the given variable name. For -- a matrix this routine should return 2 for example. type Path is array (Positive range <>) of Natural; -- A Path gives the full position of a given element in the cursor tag function Length (Cursor_Tag : access Dynamic.Cursor_Tag; Var_Name : in String; Path : in Dynamic.Path) return Natural is abstract; -- Must return the number of item for the given path. The first -- dimension is given by the Path (1), for the second column the Path is -- (1, 2). Note that each dimension can have a different length. For -- example a Matrix is not necessary square. function Value (Cursor_Tag : access Dynamic.Cursor_Tag; Var_Name : in String; Path : in Dynamic.Path) return String is abstract; -- Must return the value for the variable at the given Path. Note that -- this routine will be called only for valid items as given by the -- Dimension and Length above. Null_Cursor_Tag : constant Cursor_Tag_Access; private -- implementation removed end Templates_Parser;
------------------------------------------------------------------------------ -- Templates Parser -- -- -- -- Copyright (C) 2004 - 2005 -- -- AdaCore -- -- -- -- This library is free software; you can redistribute it and/or modify -- -- it under the terms of the GNU General Public License as published by -- -- the Free Software Foundation; either version 2 of the License, or (at -- -- your option) any later version. -- -- -- -- This library is distributed in the hope that it will be useful, but -- -- WITHOUT ANY WARRANTY; without even the implied warranty of -- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- -- General Public License for more details. -- -- -- -- You should have received a copy of the GNU General Public License -- -- along with this library; if not, write to the Free Software Foundation, -- -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -- -- ------------------------------------------------------------------------------ package Templates_Parser.Debug is procedure Print (T : in Tag); -- Print tag representation procedure Print_Tree (Filename : in String); -- Print tree for template Filename end Templates_Parser.Debug;
------------------------------------------------------------------------------ -- Templates Parser -- -- -- -- Copyright (C) 2004 - 2005 -- -- AdaCore -- -- -- -- This library is free software; you can redistribute it and/or modify -- -- it under the terms of the GNU General Public License as published by -- -- the Free Software Foundation; either version 2 of the License, or (at -- -- your option) any later version. -- -- -- -- This library is distributed in the hope that it will be useful, but -- -- WITHOUT ANY WARRANTY; without even the implied warranty of -- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- -- General Public License for more details. -- -- -- -- You should have received a copy of the GNU General Public License -- -- along with this library; if not, write to the Free Software Foundation, -- -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -- -- ------------------------------------------------------------------------------ package Templates_Parser.Utils is function Image (T : in Tag) return String; -- Returns a string representation for this tag function Value (T : in String) return Tag; -- Give a string representation of a tag (as encoded with Image above), -- build the corresponding Tag object. Raises Constraint_Error if T is -- not a valid tag representation. end Templates_Parser.Utils;
------------------------------------------------------------------------------ -- Templates Parser -- -- -- -- Copyright (C) 2004 - 2005 -- -- AdaCore -- -- -- -- This library is free software; you can redistribute it and/or modify -- -- it under the terms of the GNU General Public License as published by -- -- the Free Software Foundation; either version 2 of the License, or (at -- -- your option) any later version. -- -- -- -- This library is distributed in the hope that it will be useful, but -- -- WITHOUT ANY WARRANTY; without even the implied warranty of -- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- -- General Public License for more details. -- -- -- -- You should have received a copy of the GNU General Public License -- -- along with this library; if not, write to the Free Software Foundation, -- -- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -- -- ------------------------------------------------------------------------------ -- This API provides a way to save a Translate_Set as an XML document. -- There is special rules to know about composite tags. -- -- Composite tags : -- -- If a tag named TAG exists, then the name TAG_DESCRIPTION is used as a -- description for this specific tag. -- -- Composite tags (more than one nested level) -- -- If a tag named TAG exists, then the names TAG_DIM[n]_LABELS is used as -- a set of labels for the tag's nth axis. In this case TAG_DIM[n]_LABELS -- must be a vector tag, each entry corresponds to a label on this -- axis. Also TAG_DIM[n]_DESCRIPTION is used as a description for this -- axis. -- -- Here is the DTD : -- -- <?xml version="1.0" encoding="UTF-8"?> -- <!--Description of a tag or dimension (ex: year)--> -- <!ELEMENT Description (#PCDATA)> -- <!--a dimension--> -- <!ELEMENT Dim (Description, Labels)> -- <!ATTLIST Dim -- n CDATA #REQUIRED -- > -- <!--entry of a CompositeTag--> -- <!ELEMENT Entry (ind+, V)> -- <!--label of an indice of a dimension (ex: 2000)--> -- <!ELEMENT Label (#PCDATA)> -- <!ATTLIST Label -- ind CDATA #REQUIRED -- > -- <!--list of labels of one dimension (ex: 1999, 2000, 2001)--> -- <!ELEMENT Labels (Label+)> -- <!--alias and information--> -- <!ELEMENT Tag (Name, Description)> -- <!--tagged data to be published in templates--> -- <!ELEMENT Tagged (SimpleTag*, CompositeTag*)> -- <!--simple variable value--> -- <!ELEMENT V (#PCDATA)> -- <!ELEMENT ind (#PCDATA)> -- <!ATTLIST ind -- n CDATA #REQUIRED -- > -- <!--identification name for this tag--> -- <!ELEMENT Name (#PCDATA)> -- <!--Tag with no dimension (simple variable)--> -- <!ELEMENT SimpleTag (Tag, V)> -- <!--Tag with one or more dimensions--> -- <!ELEMENT CompositeTag (Tag, Dim+, Entry)> package Templates_Parser.XML is function Image (Translations : in Translate_Set) return Unbounded_String; -- Returns a string representation encoded in XML for this -- translate table. function Value (Translations : in String) return Translate_Set; -- Returns a translate set for this string representation function Value (Translations : in Unbounded_String) return Translate_Set; -- Save as above but based on an Ubounded_String function Load (Filename : in String) return Translate_Set; -- Read XML document Filename and create the corresponding Translate_set procedure Save (Filename : in String; Translations : in Translate_Set); -- Write the translate table into filename end Templates_Parser.XML;