[S.R.Serialization] switch to referencesources.
[mono.git] / mcs / class / System.Runtime.Serialization / ReferenceSources / XmlExceptionHelper.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 using System.Runtime.Serialization;
5 ////using System.ServiceModel.Channels;
6 using System.Globalization;
7 using System.Runtime.Serialization.Diagnostics.Application;
8
9 using SR_ = System.Runtime.Serialization.SR;
10
11 namespace System.Xml
12 {
13     static class XmlExceptionHelper
14     {
15         static void ThrowXmlException(XmlDictionaryReader reader, string res)
16         {
17             ThrowXmlException(reader, res, null);
18         }
19
20         static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1)
21         {
22             ThrowXmlException(reader, res, arg1, null);
23         }
24
25         static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2)
26         {
27             ThrowXmlException(reader, res, arg1, arg2, null);
28         }
29
30         static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2, string arg3)
31         {
32             string s = SR_.GetString(res, arg1, arg2, arg3);
33             IXmlLineInfo lineInfo = reader as IXmlLineInfo;
34             if (lineInfo != null && lineInfo.HasLineInfo())
35             {
36                 s += " " + SR_.GetString(SR_.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
37             }
38
39             if (TD.ReaderQuotaExceededIsEnabled())
40             {
41                 TD.ReaderQuotaExceeded(s);
42             }
43
44             throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
45         }
46
47         static public void ThrowXmlException(XmlDictionaryReader reader, XmlException exception)
48         {
49             string s = exception.Message;
50             IXmlLineInfo lineInfo = reader as IXmlLineInfo;
51             if (lineInfo != null && lineInfo.HasLineInfo())
52             {
53                 s += " " + SR_.GetString(SR_.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
54             }
55             throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
56         }
57
58         static string GetName(string prefix, string localName)
59         {
60             if (prefix.Length == 0)
61                 return localName;
62             else
63                 return string.Concat(prefix, ":", localName);
64         }
65
66         static string GetWhatWasFound(XmlDictionaryReader reader)
67         {
68             if (reader.EOF)
69                 return SR_.GetString(SR_.XmlFoundEndOfFile);
70             switch (reader.NodeType)
71             {
72                 case XmlNodeType.Element:
73                     return SR_.GetString(SR_.XmlFoundElement, GetName(reader.Prefix, reader.LocalName), reader.NamespaceURI);
74                 case XmlNodeType.EndElement:
75                     return SR_.GetString(SR_.XmlFoundEndElement, GetName(reader.Prefix, reader.LocalName), reader.NamespaceURI);
76                 case XmlNodeType.Text:
77                 case XmlNodeType.Whitespace:
78                 case XmlNodeType.SignificantWhitespace:
79                     return SR_.GetString(SR_.XmlFoundText, reader.Value);
80                 case XmlNodeType.Comment:
81                     return SR_.GetString(SR_.XmlFoundComment, reader.Value);
82                 case XmlNodeType.CDATA:
83                     return SR_.GetString(SR_.XmlFoundCData, reader.Value);
84             }
85             return SR_.GetString(SR_.XmlFoundNodeType, reader.NodeType);
86         }
87
88         static public void ThrowStartElementExpected(XmlDictionaryReader reader)
89         {
90             ThrowXmlException(reader, SR_.XmlStartElementExpected, GetWhatWasFound(reader));
91         }
92
93         static public void ThrowStartElementExpected(XmlDictionaryReader reader, string name)
94         {
95             ThrowXmlException(reader, SR_.XmlStartElementNameExpected, name, GetWhatWasFound(reader));
96         }
97
98         static public void ThrowStartElementExpected(XmlDictionaryReader reader, string localName, string ns)
99         {
100             ThrowXmlException(reader, SR_.XmlStartElementLocalNameNsExpected, localName, ns, GetWhatWasFound(reader));
101         }
102
103         static public void ThrowStartElementExpected(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
104         {
105             ThrowStartElementExpected(reader, XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(ns));
106         }
107
108         static public void ThrowFullStartElementExpected(XmlDictionaryReader reader)
109         {
110             ThrowXmlException(reader, SR_.XmlFullStartElementExpected, GetWhatWasFound(reader));
111         }
112
113         static public void ThrowFullStartElementExpected(XmlDictionaryReader reader, string name)
114         {
115             ThrowXmlException(reader, SR_.XmlFullStartElementNameExpected, name, GetWhatWasFound(reader));
116         }
117
118         static public void ThrowFullStartElementExpected(XmlDictionaryReader reader, string localName, string ns)
119         {
120             ThrowXmlException(reader, SR_.XmlFullStartElementLocalNameNsExpected, localName, ns, GetWhatWasFound(reader));
121         }
122
123         static public void ThrowFullStartElementExpected(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
124         {
125             ThrowFullStartElementExpected(reader, XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(ns));
126         }
127
128         static public void ThrowEndElementExpected(XmlDictionaryReader reader, string localName, string ns)
129         {
130             ThrowXmlException(reader, SR_.XmlEndElementExpected, localName, ns, GetWhatWasFound(reader));
131         }
132
133         static public void ThrowMaxStringContentLengthExceeded(XmlDictionaryReader reader, int maxStringContentLength)
134         {
135             ThrowXmlException(reader, SR_.XmlMaxStringContentLengthExceeded, maxStringContentLength.ToString(NumberFormatInfo.CurrentInfo));
136         }
137
138         static public void ThrowMaxArrayLengthExceeded(XmlDictionaryReader reader, int maxArrayLength)
139         {
140             ThrowXmlException(reader, SR_.XmlMaxArrayLengthExceeded, maxArrayLength.ToString(NumberFormatInfo.CurrentInfo));
141         }
142
143         static public void ThrowMaxArrayLengthOrMaxItemsQuotaExceeded(XmlDictionaryReader reader, int maxQuota)
144         {
145             ThrowXmlException(reader, SR_.XmlMaxArrayLengthOrMaxItemsQuotaExceeded, maxQuota.ToString(NumberFormatInfo.CurrentInfo));
146         }
147
148         static public void ThrowMaxDepthExceeded(XmlDictionaryReader reader, int maxDepth)
149         {
150             ThrowXmlException(reader, SR_.XmlMaxDepthExceeded, maxDepth.ToString(NumberFormatInfo.CurrentInfo));
151         }
152
153         static public void ThrowMaxBytesPerReadExceeded(XmlDictionaryReader reader, int maxBytesPerRead)
154         {
155             ThrowXmlException(reader, SR_.XmlMaxBytesPerReadExceeded, maxBytesPerRead.ToString(NumberFormatInfo.CurrentInfo));
156         }
157
158         static public void ThrowMaxNameTableCharCountExceeded(XmlDictionaryReader reader, int maxNameTableCharCount)
159         {
160             ThrowXmlException(reader, SR_.XmlMaxNameTableCharCountExceeded, maxNameTableCharCount.ToString(NumberFormatInfo.CurrentInfo));
161         }
162
163         static public void ThrowBase64DataExpected(XmlDictionaryReader reader)
164         {
165             ThrowXmlException(reader, SR_.XmlBase64DataExpected, GetWhatWasFound(reader));
166         }
167
168         static public void ThrowUndefinedPrefix(XmlDictionaryReader reader, string prefix)
169         {
170             ThrowXmlException(reader, SR_.XmlUndefinedPrefix, prefix);
171         }
172
173         static public void ThrowProcessingInstructionNotSupported(XmlDictionaryReader reader)
174         {
175             ThrowXmlException(reader, SR_.XmlProcessingInstructionNotSupported);
176         }
177
178         static public void ThrowInvalidXml(XmlDictionaryReader reader, byte b)
179         {
180             ThrowXmlException(reader, SR_.XmlInvalidXmlByte, b.ToString("X2", CultureInfo.InvariantCulture));
181         }
182
183         static public void ThrowUnexpectedEndOfFile(XmlDictionaryReader reader)
184         {
185             ThrowXmlException(reader, SR_.XmlUnexpectedEndOfFile, ((XmlBaseReader)reader).GetOpenElements());
186         }
187
188         static public void ThrowUnexpectedEndElement(XmlDictionaryReader reader)
189         {
190             ThrowXmlException(reader, SR_.XmlUnexpectedEndElement);
191         }
192
193         static public void ThrowTokenExpected(XmlDictionaryReader reader, string expected, char found)
194         {
195             ThrowXmlException(reader, SR_.XmlTokenExpected, expected, found.ToString());
196         }
197
198         static public void ThrowTokenExpected(XmlDictionaryReader reader, string expected, string found)
199         {
200             ThrowXmlException(reader, SR_.XmlTokenExpected, expected, found);
201         }
202
203         static public void ThrowInvalidCharRef(XmlDictionaryReader reader)
204         {
205             ThrowXmlException(reader, SR_.XmlInvalidCharRef);
206         }
207
208         static public void ThrowTagMismatch(XmlDictionaryReader reader, string expectedPrefix, string expectedLocalName, string foundPrefix, string foundLocalName)
209         {
210             ThrowXmlException(reader, SR_.XmlTagMismatch, GetName(expectedPrefix, expectedLocalName), GetName(foundPrefix, foundLocalName));
211         }
212
213         static public void ThrowDuplicateXmlnsAttribute(XmlDictionaryReader reader, string localName, string ns)
214         {
215             string name;
216             if (localName.Length == 0)
217                 name = "xmlns";
218             else
219                 name = "xmlns:" + localName;
220             ThrowXmlException(reader, SR_.XmlDuplicateAttribute, name, name, ns);
221         }
222
223         static public void ThrowDuplicateAttribute(XmlDictionaryReader reader, string prefix1, string prefix2, string localName, string ns)
224         {
225             ThrowXmlException(reader, SR_.XmlDuplicateAttribute, GetName(prefix1, localName), GetName(prefix2, localName), ns);
226         }
227
228         static public void ThrowInvalidBinaryFormat(XmlDictionaryReader reader)
229         {
230             ThrowXmlException(reader, SR_.XmlInvalidFormat);
231         }
232
233         static public void ThrowInvalidRootData(XmlDictionaryReader reader)
234         {
235             ThrowXmlException(reader, SR_.XmlInvalidRootData);
236         }
237
238         static public void ThrowMultipleRootElements(XmlDictionaryReader reader)
239         {
240             ThrowXmlException(reader, SR_.XmlMultipleRootElements);
241         }
242
243         static public void ThrowDeclarationNotFirst(XmlDictionaryReader reader)
244         {
245             ThrowXmlException(reader, SR_.XmlDeclNotFirst);
246         }
247
248         static public void ThrowConversionOverflow(XmlDictionaryReader reader, string value, string type)
249         {
250             ThrowXmlException(reader, SR_.XmlConversionOverflow, value, type);
251         }
252
253         static public void ThrowXmlDictionaryStringIDOutOfRange(XmlDictionaryReader reader)
254         {
255             ThrowXmlException(reader, SR_.XmlDictionaryStringIDRange, XmlDictionaryString.MinKey.ToString(NumberFormatInfo.CurrentInfo), XmlDictionaryString.MaxKey.ToString(NumberFormatInfo.CurrentInfo));
256         }
257
258         static public void ThrowXmlDictionaryStringIDUndefinedStatic(XmlDictionaryReader reader, int key)
259         {
260             ThrowXmlException(reader, SR_.XmlDictionaryStringIDUndefinedStatic, key.ToString(NumberFormatInfo.CurrentInfo));
261         }
262
263         static public void ThrowXmlDictionaryStringIDUndefinedSession(XmlDictionaryReader reader, int key)
264         {
265             ThrowXmlException(reader, SR_.XmlDictionaryStringIDUndefinedSession, key.ToString(NumberFormatInfo.CurrentInfo));
266         }
267
268         static public void ThrowEmptyNamespace(XmlDictionaryReader reader)
269         {
270             ThrowXmlException(reader, SR_.XmlEmptyNamespaceRequiresNullPrefix);
271         }
272
273         static public XmlException CreateConversionException(string value, string type, Exception exception)
274         {
275             return new XmlException(SR_.GetString(SR_.XmlInvalidConversion, value, type), exception);
276         }
277
278         static public XmlException CreateEncodingException(byte[] buffer, int offset, int count, Exception exception)
279         {
280             return CreateEncodingException(new System.Text.UTF8Encoding(false, false).GetString(buffer, offset, count), exception);
281         }
282
283         static public XmlException CreateEncodingException(string value, Exception exception)
284         {
285             return new XmlException(SR_.GetString(SR_.XmlInvalidUTF8Bytes, value), exception);
286         }
287     }
288 }