Menu

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

Download this file

405 lines (349 with data), 13.2 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
{
* 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.
*
* v1.0 of 25 Oct 2006 - Original version.
* v2.0 of 14 Nov 2006 - Complete rewrite.
* - Renamed unit from UProtocolHandler to UProtocols.
* - Renamed TProtocolHandler as TProtocol.
* - Removed IProtocolHandler and removed from protocol
* handler classes.
* - Moved code from THTTPProtocolHandler and
* TShellExecProtocolHandler to new UHTTProtocol unit
* where both classes were merged as THTTPProtocol.
* - Removed now unused TExecuteProtocolHandler class.
* - Renamed TProtocolHandlerFactory as TProtocolFactory.
* - Added new TProtocolRegistrar and TProtocolRegisterItem
* private classes to manage registered protocol classes
* along with private singleton instance of
* TProtocolRegistrar.
* - Changed TProtocolHandlerFactory to work with
* registered protocol classes rather than hard wired
* protocol classes. This lets new protocols be added
* without modifying this unit.
* v2.1 of 04 Nov 2007 - Removed redundant uses clause reference to removed
* UHelpTopicAction unit along with other unused units.
* v2.2 of 04 Oct 2008 - Changed TProtocolFactory to derive from
* TNoConstructObject and hence prevented it from being
* constructed.
* - Made private and protected sections of various classes
* strict.
* - Now use ClassName method in assert statement.
*
*
* ***** 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-2007 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s): None
*
* ***** END LICENSE BLOCK *****
}
unit UProtocols;
interface
uses
// Project
UBaseObjects;
type
{
TProtocolClass:
Class reference for TProtocol descendant classes.
}
TProtocolClass = class of TProtocol;
{
TProtocol:
Abstract base class for all URL protocol handler classes.
}
TProtocol = class(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.
}
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 appropriate TProtocol descendant objects that can
handle various supported URL protocols.
}
TProtocolFactory = class(TNoConstructObject)
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 Name: string;
const ClassRef: TProtocolClass);
{Registers a protocol with the factory class.
@param Name [in] Name of protocol.
@param ClassRef [in] Reference to class implementing protocol.
}
end;
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 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.
The TProtocol.Execute method must return true if it handles a protocol and
false if not. 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.
Sub-classes of TProtocol must register themselves with the factory class by
calling its RegisterProtocol method and passing the name of the supported
protocol and a class reference. In order to do this implementations expose a
constructor that accepts a URL as its only parameter. If an implementation
supports more than one protocol it must register itself separately for each
supported protocol.
Here is some boilerplate code for a protocol implementation:
TMyProtocol = class(TProtocol)
public
function Execute: Boolean; override;
end;
...
function TMyProtocol.Execute: Boolean;
begin
Result := True;
// Handle protocol here
end;
...
initialization
TProtocolFactory.RegisterProtocol('protocolname', TMyProtocol);
end.
}
uses
// Delphi
SysUtils, StrUtils, Contnrs;
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.
}
TNulProtocolHandler = class(TProtocol)
public
function Execute: Boolean; override;
{Does nothing.
@return False.
}
end;
{
TProtocolRegisterItem:
Class that represents a protocol registered in protocol registrar.
Effectively maps a protocol name to its implementing class.
}
TProtocolRegisterItem = class(TObject)
strict private
fName: string;
{Value of Protocol property}
fClassRef: TProtocolClass;
{Value of ProtocolClass property}
public
constructor Create(const Name: string;
const ClassRef: TProtocolClass);
{Class constructor. Sets object's properties.
@param Name [in] Name of protocol.
@param ClassRef [in] Reference to class that supports the protocol.
}
property Name: string read fName;
{Name of URL protocol}
property ClassRef: TProtocolClass read fClassRef;
{Type of class that implements support for protocol}
end;
{
TProtocolRegistrar:
Class that implements register of URL protocol handlers that can be
instantiated by protocol factory class.
}
TProtocolRegistrar = class(TObject)
strict private
fRegister: TObjectList;
{List of items stored in registry}
function GetCount: Integer;
{Read accessor for Count property.
@return Number of registered protocols.
}
function GetProtocol(const Idx: Integer): TProtocolRegisterItem;
{Read accessor for Protocols property.
@param Idx [in] Index of protocol in registry.
@return Instance of indexed protocol register item.
}
public
constructor Create;
{Class constructor. Sets up empty registry.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure RegisterProtocol(const Name: string;
const ClassRef: TProtocolClass);
{Registers a protocol.
@param Name [in] Name of protocol.
@param ClassRef [in] Class that implements support for the protocol.
}
property Protocols[const Idx: Integer]: TProtocolRegisterItem
read GetProtocol; default;
{List of regsitered protocols}
property Count: Integer read GetCount;
{Number of registered protocols}
end;
var
// Private protocol registrar singletion
PvtRegistrar: TProtocolRegistrar = nil;
{ 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
Idx: Integer; // loops thru registered protocols
begin
// Check for supported protocols
Result := nil;
for Idx := 0 to Pred(PvtRegistrar.Count) do
begin
if AnsiStartsStr(PvtRegistrar[Idx].Name, URL) then
begin
Result := PvtRegistrar[Idx].ClassRef.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 Name: string;
const ClassRef: TProtocolClass);
{Registers a protocol with the factory class.
@param Name [in] Name of protocol.
@param ClassRef [in] Reference to class implementing protocol.
}
begin
PvtRegistrar.RegisterProtocol(Name, ClassRef);
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;
{ TProtocolRegistrar }
constructor TProtocolRegistrar.Create;
{Class constructor. Sets up empty registry.
}
begin
inherited Create;
fRegister := TObjectList.Create(True);
end;
destructor TProtocolRegistrar.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fRegister); // frees owned objects
inherited;
end;
function TProtocolRegistrar.GetCount: Integer;
{Read accessor for Count property.
@return Number of registered protocols.
}
begin
Result := fRegister.Count;
end;
function TProtocolRegistrar.GetProtocol(
const Idx: Integer): TProtocolRegisterItem;
{Read accessor for Protocols property.
@param Idx [in] Index of protocol in registry.
@return Instance of indexed protocol register item.
}
begin
Result := fRegister[Idx] as TProtocolRegisterItem;
end;
procedure TProtocolRegistrar.RegisterProtocol(const Name: string;
const ClassRef: TProtocolClass);
{Registers a protocol.
@param Name [in] Name of protocol.
@param ClassRef [in] Class that implements support for the protocol.
}
begin
fRegister.Add(TProtocolRegisterItem.Create(Name, ClassRef));
end;
{ TProtocolRegisterItem }
constructor TProtocolRegisterItem.Create(const Name: string;
const ClassRef: TProtocolClass);
{Class constructor. Sets object's properties.
@param Name [in] Name of protocol.
@param ClassRef [in] Reference to class that supports the protocol.
}
begin
inherited Create;
Assert(Assigned(ClassRef), // ** do not localise
ClassName + '.Create: ClassRef is nil');
fName := Name;
fClassRef := ClassRef;
end;
initialization
// Instantiate protocol registrar singleton
PvtRegistrar := TProtocolRegistrar.Create;
finalization
// Free registrar singleton
FreeAndNil(PvtRegistrar);
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.