Project

Profile

Help

How to connect?
Download (37 KB) Statistics
| Branch: | Revision:

he / src / main / csharp / api / Saxon.Api / Types.cs @ 2699858e

1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Xml;
5
using JType = net.sf.saxon.type.Type;
6
using JXdmItemType = net.sf.saxon.s9api.ItemType;
7
using JItemTypeType = net.sf.saxon.type.ItemType;
8
using JAnyItemType = net.sf.saxon.type.AnyItemType;
9
using JAtomicType = net.sf.saxon.type.AtomicType;
10
using JAnyMapType = net.sf.saxon.ma.map.MapType;
11
using JAnyArrayType = net.sf.saxon.ma.arrays.ArrayItemType;
12
using JNodeTest = net.sf.saxon.pattern.NodeTest;
13
using JAnyNodeType = net.sf.saxon.pattern.AnyNodeTest;
14
using JErrorType = net.sf.saxon.type.ErrorType;
15
using JFunctionItemType = net.sf.saxon.type.FunctionItemType;
16
using JNodeKindTest = net.sf.saxon.pattern.NodeKindTest;
17
using JSequenceType = net.sf.saxon.value.SequenceType;
18
using JXdmSequenceType = net.sf.saxon.s9api.SequenceType;
19
using JStandardNames = net.sf.saxon.om.StandardNames;
20
using JStructuredQName = net.sf.saxon.om.StructuredQName;
21
using JStaticProperty = net.sf.saxon.expr.StaticProperty;
22
using JConversionRules = net.sf.saxon.lib.ConversionRules;
23
using JOccurrenceIndicator = net.sf.saxon.s9api.OccurrenceIndicator;
24

    
25
namespace Saxon.Api
26
{
27
    /// <summary>
28
    /// Abstract class representing an item type. This may be the generic item type <c>item()</c>,
29
    /// an atomic type, the generic node type <code>node()</code>, a specific node kind such as
30
    /// <c>element()</c> or <c>text()</c>, or the generic function type <code>function()</code>.
31
    /// </summary>
32
    /// <remarks>
33
	/// More specific node types (such as <c>element(E)</c> or <c>schema-element(E)</c>) cannot currently
34
    /// be instantiated in this API.
35
    /// </remarks>
36

    
37
    public abstract class XdmItemType
38
    {
39
        protected JXdmItemType type;
40

    
41
        private JConversionRules defaultConversionRules = new JConversionRules();
42

    
43
        internal static XdmItemType MakeXdmItemType(JItemTypeType type)
44
        {
45
            
46
            if (type is JAtomicType)
47
            {
48
                XdmAtomicType itype = XdmAtomicType.BuiltInAtomicType(QName.FromStructuredQName(((JAtomicType)type).getStructuredQName()));
49
               
50
                return itype;
51
            }
52
            else if (type is JErrorType)
53
            {
54
                return XdmAnyNodeType.Instance;  // TODO: need to represent this properly
55
            }
56
            else if (type is JNodeTest)
57
            {
58
                if (type is JAnyNodeType)
59
                {
60
                    return XdmAnyNodeType.Instance;
61
                }
62
                else
63
                {
64
                    int kind = ((JNodeTest)type).getPrimitiveType();
65
                    return XdmNodeKind.ForNodeKindTest((JNodeKindTest)JNodeKindTest.makeNodeKindTest(kind));
66
                }
67
            }
68
            else if (type is JAnyMapType) {
69
                return XdmAnyMapType.Instance;
70
            }
71
            else if (type is JAnyArrayType)
72
            {
73
                return XdmAnyArrayType.Instance;
74
            }
75
            else if (type is JAnyItemType)
76
            {
77
                return XdmAnyItemType.Instance;
78
            }
79
            else if (type is JFunctionItemType)
80
            {
81
                return XdmAnyFunctionType.Instance;
82
            }
83
            else
84
            {
85
                return null;
86
            }
87
        }
88

    
89
        internal JXdmItemType Unwrap() {
90
            return type;
91
        }
92

    
93
        internal void SetJXdmItemType(JXdmItemType type) {
94
            this.type = type;
95
        }
96

    
97

    
98
        /// <summary>
99
        /// Determine whether this item type matches a given item.
100
        /// </summary>
101
        /// <param name="item">the item to be tested against this item type</param>
102
        /// <returns>true if the item matches this item type, false if it does not match.</returns>
103
        virtual public bool Matches(XdmItem item) {
104
            return type.matches((net.sf.saxon.s9api.XdmItem)XdmValue.FromGroundedValueToJXdmValue(item.value));
105
        }
106

    
107
        /// <summary>
108
        /// Determine whether this ItemType subsumes another ItemType. Specifically,
109
        /// <code>A.subsumes(B)</code> is true if every value that matches the ItemType B also matches
110
        /// the ItemType A.
111
        /// </summary>
112
		/// <param name="other">the other ItemType</param>
113
        /// <returns>true if this ItemType subsumes the other ItemType. This includes the case where A and B
114
        ///         represent the same ItemType.</returns>
115
        virtual public bool Subsumes(XdmItemType other) {
116
            return type.subsumes(other.type);
117
        }
118

    
119

    
120
        /// <summary>
121
        /// Get a string representation of the type. This will be a string that confirms to
122
        /// XPath ItemType production, for example a QName (always in 'Q{uri}local' format, or a
123
        /// construct such as 'node()' or 'map(*)'). If the type is an anonymous schema type, the 
124
        /// name of the nearest named base type will be given, preceded by the character '&lt;'
125
        /// </summary>
126
        /// <returns>a string representation of the type</returns>
127
        public override String ToString() {
128
            return type.toString();
129
        }
130

    
131

    
132
    }
133

    
134
    
135

    
136
    /// <summary>
137
    /// Singleton class representing the item type <c>item()</c>, which matches any item.
138
    /// </summary>
139

    
140
    public class XdmAnyItemType : XdmItemType
141
	{
142

    
143
		/// <summary>
144
		/// The singleton instance of this class: an <c>XdmItemType</c> corresponding to the
145
		/// item type <c>item()</c>, which matches any item.
146
		/// </summary>
147

    
148
		public static XdmAnyItemType Instance = new XdmAnyItemType();
149

    
150
		internal XdmAnyItemType()
151
		{
152
			this.type = JXdmItemType.ANY_ITEM;
153
		}
154

    
155
        /// <summary>
156
        /// Determine whether this item type matches a given item.
157
        /// </summary>
158
        /// <param name="item">the item to be tested against this item type</param>
159
        /// <returns>true if the item matches this item type, false if it does not match.</returns>
160
        public override bool Matches(XdmItem item)
161
        {
162
            return true;
163
        }
164

    
165
        /// <summary>
166
        /// Determine whether this ItemType subsumes another ItemType. Specifically,
167
        /// <code>A.subsumes(B)</code> is true if every value that matches the ItemType B also matches
168
        /// the ItemType A.
169
        /// </summary>
170
		/// <param name="other">the other ItemType</param>
171
        /// <returns>true if this ItemType subsumes the other ItemType. This includes the case where A and B
172
        ///         represent the same ItemType.</returns>
173
		public override bool Subsumes(XdmItemType other)
174
        {
175
            return true;
176
        }
177
    }
178

    
179
	/// <summary>
180
	/// Singleton class representing the item type <c>map(*)</c>, which matches any map.
181
	/// </summary>
182

    
183
    public class XdmAnyMapType : XdmItemType
184
    {
185

    
186

    
187
        /// <summary>
188
        /// The singleton instance of this class: an <c>XdmMapType</c> corresponding to the
189
        /// item type <c>map(*)</c>, which matches any map.
190
        /// </summary>
191

    
192
        public static XdmAnyMapType Instance = new XdmAnyMapType();
193

    
194
        internal XdmAnyMapType()
195
        {
196
            this.type = JXdmItemType.ANY_MAP;
197
        }
198

    
199

    
200

    
201
	}
202

    
203
	/// <summary>
204
	/// Singleton class representing the item type <c>array(*)</c>, which matches any array.
205
	/// </summary>
206

    
207
    public class XdmAnyArrayType : XdmItemType
208
    {
209

    
210

    
211
        /// <summary>
212
        /// The singleton instance of this class: an <c>XdmArrayType</c> corresponding to the
213
        /// item type <c>array(*)</c>, which matches any array.
214
        /// </summary>
215

    
216
        public static XdmAnyArrayType Instance = new XdmAnyArrayType();
217

    
218
        internal XdmAnyArrayType()
219
        {
220
            this.type = JXdmItemType.ANY_ARRAY;
221
        }
222

    
223

    
224
    }
225

    
226
    /// <summary>
227
    /// Singleton class representing the item type <c>node()</c>, which matches any node.
228
    /// </summary>
229

    
230
    public class XdmAnyNodeType : XdmItemType
231
	{
232

    
233
		/// <summary>
234
		/// The singleton instance of this class: an <c>XdmItemType</c> corresponding to the
235
		/// item type <c>node()</c>, which matches any node.
236
		/// </summary>
237

    
238
		public static XdmAnyNodeType Instance = new XdmAnyNodeType();
239

    
240
		internal XdmAnyNodeType()
241
		{
242
			this.type = JXdmItemType.ANY_NODE;
243
		}
244

    
245
        /// <summary>
246
        /// Determine whether this item type matches a given item.
247
        /// </summary>
248
        /// <param name="item">the item to be tested against this item type</param>
249
        /// <returns>true if the item matches this item type, false if it does not match.</returns>
250
        public override bool Matches(XdmItem item)
251
        {
252
            return ((net.sf.saxon.om.Item)item.value) is net.sf.saxon.om.NodeInfo;
253
        }
254

    
255
        /// <summary>
256
        ///  Determine whether this ItemType subsumes another ItemType. Specifically,
257
        /// <code>A.subsumes(B)</code> is true if every value that matches the ItemType B also matches
258
        /// the ItemType A.
259
        /// </summary>
260
		/// <param name="other">the other ItemType</param>
261
        /// <returns>true if this ItemType subsumes the other ItemType. This includes the case where A and B
262
        ///         represent the same ItemType.</returns>
263
        public override bool Subsumes(XdmItemType other)
264
        {
265
            return true;
266
        }
267
    }
268

    
269

    
270
	/// <summary>
271
	/// Singleton class representing the item type <c>function(*)</c>, which matches any function item.
272
	/// </summary>
273

    
274
	public class XdmAnyFunctionType : XdmItemType
275
	{
276

    
277
		/// <summary>
278
		/// The singleton instance of this class: an <c>XdmItemType</c> corresponding to the
279
		/// item type <c>function(*)</c>, which matches any function item.
280
		/// </summary>
281

    
282
		public static XdmAnyFunctionType Instance = new XdmAnyFunctionType();
283

    
284
		internal XdmAnyFunctionType()
285
		{
286
			this.type = JXdmItemType.ANY_FUNCTION;
287
		}
288

    
289

    
290
    }
291

    
292
    
293
	/// <summary>
294
	/// An instance of class <c>XdmAtomicType</c> represents a specific atomic type, for example
295
	/// <c>xs:double</c>, <c>xs:integer</c>, or <c>xs:anyAtomicType</c>. This may be either a built-in
296
	/// atomic type or a type defined in a user-written schema.
297
	/// </summary>
298
	/// <remarks>
299
	/// To get an <c>XdmAtomicType</c> instance representing a built-in atomic type, use one of the predefined instances
300
	/// of the subclass <c>XdmBuiltInAtomicType</c>. To get an <c>XdmAtomicType</c> instance representing a user-defined
301
	/// atomic type (defined in a schema), use the method <c>GetAtomicType</c> defined on the <c>SchemaManager</c> class.
302
	/// </remarks>
303

    
304
	public class XdmAtomicType : XdmItemType
305
	{
306

    
307
        /// <summary>
308
		/// Instance object of the <c>XdmAtomicType</c> class
309
        /// </summary>
310
        public static XdmAtomicType Instance = new XdmAtomicType(JXdmItemType.ANY_ATOMIC_VALUE);
311

    
312
        internal XdmAtomicType(JXdmItemType type)
313
        {
314
            this.type = type;
315
        }
316

    
317
        /// <summary>
318
        ///  ItemType representing the built-in (but non-primitive) type xs:short
319
        /// </summary>
320
        public static XdmAtomicType SHORT = BuiltInAtomicType(JStandardNames.XS_SHORT);
321

    
322

    
323
        /// <summary>
324
        /// ItemType representing the primitive type xs:string
325
        /// </summary>
326
        public static XdmAtomicType STRING = BuiltInAtomicType(JStandardNames.XS_STRING);
327

    
328
        /// <summary>
329
        /// ItemType representing the primitive type xs:boolean
330
        /// </summary>
331
        public static XdmAtomicType BOOLEAN = BuiltInAtomicType(JStandardNames.XS_BOOLEAN);
332

    
333
        /// <summary>
334
        /// ItemType representing the primitive type xs:duration
335
        /// </summary>
336

    
337
        public static XdmAtomicType DURATION = BuiltInAtomicType(JStandardNames.XS_DURATION);
338

    
339
        /// <summary>
340
        /// ItemType representing the primitive type xs:dateTime
341
        /// </summary>
342

    
343
        public static XdmAtomicType DATE_TIME = BuiltInAtomicType(JStandardNames.XS_DATE_TIME);
344

    
345
        /// <summary>
346
        /// ItemType representing the primitive type xs:date
347
        /// </summary>
348

    
349
        public static XdmAtomicType DATE = BuiltInAtomicType(JStandardNames.XS_DATE);
350

    
351
        /// <summary>
352
        /// ItemType representing the primitive type xs:time
353
        /// </summary>
354

    
355
        public static XdmAtomicType TIME = BuiltInAtomicType(JStandardNames.XS_TIME);
356

    
357
        /// <summary>
358
        /// ItemType representing the primitive type xs:gYearMonth
359
        /// </summary>
360

    
361
        public static XdmAtomicType G_YEAR_MONTH = BuiltInAtomicType(JStandardNames.XS_G_YEAR_MONTH);
362

    
363
        /// <summary>
364
        ///  ItemType representing the primitive type xs:gMonth
365
        /// </summary>
366

    
367
        public static XdmAtomicType G_MONTH = BuiltInAtomicType(JStandardNames.XS_G_MONTH);
368

    
369
        /// <summary>
370
        /// ItemType representing the primitive type xs:gMonthDay
371
        /// </summary>
372

    
373
        public static XdmAtomicType G_MONTH_DAY = BuiltInAtomicType(JStandardNames.XS_G_MONTH_DAY);
374

    
375
        /// <summary>
376
        /// ItemType representing the primitive type xs:gYear
377
        /// </summary>
378

    
379
        public static XdmAtomicType G_YEAR = BuiltInAtomicType(JStandardNames.XS_G_YEAR);
380

    
381
        /// <summary>
382
        /// ItemType representing the primitive type xs:gDay
383
        /// </summary>
384

    
385
        public static XdmAtomicType G_DAY = BuiltInAtomicType(JStandardNames.XS_G_DAY);
386

    
387
        /// <summary>
388
        /// ItemType representing the primitive type xs:hexBinary
389
        /// </summary>
390

    
391
        public static XdmAtomicType HEX_BINARY = BuiltInAtomicType(JStandardNames.XS_HEX_BINARY);
392

    
393
        /// <summary>
394
        ///  ItemType representing the primitive type xs:base64Binary
395
        /// </summary>
396

    
397
        public static XdmAtomicType BASE64_BINARY = BuiltInAtomicType(JStandardNames.XS_BASE64_BINARY);
398

    
399
        /// <summary>
400
        /// ItemType representing the primitive type xs:anyURI
401
        /// </summary>
402

    
403
        public static XdmAtomicType ANY_URI = BuiltInAtomicType(JStandardNames.XS_ANY_URI);
404

    
405
        /// <summary>
406
        /// ItemType representing the primitive type xs:QName
407
        /// </summary>
408

    
409
        public static XdmAtomicType QNAME = BuiltInAtomicType(JStandardNames.XS_QNAME);
410

    
411
        /// <summary>
412
        /// ItemType representing the primitive type xs:NOTATION
413
        /// </summary>
414

    
415
        public static XdmAtomicType NOTATION = BuiltInAtomicType(JStandardNames.XS_NOTATION);
416

    
417
        /// <summary>
418
        ///  ItemType representing the XPath-defined type xs:untypedAtomic
419
        /// </summary>
420

    
421
        public static XdmAtomicType UNTYPED_ATOMIC = BuiltInAtomicType(JStandardNames.XS_UNTYPED_ATOMIC);
422

    
423
        /// <summary>
424
        /// ItemType representing the primitive type xs:decimal
425
        /// </summary>
426

    
427
        public static XdmAtomicType DECIMAL = BuiltInAtomicType(JStandardNames.XS_DECIMAL);
428

    
429
        /// <summary>
430
        /// ItemType representing the primitive type xs:float
431
        /// </summary>
432

    
433
        public static XdmAtomicType FLOAT = BuiltInAtomicType(JStandardNames.XS_FLOAT);
434

    
435
        /// <summary>
436
        /// ItemType representing the primitive type xs:double
437
        /// </summary>
438

    
439
        public static XdmAtomicType DOUBLE = BuiltInAtomicType(JStandardNames.XS_DOUBLE);
440

    
441
        /// <summary>
442
        /// ItemType representing the built-in (but non-primitive) type xs:integer
443
        /// </summary>
444

    
445
        public static XdmAtomicType INTEGER = BuiltInAtomicType(JStandardNames.XS_INTEGER);
446

    
447
        /// <summary>
448
        /// 
449
        /// </summary>
450

    
451
        public static XdmAtomicType NON_POSITIVE_INTEGER = BuiltInAtomicType(JStandardNames.XS_NON_POSITIVE_INTEGER);
452

    
453
        /// <summary>
454
        /// ItemType representing the built-in (but non-primitive) type xs:negativeInteger
455
        /// </summary>
456

    
457
        public static XdmAtomicType NEGATIVE_INTEGER = BuiltInAtomicType(JStandardNames.XS_NEGATIVE_INTEGER);
458

    
459
        /// <summary>
460
        /// ItemType representing the built-in (but non-primitive) type xs:long
461
        /// </summary>
462

    
463
        public static XdmAtomicType LONG = BuiltInAtomicType(JStandardNames.XS_LONG);
464

    
465
        /// <summary>
466
        /// ItemType representing the built-in (but non-primitive) type xs:int
467
        /// </summary>
468

    
469
        public static XdmAtomicType INT = BuiltInAtomicType(JStandardNames.XS_INT);
470

    
471

    
472
        /// <summary>
473
        /// ItemType representing the built-in (but non-primitive) type xs:byte
474
        /// </summary>
475

    
476
        public static XdmAtomicType BYTE = BuiltInAtomicType(JStandardNames.XS_BYTE);
477

    
478
        /// <summary>
479
        /// ItemType representing the built-in (but non-primitive) type xs:nonNegativeInteger
480
        /// </summary>
481

    
482
        public static XdmAtomicType NON_NEGATIVE_INTEGER = BuiltInAtomicType(JStandardNames.XS_NON_NEGATIVE_INTEGER);
483

    
484
        /// <summary>
485
        /// ItemType representing the built-in (but non-primitive) type xs:positiveInteger
486
        /// </summary>
487

    
488
        public static XdmAtomicType POSITIVE_INTEGER = BuiltInAtomicType(JStandardNames.XS_POSITIVE_INTEGER);
489

    
490
        /// <summary>
491
        /// ItemType representing the built-in (but non-primitive) type xs:unsignedLong
492
        /// </summary>
493

    
494
        public static XdmAtomicType UNSIGNED_LONG = BuiltInAtomicType(JStandardNames.XS_UNSIGNED_LONG);
495

    
496
        /// <summary>
497
        /// ItemType representing the built-in (but non-primitive) type xs:unsignedInt
498
        /// </summary>
499

    
500
        public static XdmAtomicType UNSIGNED_INT = BuiltInAtomicType(JStandardNames.XS_UNSIGNED_INT);
501

    
502
        /// <summary>
503
        /// ItemType representing the built-in (but non-primitive) type xs:unsignedShort
504
        /// </summary>
505

    
506
        public static XdmAtomicType UNSIGNED_SHORT = BuiltInAtomicType(JStandardNames.XS_UNSIGNED_SHORT);
507

    
508
        /// <summary>
509
        /// ItemType representing the built-in (but non-primitive) type xs:unsignedByte
510
        /// </summary>
511

    
512
        public static XdmAtomicType UNSIGNED_BYTE = BuiltInAtomicType(JStandardNames.XS_UNSIGNED_BYTE);
513

    
514
        /// <summary>
515
        /// ItemType representing the built-in (but non-primitive) type xs:yearMonthDuration
516
        /// </summary>
517

    
518
        public static XdmAtomicType YEAR_MONTH_DURATION = BuiltInAtomicType(JStandardNames.XS_YEAR_MONTH_DURATION);
519

    
520
        /// <summary>
521
        /// ItemType representing the built-in (but non-primitive) type xs:dayTimeDuration
522
        /// </summary>
523

    
524
        public static XdmAtomicType DAY_TIME_DURATION = BuiltInAtomicType(JStandardNames.XS_DAY_TIME_DURATION);
525

    
526
        /// <summary>
527
        /// ItemType representing the built-in (but non-primitive) type xs:normalizedString
528
        /// </summary>
529

    
530
        public static XdmAtomicType NORMALIZED_STRING = BuiltInAtomicType(JStandardNames.XS_NORMALIZED_STRING);
531

    
532
        /// <summary>
533
        /// ItemType representing the built-in (but non-primitive) type xs:token
534
        /// </summary>
535

    
536
        public static XdmAtomicType TOKEN = BuiltInAtomicType(JStandardNames.XS_TOKEN);
537

    
538
        /// <summary>
539
        /// ItemType representing the built-in (but non-primitive) type xs:language
540
        /// </summary>
541

    
542
        public static XdmAtomicType LANGUAGE = BuiltInAtomicType(JStandardNames.XS_LANGUAGE);
543

    
544
        /// <summary>
545
        /// ItemType representing the built-in (but non-primitive) type xs:Name
546
        /// </summary>
547

    
548
        public static XdmAtomicType NAME = BuiltInAtomicType(JStandardNames.XS_NAME);
549

    
550
        /// <summary>
551
        /// ItemType representing the built-in (but non-primitive) type xs:NMTOKEN
552
        /// </summary>
553

    
554
        public static XdmAtomicType NMTOKEN = BuiltInAtomicType(JStandardNames.XS_NMTOKEN);
555

    
556
        /// <summary>
557
        /// ItemType representing the built-in (but non-primitive) type xs:NCName
558
        /// </summary>
559

    
560
        public static XdmAtomicType NCNAME = BuiltInAtomicType(JStandardNames.XS_NCNAME);
561

    
562
        /// <summary>
563
        /// ItemType representing the built-in (but non-primitive) type xs:ID
564
        /// </summary>
565

    
566
        public static XdmAtomicType ID = BuiltInAtomicType(JStandardNames.XS_ID);
567

    
568
        /// <summary>
569
        /// ItemType representing the built-in (but non-primitive) type xs:IDREF
570
        /// </summary>
571

    
572
        public static XdmAtomicType IDREF = BuiltInAtomicType(JStandardNames.XS_IDREF);
573

    
574
        /// <summary>
575
        /// ItemType representing the built-in (but non-primitive) type xs:ENTITY
576
        /// </summary>
577

    
578
        public static XdmAtomicType ENTITY = BuiltInAtomicType(JStandardNames.XS_ENTITY);
579

    
580
        /// <summary>
581
        /// ItemType representing the built-in (but non-primitive) type xs:dateTimeStamp
582
        /// (introduced in XSD 1.1)
583
        /// </summary>
584

    
585
        public static XdmAtomicType DATE_TIME_STAMP = BuiltInAtomicType(JStandardNames.XS_DATE_TIME_STAMP);
586

    
587

    
588
        /// <summary>
589
        /// Get an <c>XdmAtomicType</c> object representing a built-in atomic type with a given name.
590
        /// </summary>
591
        /// <param name="name">The name of the required built-in atomic type</param>
592
        /// <returns>An <c>XdmAtomicType</c> object representing the built-in atomic type with the supplied name.
593
        /// Returns null if there is no built-in atomic type with this name.
594
        /// It is undefined whether two requests for the same built-in type will return the same object.</returns>
595

    
596

    
597
        public static XdmAtomicType BuiltInAtomicType(QName name) {
598
            int fingerprint = JStandardNames.getFingerprint(name.Uri, name.LocalName);
599
            return BuiltInAtomicType(fingerprint);
600
        }
601

    
602
        internal static XdmAtomicType BuiltInAtomicType(int fingerprint)
603
		{
604

    
605
			if (fingerprint == -1)
606
			{
607
				return null;
608
			}
609
            switch (fingerprint) {
610
                case JStandardNames.XS_ANY_ATOMIC_TYPE:
611
                    return XdmAtomicType.Instance;
612

    
613
                case JStandardNames.XS_STRING:
614
                    return new XdmAtomicType(JXdmItemType.STRING);
615

    
616
                case JStandardNames.XS_BOOLEAN:
617
                    return new XdmAtomicType(JXdmItemType.BOOLEAN);
618

    
619
                case JStandardNames.XS_DURATION:
620
                    return new XdmAtomicType(JXdmItemType.DURATION);
621

    
622
                case JStandardNames.XS_DATE_TIME:
623
                    return new XdmAtomicType(JXdmItemType.DATE_TIME);
624

    
625
                case JStandardNames.XS_DATE:
626
                    return new XdmAtomicType(JXdmItemType.DATE);
627

    
628
                case JStandardNames.XS_TIME:
629
                    return new XdmAtomicType(JXdmItemType.TIME);
630

    
631
                case JStandardNames.XS_G_YEAR_MONTH:
632
                    return new XdmAtomicType(JXdmItemType.G_YEAR_MONTH);
633

    
634
                case JStandardNames.XS_G_MONTH:
635
                    return new XdmAtomicType(JXdmItemType.G_MONTH);
636

    
637
                case JStandardNames.XS_G_MONTH_DAY:
638
                    return new XdmAtomicType(JXdmItemType.G_MONTH_DAY);
639

    
640
                case JStandardNames.XS_G_YEAR:
641
                    return new XdmAtomicType(JXdmItemType.G_YEAR);
642

    
643
                case JStandardNames.XS_G_DAY:
644
                    return new XdmAtomicType(JXdmItemType.G_DAY);
645

    
646
                case JStandardNames.XS_HEX_BINARY:
647
                    return new XdmAtomicType(JXdmItemType.HEX_BINARY);
648

    
649
                case JStandardNames.XS_BASE64_BINARY:
650
                    return new XdmAtomicType(JXdmItemType.BASE64_BINARY);
651

    
652
                case JStandardNames.XS_ANY_URI:
653
                    return new XdmAtomicType(JXdmItemType.ANY_URI);
654

    
655
                case JStandardNames.XS_QNAME:
656
                    return new XdmAtomicType(JXdmItemType.QNAME);
657

    
658
                case JStandardNames.XS_NOTATION:
659
                    return new XdmAtomicType(JXdmItemType.NOTATION);
660

    
661
                case JStandardNames.XS_UNTYPED_ATOMIC:
662
                    return new XdmAtomicType(JXdmItemType.UNTYPED_ATOMIC);
663

    
664
                case JStandardNames.XS_DECIMAL:
665
                    return new XdmAtomicType(JXdmItemType.DECIMAL);
666

    
667
                case JStandardNames.XS_FLOAT:
668
                    return new XdmAtomicType(JXdmItemType.FLOAT);
669

    
670
                case JStandardNames.XS_DOUBLE:
671
                    return new XdmAtomicType(JXdmItemType.DOUBLE);
672

    
673
                case JStandardNames.XS_INTEGER:
674
                    return new XdmAtomicType(JXdmItemType.INTEGER);
675

    
676
                case JStandardNames.XS_NON_POSITIVE_INTEGER:
677
                    return new XdmAtomicType(JXdmItemType.NON_POSITIVE_INTEGER);
678

    
679
                case JStandardNames.XS_NEGATIVE_INTEGER:
680
                    return new XdmAtomicType(JXdmItemType.NEGATIVE_INTEGER);
681

    
682
                case JStandardNames.XS_LONG:
683
                    return new XdmAtomicType(JXdmItemType.LONG);
684

    
685
                case JStandardNames.XS_INT:
686
                    return new XdmAtomicType(JXdmItemType.INT);
687

    
688
                case JStandardNames.XS_SHORT:
689
                    return new XdmAtomicType(JXdmItemType.SHORT);
690

    
691
                case JStandardNames.XS_BYTE:
692
                    return new XdmAtomicType(JXdmItemType.BYTE);
693

    
694
                case JStandardNames.XS_NON_NEGATIVE_INTEGER:
695
                    return new XdmAtomicType(JXdmItemType.NON_NEGATIVE_INTEGER);
696

    
697
                case JStandardNames.XS_POSITIVE_INTEGER:
698
                    return new XdmAtomicType(JXdmItemType.POSITIVE_INTEGER);
699

    
700
                case JStandardNames.XS_UNSIGNED_LONG:
701
                    return new XdmAtomicType(JXdmItemType.UNSIGNED_LONG);
702

    
703
                case JStandardNames.XS_UNSIGNED_INT:
704
                    return new XdmAtomicType(JXdmItemType.UNSIGNED_INT);
705

    
706
                case JStandardNames.XS_UNSIGNED_SHORT:
707
                    return new XdmAtomicType(JXdmItemType.UNSIGNED_SHORT);
708

    
709
                case JStandardNames.XS_UNSIGNED_BYTE:
710
                    return new XdmAtomicType(JXdmItemType.UNSIGNED_BYTE);
711

    
712
                case JStandardNames.XS_YEAR_MONTH_DURATION:
713
                    return new XdmAtomicType(JXdmItemType.YEAR_MONTH_DURATION);
714

    
715
                case JStandardNames.XS_DAY_TIME_DURATION:
716
                    return new XdmAtomicType(JXdmItemType.DAY_TIME_DURATION);
717

    
718
                case JStandardNames.XS_NORMALIZED_STRING:
719
                    return new XdmAtomicType(JXdmItemType.NORMALIZED_STRING);
720

    
721
                case JStandardNames.XS_TOKEN:
722
                    return new XdmAtomicType(JXdmItemType.TOKEN);
723

    
724
                case JStandardNames.XS_LANGUAGE:
725
                    return new XdmAtomicType(JXdmItemType.LANGUAGE);
726

    
727
                case JStandardNames.XS_NAME:
728
                    return new XdmAtomicType(JXdmItemType.NAME);
729

    
730
                case JStandardNames.XS_NMTOKEN:
731
                    return new XdmAtomicType(JXdmItemType.NMTOKEN);
732

    
733
                case JStandardNames.XS_NCNAME:
734
                    return new XdmAtomicType(JXdmItemType.NCNAME);
735

    
736
                case JStandardNames.XS_ID:
737
                    return new XdmAtomicType(JXdmItemType.ID);
738

    
739
                case JStandardNames.XS_IDREF:
740
                    return new XdmAtomicType(JXdmItemType.IDREF);
741

    
742
                case JStandardNames.XS_ENTITY:
743
                    return new XdmAtomicType(JXdmItemType.ENTITY);
744

    
745
                case JStandardNames.XS_DATE_TIME_STAMP:
746
                    return new XdmAtomicType(JXdmItemType.DATE_TIME_STAMP);
747

    
748

    
749
                case JStandardNames.XS_ERROR:
750
                    return null;
751

    
752
                default:
753
                    throw new StaticError(new net.sf.saxon.s9api.SaxonApiException("Unknown atomic type " + JStandardNames.getClarkName(fingerprint)));
754

    
755

    
756
            }
757
            
758
		}
759

    
760
    
761

    
762
        /// <summary>
763
        /// The name of the atomic type, or null if the type is anonymous.
764
        /// </summary>
765

    
766
        public QName Name
767
		{
768
			get
769
			{
770
				JStructuredQName jQName = ((JAtomicType)type).getTypeName();
771
				if (jQName == null)
772
				{
773
					return null;
774
				}
775
				return new QName(jQName.getPrefix(), jQName.getURI(), jQName.getLocalPart());
776

    
777

    
778
            }
779
		}
780

    
781
        /// <summary>
782
        /// Determine whether this item type matches a given item
783
        /// </summary>
784
        /// <param name="item">the item to be tested against this item type</param>
785
        /// <returns>true if the item matches this item type, false if it does not match</returns>
786
        public override bool Matches(XdmItem item)
787
        {
788
            return type.matches(XdmItem.FromXdmItemItemToJXdmItem(item));
789
        }
790

    
791

    
792
        /// <summary>
793
        /// Determine whether this ItemType subsumes another ItemType. Specifically
794
        /// <code>A.Sumsumes(B)</code> is true if every value that matches the ItemType B also
795
        /// matches the ItemType A.
796
        /// </summary>
797
        /// <param name="other">the other ItemType</param>
798
        /// <returns>true if this ItemType subsumes the other ItemType. This includes the case where A and B
799
        /// represent the same ItemType.</returns>
800
        public override bool Subsumes(XdmItemType other)
801
        {
802
            return type.subsumes(other.Unwrap());
803
        }
804
    }
805

    
806
	/// <summary>
807
	/// Instances of <c>XdmNodeKind</c> represent the item types denoted in XPath as <c>document-node()</c>,
808
	/// <c>element()</c>, <c>attribute()</c>, <c>text()</c>, and so on. These are all represented by singular named instances.
809
	/// </summary>
810

    
811
	public class XdmNodeKind : XdmItemType
812
	{
813

    
814
        //public static XdmNodeKind Instance = new XdmNodeKind();
815
        private JNodeKindTest nodekind;
816
        private int kind;
817

    
818
        internal XdmNodeKind(JNodeKindTest test, JXdmItemType type)
819
		{
820
			nodekind = test;
821
            this.type = type;
822
            kind = test.getNodeKind();
823
        }
824

    
825
		/// <summary>
826
		/// The item type <c>document-node()</c>
827
		/// </summary>
828

    
829
		public static XdmNodeKind Document = new XdmNodeKind(JNodeKindTest.DOCUMENT, JXdmItemType.DOCUMENT_NODE);
830

    
831
		/// <summary>
832
		/// The item type <c>element()</c>
833
		/// </summary>
834

    
835
		public static XdmNodeKind Element = new XdmNodeKind(JNodeKindTest.ELEMENT, JXdmItemType.ELEMENT_NODE);
836

    
837
		/// <summary>
838
		/// The item type <c>attribute()</c>
839
		/// </summary>
840

    
841
		public static XdmNodeKind Attribute = new XdmNodeKind(JNodeKindTest.ATTRIBUTE, JXdmItemType.ATTRIBUTE_NODE);
842

    
843
		/// <summary>
844
		/// The item type <c>text()</c>
845
		/// </summary>
846

    
847
		public static XdmNodeKind Text = new XdmNodeKind(JNodeKindTest.TEXT, JXdmItemType.TEXT_NODE);
848

    
849
		/// <summary>
850
		/// The item type <c>comment()</c>
851
		/// </summary>
852

    
853
		public static XdmNodeKind Comment = new XdmNodeKind(JNodeKindTest.COMMENT, JXdmItemType.COMMENT_NODE);
854

    
855
		/// <summary>
856
		/// The item type <c>processing-instruction()</c>
857
		/// </summary>
858

    
859
		public static XdmNodeKind ProcessingInstruction = new XdmNodeKind(JNodeKindTest.PROCESSING_INSTRUCTION, JXdmItemType.PROCESSING_INSTRUCTION_NODE);
860

    
861
		/// <summary>
862
		/// The item type <c>namespace-node()</c>
863
		/// </summary>
864

    
865
		public static XdmNodeKind Namespace = new XdmNodeKind(JNodeKindTest.NAMESPACE, JXdmItemType.NAMESPACE_NODE);
866

    
867
		internal static XdmNodeKind ForNodeKindTest(JNodeKindTest test)
868
		{
869
			int kind = test.getPrimitiveType();
870
			switch (kind)
871
			{
872
			case JType.DOCUMENT:
873
				return Document;
874
			case JType.ELEMENT:
875
				return Element;
876
			case JType.ATTRIBUTE:
877
				return Attribute;
878
			case JType.TEXT:
879
				return Text;
880
			case JType.COMMENT:
881
				return Comment;
882
			case JType.PROCESSING_INSTRUCTION:
883
				return ProcessingInstruction;
884
			case JType.NAMESPACE:
885
				return Namespace;
886
			default:
887
				throw new ArgumentException("Unknown node kind");
888
			}
889
		}
890

    
891
		/// <summary>
892
		/// Get the item type representing the node kind of a supplied node
893
		/// </summary>
894
		/// <param name="node">The node whose node kind is required</param>
895
		/// <returns>The relevant node kind</returns>
896

    
897
		public static XdmNodeKind ForNode(XdmNode node)
898
		{
899
			return ForNodeType(node.NodeKind);
900
		}
901

    
902
        /// <summary>
903
        /// Determine whether this item type matches a given item.
904
        /// </summary>
905
        /// <param name="item">item the item to be tested against this item type</param>
906
        /// <returns>true if the item matches this item type, false if it does not match.</returns>
907
        public override bool Matches(XdmItem item)
908
        {
909
            return item.Unwrap() is JNodeTest && kind == ((net.sf.saxon.om.NodeInfo)item.Unwrap()).getNodeKind();
910
        }
911

    
912
        /// <summary>
913
        ///  Determine whether this ItemType subsumes another ItemType. Specifically,
914
        /// <code>A.subsumes(B)</code> is true if every value that matches the ItemType B also matches
915
        /// the ItemType A.
916
        /// </summary>
917
		/// <param name="other">the other ItemType</param>
918
        /// <returns>true if this ItemType subsumes the other ItemType. This includes the case where A and B
919
        ///         represent the same ItemType.</returns>
920
        public override bool Subsumes(XdmItemType other)
921
        {
922
            return true;
923
        }
924

    
925
        /// <summary>
926
		/// Get the item type corresponding to an <c>XmlNodeType</c> as defined in the <c>System.Xml</c> package
927
        /// </summary>
928
        /// <param name="type">The <c>XmlNodeType</c> to be converted</param>
929
        /// <returns>The corresponding <c>XdmNodeKind</c></returns>
930

    
931
        public static XdmNodeKind ForNodeType(XmlNodeType type)
932
		{
933
			switch (type)
934
			{
935
			case XmlNodeType.Document:
936
				return Document;
937
			case XmlNodeType.Element:
938
				return Element;
939
			case XmlNodeType.Attribute:
940
				return Attribute;
941
			case XmlNodeType.Text:
942
				return Text;
943
			case XmlNodeType.Comment:
944
				return Comment;
945
			case XmlNodeType.ProcessingInstruction:
946
				return ProcessingInstruction;
947
			default:
948
				throw new ArgumentException("Unknown node kind");
949
			}
950
		}
951
	}
952

    
953
	/// <summary>
954
	/// An instance of class <c>XdmSequenceType</c> represents a sequence type, that is, the combination
955
	/// of an item type and an occurrence indicator.
956
	/// </summary>
957

    
958
	public class XdmSequenceType
959
	{
960

    
961
		internal XdmItemType itemType;
962
		internal char occurrenceIn;
963
        internal int occurrence;
964

    
965

    
966
		public const char ZERO_OR_MORE='*';
967
		public const char ONE_OR_MORE = '+';
968
		public const char ZERO_OR_ONE = '?';
969
		public const char ZERO = '0'; //xBA
970
		public const char ONE = ' ';
971

    
972
		/// <summary>
973
		/// Create an <c>XdmSequenceType</c> corresponding to a given <c>XdmItemType</c> and occurrence indicator
974
		/// </summary>
975
		/// <param name="itemType">The item type</param>
976
		/// <param name="occurrenceIndicator">The occurrence indicator, one of '?' (zero-or-one), 
977
		/// '*' (zero-or-more), '+' (one-or-more), ' ' (a single space) (exactly one),
978
		/// or 'º' (masculine ordinal indicator, xBA) (exactly zero). The type <c>empty-sequence()</c>
979
		/// can be represented by an occurrence indicator of 'º' with any item type.</param>
980

    
981
		public XdmSequenceType(XdmItemType itemType, char occurrenceIndicator)
982
		{
983
			int occ;
984
			switch (occurrenceIndicator)
985
			{
986
			case ZERO_OR_MORE:
987
				occ = JStaticProperty.ALLOWS_ZERO_OR_MORE;
988
				break;
989
			case ONE_OR_MORE:
990
				occ = JStaticProperty.ALLOWS_ONE_OR_MORE;
991
				break;
992
			case ZERO_OR_ONE:
993
				occ = JStaticProperty.ALLOWS_ZERO_OR_ONE;
994
				break;
995
			case ONE:
996

    
997
				occ = JStaticProperty.EXACTLY_ONE;
998
				break;
999
			case ZERO:
1000
				occ = JStaticProperty.ALLOWS_ZERO;
1001
				break;
1002
			default:
1003
				throw new ArgumentException("Unknown occurrence indicator");
1004
			}
1005
			this.itemType = itemType;
1006
            this.occurrence = occ;
1007
            this.occurrenceIn = occurrenceIndicator;
1008
        }
1009

    
1010
		internal static XdmSequenceType FromSequenceType(JSequenceType seqType)
1011
		{
1012
			XdmItemType itemType = XdmItemType.MakeXdmItemType(seqType.getPrimaryType());
1013
			char occ;
1014
			if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO_OR_MORE)
1015
			{
1016
				occ = ZERO_OR_MORE;
1017
			}
1018
			else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ONE_OR_MORE)
1019
			{
1020
				occ = ONE_OR_MORE;
1021
			}
1022
			else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO_OR_ONE)
1023
			{
1024
				occ = ZERO_OR_ONE;
1025
			}
1026
			else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO)
1027
			{
1028
				occ = ZERO;
1029
			}
1030
			else
1031
			{
1032
				occ = ONE;
1033
			}
1034
            if (itemType == null) {
1035
                return null;
1036
            }
1037
			return new XdmSequenceType(itemType, occ);
1038
		}
1039

    
1040
       
1041

    
1042
        internal JSequenceType ToSequenceType() {
1043
            JItemTypeType jit = itemType.Unwrap().getUnderlyingItemType();
1044
			return JSequenceType.makeSequenceType(jit, occurrence);
1045
		}
1046

    
1047
        internal JOccurrenceIndicator GetJOccurrenceIndicator(int cardinality) {
1048
            switch (cardinality) {
1049
                case JStaticProperty.EMPTY:
1050
                    return JOccurrenceIndicator.ZERO;
1051
                case JStaticProperty.ALLOWS_ZERO_OR_ONE:
1052
                    return JOccurrenceIndicator.ZERO_OR_ONE;
1053
                case JStaticProperty.ALLOWS_ZERO_OR_MORE:
1054
                    return JOccurrenceIndicator.ZERO_OR_MORE;
1055
                case JStaticProperty.ALLOWS_ONE:
1056
                    return JOccurrenceIndicator.ONE;
1057
                case JStaticProperty.ALLOWS_ONE_OR_MORE:
1058
                    return JOccurrenceIndicator.ONE_OR_MORE;
1059
                default:
1060
                    return JOccurrenceIndicator.ZERO_OR_MORE;
1061
            }
1062
        }
1063

    
1064
        internal JXdmSequenceType ToJXdmSequenceType()
1065
        {
1066
            JItemTypeType jit = itemType.Unwrap().getUnderlyingItemType();
1067
            return JXdmSequenceType.makeSequenceType(itemType.Unwrap(), GetJOccurrenceIndicator(occurrence));
1068
        }
1069

    
1070

    
1071
    }
1072

    
1073

    
1074
}
1075

    
1076
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1077
// Copyright (c) 2020 Saxonica Limited.
1078
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
1079
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
1080
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
1081
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
(8-8/11)