1------------------------------------------------------------------------------
2--                     XML/Ada - An XML suite for Ada95                     --
3--                                                                          --
4--                     Copyright (C) 2005-2017, AdaCore                     --
5--                                                                          --
6-- This library is free software;  you can redistribute it and/or modify it --
7-- under terms of the  GNU General Public License  as published by the Free --
8-- Software  Foundation;  either version 3,  or (at your  option) any later --
9-- version. This library is distributed in the hope that it will be useful, --
10-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
11-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
12--                                                                          --
13-- As a special exception under Section 7 of GPL version 3, you are granted --
14-- additional permissions described in the GCC Runtime Library Exception,   --
15-- version 3.1, as published by the Free Software Foundation.               --
16--                                                                          --
17-- You should have received a copy of the GNU General Public License and    --
18-- a copy of the GCC Runtime Library Exception along with this program;     --
19-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
20-- <http://www.gnu.org/licenses/>.                                          --
21--                                                                          --
22------------------------------------------------------------------------------
23
24--  This package implements the content models as described in the DTDs.
25--  They are not strictly part of the SAX 2.0 standard, however they are
26--  used to simply the handling in users' applications.
27
28with Sax.Symbols;
29with Unicode.CES;
30
31package Sax.Models is
32
33   type Content_Spec is
34     (Character_Data,   --  Characters, but no child node  (#PCDATA)
35      Element_Ref,      --  A specific child
36      Any_Of,           --  child is one of many choices
37      Sequence,         --  a sequence of elements (order is imposed)
38      Repeat,           --  A repeated element
39      Empty,            --  Element must be empty  (EMPTY)
40      Anything          --  Content is not described, and can be anything (ANY)
41     );
42
43   type Element_Model;
44   type Element_Model_Ptr is access Element_Model;
45   type Element_Model_Array is array (Natural range <>) of Element_Model_Ptr;
46   type Element_Model_Array_Ptr is access Element_Model_Array;
47
48   type Element_Model (Content : Content_Spec) is record
49      case Content is
50         when Character_Data | Empty | Anything => null;
51
52         when Element_Ref =>
53            Name : Sax.Symbols.Symbol; --  Name of the element
54
55         when Any_Of | Sequence =>
56            List : Element_Model_Array_Ptr; --  all the possible choices
57
58         when Repeat =>
59            Min : Natural;
60            Max : Positive;
61            Elem : Element_Model_Ptr;
62      end case;
63   end record;
64   --  Type used to describe the model used for an element, as described in
65   --  the DTD (see 3.2.* in XML specifications). For instance, the following
66   --  model "(#PCDATA|emph)*" is translated to:
67   --     (Content => Repeat,
68   --      Min     => 0,
69   --      Max     => Positive'Last,
70   --      Elem    => (Content => Any_Of,
71   --                  Choices => (0 => (Content => Character_Data),
72   --                              1 => (Content => Element,
73   --                                    Name    => "emp"))))
74
75   type Content_Model is private;
76   Unknown_Model : constant Content_Model;
77   --  This is a reference counted type that represents a content model defined
78   --  in an XML document's DTD.
79
80   procedure Ref   (Model : Content_Model);
81   procedure Unref (Model : in out Content_Model);
82   --  Increment or decrement the reference counting for Model.
83   --  When the reference counting reaches 0, the model is freed. You need to
84   --  call these methods automatically if you intend to keep a copy of the
85   --  model in your own structures.
86
87   function Create_Model (Element : Element_Model_Ptr) return Content_Model;
88   --  Create a content model based on a description.
89   --  No copy of Element is done, and the returned content model becomes
90   --  responsible for freeing that data structure when no longer needed
91
92   procedure Free (Model : in out Element_Model_Ptr);
93   --  Free the memory allocated for the model.
94
95   function Get_Element_Model
96     (Model : Content_Model) return Element_Model_Ptr;
97   --  Return a description of the content model. Do not free the resulting
98   --  pointer, since this points directly into the Content_Model structure
99
100   function To_String
101     (Model : Content_Model) return Unicode.CES.Byte_Sequence;
102   --  Return the string to put in an XML file to describe Model
103   --  Invalid_Content_Model is raised if Model can not be described in a
104   --  DTD.
105
106   function Is_Mixed (M : Element_Model_Ptr) return Boolean;
107   --  Return True if M represents a Mixed content model (3.2.2 in XML
108   --  specifications).
109
110   Invalid_Content_Model : exception;
111   --  Raised by To_String, when the model is invalid
112
113private
114
115   type Model_Item;
116   type Model_List is access Model_Item;
117   type Model_Item is record
118      State : Element_Model_Ptr;
119      Next  : Model_List;
120   end record;
121
122   type Natural_Access is access Natural;
123   type Content_Model is record
124      Ref_Count : Natural_Access;
125      Model     : Element_Model_Ptr;
126   end record;
127
128   Unknown_Model : constant Content_Model := (null, null);
129
130end Sax.Models;
131