Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Xaml / XamlParseException.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using System.Diagnostics;
21 using System.Text;
22 using System.Xml;
23
24 namespace Tizen.NUI.Xaml
25 {
26     /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class XamlParseException : Exception
29     {
30         readonly string unformattedMessage;
31
32         static private StringBuilder GetStackInfo()
33         {
34             StringBuilder ret = new StringBuilder("\nStack:\n");
35
36             StackTrace st = new StackTrace();
37
38             for (int i = 2; i < st.FrameCount; i++)
39             {
40                 StackFrame sf = st.GetFrame(i);
41                 ret.AppendFormat("File:{0}, Method:{1}, Line:{2}\n", sf.GetFileName(), sf.GetMethod().Name, sf.GetFileLineNumber());
42             }
43
44             return ret;
45         }
46
47         /// <summary> Initializes a new instance. </summary>
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public XamlParseException()
50         {
51         }
52
53         /// <summary> Initializes a new instance with message. </summary>
54         [EditorBrowsable(EditorBrowsableState.Never)]
55         public XamlParseException(string message) : base(message)
56         {
57             unformattedMessage = message;
58         }
59
60         /// <summary> Initializes a new instance with message and inner exception. </summary>
61         [EditorBrowsable(EditorBrowsableState.Never)]
62         public XamlParseException(string message, Exception innerException = null) : base(message, innerException)
63         {
64             unformattedMessage = message;
65         }
66
67         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
68         [EditorBrowsable(EditorBrowsableState.Never)]
69         public XamlParseException(string message, IXmlLineInfo xmlInfo, Exception innerException = null) : base(FormatMessage(message + GetStackInfo(), xmlInfo), innerException)
70         {
71             unformattedMessage = message;
72             XmlInfo = xmlInfo;
73         }
74
75         internal XamlParseException(string message, IServiceProvider serviceProvider, Exception innerException = null)
76             : this(message, GetLineInfo(serviceProvider), innerException)
77         {
78         }
79
80         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public IXmlLineInfo XmlInfo { get; private set; }
83
84         internal string UnformattedMessage
85         {
86             get { return unformattedMessage ?? Message; }
87         }
88
89         static string FormatMessage(string message, IXmlLineInfo xmlinfo)
90         {
91             if (xmlinfo == null || !xmlinfo.HasLineInfo())
92                 return message;
93             return string.Format("Position {0}:{1}. {2}", xmlinfo.LineNumber, xmlinfo.LinePosition, message);
94         }
95
96
97         static IXmlLineInfo GetLineInfo(IServiceProvider serviceProvider)
98             => (serviceProvider.GetService(typeof(IXmlLineInfoProvider)) is IXmlLineInfoProvider lineInfoProvider) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
99     }
100 }