[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / VBIdentifierName.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System.CodeDom.Compiler;
8     using System.Globalization;
9     using System.Windows;
10     using System.Xaml;
11     using System.Xml;
12     using Microsoft.CSharp;
13     using Microsoft.VisualBasic;
14
15     /// <summary>
16     /// The class is not only used for VB(Dev10), but also for C# (Dev11).
17     /// </summary>
18     internal class VBIdentifierName : DependencyObject
19     {
20         public static readonly DependencyProperty IdentifierNameProperty =
21             DependencyProperty.Register("IdentifierName", typeof(string), typeof(VBIdentifierName), new UIPropertyMetadata(OnIdentifierNameChanged));
22
23         public static readonly DependencyProperty IsValidProperty =
24             DependencyProperty.Register("IsValid", typeof(bool), typeof(VBIdentifierName));
25
26         public static readonly DependencyProperty ErrorMessageProperty =
27             DependencyProperty.Register("ErrorMessage", typeof(string), typeof(VBIdentifierName));
28
29         static VBCodeProvider vbProvider;
30         static CSharpCodeProvider csProvider;
31         static XamlSchemaContext xamlContext = new XamlSchemaContext();
32         static XamlType xamlType = new XamlType(typeof(string), xamlContext);
33
34         bool checkAgainstXaml;
35
36         VBCodeProvider VBProvider
37         {
38             get
39             {
40                 if (vbProvider == null)
41                 {
42                     vbProvider = CodeDomProvider.CreateProvider("VisualBasic") as VBCodeProvider;
43                 }
44                 return vbProvider;
45             }
46         }
47
48         CSharpCodeProvider CSProvider
49         {
50             get
51             {
52                 if (csProvider == null)
53                 {
54                     csProvider = CodeDomProvider.CreateProvider("C#") as CSharpCodeProvider;
55                 }
56
57                 return csProvider;
58             }
59         }
60
61         public string ErrorMessage
62         {
63             get { return (string)GetValue(ErrorMessageProperty); }
64             set { SetValue(ErrorMessageProperty, value); }
65         }
66
67         public bool IsValid
68         {
69             get { return (bool)GetValue(IsValidProperty); }
70             set { SetValue(IsValidProperty, value); }
71         }
72
73         public string IdentifierName
74         {
75             get { return (string)GetValue(IdentifierNameProperty); }
76             set { SetValue(IdentifierNameProperty, value); }
77         }
78
79         public bool CheckAgainstXaml
80         {
81             get
82             {
83                 return this.checkAgainstXaml;
84             }
85         }
86
87         public VBIdentifierName()
88         {
89             this.checkAgainstXaml = false;
90         }
91
92         public VBIdentifierName(bool checkAgainstXaml)
93         {
94             this.checkAgainstXaml = checkAgainstXaml;
95         }
96
97         static void OnIdentifierNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
98         {
99             ((VBIdentifierName)sender).OnIdentifierNameChanged();
100         }
101
102         internal static bool IsValidXamlName(string name)
103         {
104             bool isValid = new XamlMember(name, xamlType, false).IsNameValid;
105
106             if (isValid)
107             {
108                 //Work around TFS bug #825815, in some cases, XamlMember.IsNameValid returns true but it's not valid Xml Name.
109                 try
110                 {
111                     XmlConvert.VerifyName(name);
112                 }
113                 catch (XmlException)
114                 {
115                     isValid = false;
116                 }
117             }
118
119             return isValid;
120         }
121
122         void OnIdentifierNameChanged()
123         {
124             string trimedName = this.IdentifierName;
125             if (this.CheckAgainstXaml && !VBIdentifierName.IsValidXamlName(trimedName))
126             {
127                 this.IsValid = false;
128                 this.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, SR.InvalidXamlMemberName, trimedName);
129             }
130             else if (!this.VBProvider.IsValidIdentifier(trimedName) || !this.CSProvider.IsValidIdentifier(trimedName))
131             {
132                 this.IsValid = false;
133                 this.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, SR.InvalidIdentifier, trimedName);
134             }
135             else if (trimedName.StartsWith("[", StringComparison.Ordinal) && trimedName.EndsWith("]", StringComparison.Ordinal))
136             {
137                 this.IsValid = false;
138                 this.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, SR.InvalidIdentifier, trimedName);
139             }
140             else
141             {
142                 this.IsValid = true;
143                 this.ErrorMessage = string.Empty;
144             }
145         }
146     }
147 }