Menu

[r182]: / trunk / Src / UProtocols.pas  Maximize  Restore  History

Download this file

424 lines (372 with data), 13.4 kB

  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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
{
* UProtocols.pas
*
* Set of classes that can register and provide instances of appropriate
* protocol handler classes according to a URL's protocol. Also provides base
* class for all protocol implementations.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UProtocols.pas (formerly UProtocolHandler.pas).
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UProtocols;
interface
uses
// Delphi
SysUtils, Contnrs,
// Project
UBaseObjects, UExceptions;
type
{
TProtocolClass:
Class reference for TProtocol descendant classes.
}
TProtocolClass = class of TProtocol;
{
TProtocol:
Abstract base class for all URL protocol handler classes.
}
TProtocol = class abstract(TObject)
strict private
fURL: string; // Value of URL property
strict protected
property URL: string read fURL;
{URL specifying a protocol and a resource}
public
constructor Create(const URL: string);
{Class constructor. Creates protocol handler for a specified resource.
@param URL [in] URL specifying protocol and resource.
}
class function SupportsProtocol(const URL: string): Boolean;
virtual; abstract;
{Checks if this protocol handler handles a URL's protocol.
@param URL [in] URL whose protocol is to be checked.
@return True if URL's protocol is supported, False if not.
}
function Execute: Boolean; virtual; abstract;
{Attempts to execute a resource. Descendants must implement.
@return True if resource could be handled (executed) or false if not.
}
end;
{
TProtocolFactory:
Factory class that creates and registers various TProtocol descendant
objects that can handle various supported URL protocols.
}
TProtocolFactory = class(TNoConstructObject)
strict private
type
{
TProtocolRegistrar:
Class that implements register of URL protocol handlers that can be
instantiated by protocol factory class.
}
TProtocolRegistrar = class(TObject)
strict private
fRegister: TClassList; // List of registered TProtocolClass references
function GetCount: Integer;
{Read accessor for Count property.
@return Number of registered protocols.
}
function GetProtocol(const Idx: Integer): TProtocolClass;
{Read accessor for Protocols property.
@param Idx [in] Index of protocol class reference in registry.
@return Instance of indexed protocol class reference.
}
type
{
TEnumerator:
Enumerator for TProtocolRegistrar.
}
TEnumerator = class(TObject)
strict private
fReg: TProtocolRegistrar; // Reference to register being enumerated
fIndex: Integer; // Index of current item in enumeration
public
constructor Create(const Reg: TProtocolRegistrar);
{Class constructor. Sets up enumeration.
@param Reg [in] Protocol registrar instance being enumerated.
}
function GetCurrent: TProtocolClass;
{Gets current protocol class in enumeration.
@return Current class.
}
function MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, false if enumeration
completed.
}
property Current: TProtocolClass read GetCurrent;
{Current item in enumeration}
end;
public
constructor Create;
{Class constructor. Sets up empty registry.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
function GetEnumerator: TEnumerator;
{Gets an instance of the registrar's enumerator.
@return Required enumerator.
}
procedure RegisterProtocol(const ClassRef: TProtocolClass);
{Registers a protocol.
@param ClassRef [in] Class that implements support for the protocol.
}
property Protocols[const Idx: Integer]: TProtocolClass
read GetProtocol; default;
{List of regsitered protocol classes}
property Count: Integer read GetCount;
{Number of registered protocols}
end;
class var fRegWrapper: IInterface;
{Wrapper object for protocol registrar instance that automatically frees
it when unit goes out of scope}
class var fRegistrar: TProtocolRegistrar;
{Stores singleton instance of protocol registrar object}
class function Registrar: TProtocolRegistrar;
{Gets singleton instance of protocol registar.
@return Singleton regsitrar object.
}
public
class function CreateHandler(const URL: string): TProtocol;
{Creates an appropriate TProtocol class instance for a protocol defined
in a URL.
@param URL [in] URL of a resource, prefixed by appropriate protocol.
@return Protocol handler for URL's protocol. If no handler is available
for the resource a nul handler instance is returned.
}
class procedure RegisterProtocol(const ClassRef: TProtocolClass);
{Registers a protocol with the factory class.
@param ClassRef [in] Reference to class implementing protocol.
}
end;
{
EProtocol:
Exception class for use in TProtocol descendant classes.
}
EProtocol = class(ECodeSnip);
implementation
{
NOTES
-----
In this program we don't want the browser control to perform its default
activity when certain URLs are clicked, that is we don't want the browser
control to display the referenced URL in its window. We may want to intercept
the URL and perform special processing depending on the URL's protocol.
In addition to performing custom processing on some standard URL protocols,
the program also defines its own "fake" protocols that have special meaning
within the program.
The technique used is to define a series of classes that descend from the
TProtocol abstract class. There is a separate class for each supported
protocol and each class knows how to handle its own protocol. Each class
registers itself with TProtocolFactory.
TProtocol.SupportsProtocol must return true if the class handles a protocol
or false if not. The first of the registered TProtocol sub classes to return
true from it SupportsProtocol method is used to handle the protocol and its
TProtcol.Execute method is called.
The TProtocol.Execute method must return true if it handles a protocol and
wishes to inhibit further processing. Code using the protocol classes must
prevent the browser from displaying the URL if Execute returns true but must
allow the browser control to process the URL normally if Execute returns
false.
A static factory class is provided that analyses URLs and creates the
appropriate TProtocol object, based on the URL's protocol.
}
uses
// Project
UAutoFree;
type
{
TNulProtocolHandler:
Nul protocol used when there is no suitable handler for a protocol. This
object is used to indicate that default handling of protocol by the browser
control is to be permitted. To this end the Execute method does nothing and
returns false to indicate the protocol was not handled and SupportsProtocol
returns false because this handler recognises no protocols.
}
TNulProtocolHandler = class(TProtocol)
public
class function SupportsProtocol(const URL: string): Boolean; override;
{Checks if this protocol handler handles a URL's protocol. It never does
so it returns False.
@param URL [in] URL whose protocol is to be checked. Ignored.
@return False.
}
function Execute: Boolean; override;
{Does nothing.
@return False.
}
end;
{ TProtocolFactory }
class function TProtocolFactory.CreateHandler(
const URL: string): TProtocol;
{Creates an appropriate TProtocol class instance for a protocol defined in a
URL.
@param URL [in] URL of a resource, prefixed by appropriate protocol.
@return Protocol handler for URL's protocol. If no handler is available for
the resource a nul handler instance is returned.
}
var
RegisteredCls: TProtocolClass; // enumerates registered protocol classes
begin
// Check for supported protocols
Result := nil;
for RegisteredCls in Registrar do
begin
if RegisteredCls.SupportsProtocol(URL) then
begin
Result := RegisteredCls.Create(URL);
Break;
end;
end;
if not Assigned(Result) then
// Protocol not supported: use nul protocol to indicate normal handling.
Result := TNulProtocolHandler.Create('');
end;
class procedure TProtocolFactory.RegisterProtocol(
const ClassRef: TProtocolClass);
{Registers a protocol with the factory class.
@param ClassRef [in] Reference to class implementing protocol.
}
begin
Registrar.RegisterProtocol(ClassRef);
end;
class function TProtocolFactory.Registrar: TProtocolRegistrar;
{Gets singleton instance of protocol registar.
@return Singleton regsitrar object.
}
begin
if not Assigned(fRegistrar) then
begin
fRegistrar := TProtocolRegistrar.Create;
fRegWrapper := TAutoObjFree.Create(fRegistrar);
end;
Result := fRegistrar;
end;
{ TProtocolFactory.TProtocolRegistrar }
constructor TProtocolFactory.TProtocolRegistrar.Create;
{Class constructor. Sets up empty registry.
}
begin
inherited Create;
fRegister := TClassList.Create;
end;
destructor TProtocolFactory.TProtocolRegistrar.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fRegister);
inherited;
end;
function TProtocolFactory.TProtocolRegistrar.GetCount: Integer;
{Read accessor for Count property.
@return Number of registered protocols.
}
begin
Result := fRegister.Count;
end;
function TProtocolFactory.TProtocolRegistrar.GetEnumerator: TEnumerator;
{Gets an instance of the registrar's enumerator.
@return Required enumerator.
}
begin
Result := TEnumerator.Create(Self);
end;
function TProtocolFactory.TProtocolRegistrar.GetProtocol(
const Idx: Integer): TProtocolClass;
{Read accessor for Protocols property.
@param Idx [in] Index of protocol class reference in registry.
@return Instance of indexed protocol class reference.
}
begin
Result := TProtocolClass(fRegister[Idx]);
end;
procedure TProtocolFactory.TProtocolRegistrar.RegisterProtocol(
const ClassRef: TProtocolClass);
{Registers a protocol.
@param ClassRef [in] Class that implements support for the protocol.
}
begin
fRegister.Add(ClassRef);
end;
{ TProtocolFactory.TProtocolRegistrar.TEnumerator }
constructor TProtocolFactory.TProtocolRegistrar.TEnumerator.Create(
const Reg: TProtocolRegistrar);
{Class constructor. Sets up enumeration.
@param Reg [in] Protocol registrar instance being enumerated.
}
begin
inherited Create;
fReg := Reg;
fIndex := -1;
end;
function TProtocolFactory.TProtocolRegistrar.TEnumerator.GetCurrent:
TProtocolClass;
{Gets current protocol class in enumeration.
@return Current class.
}
begin
Result := fReg[fIndex];
end;
function TProtocolFactory.TProtocolRegistrar.TEnumerator.MoveNext: Boolean;
{Moves to next item in enumeration.
@return True if there is a next item, false if enumeration completed.
}
begin
Result := fIndex < Pred(fReg.Count);
if Result then
Inc(fIndex);
end;
{ TProtocol }
constructor TProtocol.Create(const URL: string);
{Class constructor. Creates protocol handler for a specified resource.
@param URL [in] URL specifying protocol and resource.
}
begin
inherited Create;
fURL := URL;
end;
{ TNulProtocolHandler }
function TNulProtocolHandler.Execute: Boolean;
{Does nothing.
@return False.
}
begin
Result := False;
end;
class function TNulProtocolHandler.SupportsProtocol(const URL: string): Boolean;
{Checks if this protocol handler handles a URL's protocol. It never does so it
returns False.
@param URL [in] URL whose protocol is to be checked. Ignored.
@return False.
}
begin
Result := False;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.