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
|
------------------------------------------------------------------------
-- --
-- McKae Software Utilities --
-- --
-- Copyright (C) 2005-2009 McKae Technologies --
-- --
-- The McKae software utilities are free software; you can --
-- redistribute it and/or modify it under terms of the GNU General --
-- Public License as published by the Free Software Foundation; --
-- either version 2, or (at your option) any later version. McKae --
-- Software Utilities are distributed in the hope that they 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 distributed --
-- with DTraq; see file COPYING. If not, write to the Free Software --
-- Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, --
-- USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
-- --
-- The McKae Software Utilities are maintained by McKae Technologies --
-- (http://www.mckae.com). --
------------------------------------------------------------------------
with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
package body McKae.XML.EZ_Out.Generic_Medium is
------------------------------------------------------------------------
-- A very basic bounded stack implementation for keeping track of
-- nested XML elements.
type Stack_Size is new Natural range 0 .. Max_Element_Nesting;
subtype Stack_Indices is Stack_Size range 1 .. Stack_Size'Last;
type Element_Stacks is array (Stack_Indices) of Unbounded_String;
Tag_Stack : Element_Stacks;
Top_Of_Stack : Stack_Size := 0;
procedure Push (Tag : Unbounded_String);
procedure Push (Tag : Unbounded_String) is
begin
if Top_Of_Stack /= Stack_Size'Last then
Top_Of_Stack := Top_Of_Stack + 1;
Tag_Stack (Top_Of_Stack) := Tag;
else
raise Nesting_Too_Deep;
end if;
end Push;
procedure Pop (Tag : out Unbounded_String);
procedure Pop (Tag : out Unbounded_String) is
begin
if Top_Of_Stack /= 0 then
Tag := Tag_Stack (Top_Of_Stack);
Top_Of_Stack := Top_Of_Stack - 1;
else
raise Element_Not_Open;
end if;
end Pop;
------------------------------------------------------------------------
type XML_Component_Kind is (Header_Component,
Start_Tag_Component,
Content_Component,
End_Tag_Component);
------------------------------------------------------------------------
-- Constructed Put_Line from provided primitives
procedure Put_Line (F : in Output_Medium;
S : in String);
procedure Put_Line (F : in Output_Medium;
S : in String) is
begin
Put (F, S);
New_Line (F);
end Put_Line;
------------------------------------------------------------------------
procedure Replace_Special (C : in String;
R : in String;
S : in out Unbounded_String);
procedure Replace_Special (C : in String;
R : in String;
S : in out Unbounded_String) is
P : Natural := 0;
begin
if Index (R, C) /= 0 then
-- The string to be replaced is present within the replacing
-- string (e.g., "&" by "&"), so the replacement has to
-- take this into account.
P := 1;
while (P + C'Length - 1) <= Length (S) loop
if Slice (S, P, P + C'Length - 1) = C then
Replace_Slice (S, P, P + C'Length - 1, R);
P := P + R'Length; -- Skip over replacement string
else
P := P + 1;
end if;
end loop;
else
-- The string to be replaced is not present within the
-- replacing string, so a simple find and replace can be
-- done.
loop
P := Index (S, C);
exit when P = 0;
Replace_Slice (S, P, P + C'Length - 1, R);
end loop;
end if;
end Replace_Special;
------------------------------------------------------------------------
function Replace_Specials (S : Unbounded_String;
Subst : Boolean;
Replace_Quotes : Boolean := False;
Replace_Apos : Boolean := False)
return Unbounded_String;
function Replace_Specials (S : Unbounded_String;
Subst : Boolean;
Replace_Quotes : Boolean := False;
Replace_Apos : Boolean := False)
return Unbounded_String is
New_S : Unbounded_String := S;
begin
if Subst then
-- Ampersands must be replaced first, since the replacement
-- strings contain ampersands
Replace_Special ("&", "&", New_S);
Replace_Special ("<", "<", New_S);
Replace_Special ("]]>", "]]>", New_S);
if Replace_Quotes then
Replace_Special ("""", """, New_S);
end if;
if Replace_Apos then
Replace_Special ("'", "'", New_S);
end if;
end if;
return New_S;
end Replace_Specials;
------------------------------------------------------------------------
-- Output the string in accordance with the specified format option.
procedure Formatted_Put (F : in Output_Medium;
S : in Unbounded_String;
K : in XML_Component_Kind);
procedure Formatted_Put (F : in Output_Medium;
S : in Unbounded_String;
K : in XML_Component_Kind) is
-- The number of items in the element nesting stack is directly
-- proportional to the amount of required indenting
Indentation : constant Natural := Natural (Top_Of_Stack) * 3;
Value : constant String := To_String (S);
begin
case K is
when Header_Component =>
pragma Assert (Top_Of_Stack = 0);
case Current_Format is
when Continuous_Stream =>
Put (F, Value);
when Spread_Indented =>
Put_Line (F, Value);
end case;
when Start_Tag_Component =>
case Current_Format is
when Continuous_Stream =>
Put (F, Value);
when Spread_Indented =>
Put (F, Indentation * ' ');
Put_Line (F, Value);
end case;
when Content_Component =>
case Current_Format is
when Continuous_Stream =>
Put (F, Value);
when Spread_Indented =>
Put (F, Indentation * ' ');
Put_Line (F, Value);
end case;
when End_Tag_Component =>
case Current_Format is
when Continuous_Stream =>
Put (F, Value);
when Spread_Indented =>
Put (F, Indentation * ' ');
Put_Line (F, Value);
end case;
end case;
end Formatted_Put;
------------------------------------------------------------------------
-- Output a standard XML header line, as amended by the supplied
-- arguments. To omit the attribute, pass an empty string.
-- <?xml version="1.0" encoding="UTF-8" ?>
procedure Output_XML_Header (F : in Output_Medium;
Standalone : in Standalone_Values := Omit;
Encoding : in String := "UTF-8";
Version : in String := "1.0") is
Header : Unbounded_String := To_Unbounded_String ("<?xml");
begin
if Top_Of_Stack = 0 then
if Version /= "" then
Append (Header, " version=""" & Version & """");
end if;
if Encoding /= "" then
Append (Header, " encoding=""" & Encoding & """");
end if;
if Standalone /= Omit then
Append (Header, " standalone=""");
if Standalone = Yes then
Append (Header, "yes""");
else
Append (Header, "no""");
end if;
end if;
Append (Header, " ?>");
Formatted_Put (F, Header, Header_Component);
else
raise Invalid_Construction;
end if;
end Output_XML_Header;
------------------------------------------------------------------------
-- Add a processing instruction to the XML document.
procedure Output_Processing_Instruction (F : in Output_Medium;
Target : in String;
Data : in String) is
begin
if Top_Of_Stack = 0 then
Formatted_Put
(F,
To_Unbounded_String ("<?" & Target & " " & Data & " ?>"),
Header_Component);
else
raise Invalid_Construction;
end if;
end Output_Processing_Instruction;
------------------------------------------------------------------------
-- Generate an entire element designated with the given tag and
-- containing the provided content and list of attributes
procedure Output_Element (F : in Output_Medium;
Tag : in String;
Content : in String;
Attrs : in Attributes_List := No_Attributes;
Subst : in Boolean := True) is
Tag_Start : Unbounded_String := "<" & To_Unbounded_String (Tag);
Tag_End : constant Unbounded_String :=
"</" & To_Unbounded_String (Tag) & ">";
begin
if Attrs /= No_Attributes then
for A in Attrs'Range loop
if (Attrs (A).Value /= Null_Unbounded_String)
or Default_Output_Null_Attributes
then
Append (Tag_Start,
" " & Attrs (A).Attr &
"=""" &
Replace_Specials (Attrs (A).Value, Subst,
Replace_Quotes => True,
Replace_Apos => True) & """");
end if;
end loop;
end if;
Append (Tag_Start, ">");
Formatted_Put (F, Tag_Start, Start_Tag_Component);
Formatted_Put (F,
Replace_Specials (To_Unbounded_String (Content), Subst),
Content_Component);
Formatted_Put (F, Tag_End, End_Tag_Component);
end Output_Element;
------------------------------------------------------------------------
-- Generate an entire element designated with the given tag and
-- containing the provided content single attribute specification
procedure Output_Element (F : in Output_Medium;
Tag : in String;
Content : in String;
Attrs : in Attribute_Value_Pairs;
Subst : in Boolean := True) is
begin
Output_Element (F, Tag, Content,
Attributes_List'(1 => Attrs),
Subst);
end Output_Element;
------------------------------------------------------------------------
-- Generate an entire element designated with the given tag and
-- containing zero or more attributes. By default the element is
-- created using the compact, no-end-tag notation; to force
-- generation of an element that has both start and end tags and
-- no content, set End_Tag to True.
procedure Output_Tag (F : in Output_Medium;
Tag : in String;
Attrs : in Attributes_List := No_Attributes;
End_Tag : in Boolean := False;
Subst : in Boolean := True) is
Tag_Start : Unbounded_String := "<" & To_Unbounded_String (Tag);
Tag_End : constant Unbounded_String :=
"</" & To_Unbounded_String (Tag) & ">";
begin
if Attrs /= No_Attributes then
for A in Attrs'Range loop
if (Attrs (A).Value /= Null_Unbounded_String)
or Default_Output_Null_Attributes
then
Append (Tag_Start,
" " & Attrs (A).Attr &
"=""" &
Replace_Specials (Attrs (A).Value, Subst,
Replace_Quotes => True,
Replace_Apos => True) & """");
end if;
end loop;
end if;
if End_Tag then
Append (Tag_Start, ">");
Formatted_Put (F, Tag_Start, Start_Tag_Component);
Formatted_Put (F, Tag_End, End_Tag_Component);
else
Append (Tag_Start, "/>");
Formatted_Put (F, Tag_Start, Start_Tag_Component);
end if;
end Output_Tag;
------------------------------------------------------------------------
-- Generate an element tag with a single attribute specElementification.
-- By default the element is created using the compact, no-end-tag
-- notation; to force generation of an element that has both start
-- and end tags and no content, set End_Tag to True.
procedure Output_Tag (F : in Output_Medium;
Tag : in String;
Attrs : in Attribute_Value_Pairs;
End_Tag : in Boolean := False;
Subst : in Boolean := True) is
begin
Output_Tag (F, Tag, Attributes_List'(1 => Attrs), End_Tag, Subst);
end Output_Tag;
------------------------------------------------------------------------
-- Initiate the generation of an XML element with the given tag and
-- zero or more attribute specifications using an Attributes_List
-- initializing aggregate. If there is only one attribute to be
-- specified, the single attribute version of Start_Element may be
-- used instead so as to avoid having to use named notation to
-- specify the single element of the list.
procedure Start_Element (F : in Output_Medium;
Tag : in String;
Attrs : in Attributes_List := No_Attributes;
Subst : in Boolean := True) is
Tag_Start : Unbounded_String := "<" & To_Unbounded_String (Tag);
begin
-- First output the tag and any attributes.
if Attrs /= No_Attributes then
for A in Attrs'Range loop
if (Attrs (A).Value /= Null_Unbounded_String)
or Default_Output_Null_Attributes
then
Append (Tag_Start,
" " & Attrs (A).Attr &
"=""" &
Replace_Specials (Attrs (A).Value, Subst,
Replace_Quotes => True,
Replace_Apos => True) & """");
end if;
end loop;
end if;
Append (Tag_Start, ">");
Formatted_Put (F, Tag_Start, Start_Tag_Component);
Push (To_Unbounded_String (Tag));
end Start_Element;
------------------------------------------------------------------------
-- Initiate the generation of an XML element with the given tag and
-- a single attribute specification.
procedure Start_Element (F : in Output_Medium;
Tag : in String;
Attrs : in Attribute_Value_Pairs;
Subst : in Boolean := True) is
begin
Start_Element (F, Tag, Attributes_List'(1 => Attrs), Subst);
end Start_Element;
------------------------------------------------------------------------
-- Indicate the completion of the output of an XML element. If a
-- Tag is specified, compare it against the element tag that is
-- currently open, and raise Element_End_Mismatch if the two do
-- not match. If there is no open element, then raise
-- Element_Not_Open.
procedure End_Element (F : in Output_Medium;
Tag : in String := "") is
Open_Tag : Unbounded_String;
begin
Pop (Open_Tag);
-- Validate the tag only if one was supplied
if (Tag = "") or else (Tag = To_String (Open_Tag)) then
Formatted_Put (F,
"</" & Open_Tag & ">",
End_Tag_Component);
else
raise Element_End_Mismatch;
end if;
end End_Element;
------------------------------------------------------------------------
-- Place the text, as is, as the content of the currently open XML
-- element. Output_Content can be called repeatedly, and will
-- simply continue to append the additional content. If there is
-- no open element, raise Element_Not_Open.
procedure Output_Content (F : in Output_Medium;
S : in String;
Subst : in Boolean := True) is
begin
Formatted_Put (F, Replace_Specials (To_Unbounded_String (S), Subst),
Content_Component);
end Output_Content;
------------------------------------------------------------------------
-- Place the numeric Value, as a base 10 text representation, as
-- the content of the currently open XML element. Output_Content
-- can be called repeatedly, and will simply continue to append
-- the additional content. If there is no open element, raise
-- Element_Not_Open.
procedure Output_Content (F : in Output_Medium;
N : in Integer'Base) is
N_Rep : constant String := Integer'Base'Image (N);
Start_Index : constant Positive := Boolean'Pos (N >= 0) + 1;
begin
Output_Content (F, N_Rep (Start_Index .. N_Rep'Length));
end Output_Content;
------------------------------------------------------------------------
-- Place the text represenatation of the numeric Value as the
-- content of the currently open XML element. Output_Content can
-- be called repeatedly, and will simply continue to append the
-- additional content. If there is no open element, raise
-- Element_Not_Open.
procedure Output_Content (F : in Output_Medium;
N : in Float) is
N_Rep : constant String := Float'Image (N);
Start_Index : constant Positive := Boolean'Pos (N >= 0.0) + 1;
begin
Output_Content (F, N_Rep (Start_Index .. N_Rep'Length));
end Output_Content;
------------------------------------------------------------------------
-- The following overloaded "=" functions are the only means by
-- which to create attribute/Value pairs.
-- Attribute provided as String
-- Associate an attribute with a string Value.
function "=" (Attr : String;
Value : String)
return Attribute_Value_Pairs is
begin
return To_Unbounded_String (Attr) = To_Unbounded_String (Value);
end "=";
-- Associate an attribute with a string Value.
function "=" (Attr : String;
Value : Character)
return Attribute_Value_Pairs is
begin
return To_Unbounded_String (Attr) = To_Unbounded_String ((1 => Value));
end "=";
-- Associate an attribute with a string Value.
function "=" (Attr : String;
Value : Unbounded_String)
return Attribute_Value_Pairs is
begin
return To_Unbounded_String (Attr) = Value;
end "=";
-- Associate an attribute with an integral Value.
function "=" (Attr : String;
Value : Integer'Base)
return Attribute_Value_Pairs is
Value_Rep : constant String := Integer'Base'Image (Value);
Is_Natural : constant Boolean := Value >= 0;
begin
if Is_Natural then
return Attr = Value_Rep (2 .. Value_Rep'Last);
else
return Attr = Value_Rep;
end if;
end "=";
-- Associate an attribute with a floating point Value.
function "=" (Attr : String;
Value : Float)
return Attribute_Value_Pairs is
Value_Rep : constant String := Float'Image (Value);
Is_Nonnegative : constant Boolean := Value >= 0.0;
begin
if Is_Nonnegative then
return Attr = Value_Rep (2 .. Value_Rep'Last);
else
return Attr = Value_Rep;
end if;
end "=";
-- Attribute provided as Unbounded_String
-- Associate an attribute with a string Value.
function "=" (Attr : Unbounded_String;
Value : String)
return Attribute_Value_Pairs is
begin
return Attr = To_Unbounded_String (Value);
end "=";
-- Associate an attribute with a string Value.
function "=" (Attr : Unbounded_String;
Value : Character)
return Attribute_Value_Pairs is
begin
return Attr = To_Unbounded_String ((1 => Value));
end "=";
-- Associate an attribute with a string Value.
function "=" (Attr : Unbounded_String;
Value : Unbounded_String)
return Attribute_Value_Pairs is
begin
return (Attr, Value);
end "=";
-- Associate an attribute with an integral Value.
function "=" (Attr : Unbounded_String;
Value : Integer'Base)
return Attribute_Value_Pairs is
Value_Rep : constant String := Integer'Base'Image (Value);
Is_Natural : constant Boolean := Value >= 0;
begin
if Is_Natural then
return Attr = To_Unbounded_String (Value_Rep (2 .. Value_Rep'Last));
else
return Attr = To_Unbounded_String (Value_Rep);
end if;
end "=";
-- Associate an attribute with a floating point Value.
function "=" (Attr : Unbounded_String;
Value : Float)
return Attribute_Value_Pairs is
Value_Rep : constant String := Float'Image (Value);
Is_Nonnegative : constant Boolean := Value >= 0.0;
begin
if Is_Nonnegative then
return Attr = To_Unbounded_String (Value_Rep (2 .. Value_Rep'Last));
else
return Attr = To_Unbounded_String (Value_Rep);
end if;
end "=";
end McKae.XML.EZ_Out.Generic_Medium;
|