/
SrgsDocument.cs
425 lines (368 loc) · 10.8 KB
/
SrgsDocument.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
422
423
424
425
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.ObjectModel;
using System.Globalization;
using System.Speech.Internal;
using System.Speech.Internal.SrgsCompiler;
using System.Speech.Internal.SrgsParser;
using System.Xml;
namespace System.Speech.Recognition.SrgsGrammar
{
/// <summary>
/// This class allows a _grammar to be specified in SRGS form.
/// APITODO: needs programmatic access to SRGS DOM; PACOG
/// APITODO: needs rule activation/deactivation methods
/// </summary>
[Serializable]
public class SrgsDocument
{
#region Constructors / Destructors
/// <summary>
/// The default constructor - creates an empty SrgsGrammar object
/// </summary>
public SrgsDocument()
{
_grammar = new SrgsGrammar();
}
public SrgsDocument(string path)
{
Helpers.ThrowIfEmptyOrNull(path, nameof(path));
using (XmlTextReader reader = new(path))
{
Load(reader);
}
}
public SrgsDocument(XmlReader srgsGrammar)
{
Helpers.ThrowIfNull(srgsGrammar, nameof(srgsGrammar));
Load(srgsGrammar);
}
public SrgsDocument(GrammarBuilder builder)
{
Helpers.ThrowIfNull(builder, nameof(builder));
// New grammar
_grammar = new SrgsGrammar
{
#pragma warning disable 56504 // The Culture property is the Grammar builder is already checked.
Culture = builder.Culture
};
#pragma warning restore 56504
// Creates SrgsDocument elements
IElementFactory elementFactory = new SrgsElementFactory(_grammar);
// Do it
builder.CreateGrammar(elementFactory);
}
public SrgsDocument(SrgsRule grammarRootRule) : this()
{
Helpers.ThrowIfNull(grammarRootRule, nameof(grammarRootRule));
Root = grammarRootRule;
Rules.Add(grammarRootRule);
}
#endregion
#region public methods
public void WriteSrgs(XmlWriter srgsGrammar)
{
Helpers.ThrowIfNull(srgsGrammar, nameof(srgsGrammar));
// Make sure the grammar is ok
_grammar.Validate();
// Write the data.
_grammar.WriteSrgs(srgsGrammar);
}
#endregion
#region Public Properties
/// <summary>
/// Base URI of _grammar (xml:base).
/// </summary>
public Uri XmlBase
{
get
{
return _grammar.XmlBase;
}
set
{
// base value can be null
#pragma warning disable 56526
_grammar.XmlBase = value;
#pragma warning restore 56526
}
}
/// <summary>
/// Grammar language (xml:lang)
/// </summary>
public CultureInfo Culture
{
get
{
return _grammar.Culture;
}
set
{
Helpers.ThrowIfNull(value, nameof(value));
if (value.Equals(CultureInfo.InvariantCulture))
{
throw new ArgumentException(SR.Get(SRID.InvariantCultureInfo), nameof(value));
}
_grammar.Culture = value;
}
}
/// <summary>
/// Root rule (srgs:root)
/// </summary>
public SrgsRule Root
{
get
{
return _grammar.Root;
}
set
{
// base value can be null
#pragma warning disable 56526
_grammar.Root = value;
#pragma warning restore 56526
}
}
/// <summary>
/// Grammar mode (srgs:mode) - voice, dtmf
/// </summary>
public SrgsGrammarMode Mode
{
get
{
return _grammar.Mode == GrammarType.VoiceGrammar ? SrgsGrammarMode.Voice : SrgsGrammarMode.Dtmf;
}
set
{
_grammar.Mode = value == SrgsGrammarMode.Voice ? GrammarType.VoiceGrammar : GrammarType.DtmfGrammar;
}
}
/// <summary>
/// Grammar mode (srgs:mode) - voice, dtmf
/// </summary>
public SrgsPhoneticAlphabet PhoneticAlphabet
{
get
{
return (SrgsPhoneticAlphabet)_grammar.PhoneticAlphabet;
}
set
{
_grammar.PhoneticAlphabet = (AlphabetType)value;
_grammar.HasPhoneticAlphabetBeenSet = true;
}
}
/// <summary>
/// A collection of rules that this _grammar houses.
/// </summary>
// APITODO: Implementations of Rules and all other SRGS objects not here for now
public SrgsRulesCollection Rules
{
get
{
return _grammar.Rules;
}
}
/// <summary>
/// Programming Language used for the inline code; C#, VB or JScript
/// </summary>
public string Language
{
get
{
return _grammar.Language;
}
set
{
// Language can be set to null
#pragma warning disable 56526
_grammar.Language = value;
#pragma warning restore 56526
}
}
/// <summary>
/// namespace
/// </summary>
public string Namespace
{
get
{
return _grammar.Namespace;
}
set
{
// namespace can be set to null
#pragma warning disable 56526
_grammar.Namespace = value;
#pragma warning restore 56526
}
}
/// <summary>
/// CodeBehind
/// </summary>
public Collection<string> CodeBehind
{
get
{
return _grammar.CodeBehind;
}
}
/// <summary>
/// Add #line statements to the inline scripts if set
/// </summary>
public bool Debug
{
get
{
return _grammar.Debug;
}
set
{
_grammar.Debug = value;
}
}
/// <summary>
/// language
/// </summary>
public string Script
{
get
{
return _grammar.Script;
}
set
{
Helpers.ThrowIfEmptyOrNull(value, nameof(value));
_grammar.Script = value;
}
}
/// <summary>
/// ImportNameSpaces
/// </summary>
public Collection<string> ImportNamespaces
{
get
{
return _grammar.ImportNamespaces;
}
}
/// <summary>
/// ImportNameSpaces
/// </summary>
public Collection<string> AssemblyReferences
{
get
{
return _grammar.AssemblyReferences;
}
}
#endregion
#region Internal methods
// Initialize an SrgsDocument from an Srgs text source.
internal void Load(XmlReader srgsGrammar)
{
// New grammar
_grammar = new SrgsGrammar
{
// For SrgsGrammar, the default is IPA, for xml grammars, it is sapi.
PhoneticAlphabet = AlphabetType.Sapi
};
// create an XMl Parser
XmlParser srgsParser = new(srgsGrammar, null);
// Creates SrgsDocument elements
srgsParser.ElementFactory = new SrgsElementFactory(_grammar);
// Do it
srgsParser.Parse();
// This provides the path the XML was loaded from.
// {Note potentially this may also be overridden by an xml:base attribute in the XML itself.
// But for this scenario that doesn't matter since this is used to calculate the correct base path.}
if (!string.IsNullOrEmpty(srgsGrammar.BaseURI))
{
_baseUri = new Uri(srgsGrammar.BaseURI);
}
}
internal static GrammarOptions TagFormat2GrammarOptions(SrgsTagFormat value)
{
GrammarOptions newValue = 0;
switch (value)
{
case SrgsTagFormat.KeyValuePairs:
newValue = GrammarOptions.KeyValuePairSrgs;
break;
case SrgsTagFormat.MssV1:
newValue = GrammarOptions.MssV1;
break;
case SrgsTagFormat.W3cV1:
newValue = GrammarOptions.W3cV1;
break;
}
return newValue;
}
internal static SrgsTagFormat GrammarOptions2TagFormat(GrammarOptions value)
{
SrgsTagFormat tagFormat = SrgsTagFormat.Default;
switch (value & GrammarOptions.TagFormat)
{
case GrammarOptions.MssV1:
tagFormat = SrgsTagFormat.MssV1;
break;
case GrammarOptions.W3cV1:
tagFormat = SrgsTagFormat.W3cV1;
break;
case GrammarOptions.KeyValuePairSrgs:
case GrammarOptions.KeyValuePairs:
tagFormat = SrgsTagFormat.KeyValuePairs;
break;
}
return tagFormat;
}
#endregion
#region Internal Properties
/// <summary>
/// Tag format (srgs:tag-format)
/// </summary>summary>
internal SrgsTagFormat TagFormat
{
set
{
_grammar.TagFormat = value;
}
}
internal Uri BaseUri
{
get
{
return _baseUri;
}
}
internal SrgsGrammar Grammar
{
get
{
return _grammar;
}
}
#endregion
#region Private Fields
private SrgsGrammar _grammar;
// Path the grammar was actually loaded from, if this exists.
// Note this is different to SrgsGrammar.XmlBase which is the value of the xml:base attribute in the document itself.
private Uri _baseUri;
#endregion Fields
}
#region Enumerations
// Grammar mode. Voice, Dtmf
public enum SrgsGrammarMode
{
Voice,
Dtmf
}
// Grammar mode. Voice, Dtmf
public enum SrgsPhoneticAlphabet
{
Sapi,
Ipa,
Ups
}
#endregion Enumerations
}