File: ImportContext.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (453 lines) | stat: -rw-r--r-- 18,362 bytes parent folder | download | duplicates (6)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//------------------------------------------------------------------------------
// <copyright file="ImportContext.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>                                                                
//------------------------------------------------------------------------------

namespace System.Xml.Serialization {
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using System.Collections;
    using System.Collections.Specialized;

#if !MONO_HYBRID_SYSTEM_XML
    public class ImportContext {
        bool shareTypes;
        SchemaObjectCache cache; // cached schema top-level items
        Hashtable mappings; // XmlSchema -> SerializableMapping, XmlSchemaSimpleType -> EnumMapping, XmlSchemaComplexType -> StructMapping
        Hashtable elements; // XmlSchemaElement -> ElementAccessor
        CodeIdentifiers typeIdentifiers;

        /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.ImportContext"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public ImportContext(CodeIdentifiers identifiers, bool shareTypes) {
            this.typeIdentifiers = identifiers;
            this.shareTypes = shareTypes;
        }
        internal ImportContext() : this(null, false) {}

        internal SchemaObjectCache Cache {
            get {
                if (cache == null)
                    cache = new SchemaObjectCache();
                return cache; 
            }
        }

        internal Hashtable Elements {
            get { 
                if (elements == null)
                    elements = new Hashtable();
                return elements;
            }
        }

        internal Hashtable Mappings {
            get { 
                if (mappings == null)
                    mappings = new Hashtable();
                return mappings;
            }
        }

        /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.TypeIdentifiers"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public CodeIdentifiers TypeIdentifiers {
            get { 
                if (typeIdentifiers == null)
                    typeIdentifiers = new CodeIdentifiers();
                return typeIdentifiers; 
            }
        }

        /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.ShareTypes"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public bool ShareTypes {
            get { return shareTypes; }
        }

        /// <include file='doc\ImportContext.uex' path='docs/doc[@for="ImportContext.Warnings"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public StringCollection Warnings {
            get { return Cache.Warnings; }
        }
    }
#endif

    internal class SchemaObjectCache {
        Hashtable graph;
        Hashtable hash;
        Hashtable objectCache;
        StringCollection warnings;
        // 
        internal Hashtable looks = new Hashtable();
        Hashtable Graph {
            get {
                if (graph == null) 
                    graph = new Hashtable();
                return graph;
            }
        }

        Hashtable Hash {
            get {
                if (hash == null) 
                    hash = new Hashtable();
                return hash;
            }
        }

        Hashtable ObjectCache {
            get {
                if (objectCache == null) 
                    objectCache = new Hashtable();
                return objectCache;
            }
        }

        internal StringCollection Warnings {
            get {
                if (warnings == null) 
                    warnings = new StringCollection();
                return warnings;
            }
        }
      
        internal XmlSchemaObject AddItem(XmlSchemaObject item, XmlQualifiedName qname, XmlSchemas schemas) {
            if (item == null)
                return null;
            if (qname == null || qname.IsEmpty) 
                return null;

            string key = item.GetType().Name + ":" + qname.ToString();
            ArrayList list = (ArrayList)ObjectCache[key];
            if (list == null) {
                list = new ArrayList();
                ObjectCache[key] = list;
            }

            for (int i = 0; i < list.Count; i++) {
                XmlSchemaObject cachedItem = (XmlSchemaObject)list[i];
                if (cachedItem == item)
                    return cachedItem;

                if (Match(cachedItem, item, true)) {
                    return cachedItem;
                }
                else {
                    Warnings.Add(Res.GetString(Res.XmlMismatchSchemaObjects, item.GetType().Name, qname.Name, qname.Namespace));
                    Warnings.Add("DEBUG:Cached item key:\r\n" + (string)looks[cachedItem] + "\r\nnew item key:\r\n" + (string)looks[item]);
                }
            }
            // no match found we need to insert the new type in the cache
            list.Add(item);
            return item;
        }

        internal bool Match(XmlSchemaObject o1, XmlSchemaObject o2, bool shareTypes) {
            if (o1 == o2)
                return true;
            if (o1.GetType() != o2.GetType())
                return false;
            if (Hash[o1] == null)
                Hash[o1] = GetHash(o1);
            int hash1 = (int)Hash[o1];
            int hash2 = GetHash(o2);
            if (hash1 != hash2)
                return false;

            if (shareTypes)
                return CompositeHash(o1, hash1) == CompositeHash(o2, hash2);
            return true;
        }

        private ArrayList GetDependencies(XmlSchemaObject o, ArrayList deps, Hashtable refs) {
            if (refs[o] == null) {
                refs[o] = o;
                deps.Add(o);
                ArrayList list = Graph[o] as ArrayList;
                if (list != null) {
                    for (int i = 0; i < list.Count; i++) {
                        GetDependencies((XmlSchemaObject)list[i], deps, refs);
                    }
                }
            }
            return deps;
        }

        private int CompositeHash(XmlSchemaObject o, int hash) {
            ArrayList list = GetDependencies(o, new ArrayList(), new Hashtable());
            double tmp = 0;
            for (int i = 0; i < list.Count; i++) {
                object cachedHash = Hash[list[i]];
                if (cachedHash is int) {
                    tmp += (int)cachedHash/list.Count;
                }
            }
            return (int)tmp;
        }

        internal void GenerateSchemaGraph(XmlSchemas schemas) {
            SchemaGraph graph = new SchemaGraph(Graph, schemas);
            ArrayList items = graph.GetItems();

            for (int i = 0; i < items.Count; i++) {
                GetHash((XmlSchemaObject)items[i]);
            }
        }

        private int GetHash(XmlSchemaObject o) {
            object hash = Hash[o];
            if (hash != null) {
                if (hash is XmlSchemaObject) {
                }
                else {
                    return (int)hash;
                }
            }
            // new object, generate the hash
            string hashString = ToString(o, new SchemaObjectWriter());
            looks[o] = hashString;
            int code = hashString.GetHashCode();
            Hash[o] = code;
            return code;
        }

        string ToString(XmlSchemaObject o, SchemaObjectWriter writer) {
            return writer.WriteXmlSchemaObject(o);
        }
    }

    internal class SchemaGraph {
        ArrayList empty = new ArrayList();
        XmlSchemas schemas;
        Hashtable scope;
        int items;

        internal SchemaGraph(Hashtable scope, XmlSchemas schemas) {
            this.scope = scope;
            schemas.Compile(null, false);
            this.schemas = schemas;
            items = 0;
            foreach(XmlSchema s in schemas) {
                items += s.Items.Count;
                foreach (XmlSchemaObject item in s.Items) {
                    Depends(item);
                }
            }
        }

        internal ArrayList GetItems() {
            return new ArrayList(scope.Keys);
        }

        internal void AddRef(ArrayList list, XmlSchemaObject o) {
            if (o == null)
                return;
            if (schemas.IsReference(o))
                return;
            if (o.Parent is XmlSchema) {
                string ns = ((XmlSchema)o.Parent).TargetNamespace;
                if (ns == XmlSchema.Namespace)
                    return;
                if (list.Contains(o))
                    return;
                list.Add(o);
            }
        }

        internal ArrayList Depends(XmlSchemaObject item) {

            if (item.Parent is XmlSchema) {
                if (scope[item] != null)
                    return (ArrayList)scope[item];

                ArrayList refs = new ArrayList();
                Depends(item, refs);
                scope.Add(item, refs);
                return refs;
            }
            return empty;
        }

        internal void Depends(XmlSchemaObject item, ArrayList refs) {
            if (item == null || scope[item] != null)
                return;

            Type t = item.GetType();
            if (typeof(XmlSchemaType).IsAssignableFrom(t)) {
                XmlQualifiedName baseName = XmlQualifiedName.Empty;
                XmlSchemaType baseType = null;
                XmlSchemaParticle particle = null;
                XmlSchemaObjectCollection attributes = null;

                if (item is XmlSchemaComplexType) {
                    XmlSchemaComplexType ct = (XmlSchemaComplexType)item;
                    if (ct.ContentModel != null) {
                        XmlSchemaContent content = ct.ContentModel.Content;
                        if (content is XmlSchemaComplexContentRestriction) {
                            baseName = ((XmlSchemaComplexContentRestriction)content).BaseTypeName;
                            attributes = ((XmlSchemaComplexContentRestriction)content).Attributes;
                        }
                        else if (content is XmlSchemaSimpleContentRestriction) {
                            XmlSchemaSimpleContentRestriction restriction = (XmlSchemaSimpleContentRestriction)content;
                            if (restriction.BaseType != null)
                                baseType = restriction.BaseType;
                            else
                                baseName = restriction.BaseTypeName;
                            attributes = restriction.Attributes;
                        }
                        else if (content is XmlSchemaComplexContentExtension) {
                            XmlSchemaComplexContentExtension extension = (XmlSchemaComplexContentExtension)content;
                            attributes = extension.Attributes;
                            particle = extension.Particle;
                            baseName = extension.BaseTypeName;
                        }
                        else if (content is XmlSchemaSimpleContentExtension) {
                            XmlSchemaSimpleContentExtension extension = (XmlSchemaSimpleContentExtension)content;
                            attributes = extension.Attributes;
                            baseName = extension.BaseTypeName;
                        }
                    }
                    else {
                        attributes = ct.Attributes;
                        particle = ct.Particle;
                    }
                    if (particle is XmlSchemaGroupRef) {
                        XmlSchemaGroupRef refGroup = (XmlSchemaGroupRef)particle;
                        particle = ((XmlSchemaGroup)schemas.Find(refGroup.RefName, typeof(XmlSchemaGroup), false)).Particle;
                    }
                    else if (particle is XmlSchemaGroupBase) {
                        particle = (XmlSchemaGroupBase)particle;
                    }
                }
                else if (item is XmlSchemaSimpleType) {
                    XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)item;
                    XmlSchemaSimpleTypeContent content = simpleType.Content;
                    if (content is XmlSchemaSimpleTypeRestriction) {
                        baseType = ((XmlSchemaSimpleTypeRestriction)content).BaseType;
                        baseName = ((XmlSchemaSimpleTypeRestriction)content).BaseTypeName;
                    }
                    else if (content is XmlSchemaSimpleTypeList) {
                        XmlSchemaSimpleTypeList list  = (XmlSchemaSimpleTypeList)content;
                        if (list.ItemTypeName != null && !list.ItemTypeName.IsEmpty)
                            baseName = list.ItemTypeName;
                        if (list.ItemType != null) {
                            baseType = list.ItemType;
                        }
                    }
                    else if (content is XmlSchemaSimpleTypeRestriction) {
                        baseName = ((XmlSchemaSimpleTypeRestriction)content).BaseTypeName;
                    }
                    else if (t == typeof(XmlSchemaSimpleTypeUnion)) {
                        XmlQualifiedName[] memberTypes = ((XmlSchemaSimpleTypeUnion)item).MemberTypes;
                        
                        if (memberTypes != null) {
                            for (int i = 0; i < memberTypes.Length; i++) {
                                XmlSchemaType type = (XmlSchemaType)schemas.Find(memberTypes[i], typeof(XmlSchemaType), false);
                                AddRef(refs, type);
                            }
                        }
                    }
                }
                if (baseType == null && !baseName.IsEmpty && baseName.Namespace != XmlSchema.Namespace)
                    baseType = (XmlSchemaType)schemas.Find(baseName, typeof(XmlSchemaType), false);

                if (baseType != null) {
                    AddRef(refs, baseType);
                }
                if (particle != null) {
                    Depends(particle, refs);
                }
                if (attributes != null) {
                    for (int i = 0; i < attributes.Count; i++) {
                        Depends(attributes[i], refs);
                    }
                }
            }
            else if (t == typeof(XmlSchemaElement)) {
                XmlSchemaElement el = (XmlSchemaElement)item;
                if (!el.SubstitutionGroup.IsEmpty) {
                    if (el.SubstitutionGroup.Namespace != XmlSchema.Namespace) {
                        XmlSchemaElement head = (XmlSchemaElement)schemas.Find(el.SubstitutionGroup, typeof(XmlSchemaElement), false);
                        AddRef(refs, head);
                    }
                }
                if (!el.RefName.IsEmpty) {
                    el = (XmlSchemaElement)schemas.Find(el.RefName, typeof(XmlSchemaElement), false);
                    AddRef(refs, el);
                }
                else if (!el.SchemaTypeName.IsEmpty) {
                    XmlSchemaType type = (XmlSchemaType)schemas.Find(el.SchemaTypeName, typeof(XmlSchemaType), false);
                    AddRef(refs, type);
                }
                else {
                    Depends(el.SchemaType, refs);
                }
            }
            else if (t == typeof(XmlSchemaGroup)) {
                Depends(((XmlSchemaGroup)item).Particle);
            }
            else if (t == typeof(XmlSchemaGroupRef)) {
                XmlSchemaGroup group = (XmlSchemaGroup)schemas.Find(((XmlSchemaGroupRef)item).RefName, typeof(XmlSchemaGroup), false);
                AddRef(refs, group);
            }
            else if (typeof(XmlSchemaGroupBase).IsAssignableFrom(t)) {
                foreach (XmlSchemaObject o in ((XmlSchemaGroupBase)item).Items) {
                    Depends(o, refs);
                }
            }
            else if (t == typeof(XmlSchemaAttributeGroupRef)) {
                XmlSchemaAttributeGroup group = (XmlSchemaAttributeGroup)schemas.Find(((XmlSchemaAttributeGroupRef)item).RefName, typeof(XmlSchemaAttributeGroup), false);
                AddRef(refs, group);
            }
            else if (t == typeof(XmlSchemaAttributeGroup)) {
                foreach (XmlSchemaObject o in ((XmlSchemaAttributeGroup)item).Attributes) {
                    Depends(o, refs);
                }
            }
            else if (t == typeof(XmlSchemaAttribute)) {
                XmlSchemaAttribute at = (XmlSchemaAttribute)item;
                if (!at.RefName.IsEmpty) {
                    at = (XmlSchemaAttribute)schemas.Find(at.RefName, typeof(XmlSchemaAttribute), false);
                    AddRef(refs, at);
                }
                else if (!at.SchemaTypeName.IsEmpty) {
                    XmlSchemaType type = (XmlSchemaType)schemas.Find(at.SchemaTypeName, typeof(XmlSchemaType), false);
                    AddRef(refs, type);
                }
                else {
                    Depends(at.SchemaType, refs);
                }
            }
            if (typeof(XmlSchemaAnnotated).IsAssignableFrom(t)) {
                XmlAttribute[] attrs = (XmlAttribute[])((XmlSchemaAnnotated)item).UnhandledAttributes;

                if (attrs != null) {
                    for (int i = 0; i < attrs.Length; i++) {
                        XmlAttribute attribute = attrs[i];
                        if (attribute.LocalName == Wsdl.ArrayType && attribute.NamespaceURI == Wsdl.Namespace) {
                            string dims;
                            XmlQualifiedName qname = TypeScope.ParseWsdlArrayType(attribute.Value, out dims, item);
                            XmlSchemaType type = (XmlSchemaType)schemas.Find(qname, typeof(XmlSchemaType), false);
                            AddRef(refs, type);
                        }
                    }
                }
            }
        }
    }
}