ada_asf_86adb194/src/asf-views-nodes-core.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
-----------------------------------------------------------------------
--  nodes-core -- Core nodes
--  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2015, 2018, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Util.Strings.Transforms;
with Util.Files;

with Ada.Exceptions;
with Ada.Strings.Fixed;
with Ada.Strings.Wide_Wide_Fixed;
package body ASF.Views.Nodes.Core is

   --  ------------------------------
   --  Set Tag
   --  ------------------------------

   --  ------------------------------
   --  Create the Set Tag
   --  ------------------------------
   function Create_Set_Tag_Node (Binding    : in Binding_Type;
                                 Line       : in Line_Info;
                                 Parent     : in Tag_Node_Access;
                                 Attributes : in Tag_Attribute_Array_Access)
                                 return Tag_Node_Access is
      Node : constant Set_Tag_Node_Access := new Set_Tag_Node;
   begin
      Initialize (Node.all'Access, Binding, Line, Parent, Attributes);
      Node.Value      := Find_Attribute (Attributes, "value");
      Node.Var        := Find_Attribute (Attributes, "var");
      if Node.Value = null then
         Node.Error ("Missing 'value' attribute");
      end if;
      if Node.Var = null then
         Node.Error ("Missing 'var' attribute");
      end if;
      return Node.all'Access;
   end Create_Set_Tag_Node;

   --  ------------------------------
   --  Build the component tree from the tag node and attach it as
   --  the last child of the given parent.  Calls recursively the
   --  method to create children.
   --  ------------------------------
   overriding
   procedure Build_Components (Node    : access Set_Tag_Node;
                               Parent  : in UIComponent_Access;
                               Context : in out Facelet_Context'Class) is
      pragma Unreferenced (Parent);
   begin
      if Node.Value /= null then
         declare
            Value  : constant EL.Expressions.Expression := Get_Expression (Node.Value.all);
         begin
            Context.Set_Variable (Node.Var.Value, Value);
         end;
      end if;
   end Build_Components;

   --  ------------------------------
   --  If Tag
   --  ------------------------------

   --  ------------------------------
   --  Create the If Tag
   --  ------------------------------
   function Create_If_Tag_Node (Binding    : in Binding_Type;
                                Line       : in Line_Info;
                                Parent     : in Tag_Node_Access;
                                Attributes : in Tag_Attribute_Array_Access)
                                return Tag_Node_Access is
      Node : constant If_Tag_Node_Access := new If_Tag_Node;
   begin
      Initialize (Node.all'Access, Binding, Line, Parent, Attributes);
      Node.Condition  := Find_Attribute (Attributes, "test");
      Node.Var        := Find_Attribute (Attributes, "var");
      if Node.Condition = null then
         Node.Error ("Missing 'test' attribute");
      end if;
      return Node.all'Access;
   end Create_If_Tag_Node;

   --  ------------------------------
   --  Build the component tree from the tag node and attach it as
   --  the last child of the given parent.  Calls recursively the
   --  method to create children.
   --  ------------------------------
   overriding
   procedure Build_Components (Node    : access If_Tag_Node;
                               Parent  : in UIComponent_Access;
                               Context : in out Facelet_Context'Class) is
   begin
      if Node.Condition /= null then
         declare
            Value : constant EL.Objects.Object := Get_Value (Node.Condition.all, Context);
         begin
            if Node.Var /= null then
               Context.Set_Attribute (Node.Var.Value, Value);
            end if;
            if EL.Objects.To_Boolean (Value) then
               Tag_Node (Node.all).Build_Children (Parent, Context);
            end if;
         end;
      end if;
   end Build_Components;

   --  ------------------------------
   --  Choose Tag
   --  ------------------------------

   --  ------------------------------
   --  Create the Choose Tag
   --  ------------------------------
   function Create_Choose_Tag_Node (Binding    : in Binding_Type;
                                    Line       : in Line_Info;
                                    Parent     : in Tag_Node_Access;
                                    Attributes : in Tag_Attribute_Array_Access)
                                    return Tag_Node_Access is
      Node : constant Choose_Tag_Node_Access := new Choose_Tag_Node;
   begin
      Initialize (Node.all'Access, Binding, Line, Parent, Attributes);
      return Node.all'Access;
   end Create_Choose_Tag_Node;

   --  ------------------------------
   --  Freeze the tag node tree and perform any initialization steps
   --  necessary to build the components efficiently.
   --  Prepare the evaluation of choices by identifying the <c:when> and
   --  <c:otherwise> conditions.
   --  ------------------------------
   overriding
   procedure Freeze (Node : access Choose_Tag_Node) is
      Child  : Tag_Node_Access := Node.First_Child;
      Choice : When_Tag_Node_Access := null;
   begin
      while Child /= null loop
         if Child.all in Otherwise_Tag_Node'Class then
            Node.Otherwise := Child;

         elsif Child.all in When_Tag_Node'Class then
            if Choice = null then
               Node.Choices := When_Tag_Node (Child.all)'Access;
            else
               Choice.Next_Choice := When_Tag_Node (Child.all)'Access;
            end if;
            Choice := When_Tag_Node (Child.all)'Access;

         else
            null;
            --  @todo: report a warning in a log
            --  @todo: clean the sub-tree and remove what is not necessary
         end if;
         Child := Child.Next;
      end loop;
   end Freeze;

   --  ------------------------------
   --  Build the component tree from the tag node and attach it as
   --  the last child of the given parent.  Calls recursively the
   --  method to create children.
   --  ------------------------------
   overriding
   procedure Build_Components (Node    : access Choose_Tag_Node;
                               Parent  : in UIComponent_Access;
                               Context : in out Facelet_Context'Class) is
      Choice : When_Tag_Node_Access := Node.Choices;
   begin
      --  Evaluate the choices and stop at the first which succeeds
      while Choice /= null loop
         if Choice.Is_Selected (Context) then
            Choice.Build_Children (Parent, Context);
            return;
         end if;
         Choice := Choice.Next_Choice;
      end loop;

      --  No choice matched, build the otherwise clause.
      if Node.Otherwise /= null then
         Node.Otherwise.Build_Children (Parent, Context);
      end if;
   end Build_Components;

   --  ------------------------------
   --  Create the When Tag
   --  ------------------------------
   function Create_When_Tag_Node (Binding    : in Binding_Type;
                                  Line       : in Line_Info;
                                  Parent     : in Tag_Node_Access;
                                  Attributes : in Tag_Attribute_Array_Access)
                                  return Tag_Node_Access is
      Node : constant When_Tag_Node_Access := new When_Tag_Node;
   begin
      Initialize (Node.all'Access, Binding, Line, Parent, Attributes);
      Node.Condition  := Find_Attribute (Attributes, "test");
      if Node.Condition = null then
         Node.Error ("Missing 'test' attribute");
      end if;
      --        Node.Var        := Find_Attribute (Attributes, "var");
      return Node.all'Access;
   end Create_When_Tag_Node;

   --  ------------------------------
   --  Check whether the node condition is selected.
   --  ------------------------------
   function Is_Selected (Node    : When_Tag_Node;
                         Context : Facelet_Context'Class) return Boolean is
   begin
      if Node.Condition = null then
         return False;
      else
         return EL.Objects.To_Boolean (Get_Value (Node.Condition.all, Context));
      end if;

   exception
      when E : others =>
            Node.Error ("Exception: {0}", Ada.Exceptions.Exception_Message (E));
            return False;
   end Is_Selected;

   --  ------------------------------
   --  Create the Otherwise Tag
   --  ------------------------------
   function Create_Otherwise_Tag_Node (Binding    : in Binding_Type;
                                       Line       : in Line_Info;
                                       Parent     : in Tag_Node_Access;
                                       Attributes : in Tag_Attribute_Array_Access)
                                       return Tag_Node_Access is
      Node : constant Otherwise_Tag_Node_Access := new Otherwise_Tag_Node;
   begin
      Initialize (Node.all'Access, Binding, Line, Parent, Attributes);
      return Node.all'Access;
   end Create_Otherwise_Tag_Node;

   --  Tag names
   CHOOSE_TAG    : aliased constant String := "choose";
   IF_TAG        : aliased constant String := "if";
   OTHERWISE_TAG : aliased constant String := "otherwise";
   SET_TAG       : aliased constant String := "set";
   WHEN_TAG      : aliased constant String := "when";

   --  Name-space URI.  Use the JSTL name-space to make the XHTML views compatible
   --  the JSF.
   URI           : aliased constant String := "http://java.sun.com/jstl/core";

   --  ------------------------------
   --  Tag factory for nodes defined in this package.
   --  ------------------------------
   --  Register the facelets component factory.
   procedure Register (Factory : in out ASF.Factory.Component_Factory) is
   begin
      ASF.Factory.Register (Factory,
                            URI    => URI'Access,
                            Name   => CHOOSE_TAG'Access,
                            Tag    => Create_Choose_Tag_Node'Access,
                            Create => null);
      ASF.Factory.Register (Factory,
                            URI    => URI'Access,
                            Name   => IF_TAG'Access,
                            Tag    => Create_If_Tag_Node'Access,
                            Create => null);
      ASF.Factory.Register (Factory,
                            URI    => URI'Access,
                            Name   => OTHERWISE_TAG'Access,
                            Tag    => Create_Otherwise_Tag_Node'Access,
                            Create => null);
      ASF.Factory.Register (Factory,
                            URI    => URI'Access,
                            Name   => SET_TAG'Access,
                            Tag    => Create_Set_Tag_Node'Access,
                            Create => null);
      ASF.Factory.Register (Factory,
                            URI    => URI'Access,
                            Name   => WHEN_TAG'Access,
                            Tag    => Create_When_Tag_Node'Access,
                            Create => null);
   end Register;

   --  Function names
   CAPITALIZE_FN       : aliased constant String := "capitalize";
   COMPOSE_PATH_FN     : aliased constant String := "composePath";
   CONTAINS_FN         : aliased constant String := "contains";
   ENDS_WITH_FN        : aliased constant String := "endsWith";
   LENGTH_FN           : aliased constant String := "length";
   TO_UPPER_CASE_FN    : aliased constant String := "toUpperCase";
   TO_LOWER_CASE_FN    : aliased constant String := "toLowerCase";
   STARTS_WITH_FN      : aliased constant String := "startsWith";
   SUBSTRING_FN        : aliased constant String := "substring";
   SUBSTRING_AFTER_FN  : aliased constant String := "substringAfter";
   SUBSTRING_BEFORE_FN : aliased constant String := "substringBefore";
   TRIM_FN             : aliased constant String := "trim";
   INDEX_OF_FN         : aliased constant String := "indexOf";
   ESCAPE_XML_FN       : aliased constant String := "escapeXml";
   REPLACE_FN          : aliased constant String := "replace";

   --  JSFL function not implemented
   --     CONTAINS_IGNORE_CASE_FN : aliased constant String := "containsIgnoreCase";
   --     JOIN_FN             : aliased constant String := "join";
   --     SPLIT_FN            : aliased constant String := "split";

   function Length (Value : in EL.Objects.Object) return EL.Objects.Object;
   function Contains (Value : in EL.Objects.Object;
                      Search : in EL.Objects.Object) return EL.Objects.Object;
   function Capitalize (Value : EL.Objects.Object) return EL.Objects.Object;
   function To_Upper_Case (Value : EL.Objects.Object) return EL.Objects.Object;
   function To_Lower_Case (Value : EL.Objects.Object) return EL.Objects.Object;
   function Trim (Value : in EL.Objects.Object) return EL.Objects.Object;
   function Ends_With (Value  : in EL.Objects.Object;
                       Search : in EL.Objects.Object) return EL.Objects.Object;
   function Starts_With (Value  : in EL.Objects.Object;
                         Search : in EL.Objects.Object) return EL.Objects.Object;
   function Index_Of (Value  : in EL.Objects.Object;
                      Search : in EL.Objects.Object) return EL.Objects.Object;

   function Substring_Before (Value : in EL.Objects.Object;
                              Token : in EL.Objects.Object) return EL.Objects.Object;
   function Substring_After (Value : in EL.Objects.Object;
                             Token : in EL.Objects.Object) return EL.Objects.Object;
   function Compose_Path (Paths : in EL.Objects.Object;
                          Dir   : in EL.Objects.Object) return EL.Objects.Object;
   function Index (Value  : in EL.Objects.Object;
                   Search : in EL.Objects.Object) return Natural;
   function Substring (Value  : in EL.Objects.Object;
                       Start  : in EL.Objects.Object;
                       Finish : in EL.Objects.Object) return EL.Objects.Object;

   --  Escapes characters that could be interpreted as XML markup.
   function Escape_Xml (Value : in EL.Objects.Object) return EL.Objects.Object;

   --  Returns a string resulting from replacing in an input string
   --  all occurrences of a "before" string into an "after" substring.
   function Replace (From, Before, After : in EL.Objects.Object) return EL.Objects.Object;

   --  ------------------------------
   --  Escapes characters that could be interpreted as XML markup.
   --  ------------------------------
   function Escape_Xml (Value : in EL.Objects.Object) return EL.Objects.Object is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (Value);
   begin
      case Of_Type is
         when EL.Objects.TYPE_STRING =>
            declare
               S : constant String := EL.Objects.To_String (Value);
            begin
               return EL.Objects.To_Object (Util.Strings.Transforms.Escape_Xml (S));
            end;

         when others =>
            return Value;

      end case;
   end Escape_Xml;

   --  ------------------------------
   --  Get the length of the object.
   --  ------------------------------
   function Length (Value : in EL.Objects.Object) return EL.Objects.Object is
      S : constant String := EL.Objects.To_String (Value);
   begin
      return EL.Objects.To_Object (Integer (S'Length));
   end Length;

   --  ------------------------------
   --  Find the index of the search string in the value.
   --  ------------------------------
   function Index (Value  : in EL.Objects.Object;
                   Search : in EL.Objects.Object) return Natural is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (Value);
   begin
      case Of_Type is
         when EL.Objects.TYPE_NULL =>
            return 0;

         when EL.Objects.TYPE_WIDE_STRING =>
            declare
               S : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Value);
               P : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Search);
            begin
               return Ada.Strings.Wide_Wide_Fixed.Index (S, P);
            end;

         when others =>
            declare
               S : constant String := EL.Objects.To_String (Value);
               P : constant String := EL.Objects.To_String (Search);
            begin
               return Ada.Strings.Fixed.Index (S, P);
            end;

      end case;
   end Index;

   --  ------------------------------
   --  Check if the search string is contained in the value.  If the value is a wide string,
   --  the search string is converted to a wide string and the search is made using wide string.
   --  Otherwise the value and search string are converted to a string.
   --  Returns true if the <b>Search</b> is contained in <b>Value</b>
   --  ------------------------------
   function Contains (Value  : in EL.Objects.Object;
                      Search : in EL.Objects.Object) return EL.Objects.Object is
   begin
      return EL.Objects.To_Object (Index (Value, Search) > 0);
   end Contains;

   --  ------------------------------
   --  Find the index of the search string in the value.
   --  Returns the index true if the <b>Search</b> is contained in <b>Value</b>
   --  ------------------------------
   function Index_Of (Value  : in EL.Objects.Object;
                      Search : in EL.Objects.Object) return EL.Objects.Object is
   begin
      return EL.Objects.To_Object (Index (Value, Search));
   end Index_Of;

   --  ------------------------------
   --  Check if the value starts with the given search string.
   --  Returns true if the <b>Value</b> starts with <b>Search</b>
   --  ------------------------------
   function Starts_With (Value  : in EL.Objects.Object;
                         Search : in EL.Objects.Object) return EL.Objects.Object is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (Value);
   begin
      case Of_Type is
         when EL.Objects.TYPE_NULL =>
            return EL.Objects.To_Object (False);

         when EL.Objects.TYPE_WIDE_STRING =>
            declare
               S : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Value);
               P : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Search);
            begin
               return EL.Objects.To_Object (Ada.Strings.Wide_Wide_Fixed.Index (S, P) = S'First);
            end;

         when others =>
            declare
               S : constant String := EL.Objects.To_String (Value);
               P : constant String := EL.Objects.To_String (Search);
            begin
               return EL.Objects.To_Object (Ada.Strings.Fixed.Index (S, P) = S'First);
            end;

      end case;
   end Starts_With;

   --  ------------------------------
   --  Check if the value ends with the given search string.
   --  Returns true if the <b>Value</b> starts with <b>Search</b>
   --  ------------------------------
   function Ends_With (Value  : in EL.Objects.Object;
                       Search : in EL.Objects.Object) return EL.Objects.Object is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (Value);
   begin
      case Of_Type is
         when EL.Objects.TYPE_NULL =>
            return EL.Objects.To_Object (False);

         when EL.Objects.TYPE_WIDE_STRING =>
            declare
               S : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Value);
               P : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Search);
               I : constant Natural := Ada.Strings.Wide_Wide_Fixed.Index (S, P);
            begin
               return EL.Objects.To_Object (I > 0 and then I + P'Length - 1 = S'Last);
            end;

         when others =>
            declare
               S : constant String := EL.Objects.To_String (Value);
               P : constant String := EL.Objects.To_String (Search);
               I : constant Natural := Ada.Strings.Fixed.Index (S, P);
            begin
               return EL.Objects.To_Object (I > 0 and then I + P'Length - 1 = S'Last);
            end;

      end case;
   end Ends_With;

   --  ------------------------------
   --  Returns the substring starting from the <b>Start</b> index up to the <b>Finish</b>
   --  index inclusive.
   --  ------------------------------
   function Substring (Value  : in EL.Objects.Object;
                       Start  : in EL.Objects.Object;
                       Finish : in EL.Objects.Object) return EL.Objects.Object is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (Value);
   begin
      case Of_Type is
         when EL.Objects.TYPE_NULL =>
            return Value;

         when EL.Objects.TYPE_WIDE_STRING =>
            declare
               S     : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Value);
               First : Natural := EL.Objects.To_Integer (Start);
               Last  : Natural := EL.Objects.To_Integer (Finish);
            begin
               if First <= S'First then
                  First := S'First;
               end if;
               if Last >= S'Last then
                  Last := S'Last;
               end if;
               return EL.Objects.To_Object (S (First .. Last));
            end;

         when others =>
            declare
               S : constant String := EL.Objects.To_String (Value);
               First : Natural := EL.Objects.To_Integer (Start);
               Last  : Natural := EL.Objects.To_Integer (Finish);
            begin
               if First <= S'First then
                  First := S'First;
               end if;
               if Last >= S'Last then
                  Last := S'Last;
               end if;
               return EL.Objects.To_Object (S (First .. Last));
            end;

      end case;

   exception
      when others =>
         return Value;
   end Substring;

   function Capitalize (Value : EL.Objects.Object) return EL.Objects.Object is
      S : constant String := EL.Objects.To_String (Value);
   begin
      return EL.Objects.To_Object (Util.Strings.Transforms.Capitalize (S));
   end Capitalize;

   function To_Upper_Case (Value : EL.Objects.Object) return EL.Objects.Object is
      S : constant String := EL.Objects.To_String (Value);
   begin
      return EL.Objects.To_Object (Util.Strings.Transforms.To_Upper_Case (S));
   end To_Upper_Case;

   function To_Lower_Case (Value : EL.Objects.Object) return EL.Objects.Object is
      S : constant String := EL.Objects.To_String (Value);
   begin
      return EL.Objects.To_Object (Util.Strings.Transforms.To_Lower_Case (S));
   end To_Lower_Case;

   --  ------------------------------
   --  Trim the white spaces at beginning and end of the string.
   --  ------------------------------
   function Trim (Value : in EL.Objects.Object) return EL.Objects.Object is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (Value);
   begin
      case Of_Type is
         when EL.Objects.TYPE_STRING =>
            declare
               S : constant String := EL.Objects.To_String (Value);
            begin
               return EL.Objects.To_Object (Ada.Strings.Fixed.Trim (S, Ada.Strings.Both));
            end;

         when EL.Objects.TYPE_WIDE_STRING =>
            declare
               S : constant Wide_Wide_String := EL.Objects.To_Wide_Wide_String (Value);
            begin
               return EL.Objects.To_Object
                 (Ada.Strings.Wide_Wide_Fixed.Trim (S, Ada.Strings.Both));
            end;

         when others =>
            return Value;
      end case;
   end Trim;

   --  ------------------------------
   --  Returns a string resulting from replacing in an input string
   --  all occurrences of a "before" string into an "after" substring.
   --  ------------------------------
   function Replace (From, Before, After : in EL.Objects.Object) return EL.Objects.Object is
      Of_Type : constant EL.Objects.Data_Type := EL.Objects.Get_Type (From);
   begin
      case Of_Type is
         when EL.Objects.TYPE_NULL =>
            return From;

         when others =>
            declare
               S : Unbounded_String := EL.Objects.To_Unbounded_String (From);
               B : constant String := EL.Objects.To_String (Before);
               A : constant String := EL.Objects.To_String (After);
               I : Natural := 1;
            begin
               loop
                  I := Ada.Strings.Unbounded.Index (S, B, I);
                  exit when I = 0;
                  Ada.Strings.Unbounded.Replace_Slice (S, I, I + B'Length - 1, A);
                  I := I + B'Length;
               end loop;
               return EL.Objects.To_Object (S);
            end;

      end case;
   end Replace;

   --  ------------------------------
   --  Return the substring before the token string
   --  ------------------------------
   function Substring_Before (Value : in EL.Objects.Object;
                              Token : in EL.Objects.Object) return EL.Objects.Object is
      S   : constant String := EL.Objects.To_String (Value);
      T   : constant String := EL.Objects.To_String (Token);
      Pos : constant Natural := Ada.Strings.Fixed.Index (S, T);
   begin
      if Pos = 0 then
         return EL.Objects.Null_Object;
      else
         return EL.Objects.To_Object (S (S'First .. Pos - 1));
      end if;
   end Substring_Before;

   --  ------------------------------
   --  Return the substring after the token string
   --  ------------------------------
   function Substring_After (Value : in EL.Objects.Object;
                              Token : in EL.Objects.Object) return EL.Objects.Object is
      S   : constant String := EL.Objects.To_String (Value);
      T   : constant String := EL.Objects.To_String (Token);
      Pos : constant Natural := Ada.Strings.Fixed.Index (S, T);
   begin
      if Pos = 0 then
         return EL.Objects.Null_Object;
      else
         return EL.Objects.To_Object (S (Pos + T'Length .. S'Last));
      end if;
   end Substring_After;

   --  ------------------------------
   --  Expand the search paths <b>Paths</b> into a new search path which adds the
   --  directory component <b>Dir</b>.  Example:
   --    Paths= /usr;/usr/local;/opt
   --    Dir  = bin
   --    Result= /usr/bin;/usr/local/bin;/opt/bin
   --  ------------------------------
   function Compose_Path (Paths : in EL.Objects.Object;
                          Dir   : in EL.Objects.Object) return EL.Objects.Object is
      P   : constant String := EL.Objects.To_String (Paths);
      D   : constant String := EL.Objects.To_String (Dir);
      R   : constant String := Util.Files.Compose_Path (P, D);
   begin
      return EL.Objects.To_Object (R);
   end Compose_Path;

   --  ------------------------------
   --  Register a set of functions in the namespace
   --  xmlns:fn="http://java.sun.com/jsp/jstl/functions"
   --  Functions:
   --    capitalize, toUpperCase, toLowerCase
   --  ------------------------------
   procedure Set_Functions (Mapper : in out EL.Functions.Function_Mapper'Class) is
   begin
      Mapper.Set_Function (Name      => STARTS_WITH_FN,
                           Namespace => FN_URI,
                           Func      => Starts_With'Access);
      Mapper.Set_Function (Name      => ENDS_WITH_FN,
                           Namespace => FN_URI,
                           Func      => Ends_With'Access);
      Mapper.Set_Function (Name      => INDEX_OF_FN,
                           Namespace => FN_URI,
                           Func      => Index_Of'Access);
      Mapper.Set_Function (Name      => SUBSTRING_FN,
                           Namespace => FN_URI,
                           Func      => Substring'Access);

      Mapper.Set_Function (Name      => CAPITALIZE_FN,
                           Namespace => FN_URI,
                           Func      => Capitalize'Access);
      Mapper.Set_Function (Name      => CONTAINS_FN,
                           Namespace => FN_URI,
                           Func      => Contains'Access);
      Mapper.Set_Function (Name      => LENGTH_FN,
                           Namespace => FN_URI,
                           Func      => Length'Access);
      Mapper.Set_Function (Name      => TO_LOWER_CASE_FN,
                           Namespace => FN_URI,
                           Func      => To_Lower_Case'Access);
      Mapper.Set_Function (Name      => TO_UPPER_CASE_FN,
                           Namespace => FN_URI,
                           Func      => To_Upper_Case'Access);
      Mapper.Set_Function (Name      => SUBSTRING_BEFORE_FN,
                           Namespace => FN_URI,
                           Func      => Substring_Before'Access);
      Mapper.Set_Function (Name      => TRIM_FN,
                           Namespace => FN_URI,
                           Func      => Trim'Access);
      Mapper.Set_Function (Name      => SUBSTRING_AFTER_FN,
                           Namespace => FN_URI,
                           Func      => Substring_After'Access);
      Mapper.Set_Function (Name      => COMPOSE_PATH_FN,
                           Namespace => FN_URI,
                           Func      => Compose_Path'Access);

      Mapper.Set_Function (Name      => ESCAPE_XML_FN,
                           Namespace => FN_URI,
                           Func      => Escape_Xml'Access);

      Mapper.Set_Function (Name      => REPLACE_FN,
                           Namespace => FN_URI,
                           Func      => Replace'Access);
   end Set_Functions;

end ASF.Views.Nodes.Core;