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 | -----------------------------------------------------------------------
-- swagger-streams-forms -- x-www-form-urlencoded streams
-- Copyright (C) 2018, 2022 Stephane Carrez
-- Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-----------------------------------------------------------------------
with Ada.IO_Exceptions;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Util.Strings;
with Util.Beans.Objects.Readers;
package body Util.Serialize.IO.Form is
use Ada.Strings.Unbounded;
procedure Initialize (Stream : in out Output_Stream;
Output : in Util.Streams.Texts.Print_Stream_Access) is
begin
Stream.Stream := Output;
end Initialize;
-- ------------------------------
-- Flush the buffer (if any) to the sink.
-- ------------------------------
overriding
procedure Flush (Stream : in out Output_Stream) is
begin
Stream.Stream.Flush;
end Flush;
-- ------------------------------
-- Close the sink.
-- ------------------------------
overriding
procedure Close (Stream : in out Output_Stream) is
begin
Stream.Stream.Close;
end Close;
-- ------------------------------
-- Write the buffer array to the output stream.
-- ------------------------------
overriding
procedure Write (Stream : in out Output_Stream;
Buffer : in Ada.Streams.Stream_Element_Array) is
begin
Stream.Stream.Write (Buffer);
end Write;
Conversion : constant String (1 .. 16) := "0123456789ABCDEF";
procedure Write_Escape (Stream : in out Output_Stream;
Value : in String) is
begin
for C of Value loop
if C = ' ' then
Stream.Stream.Write ('+');
elsif C >= 'a' and then C <= 'z' then
Stream.Stream.Write (C);
elsif C >= 'A' and then C <= 'Z' then
Stream.Stream.Write (C);
elsif C >= '0' and then C <= '9' then
Stream.Stream.Write (C);
elsif C = '_' or else C = '-' then
Stream.Stream.Write (C);
else
Stream.Stream.Write ('%');
Stream.Stream.Write (Conversion (1 + (Character'Pos (C) / 16)));
Stream.Stream.Write (Conversion (1 + (Character'Pos (C) mod 16)));
end if;
end loop;
end Write_Escape;
-- Write the attribute name/value pair.
overriding
procedure Write_Attribute (Stream : in out Output_Stream;
Name : in String;
Value : in String) is
begin
if Stream.Has_Param then
Stream.Stream.Write ('&');
end if;
Stream.Has_Param := True;
Stream.Write_Escape (Name);
Stream.Stream.Write ('=');
Stream.Write_Escape (Value);
end Write_Attribute;
overriding
procedure Write_Wide_Attribute (Stream : in out Output_Stream;
Name : in String;
Value : in Wide_Wide_String) is
begin
if Stream.Has_Param then
Stream.Stream.Write ('&');
end if;
Stream.Has_Param := True;
Stream.Write_Escape (Name);
Stream.Stream.Write ('=');
Stream.Write_Escape (Ada.Strings.UTF_Encoding.Wide_Wide_Strings.Encode (Value));
end Write_Wide_Attribute;
overriding
procedure Write_Attribute (Stream : in out Output_Stream;
Name : in String;
Value : in Integer) is
begin
if Stream.Has_Param then
Stream.Stream.Write ('&');
end if;
Stream.Has_Param := True;
Stream.Write_Escape (Name);
Stream.Stream.Write ('=');
Stream.Stream.Write (Util.Strings.Image (Value));
end Write_Attribute;
overriding
procedure Write_Attribute (Stream : in out Output_Stream;
Name : in String;
Value : in Boolean) is
begin
if Stream.Has_Param then
Stream.Stream.Write ('&');
end if;
Stream.Has_Param := True;
Stream.Write_Escape (Name);
Stream.Stream.Write ('=');
Stream.Stream.Write (if Value then "true" else "false");
end Write_Attribute;
overriding
procedure Write_Attribute (Stream : in out Output_Stream;
Name : in String;
Value : in Util.Beans.Objects.Object) is
begin
null;
end Write_Attribute;
-- Write the attribute with a null value.
overriding
procedure Write_Null_Attribute (Stream : in out Output_Stream;
Name : in String) is
begin
null;
end Write_Null_Attribute;
-- Write the entity value.
overriding
procedure Write_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in String) is
begin
Stream.Write_Attribute (Name, Value);
end Write_Entity;
overriding
procedure Write_Wide_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Wide_Wide_String) is
begin
Stream.Write_Wide_Attribute (Name, Value);
end Write_Wide_Entity;
overriding
procedure Write_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Boolean) is
begin
Stream.Write_Attribute (Name, Value);
end Write_Entity;
overriding
procedure Write_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Integer) is
begin
Stream.Write_Attribute (Name, Value);
end Write_Entity;
overriding
procedure Write_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Ada.Calendar.Time) is
begin
null;
end Write_Entity;
overriding
procedure Write_Long_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Long_Long_Integer) is
begin
null;
end Write_Long_Entity;
overriding
procedure Write_Long_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Long_Long_Float) is
begin
null;
end Write_Long_Entity;
overriding
procedure Write_Enum_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in String) is
begin
null;
end Write_Enum_Entity;
overriding
procedure Write_Entity (Stream : in out Output_Stream;
Name : in String;
Value : in Util.Beans.Objects.Object) is
begin
null;
end Write_Entity;
-- Write an entity with a null value.
overriding
procedure Write_Null_Entity (Stream : in out Output_Stream;
Name : in String) is
begin
null;
end Write_Null_Entity;
-- ------------------------------
-- Parse the stream using the form parser.
-- ------------------------------
overriding
procedure Parse (Handler : in out Parser;
Stream : in out Util.Streams.Buffered.Input_Buffer_Stream'Class;
Sink : in out Reader'Class) is
function From_Hex (C : in Character) return Natural;
procedure Parse_Token (Into : out Ada.Strings.Unbounded.Unbounded_String);
Name : Ada.Strings.Unbounded.Unbounded_String;
Value : Ada.Strings.Unbounded.Unbounded_String;
Last : Character;
function From_Hex (C : in Character) return Natural is
begin
if C >= 'a' and then C <= 'f' then
return Character'Pos (C) - Character'Pos ('a') + 10;
elsif C >= 'A' and then C <= 'F' then
return Character'Pos (C) - Character'Pos ('A') + 10;
elsif C >= '0' and then C <= '9' then
return Character'Pos (C) - Character'Pos ('0');
else
raise Parse_Error with "Invalid hexadecimal character: " & C;
end if;
end From_Hex;
procedure Parse_Token (Into : out Ada.Strings.Unbounded.Unbounded_String) is
C1, C2 : Character;
begin
Into := Ada.Strings.Unbounded.Null_Unbounded_String;
loop
Stream.Read (C1);
if C1 = '&' or else C1 = '=' then
Last := C1;
return;
end if;
if C1 = '+' then
Ada.Strings.Unbounded.Append (Into, ' ');
elsif C1 = '%' then
Stream.Read (C1);
Stream.Read (C2);
Ada.Strings.Unbounded.Append (Into,
Character'Val ((16 * From_Hex (C1)) + From_Hex (C2)));
else
Ada.Strings.Unbounded.Append (Into, C1);
end if;
end loop;
exception
when Ada.IO_Exceptions.Data_Error =>
Last := ' ';
return;
end Parse_Token;
begin
Sink.Start_Object ("", Handler);
loop
Parse_Token (Name);
if Last /= '=' then
raise Parse_Error with "Missing '=' after parameter name";
end if;
Parse_Token (Value);
Sink.Set_Member (To_String (Name), Util.Beans.Objects.To_Object (Value), Handler);
if Stream.Is_Eof then
Sink.Finish_Object ("", Handler);
return;
end if;
if Last /= '&' then
raise Parse_Error with "Missing '&' after parameter value";
end if;
end loop;
end Parse;
-- ------------------------------
-- Get the current location (file and line) to report an error message.
-- ------------------------------
overriding
function Get_Location (Handler : in Parser) return String is
pragma Unreferenced (Handler);
begin
return "";
end Get_Location;
-- ------------------------------
-- Read a form file and return an object.
-- ------------------------------
function Read (Path : in String) return Util.Beans.Objects.Object is
P : Parser;
R : Util.Beans.Objects.Readers.Reader;
begin
P.Parse (Path, R);
return R.Get_Root;
end Read;
end Util.Serialize.IO.Form;
|