Project

Profile

Help

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

he / tags / 9.6.0.7 / hen / csource / api / Saxon.Api / Types.cs @ aa733b18

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 JItemType = net.sf.saxon.type.ItemType;
7
using JAnyItemType = net.sf.saxon.type.AnyItemType;
8
using JNodeTest = net.sf.saxon.pattern.NodeTest;
9
using JAnyNodeType = net.sf.saxon.pattern.AnyNodeTest;
10
using JAnyFunctionType = net.sf.saxon.type.AnyFunctionType;
11
using JErrorType = net.sf.saxon.type.ErrorType;
12
using JFunctionItemType = net.sf.saxon.type.FunctionItemType;
13
using JNodeKindTest = net.sf.saxon.pattern.NodeKindTest;
14
using JAtomicType = net.sf.saxon.type.AtomicType;
15
using JBuiltInAtomicType = net.sf.saxon.type.BuiltInAtomicType;
16
using JBuiltInType = net.sf.saxon.type.BuiltInType;
17
using JSequenceType = net.sf.saxon.value.SequenceType;
18
using JStandardNames = net.sf.saxon.om.StandardNames;
19
using JStructuredQName = net.sf.saxon.om.StructuredQName;
20
using JCardinality = net.sf.saxon.value.Cardinality;
21
using JStaticProperty = net.sf.saxon.expr.StaticProperty;
22

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

    
35
    public abstract class XdmItemType
36
    {
37
        protected JItemType type;
38

    
39
        internal static XdmItemType MakeXdmItemType(JItemType type)
40
        {
41
            if (type.isAtomicType())
42
            {
43
                return new XdmAtomicType(((JAtomicType)type));
44
            }
45
            else if (type is JErrorType)
46
            {
47
                return XdmAnyNodeType.Instance;  // TODO: need to represent this properly
48
            }
49
            else if (type is JNodeTest)
50
            {
51
                if (type is JAnyNodeType)
52
                {
53
                    return XdmAnyNodeType.Instance;
54
                }
55
                else
56
                {
57
                    int kind = ((JNodeTest)type).getPrimitiveType();
58
                    Console.WriteLine("Kind " + kind);
59
                    return XdmNodeKind.ForNodeKindTest((JNodeKindTest)JNodeKindTest.makeNodeKindTest(kind));
60
                }
61
            }
62
            else if (type is JAnyItemType)
63
            {
64
                return XdmAnyItemType.Instance;
65
            }
66
            else if (type is JFunctionItemType)
67
            {
68
                return XdmAnyFunctionType.Instance;
69
            }
70
            else
71
            {
72
                return null;
73
            }
74
        }
75

    
76
        internal JItemType Unwrap() {
77
            return type;
78
        }
79
    }
80

    
81
        /// <summary>
82
        /// Singleton class representing the item type item(), which matches any item.
83
        /// </summary>
84

    
85
        public class XdmAnyItemType : XdmItemType
86
        {
87

    
88
            /// <summary>
89
            /// The singleton instance of this class: an <c>XdmItemType</c> corresponding to the
90
            /// item type <c>item()</c>, which matches any item.
91
            /// </summary>
92

    
93
            public static XdmAnyItemType Instance = new XdmAnyItemType();
94

    
95
            internal XdmAnyItemType()
96
            {
97
                this.type =  JAnyItemType.getInstance();
98
            }
99
        }
100

    
101

    
102
        /// <summary>
103
        /// Singleton class representing the item type node(), which matches any node.
104
        /// </summary>
105

    
106
        public class XdmAnyNodeType : XdmItemType
107
        {
108

    
109
            /// <summary>
110
            /// The singleton instance of this class: an <c>XdmItemType</c> corresponding to the
111
            /// item type <c>node()</c>, which matches any node.
112
            /// </summary>
113

    
114
            public static XdmAnyNodeType Instance = new XdmAnyNodeType();
115

    
116
            internal XdmAnyNodeType()
117
            {
118
                this.type = JAnyNodeType.getInstance();
119
            }
120
        }
121

    
122

    
123
        /// <summary>
124
        /// Singleton class representing the item type function(), which matches any function item.
125
        /// </summary>
126

    
127
        public class XdmAnyFunctionType : XdmItemType
128
        {
129

    
130
            /// <summary>
131
            /// The singleton instance of this class: an <c>XdmItemType</c> corresponding to the
132
            /// item type <c>function()</c>, which matches any function item.
133
            /// </summary>
134

    
135
            public static XdmAnyFunctionType Instance = new XdmAnyFunctionType();
136

    
137
            internal XdmAnyFunctionType()
138
            {
139
                this.type = JAnyFunctionType.getInstance();
140
            }
141
        }
142

    
143
        /// <summary>
144
        /// An instance of class <c>XdmAtomicType</c> represents a specific atomic type, for example
145
        /// <c>xs:double</c>, <c>xs:integer</c>, or <c>xs:anyAtomicType</c>. This may be either a built-in
146
        /// atomic type or a type defined in a user-written schema.
147
        /// </summary>
148
        /// <remarks>
149
        /// To get an <c>XdmAtomicType</c> instance representing a built-in atomic type, use one of the predefined instances
150
        /// of the subclass <c>XdmBuiltInAtomicType</c>. To get an <c>XdmAtomicType</c> instance representing a user-defined
151
        /// atomic type (defined in a schema), use the method <c>GetAtomicType</c> defined on the <c>SchemaManager</c> class.
152
        /// </remarks>
153

    
154
        public class XdmAtomicType : XdmItemType
155
        {
156

    
157
            internal XdmAtomicType(JAtomicType type)
158
            {
159
                this.type = type;
160
            }
161

    
162
            /// <summary>
163
            /// Get an XdmAtomicType object representing a built-in atomic type with a given name
164
            /// </summary>
165
            /// <param name="name">The name of the required built-in atomic type</param>
166
            /// <returns>An XdmAtomicType object representing the built-in atomic type with the supplied name.
167
            /// Returns null if there is no built-in atomic type with this name.
168
            /// It is undefined whether two requests for the same built-in type will return the same object.</returns>
169

    
170
            public static XdmAtomicType BuiltInAtomicType(QName name)
171
            {
172
                int fingerprint = JStandardNames.getFingerprint(name.Uri, name.LocalName);
173
                if (fingerprint == -1)
174
                {
175
                    return null;
176
                }
177
                JAtomicType jat = (JAtomicType)JBuiltInType.getSchemaType(fingerprint);
178
                if (jat == null)
179
                {
180
                    return null;
181
                }
182
                return new XdmAtomicType((JAtomicType)JBuiltInType.getSchemaType(fingerprint));
183
            }
184

    
185
            /// <summary>
186
            /// The name of the atomic type, or null if the type is anonymous
187
            /// </summary>
188

    
189
            public QName Name
190
            {
191
                get
192
                {
193
                    JStructuredQName jQName = ((JAtomicType)type).getTypeName();
194
                    if (jQName == null)
195
                    {
196
                        return null;
197
                    }
198
                    return new QName(jQName.getPrefix(), jQName.getURI(), jQName.getLocalPart());
199
                }
200
            }
201
        }
202

    
203
        /// <summary>
204
        /// Instances of <c>XdmNodeKind</c> represent the item types denoted in XPath as <c>document-node()</c>,
205
        /// <c>element()</c>, <c>attribute()</c>, <c>text()</c>, and so on. These are all represented by singular named instances.
206
        /// </summary>
207

    
208
        public class XdmNodeKind : XdmItemType
209
        {
210

    
211
            internal XdmNodeKind(JNodeKindTest test)
212
            {
213
                type = test;
214
            }
215

    
216
            /// <summary>
217
            /// The item type <c>document-node()</c>
218
            /// </summary>
219

    
220
            public static XdmNodeKind Document = new XdmNodeKind(JNodeKindTest.DOCUMENT);
221

    
222
            /// <summary>
223
            /// The item type <c>element()</c>
224
            /// </summary>
225

    
226
            public static XdmNodeKind Element = new XdmNodeKind(JNodeKindTest.ELEMENT);
227

    
228
            /// <summary>
229
            /// The item type <c>attribute()</c>
230
            /// </summary>
231

    
232
            public static XdmNodeKind Attribute = new XdmNodeKind(JNodeKindTest.ATTRIBUTE);
233

    
234
            /// <summary>
235
            /// The item type <c>text()</c>
236
            /// </summary>
237

    
238
            public static XdmNodeKind Text = new XdmNodeKind(JNodeKindTest.TEXT);
239

    
240
            /// <summary>
241
            /// The item type <c>comment()</c>
242
            /// </summary>
243

    
244
            public static XdmNodeKind Comment = new XdmNodeKind(JNodeKindTest.COMMENT);
245

    
246
            /// <summary>
247
            /// The item type <c>processing-instruction()</c>
248
            /// </summary>
249

    
250
            public static XdmNodeKind ProcessingInstruction = new XdmNodeKind(JNodeKindTest.PROCESSING_INSTRUCTION);
251

    
252
            /// <summary>
253
            /// The item type <c>namespace-node()</c>
254
            /// </summary>
255

    
256
            public static XdmNodeKind Namespace = new XdmNodeKind(JNodeKindTest.NAMESPACE);
257

    
258
            internal static XdmNodeKind ForNodeKindTest(JNodeKindTest test)
259
            {
260
                int kind = test.getPrimitiveType();
261
                switch (kind)
262
                {
263
                    case JType.DOCUMENT:
264
                        return Document;
265
                    case JType.ELEMENT:
266
                        return Element;
267
                    case JType.ATTRIBUTE:
268
                        return Attribute;
269
                    case JType.TEXT:
270
                        return Text;
271
                    case JType.COMMENT:
272
                        return Comment;
273
                    case JType.PROCESSING_INSTRUCTION:
274
                        return ProcessingInstruction;
275
                    case JType.NAMESPACE:
276
                        return Namespace;
277
                    default:
278
                        throw new ArgumentException("Unknown node kind");
279
                }
280
            }
281

    
282
            /// <summary>
283
            /// Get the item type representing the node kind of a supplied node
284
            /// </summary>
285
            /// <param name="node">The node whose node kind is required</param>
286
            /// <returns>The relevant node kind</returns>
287

    
288
            public static XdmNodeKind ForNode(XdmNode node)
289
            {
290
                return ForNodeType(node.NodeKind);
291
            }
292

    
293
            /// <summary>
294
            /// Get the item type corresponding to an <c>XmlNodeType</c> as defined in the System.Xml package
295
            /// </summary>
296
            /// <param name="type">The <c>XmlNodeType</c> to be converted</param>
297
            /// <returns>The corresponding <c>XdmNodeKind</c></returns>
298

    
299
            public static XdmNodeKind ForNodeType(XmlNodeType type)
300
            {
301
                switch (type)
302
                {
303
                    case XmlNodeType.Document:
304
                        return Document;
305
                    case XmlNodeType.Element:
306
                        return Element;
307
                    case XmlNodeType.Attribute:
308
                        return Attribute;
309
                    case XmlNodeType.Text:
310
                        return Text;
311
                    case XmlNodeType.Comment:
312
                        return Comment;
313
                    case XmlNodeType.ProcessingInstruction:
314
                        return ProcessingInstruction;
315
                    default:
316
                        throw new ArgumentException("Unknown node kind");
317
                }
318
            }
319
        }
320

    
321
        /// <summary>
322
        /// An instance of class <c>XdmSequenceType</c> represents a sequence type, that is, the combination
323
        /// of an item type and an occurrence indicator.
324
        /// </summary>
325

    
326
        public class XdmSequenceType
327
        {
328

    
329
            internal XdmItemType itemType;
330
            internal int occurrences;
331
            public const char ZERO_OR_MORE='*';
332
            public const char ONE_OR_MORE = '+';
333
            public const char ZERO_OR_ONE = '?';
334
            public const char ZERO = 'º'; //xBA
335
            public const char ONE = ' ';
336

    
337
            /// <summary>
338
            /// Create an XdmSequenceType corresponding to a given XdmItemType and occurrence indicator
339
            /// </summary>
340
            /// <param name="itemType">The item type</param>
341
            /// <param name="occurrenceIndicator">The occurrence indicator, one of '?' (zero-or-one), 
342
            /// '*' (zero-or-more), '+' (one-or-more), ' ' (a single space) (exactly one),
343
            /// or 'º' (masculine ordinal indicator, xBA) (exactly zero). The type empty-sequence()
344
            /// can be represented by an occurrence indicator of 'º' with any item type.</param>
345

    
346
            public XdmSequenceType(XdmItemType itemType, char occurrenceIndicator)
347
            {
348
                int occ;
349
                switch (occurrenceIndicator)
350
                {
351
                    case ZERO_OR_MORE:
352
                        occ = JStaticProperty.ALLOWS_ZERO_OR_MORE;
353
                        break;
354
                    case ONE_OR_MORE:
355
                        occ = JStaticProperty.ALLOWS_ONE_OR_MORE;
356
                        break;
357
                    case ZERO_OR_ONE:
358
                        occ = JStaticProperty.ALLOWS_ZERO_OR_ONE;
359
                        break;
360
                    case ONE:
361
                        occ = JStaticProperty.EXACTLY_ONE;
362
                        break;
363
                    case ZERO:
364
                        occ = JStaticProperty.ALLOWS_ZERO;
365
                        break;
366
                    default:
367
                        throw new ArgumentException("Unknown occurrence indicator");
368
                }
369
                this.itemType = itemType;
370
                this.occurrences = occ;
371
            }
372

    
373
            internal static XdmSequenceType FromSequenceType(JSequenceType seqType)
374
            {
375
                XdmItemType itemType = XdmItemType.MakeXdmItemType(seqType.getPrimaryType());
376
                char occ;
377
                if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO_OR_MORE)
378
                {
379
                    occ = ZERO_OR_MORE;
380
                }
381
                else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ONE_OR_MORE)
382
                {
383
                    occ = ONE_OR_MORE;
384
                }
385
                else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO_OR_ONE)
386
                {
387
                    occ = ZERO_OR_ONE;
388
                }
389
                else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO)
390
                {
391
                    occ = ZERO;
392
                }
393
                else
394
                {
395
                    occ = ONE;
396
                }
397
                return new XdmSequenceType(itemType, occ);
398
            }
399

    
400
            internal JSequenceType ToSequenceType() {
401
                JItemType jit = itemType.Unwrap();
402
                return JSequenceType.makeSequenceType(jit, occurrences);
403
            }
404
 
405
                
406
        }
407

    
408

    
409
}
410

    
411
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
412
// Copyright (c) 2013 Saxonica Limited.
413
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
414
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
415
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
416
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
(10-10/13)