-
Notifications
You must be signed in to change notification settings - Fork 87
/
BaseHandler.cs
421 lines (369 loc) · 14 KB
/
BaseHandler.cs
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
/*
// <copyright>
// dotNetRDF is free and open source software licensed under the MIT License
// -------------------------------------------------------------------------
//
// Copyright (c) 2009-2021 dotNetRDF Project (http://dotnetrdf.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
*/
using System;
using VDS.RDF.Query;
namespace VDS.RDF.Parsing.Handlers
{
/// <summary>
/// Abstract Base Class for Handlers.
/// </summary>
public abstract class BaseHandler
: INodeFactory
{
private INodeFactory _factory;
/// <summary>
/// Creates a new Handler.
/// </summary>
public BaseHandler()
: this(new NodeFactory()) { }
/// <summary>
/// Creates a new Handler using the given Node Factory.
/// </summary>
/// <param name="factory">Node Factory.</param>
public BaseHandler(INodeFactory factory)
{
if (factory == null) throw new ArgumentNullException("factory");
_factory = factory;
}
/// <summary>
/// Gets/Sets the in-use Node Factory.
/// </summary>
protected INodeFactory NodeFactory
{
get
{
return _factory;
}
set
{
if (value == null) throw new InvalidOperationException("Cannot set the NodeFactory of a Handler to be null");
_factory = value;
}
}
#region INodeFactory Members
/// <summary>
/// Creates a Blank Node.
/// </summary>
/// <returns></returns>
public virtual IBlankNode CreateBlankNode()
{
return _factory.CreateBlankNode();
}
/// <summary>
/// Creates a Blank Node with the given ID.
/// </summary>
/// <param name="nodeId">Node ID.</param>
/// <returns></returns>
public virtual IBlankNode CreateBlankNode(string nodeId)
{
return _factory.CreateBlankNode(nodeId);
}
/// <summary>
/// Creates a Graph Literal Node.
/// </summary>
/// <returns></returns>
public virtual IGraphLiteralNode CreateGraphLiteralNode()
{
return _factory.CreateGraphLiteralNode();
}
/// <summary>
/// Creates a Graph Literal Node with the given sub-graph.
/// </summary>
/// <param name="subgraph">Sub-graph.</param>
/// <returns></returns>
public virtual IGraphLiteralNode CreateGraphLiteralNode(IGraph subgraph)
{
return _factory.CreateGraphLiteralNode(subgraph);
}
/// <summary>
/// Creates a Literal Node with the given Datatype.
/// </summary>
/// <param name="literal">Value.</param>
/// <param name="datatype">Datatype URI.</param>
/// <returns></returns>
public virtual ILiteralNode CreateLiteralNode(string literal, Uri datatype)
{
return _factory.CreateLiteralNode(literal, datatype);
}
/// <summary>
/// Creates a Literal Node.
/// </summary>
/// <param name="literal">Value.</param>
/// <returns></returns>
public virtual ILiteralNode CreateLiteralNode(string literal)
{
return _factory.CreateLiteralNode(literal);
}
/// <summary>
/// Creates a Literal Node with the given Language.
/// </summary>
/// <param name="literal">Value.</param>
/// <param name="langspec">Language.</param>
/// <returns></returns>
public virtual ILiteralNode CreateLiteralNode(string literal, string langspec)
{
return _factory.CreateLiteralNode(literal, langspec);
}
/// <summary>
/// Creates a URI Node.
/// </summary>
/// <param name="uri">URI.</param>
/// <returns></returns>
public virtual IUriNode CreateUriNode(Uri uri)
{
return _factory.CreateUriNode(uri);
}
/// <summary>
/// Creates a Variable Node.
/// </summary>
/// <param name="varname">Variable Name.</param>
/// <returns></returns>
public virtual IVariableNode CreateVariableNode(string varname)
{
return _factory.CreateVariableNode(varname);
}
/// <summary>
/// Gets the next available Blank Node ID.
/// </summary>
/// <returns></returns>
public virtual string GetNextBlankNodeID()
{
return _factory.GetNextBlankNodeID();
}
#endregion
}
/// <summary>
/// Abstract Base Class for RDF Handlers.
/// </summary>
public abstract class BaseRdfHandler
: BaseHandler, IRdfHandler
{
private bool _inUse = false;
/// <summary>
/// Creates a new RDF Handler.
/// </summary>
public BaseRdfHandler()
: this(new NodeFactory()) { }
/// <summary>
/// Creates a new RDF Handler using the given Node Factory.
/// </summary>
/// <param name="factory">Node Factory.</param>
public BaseRdfHandler(INodeFactory factory)
: base(factory) { }
#region IRdfHandler Members
/// <summary>
/// Starts the Handling of RDF.
/// </summary>
public void StartRdf()
{
if (_inUse) throw new RdfParseException("Cannot use this Handler as an RDF Handler for parsing as it is already in-use");
StartRdfInternal();
_inUse = true;
}
/// <summary>
/// Optionally used by derived Handlers to do additional actions on starting RDF handling.
/// </summary>
protected virtual void StartRdfInternal()
{ }
/// <summary>
/// Ends the Handling of RDF.
/// </summary>
/// <param name="ok">Whether the parsing completed without error.</param>
public void EndRdf(bool ok)
{
if (!_inUse) throw new RdfParseException("Cannot End RDF Handling as this RDF Handler is not currently in-use");
EndRdfInternal(ok);
_inUse = false;
}
/// <summary>
/// Optionally used by derived Handlers to do additional actions on ending RDF handling.
/// </summary>
/// <param name="ok">Whether the parsing completed without error.</param>
protected virtual void EndRdfInternal(bool ok)
{ }
/// <summary>
/// Handles Namespace declarations.
/// </summary>
/// <param name="prefix">Prefix.</param>
/// <param name="namespaceUri">Namespace URI.</param>
/// <returns></returns>
public bool HandleNamespace(string prefix, Uri namespaceUri)
{
if (!_inUse) throw new RdfParseException("Cannot Handle Namespace as this RDF Handler is not currently in-use");
return HandleNamespaceInternal(prefix, namespaceUri);
}
/// <summary>
/// Optionally used by derived Handlers to do additional actions on handling namespace declarations.
/// </summary>
/// <param name="prefix">Prefix.</param>
/// <param name="namespaceUri">Namespace URI.</param>
/// <returns></returns>
protected virtual bool HandleNamespaceInternal(String prefix, Uri namespaceUri)
{
return true;
}
/// <summary>
/// Handles Base URI declarations.
/// </summary>
/// <param name="baseUri">Base URI.</param>
/// <returns></returns>
public bool HandleBaseUri(Uri baseUri)
{
if (!_inUse) throw new RdfParseException("Cannot Handle Base URI as this RDF Handler is not currently in-use");
return HandleBaseUriInternal(baseUri);
}
/// <summary>
/// Optionally used by derived Handlers to do additional actions on handling Base URI declarations.
/// </summary>
/// <param name="baseUri">Base URI.</param>
/// <returns></returns>
protected virtual bool HandleBaseUriInternal(Uri baseUri)
{
return true;
}
/// <summary>
/// Handles Triples.
/// </summary>
/// <param name="t">Triple.</param>
/// <returns></returns>
public bool HandleTriple(Triple t)
{
if (!_inUse) throw new RdfParseException("Cannot Handle Triple as this RDF Handler is not currently in-use");
return HandleTripleInternal(t);
}
/// <summary>
/// Must be overridden by derived handlers to take appropriate Triple handling action.
/// </summary>
/// <param name="t">Triple.</param>
/// <returns></returns>
protected abstract bool HandleTripleInternal(Triple t);
/// <summary>
/// Gets whether the Handler will accept all Triples i.e. it will never abort handling early.
/// </summary>
public abstract bool AcceptsAll
{
get;
}
#endregion
}
/// <summary>
/// Abstract Base Class for SPARQL Results Handlers.
/// </summary>
public abstract class BaseResultsHandler
: BaseHandler, ISparqlResultsHandler
{
private bool _inUse = false;
/// <summary>
/// Creates a new SPARQL Results Handler.
/// </summary>
/// <param name="factory">Node Factory.</param>
public BaseResultsHandler(INodeFactory factory)
: base(factory) { }
/// <summary>
/// Creates a new SPARQL Results Handler.
/// </summary>
public BaseResultsHandler()
: this(new NodeFactory()) { }
#region ISparqlResultsHandler Members
/// <summary>
/// Starts Results Handling.
/// </summary>
public void StartResults()
{
if (_inUse) throw new RdfParseException("Cannot use this Handler as an Results Handler for parsing as it is already in-use");
StartResultsInternal();
_inUse = true;
}
/// <summary>
/// Optionally used by derived classes to take additional actions on starting Results Handling.
/// </summary>
protected virtual void StartResultsInternal()
{ }
/// <summary>
/// Ends Results Handling.
/// </summary>
/// <param name="ok">Whether parsing completed without error.</param>
public void EndResults(bool ok)
{
if (!_inUse) throw new RdfParseException("Cannot End Results Handling as this Results Handler is not currently in-use");
EndResultsInternal(ok);
_inUse = false;
}
/// <summary>
/// Optionally used by derived classes to take additional actions on ending Results Handling.
/// </summary>
/// <param name="ok">Whether parsing completed without error.</param>
protected virtual void EndResultsInternal(bool ok)
{ }
/// <summary>
/// Handles a Boolean Results.
/// </summary>
/// <param name="result">Result.</param>
public void HandleBooleanResult(bool result)
{
if (!_inUse) throw new RdfParseException("Cannot Handle a Boolean Result as this Handler is not currently in-use");
HandleBooleanResultInternal(result);
}
/// <summary>
/// Must be overridden by derived handlers to appropriately handle boolean results.
/// </summary>
/// <param name="result">Result.</param>
protected abstract void HandleBooleanResultInternal(bool result);
/// <summary>
/// Handles a Variable declaration.
/// </summary>
/// <param name="var">Variable Name.</param>
/// <returns></returns>
public bool HandleVariable(String var)
{
if (!_inUse) throw new RdfParseException("Cannot Handle a Variable as this Handler is not currently in-use");
return HandleVariableInternal(var);
}
/// <summary>
/// Must be overridden by derived handlers to appropriately handle variable declarations.
/// </summary>
/// <param name="var">Variable Name.</param>
/// <returns></returns>
protected abstract bool HandleVariableInternal(String var);
/// <summary>
/// Handlers SPARQL Results.
/// </summary>
/// <param name="result">Result.</param>
/// <returns></returns>
public bool HandleResult(SparqlResult result)
{
if (!_inUse) throw new RdfParseException("Cannot Handle a Result as this Handler is not currently in-use");
return HandleResultInternal(result);
}
/// <summary>
/// Must be overridden by derived handlers to appropriately handler SPARQL Results.
/// </summary>
/// <param name="result">Result.</param>
/// <returns></returns>
protected abstract bool HandleResultInternal(SparqlResult result);
#endregion
}
}