1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--                          Ada Modeling Framework                          --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2011-2012, 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: 2990 $ $Date: 2012-05-16 11:25:25 +0400 (Wed, 16 May 2012) $
43------------------------------------------------------------------------------
44with AMF.Internals.Elements;
45with AMF.Internals.Extents;
46with AMF.Internals.Factories;
47with AMF.Internals.Helpers;
48with AMF.Internals.Links;
49
50package body AMF.Internals.AMF_URI_Stores is
51
52   -----------------------
53   -- Convert_To_String --
54   -----------------------
55
56   overriding function Convert_To_String
57    (Self      : not null access AMF_URI_Store;
58     Data_Type : not null access AMF.CMOF.Data_Types.CMOF_Data_Type'Class;
59     Value     : League.Holders.Holder)
60       return League.Strings.Universal_String
61   is
62      The_Package : constant AMF.CMOF.Packages.CMOF_Package_Access
63        := Data_Type.Get_Package;
64
65   begin
66      return
67        Self.Get_Factory
68         (The_Package.Get_URI.Value).Convert_To_String (Data_Type, Value);
69   end Convert_To_String;
70
71   ------------
72   -- Create --
73   ------------
74
75   overriding function Create
76    (Self       : not null access AMF_URI_Store;
77     Meta_Class : not null access AMF.CMOF.Classes.CMOF_Class'Class)
78       return not null AMF.Elements.Element_Access
79   is
80      The_Package : constant AMF.CMOF.Packages.CMOF_Package_Access
81        := Meta_Class.Get_Package;
82
83   begin
84      return Self.Get_Factory (The_Package.Get_URI.Value).Create (Meta_Class);
85   end Create;
86
87   ------------------------
88   -- Create_From_String --
89   ------------------------
90
91   overriding function Create_From_String
92    (Self      : not null access AMF_URI_Store;
93     Data_Type : not null access AMF.CMOF.Data_Types.CMOF_Data_Type'Class;
94     Image     : League.Strings.Universal_String)
95       return League.Holders.Holder
96   is
97      The_Package : constant AMF.CMOF.Packages.CMOF_Package_Access
98        := Data_Type.Get_Package;
99
100   begin
101      return
102        Self.Get_Factory
103         (The_Package.Get_URI.Value).Create_From_String (Data_Type, Image);
104   end Create_From_String;
105
106   -----------------
107   -- Create_Link --
108   -----------------
109
110   overriding function Create_Link
111    (Self           : not null access AMF_URI_Store;
112     Association    :
113       not null access AMF.CMOF.Associations.CMOF_Association'Class;
114     First_Element  : not null AMF.Elements.Element_Access;
115     Second_Element : not null AMF.Elements.Element_Access)
116       return not null AMF.Links.Link_Access
117   is
118      pragma Unreferenced (Self);
119
120   begin
121      return
122        AMF.Internals.Links.Proxy
123         (AMF.Internals.Links.Create_Link
124           (AMF.Internals.Elements.Element_Base'Class
125             (Association.all).Element,
126            AMF.Internals.Helpers.To_Element (First_Element),
127            AMF.Internals.Helpers.To_Element (Second_Element)));
128   end Create_Link;
129
130   -----------------
131   -- Get_Factory --
132   -----------------
133
134   overriding function Get_Factory
135    (Self          : not null access AMF_URI_Store;
136     Metamodel_URI : League.Strings.Universal_String)
137       return AMF.Factories.Factory_Access
138   is
139      use type AMF.Factories.Factory_Access;
140
141      Position : constant String_Factory_Maps.Cursor
142        := Self.Factories.Find (Metamodel_URI);
143      Factory  : AMF.Factories.Factory_Access;
144
145   begin
146      --  Lookup factory in the map.
147
148      if String_Factory_Maps.Has_Element (Position) then
149         return String_Factory_Maps.Element (Position);
150      end if;
151
152      --  Create factory.
153
154      Factory :=
155        AMF.Internals.Factories.Create_Factory (Metamodel_URI, Self.Id);
156
157      if Factory /= null then
158         --  If factory is created register mapping of all its packages.
159
160         declare
161            Packages :
162              constant AMF.CMOF.Packages.Collections.Set_Of_CMOF_Package
163                := Factory.Get_Package;
164
165         begin
166            for J in 1 .. Packages.Length loop
167               Self.Factories.Insert
168                (Packages.Element (J).Get_URI.Value, Factory);
169            end loop;
170         end;
171      end if;
172
173      return Factory;
174   end Get_Factory;
175
176   ------------
177   -- Get_Id --
178   ------------
179
180   overriding function Get_Id
181    (Self    : not null access AMF_URI_Store;
182     Element : not null AMF.Elements.Element_Access)
183       return League.Strings.Universal_String is
184   begin
185      return
186        AMF.Internals.Extents.Get_Id
187         (Self.Id, AMF.Internals.Helpers.To_Element (Element));
188   end Get_Id;
189
190   -----------------
191   -- Get_Package --
192   -----------------
193
194   overriding function Get_Package
195    (Self : not null access constant AMF_URI_Store)
196       return AMF.CMOF.Packages.Collections.Set_Of_CMOF_Package
197   is
198      pragma Unreferenced (Self);
199
200   begin
201      return AMF.Internals.Factories.Get_Packages;
202   end Get_Package;
203
204   ------------
205   -- Set_Id --
206   ------------
207
208   overriding procedure Set_Id
209    (Self    : not null access AMF_URI_Store;
210     Element : not null AMF.Elements.Element_Access;
211     Id      : League.Strings.Universal_String) is
212   begin
213      AMF.Internals.Extents.Set_Id
214       (Self.Id, AMF.Internals.Helpers.To_Element (Element), Id);
215   end Set_Id;
216
217end AMF.Internals.AMF_URI_Stores;
218