Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Utils.cs « SchemaObjectModel « EntityModel « Data « System « System.Data.Entity « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 61003c00172c1ac387b0e0074ca561ffc6a7fcd7 (plain)
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
//---------------------------------------------------------------------
// <copyright file="Utils.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       [....]
// @backupOwner [....]
//---------------------------------------------------------------------

namespace System.Data.EntityModel.SchemaObjectModel
{
    using System;
    using System.Data.Metadata.Edm;
    using System.Diagnostics;
    using System.Globalization;
    using System.Text.RegularExpressions;
    using System.Xml;

    /// <summary>
    /// Summary description for Utils.
    /// </summary>
    // make class internal when friend assemblies are available
    internal static class Utils
    {
        #region Static Fields


        // this is what we should be doing for CDM schemas
        // the RegEx for valid identifiers are taken from the C# Language Specification (2.4.2 Identifiers)
        // (except that we exclude _ as a valid starting character).
        // This results in a somewhat smaller set of identifier from what System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier
        // allows. Not all identifiers allowed by IsValidLanguageIndependentIdentifier are valid in C#.IsValidLanguageIndependentIdentifier allows:
        //    Mn, Mc, and Pc as a leading character (which the spec and C# (at least for some Mn and Mc characters) do not allow)
        //    characters that Char.GetUnicodeCategory says are in Nl and Cf but which the RegEx does not accept (and which C# does allow).
        //
        // we could create the StartCharacterExp and OtherCharacterExp dynamically to force inclusion of the missing Nl and Cf characters...
        private const string StartCharacterExp = @"[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Lm}\p{Nl}]";
        private const string OtherCharacterExp = @"[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Lm}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]";
        private const string NameExp = StartCharacterExp+OtherCharacterExp+"{0,}";
        //private static Regex ValidDottedName=new Regex(@"^"+NameExp+@"(\."+NameExp+@"){0,}$",RegexOptions.Singleline);
        private static Regex UndottedNameValidator = new Regex(@"^"+NameExp+@"$",RegexOptions.Singleline | RegexOptions.Compiled );
        #endregion

        #region Static Methods

        internal static void ExtractNamespaceAndName(SchemaDataModelOption dataModel, string qualifiedTypeName, out string namespaceName, out string name)
        {
            Debug.Assert(!string.IsNullOrEmpty(qualifiedTypeName), "qualifiedTypeName parameter is null");
            GetBeforeAndAfterLastPeriod(qualifiedTypeName, out namespaceName, out name); 
        }


        internal static string ExtractTypeName(SchemaDataModelOption dataModel, string qualifiedTypeName)
        {
            Debug.Assert(!string.IsNullOrEmpty(qualifiedTypeName), "qualifiedTypeName parameter is null or empty");
            return GetEverythingAfterLastPeriod(qualifiedTypeName);
        }

        private static void GetBeforeAndAfterLastPeriod(string qualifiedTypeName, out string before, out string after)
        {
            int lastDot = qualifiedTypeName.LastIndexOf('.');
            if (lastDot < 0)
            {
                before = null;
                after = qualifiedTypeName;
            }
            else
            {
                before = qualifiedTypeName.Substring(0, lastDot);
                after = qualifiedTypeName.Substring(lastDot + 1);
            }
        }
        internal static string GetEverythingBeforeLastPeriod(string qualifiedTypeName)
        {
            int lastDot = qualifiedTypeName.LastIndexOf('.');
            if (lastDot < 0)
                return null;
            return qualifiedTypeName.Substring(0, lastDot);
        }

        private static string GetEverythingAfterLastPeriod(string qualifiedTypeName)
        {
            int lastDot = qualifiedTypeName.LastIndexOf('.');
            if (lastDot < 0)
                return qualifiedTypeName;

            return qualifiedTypeName.Substring(lastDot + 1);
        }
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="reader"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool GetString(Schema schema, XmlReader reader, out string value)
        {
            Debug.Assert(schema != null, "schema parameter is null");
            Debug.Assert(reader != null, "reader parameter is null");

            if (reader.SchemaInfo.Validity == System.Xml.Schema.XmlSchemaValidity.Invalid)
            {
                // an error has already been issued by the xsd validation
                value = null;
                return false;
            }

            value = reader.Value;

            if ( string.IsNullOrEmpty(value) )
            {
                schema.AddError( ErrorCode.InvalidName, EdmSchemaErrorSeverity.Error, reader,
                    System.Data.Entity.Strings.InvalidName(value, reader.Name));
                return false;
            }
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="reader"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool GetDottedName(Schema schema, XmlReader reader,out string name)
        {
            if (!GetString(schema, reader, out name))
            {
                return false;
            }
            
            return ValidateDottedName(schema, reader, name);
        }

        internal static bool ValidateDottedName(Schema schema, XmlReader reader, string name)
        {
            Debug.Assert(schema != null, "schema parameter is null");
            Debug.Assert(reader != null, "reader parameter is null");
            Debug.Assert(!string.IsNullOrEmpty(name), "name parameter is null or empty");
            Debug.Assert(reader.SchemaInfo.Validity != System.Xml.Schema.XmlSchemaValidity.Invalid, "This method should not be called when the schema is invalid");

            if (schema.DataModel == SchemaDataModelOption.EntityDataModel)
            {
                // each part of the dotted name needs to be a valid name
                foreach (string namePart in name.Split('.'))
                {
                    if (!ValidUndottedName(namePart))
                    {
                        schema.AddError(ErrorCode.InvalidName, EdmSchemaErrorSeverity.Error, reader,
                            System.Data.Entity.Strings.InvalidName(name, reader.Name));
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="reader"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool GetUndottedName(Schema schema,XmlReader reader,out string name)
        {
            Debug.Assert(schema != null, "schema parameter is null");
            Debug.Assert(reader != null, "reader parameter is null");

            if (reader.SchemaInfo.Validity == System.Xml.Schema.XmlSchemaValidity.Invalid)
            {
                // the xsd already put in an error
                name = null;
                return false;
            }

            name = reader.Value;
            if (string.IsNullOrEmpty(name))
            {
                schema.AddError( ErrorCode.InvalidName, EdmSchemaErrorSeverity.Error, reader,
                    System.Data.Entity.Strings.EmptyName(reader.Name));
                return false;
            }

            if (schema.DataModel == SchemaDataModelOption.EntityDataModel &&
                !ValidUndottedName(name) )
            {
                schema.AddError( ErrorCode.InvalidName, EdmSchemaErrorSeverity.Error, reader,
                    System.Data.Entity.Strings.InvalidName(name,reader.Name));
                return false;
            }

            Debug.Assert(!(schema.DataModel == SchemaDataModelOption.EntityDataModel && name.IndexOf('.') >= 0),
                string.Format(CultureInfo.CurrentCulture, "{1} ({0}) is not valid. {1} cannot be qualified.", name, reader.Name));

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        internal static bool ValidUndottedName(string name)
        {
            // CodeGenerator.IsValidLanguageIndependentIdentifier does demand a FullTrust Link
            // but this is safe since the function only walks over the string no risk is introduced
            return !string.IsNullOrEmpty(name) && UndottedNameValidator.IsMatch(name) 
                && IsValidLanguageIndependentIdentifier(name);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
        [System.Security.SecuritySafeCritical]
        private static bool IsValidLanguageIndependentIdentifier(string name)
        {
            return System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="reader"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool GetBool(Schema schema, XmlReader reader, out bool value)
        {
            Debug.Assert(schema != null, "schema parameter is null");
            Debug.Assert(reader != null, "reader parameter is null");

            if ( reader.SchemaInfo.Validity == System.Xml.Schema.XmlSchemaValidity.Invalid )
            {
                value = true; // we have to set the value to something before returning.
                return false;
            }

            // do this in a try catch, just in case the attribute wasn't validated against an xsd:boolean
            try
            {
                value = reader.ReadContentAsBoolean();
                return true;
            }
            catch (System.Xml.XmlException)
            {
                // we already handled the valid and invalid cases, so it must be NotKnown now.
                Debug.Assert(reader.SchemaInfo.Validity == Xml.Schema.XmlSchemaValidity.NotKnown, "The schema validity must be NotKnown at this point");
                schema.AddError(ErrorCode.BoolValueExpected, EdmSchemaErrorSeverity.Error, reader,
                    System.Data.Entity.Strings.ValueNotUnderstood(reader.Value, reader.Name));
            }
            
            value = true; // we have to set the value to something before returning.
            return false;
        }

        public static bool GetInt(Schema schema,XmlReader reader,out int value)
        {
            Debug.Assert(schema != null, "schema parameter is null");
            Debug.Assert(reader != null, "reader parameter is null");

            if (reader.SchemaInfo.Validity == System.Xml.Schema.XmlSchemaValidity.Invalid)
            {
                // an error has already been issued by the xsd validation
                value = 0; ;
                return false;
            }

            string text = reader.Value;
            value = int.MinValue;

            if ( int.TryParse(text,NumberStyles.Integer,System.Globalization.CultureInfo.InvariantCulture,out value) )
                return true;

            schema.AddError( ErrorCode.IntegerExpected, EdmSchemaErrorSeverity.Error, reader,
                    System.Data.Entity.Strings.ValueNotUnderstood(reader.Value,reader.Name));
            return false;
        }

        public static bool GetByte(Schema schema,XmlReader reader,out byte value)
        {
            Debug.Assert(schema != null, "schema parameter is null");
            Debug.Assert(reader != null, "reader parameter is null");

            if (reader.SchemaInfo.Validity == System.Xml.Schema.XmlSchemaValidity.Invalid)
            {
                // an error has already been issued by the xsd validation
                value = 0; ;
                return false;
            }

            string text = reader.Value;
            value = byte.MinValue;

            if (byte.TryParse(text, NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out value))
            {
                return true;
            }

            schema.AddError( ErrorCode.ByteValueExpected, EdmSchemaErrorSeverity.Error, reader,
                    System.Data.Entity.Strings.ValueNotUnderstood(reader.Value, reader.Name));

            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lhsName"></param>
        /// <param name="rhsName"></param>
        /// <returns></returns>
        public static int CompareNames(string lhsName, string rhsName)
        {
            return string.Compare(lhsName,rhsName,StringComparison.Ordinal);
        }


        #endregion
    }
}