1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--                               XML Processor                              --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2013, Vadim Godunko <vgodunko@gmail.com>                     --
12-- All rights reserved.                                                     --
13--                                                                          --
14-- Redistribution and use in source and binary forms, with or without       --
15-- modification, are permitted provided that the following conditions       --
16-- are met:                                                                 --
17--                                                                          --
18--  * Redistributions of source code must retain the above copyright        --
19--    notice, this list of conditions and the following disclaimer.         --
20--                                                                          --
21--  * Redistributions in binary form must reproduce the above copyright     --
22--    notice, this list of conditions and the following disclaimer in the   --
23--    documentation and/or other materials provided with the distribution.  --
24--                                                                          --
25--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
26--    contributors may be used to endorse or promote products derived from  --
27--    this software without specific prior written permission.              --
28--                                                                          --
29-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
30-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
31-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
32-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
33-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
34-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
35-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
36-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
37-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
38-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
39-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
40--                                                                          --
41------------------------------------------------------------------------------
42--  $Revision: 4112 $ $Date: 2013-09-30 13:14:15 +0400 (Mon, 30 Sep 2013) $
43------------------------------------------------------------------------------
44private with Ada.Containers.Hashed_Maps;
45private with Ada.Containers.Vectors;
46private with Ada.Finalization;
47
48with League.String_Vectors;
49with League.Strings;
50private with League.Strings.Hash;
51
52package XML.Utilities.Namespace_Supports is
53
54   pragma Preelaborate;
55
56   type XML_Namespace_Support is tagged private;
57
58   type Component_Kinds is (Attribute, Element);
59
60   --  Process_Name
61
62   procedure Declare_Prefix
63    (Self          : in out XML_Namespace_Support'Class;
64     Prefix        : League.Strings.Universal_String;
65     Namespace_URI : League.Strings.Universal_String);
66   --  This procedure declares a prefix in the future namespace context to be
67   --  namespace uri. The prefix is activated then context is pushed and
68   --  remains in force until this context is popped, unless it is shadowed in
69   --  a descendant context.
70
71   function Namespace_URI
72    (Self      : XML_Namespace_Support'Class;
73     Prefix    : League.Strings.Universal_String;
74     Component : Component_Kinds := Element)
75       return League.Strings.Universal_String;
76   --  Looks up the prefix prefix in the current context and returns the
77   --  currently-mapped namespace URI. Use the empty string ("") for the
78   --  default namespace.
79
80   function Prefix
81    (Self          : XML_Namespace_Support'Class;
82     Namespace_URI : League.Strings.Universal_String;
83     Component     : Component_Kinds := Element)
84       return League.Strings.Universal_String;
85   --  Returns one of the prefixes mapped to the namespace URI.
86   --
87   --  If more than one prefix is currently mapped to the same URI, this method
88   --  will make an arbitrary selection; if you want all of the prefixes, use
89   --  the Prefixes function instead.
90   --
91   --  This function return the empty (default) prefix only for Element
92   --  component.
93
94   function Prefixes
95    (Self      : XML_Namespace_Support'Class;
96     Component : Component_Kinds := Element)
97       return League.String_Vectors.Universal_String_Vector;
98   --  Return an enumeration of all prefixes declared in this context. The
99   --  'xml:' prefix will be included.
100   --
101   --  The empty (default) prefix will be included in this enumeration; then
102   --  specified component is Element.
103
104   function Prefixes
105    (Self          : XML_Namespace_Support'Class;
106     Namespace_URI : League.Strings.Universal_String;
107     Component     : Component_Kinds := Element)
108       return League.String_Vectors.Universal_String_Vector;
109   --  Return an enumeration of all prefixes for a given URI whose declarations
110   --  are active in the current context. This includes declarations from
111   --  parent contexts that have not been overridden.
112   --
113   --  The empty (default) prefix will be included in this enumeration; then
114   --  specified component is Element.
115
116   procedure Process_Name
117    (Self           : XML_Namespace_Support'Class;
118     Qualified_Name : League.Strings.Universal_String;
119     Component      : Component_Kinds;
120     Namespace_URI  : out League.Strings.Universal_String;
121     Local_Name     : out League.Strings.Universal_String);
122   --  Process a raw XML qualified name, after all declarations in the current
123   --  context.
124   --
125   --  Note that attribute names are processed differently than element names:
126   --  an unprefixed element name will receive the default Namespace (if any),
127   --  while an unprefixed attribute name will not.
128
129   procedure Pop_Context (Self : in out XML_Namespace_Support'Class);
130   --  Revert to the previous Namespace context.
131   --
132   --  Normally, you should pop the context at the end of each XML element.
133   --  After popping the context, all Namespace prefix mappings that were
134   --  previously in force are restored.
135   --
136   --  You must not attempt to declare additional Namespace prefixes after
137   --  popping a context, unless you push another context first.
138
139   procedure Push_Context (Self : in out XML_Namespace_Support'Class);
140   --  Starts a new namespace context.
141   --
142   --  Normally, you should push a new context at the beginning of each XML
143   --  element: the new context automatically inherits the declarations of its
144   --  parent context, and it also keeps track of which declarations were made
145   --  within this context.
146   --
147   --  The namespace support object always starts with a base context already
148   --  in force: in this context, only the "xml" prefix is declared.
149
150   procedure Reset (Self : in out XML_Namespace_Support'Class);
151   --  Reset this Namespace support object for reuse.
152   --
153   --  It is necessary to invoke this method before reusing the Namespace
154   --  support object for a new session.
155
156private
157
158   package String_Maps is
159     new Ada.Containers.Hashed_Maps
160          (League.Strings.Universal_String,
161           League.Strings.Universal_String,
162           League.Strings.Hash,
163           League.Strings."=",
164           League.Strings."=");
165
166   type Context is record
167      Prefix        : String_Maps.Map;
168      Namespace_URI : String_Maps.Map;
169   end record;
170
171   package Context_Vectors is
172     new Ada.Containers.Vectors (Positive, Context);
173
174   type XML_Namespace_Support is new Ada.Finalization.Controlled with record
175      Current : Context;
176      Future  : Context;
177      Stack   : Context_Vectors.Vector;
178   end record;
179
180   overriding procedure Initialize (Self : in out XML_Namespace_Support);
181   --  Define default prefix mapping.
182
183end XML.Utilities.Namespace_Supports;
184