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
|
-- This file demonstrates how to preload multiple independent XSD files, and
-- then reuse them to validate multiple XML files.
-- Which XSD file to use for a given XML file is not known in advance, but
-- given via attributes in the XML files themselves.
-- In the XSD files were using namespaces, we would not need to do any
-- special setup. Just load them all in the same XML_Grammar, and use that
-- common grammar to validate the XML files.
-- In this case, though, the XML files do not use namespaces, so the XSD
-- have to be loaded in their own XML_Grammar, or we would end up with
-- conflicts.
with Ada.Text_IO; use Ada.Text_IO;
with Sax.Readers; use Sax.Readers;
with Sax.Symbols; use Sax.Symbols;
with Sax.Utils; use Sax.Utils;
with Schema.Validators; use Schema.Validators;
with Schema.Readers; use Schema.Readers;
with Schema.Schema_Readers; use Schema.Schema_Readers;
with Input_Sources.File; use Input_Sources.File;
procedure Validate is
type Reader_With_Preloaded_XSD is new Validating_Reader with record
G1, G2 : XML_Grammar;
end record;
overriding procedure Parse_Grammar
(Self : not null access Reader_With_Preloaded_XSD;
URI, Xsd_File : Sax.Symbols.Symbol;
Do_Create_NFA : Boolean := True);
overriding procedure Parse_Grammar
(Self : not null access Reader_With_Preloaded_XSD;
URI, Xsd_File : Sax.Symbols.Symbol;
Do_Create_NFA : Boolean := True) is
begin
if Xsd_File = "algo1.xsd" then
Self.Set_Grammar (Self.G1);
elsif Xsd_File = "algo2.xsd" then
Self.Set_Grammar (Self.G2);
else
-- Call the inherited procedure
Validating_Reader (Self.all).Parse_Grammar
(URI, Xsd_File, Do_Create_NFA);
end if;
end Parse_Grammar;
Symbols : Symbol_Table;
F : File_Input;
S : Schema_Reader;
R : Reader_With_Preloaded_XSD;
begin
-- We need to share the symbol table between all involved parsers and
-- grammars. These symbol tables are used to efficiently store internal
-- strings.
Symbols := Allocate;
S.Set_Symbol_Table (Symbols);
R.Set_Symbol_Table (Symbols);
-- Load multiple grammars. We need to reset the grammar before parsing
-- each new file, or the files will be merged and might conflict with
-- each other.
Open ("algo1.xsd", F);
S.Parse (F);
F.Close;
R.G1 := S.Get_Grammar;
Open ("algo2.xsd", F);
S.Set_Grammar (No_Grammar);
S.Parse (F);
F.Close;
R.G2 := S.Get_Grammar;
-- Now parse all the XML files, and let the reader chose which grammar
-- to use via the overriding of Parse_Grammar
R.Set_Feature (Schema_Validation_Feature, True);
Open ("test1.xml", F);
R.Parse (F);
F.Close;
Open ("test2.xml", F);
R.Parse (F);
F.Close;
exception
when Xml_Validation_Error =>
Put_Line ("Error: " & R.Get_Error_Message & S.Get_Error_Message);
end Validate;
|