Project

Profile

Help

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

he / latest9.4 / hen / csource / api / Saxon.Api / Types.cs @ c3c84ba2

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 JEmptySequenceTest = net.sf.saxon.pattern.EmptySequenceTest;
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 JEmptySequenceTest)
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
        /// <summary>
102
        /// Singleton class representing the item type node(), which matches any node.
103
        /// </summary>
104

    
105
        public class XdmAnyNodeType : XdmItemType
106
        {
107

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

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

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

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

    
125
        public class XdmAnyFunctionType : XdmItemType
126
        {
127

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

    
133
            public static XdmAnyFunctionType Instance = new XdmAnyFunctionType();
134

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

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

    
152
        public class XdmAtomicType : XdmItemType
153
        {
154

    
155
            internal XdmAtomicType(JAtomicType type)
156
            {
157
                this.type = type;
158
            }
159

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

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

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

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

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

    
206
        public class XdmNodeKind : XdmItemType
207
        {
208

    
209
            internal XdmNodeKind(JNodeKindTest test)
210
            {
211
                type = test;
212
            }
213

    
214
            /// <summary>
215
            /// The item type <c>document-node()</c>
216
            /// </summary>
217

    
218
            public static XdmNodeKind Document = new XdmNodeKind(JNodeKindTest.DOCUMENT);
219

    
220
            /// <summary>
221
            /// The item type <c>element()</c>
222
            /// </summary>
223

    
224
            public static XdmNodeKind Element = new XdmNodeKind(JNodeKindTest.ELEMENT);
225

    
226
            /// <summary>
227
            /// The item type <c>attribute()</c>
228
            /// </summary>
229

    
230
            public static XdmNodeKind Attribute = new XdmNodeKind(JNodeKindTest.ATTRIBUTE);
231

    
232
            /// <summary>
233
            /// The item type <c>text()</c>
234
            /// </summary>
235

    
236
            public static XdmNodeKind Text = new XdmNodeKind(JNodeKindTest.TEXT);
237

    
238
            /// <summary>
239
            /// The item type <c>comment()</c>
240
            /// </summary>
241

    
242
            public static XdmNodeKind Comment = new XdmNodeKind(JNodeKindTest.COMMENT);
243

    
244
            /// <summary>
245
            /// The item type <c>processing-instruction()</c>
246
            /// </summary>
247

    
248
            public static XdmNodeKind ProcessingInstruction = new XdmNodeKind(JNodeKindTest.PROCESSING_INSTRUCTION);
249

    
250
            /// <summary>
251
            /// The item type <c>namespace-node()</c>
252
            /// </summary>
253

    
254
            public static XdmNodeKind Namespace = new XdmNodeKind(JNodeKindTest.NAMESPACE);
255

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

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

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

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

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

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

    
324
        public class XdmSequenceType
325
        {
326

    
327
            internal XdmItemType itemType;
328
            internal int occurrences;
329

    
330
            /// <summary>
331
            /// Create an XdmSequenceType corresponding to a given XdmItemType and occurrence indicator
332
            /// </summary>
333
            /// <param name="itemType">The item type</param>
334
            /// <param name="occurrenceIndicator">The occurrence indicator, one of '?' (zero-or-one), 
335
            /// '*' (zero-or-more), '+' (one-or-more), ' ' (a single space) (exactly one),
336
            /// or 'º' (masculine ordinal indicator, xBA) (exactly zero). The type empty-sequence()
337
            /// can be represented by an occurrence indicator of 'º' with any item type.</param>
338

    
339
            public XdmSequenceType(XdmItemType itemType, char occurrenceIndicator)
340
            {
341
                int occ;
342
                switch (occurrenceIndicator)
343
                {
344
                    case '*':
345
                        occ = JStaticProperty.ALLOWS_ZERO_OR_MORE;
346
                        break;
347
                    case '+':
348
                        occ = JStaticProperty.ALLOWS_ONE_OR_MORE;
349
                        break;
350
                    case '?':
351
                        occ = JStaticProperty.ALLOWS_ZERO_OR_ONE;
352
                        break;
353
                    case ' ':
354
                        occ = JStaticProperty.EXACTLY_ONE;
355
                        break;
356
                    case 'º': //xBA
357
                        occ = JStaticProperty.ALLOWS_ZERO;
358
                        break;
359
                    default:
360
                        throw new ArgumentException("Unknown occurrence indicator");
361
                }
362
                this.itemType = itemType;
363
                this.occurrences = occ;
364
            }
365

    
366
            internal static XdmSequenceType FromSequenceType(JSequenceType seqType)
367
            {
368
                XdmItemType itemType = XdmItemType.MakeXdmItemType(seqType.getPrimaryType());
369
                char occ;
370
                if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO_OR_MORE)
371
                {
372
                    occ = '*';
373
                }
374
                else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ONE_OR_MORE)
375
                {
376
                    occ = '+';
377
                }
378
                else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO_OR_ONE)
379
                {
380
                    occ = '?';
381
                }
382
                else if (seqType.getCardinality() == JStaticProperty.ALLOWS_ZERO)
383
                {
384
                    occ = 'º'; //xBA
385
                }
386
                else
387
                {
388
                    occ = ' ';
389
                }
390
                return new XdmSequenceType(itemType, occ);
391
            }
392

    
393
            internal JSequenceType ToSequenceType() {
394
                JItemType jit = itemType.Unwrap();
395
                return JSequenceType.makeSequenceType(jit, occurrences);
396
            }
397
 
398
                
399
        }
400

    
401

    
402
}
(9-9/12)