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

ErrorFactory.cs « Factories « Composition « ComponentModel « System « ComponentModelUnitTest « Tests « System.ComponentModel.Composition « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2424739283f067e0d87e05dbffaed1722f087e56 (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
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace System.ComponentModel.Composition.Factories
{
    internal static partial class ErrorFactory
    {
        public static CompositionError Create(ICompositionElement element)
        {
            return Create(CompositionErrorId.Unknown, (string)null, element, (Exception)null);
        }

        public static CompositionError Create(Exception exception)
        {
            return Create(CompositionErrorId.Unknown, (string)null, (ICompositionElement)null, exception);
        }

        public static CompositionError Create(string message)
        {
            return Create(CompositionErrorId.Unknown, message, (ICompositionElement)null, (Exception)null);
        }

        public static CompositionError Create(string message, Exception exception)
        {
            return Create(CompositionErrorId.Unknown, message, (ICompositionElement)null, exception);
        }

        public static CompositionError Create(CompositionErrorId errorId)
        {
            return Create(errorId, errorId.ToString(), (ICompositionElement)null, (Exception)null);
        }

        public static CompositionError Create(CompositionErrorId errorId, string message, Exception exception)
        {
            return new CompositionError(errorId, message, (ICompositionElement)null, exception);
        }

        public static CompositionError Create(CompositionErrorId errorId, string message, ICompositionElement element, Exception exception)
        {
            return new CompositionError(errorId, message, element, exception);
        }

        public static IEnumerable<CompositionError> CreateFromDsl(string format)
        {
            CompositionException exception = (CompositionException)CreateFromDslCore(format);

            return exception.Errors;
        }

        private static Exception CreateFromDslCore(string format)
        {
            List<Tuple<string, string>> identifiers = new List<Tuple<string,string>>();

            StringBuilder token = new StringBuilder();
            StringReader reader = new StringReader(format);

            while (reader.Peek() != -1)
            {
                char c = (char)reader.Read();
                if (c == '|')
                {
                    AddIdentifier(identifiers, token);
                    continue;
                }

                if (c == '(')
                {
                    string dsl = ReadToNextMatchingParenthesis(reader);

                    AddIdentifier(identifiers, token, dsl);
                    continue;
                }

                token.Append(c);
            }

            AddIdentifier(identifiers, token);

            return CreateFromList(identifiers);
        }

        private static Exception CreateFromList(List<Tuple<string, string>> identifiers)
        {
            List<CompositionError> errors = new List<CompositionError>();
            Exception exception = null;
            foreach (var identifier in identifiers)
            {
                Exception innerException = null;
                if (identifier.Item2 != null)
                {
                    innerException = CreateFromDslCore(identifier.Item2);
                }

                if (identifier.Item1 == "Exception")
                {
                    Assert.IsNull(exception);
                    exception = new Exception(identifier.Item1, innerException);
                }
                else
                {
                    Assert.AreEqual("Error", identifier.Item1);

                    errors.Add(Create(identifier.Item1, innerException));
                }                
            }

            if (errors.Count == 0)
            {   
                return exception;
            }

            return new CompositionException("", exception, errors);
        }

        private static void AddIdentifier(List<Tuple<string, string>> identifiers, StringBuilder identifier)
        {
            AddIdentifier(identifiers, identifier, (string)null);
        }

        private static void AddIdentifier(List<Tuple<string, string>> identifiers, StringBuilder identifier, string dsl)
        {
            if (identifier.Length == 0)
                return;

            identifiers.Add(new Tuple<string, string>(identifier.ToString(), dsl));

            identifier.Length = 0;
        }

        private static IEnumerable<CompositionError> CreateFromList(List<string> errors)
        {
            foreach (string value in errors)
            {
                yield return Create(value);
            }
        }

        private static string ReadToNextMatchingParenthesis(StringReader reader)
        {
            Stack<char> parenthesis = new Stack<char>();
            parenthesis.Push('(');

            StringBuilder builder = new StringBuilder();
            while (reader.Peek() != -1)
            {
                char c = (char)reader.Read();
                if (c == '(')
                {
                    parenthesis.Push(c);

                    if (parenthesis.Count == 1)
                    {
                        continue;
                    }
                }

                if (c == ')')
                {
                    char pop = parenthesis.Pop();
                    Assert.AreEqual('(', pop);

                    if (parenthesis.Count == 0)
                    {
                        break;
                    }
                }
                
                builder.Append(c);
            }

            return builder.ToString();
        }
    }
}