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;
9 using SR_ = System.Runtime.Serialization.SR;
13 static class XmlExceptionHelper
15 static void ThrowXmlException(XmlDictionaryReader reader, string res)
17 ThrowXmlException(reader, res, null);
20 static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1)
22 ThrowXmlException(reader, res, arg1, null);
25 static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2)
27 ThrowXmlException(reader, res, arg1, arg2, null);
30 static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2, string arg3)
32 string s = SR_.GetString(res, arg1, arg2, arg3);
33 IXmlLineInfo lineInfo = reader as IXmlLineInfo;
34 if (lineInfo != null && lineInfo.HasLineInfo())
36 s += " " + SR_.GetString(SR_.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
39 if (TD.ReaderQuotaExceededIsEnabled())
41 TD.ReaderQuotaExceeded(s);
44 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
47 static public void ThrowXmlException(XmlDictionaryReader reader, XmlException exception)
49 string s = exception.Message;
50 IXmlLineInfo lineInfo = reader as IXmlLineInfo;
51 if (lineInfo != null && lineInfo.HasLineInfo())
53 s += " " + SR_.GetString(SR_.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
55 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
58 static string GetName(string prefix, string localName)
60 if (prefix.Length == 0)
63 return string.Concat(prefix, ":", localName);
66 static string GetWhatWasFound(XmlDictionaryReader reader)
69 return SR_.GetString(SR_.XmlFoundEndOfFile);
70 switch (reader.NodeType)
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);
85 return SR_.GetString(SR_.XmlFoundNodeType, reader.NodeType);
88 static public void ThrowStartElementExpected(XmlDictionaryReader reader)
90 ThrowXmlException(reader, SR_.XmlStartElementExpected, GetWhatWasFound(reader));
93 static public void ThrowStartElementExpected(XmlDictionaryReader reader, string name)
95 ThrowXmlException(reader, SR_.XmlStartElementNameExpected, name, GetWhatWasFound(reader));
98 static public void ThrowStartElementExpected(XmlDictionaryReader reader, string localName, string ns)
100 ThrowXmlException(reader, SR_.XmlStartElementLocalNameNsExpected, localName, ns, GetWhatWasFound(reader));
103 static public void ThrowStartElementExpected(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
105 ThrowStartElementExpected(reader, XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(ns));
108 static public void ThrowFullStartElementExpected(XmlDictionaryReader reader)
110 ThrowXmlException(reader, SR_.XmlFullStartElementExpected, GetWhatWasFound(reader));
113 static public void ThrowFullStartElementExpected(XmlDictionaryReader reader, string name)
115 ThrowXmlException(reader, SR_.XmlFullStartElementNameExpected, name, GetWhatWasFound(reader));
118 static public void ThrowFullStartElementExpected(XmlDictionaryReader reader, string localName, string ns)
120 ThrowXmlException(reader, SR_.XmlFullStartElementLocalNameNsExpected, localName, ns, GetWhatWasFound(reader));
123 static public void ThrowFullStartElementExpected(XmlDictionaryReader reader, XmlDictionaryString localName, XmlDictionaryString ns)
125 ThrowFullStartElementExpected(reader, XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(ns));
128 static public void ThrowEndElementExpected(XmlDictionaryReader reader, string localName, string ns)
130 ThrowXmlException(reader, SR_.XmlEndElementExpected, localName, ns, GetWhatWasFound(reader));
133 static public void ThrowMaxStringContentLengthExceeded(XmlDictionaryReader reader, int maxStringContentLength)
135 ThrowXmlException(reader, SR_.XmlMaxStringContentLengthExceeded, maxStringContentLength.ToString(NumberFormatInfo.CurrentInfo));
138 static public void ThrowMaxArrayLengthExceeded(XmlDictionaryReader reader, int maxArrayLength)
140 ThrowXmlException(reader, SR_.XmlMaxArrayLengthExceeded, maxArrayLength.ToString(NumberFormatInfo.CurrentInfo));
143 static public void ThrowMaxArrayLengthOrMaxItemsQuotaExceeded(XmlDictionaryReader reader, int maxQuota)
145 ThrowXmlException(reader, SR_.XmlMaxArrayLengthOrMaxItemsQuotaExceeded, maxQuota.ToString(NumberFormatInfo.CurrentInfo));
148 static public void ThrowMaxDepthExceeded(XmlDictionaryReader reader, int maxDepth)
150 ThrowXmlException(reader, SR_.XmlMaxDepthExceeded, maxDepth.ToString(NumberFormatInfo.CurrentInfo));
153 static public void ThrowMaxBytesPerReadExceeded(XmlDictionaryReader reader, int maxBytesPerRead)
155 ThrowXmlException(reader, SR_.XmlMaxBytesPerReadExceeded, maxBytesPerRead.ToString(NumberFormatInfo.CurrentInfo));
158 static public void ThrowMaxNameTableCharCountExceeded(XmlDictionaryReader reader, int maxNameTableCharCount)
160 ThrowXmlException(reader, SR_.XmlMaxNameTableCharCountExceeded, maxNameTableCharCount.ToString(NumberFormatInfo.CurrentInfo));
163 static public void ThrowBase64DataExpected(XmlDictionaryReader reader)
165 ThrowXmlException(reader, SR_.XmlBase64DataExpected, GetWhatWasFound(reader));
168 static public void ThrowUndefinedPrefix(XmlDictionaryReader reader, string prefix)
170 ThrowXmlException(reader, SR_.XmlUndefinedPrefix, prefix);
173 static public void ThrowProcessingInstructionNotSupported(XmlDictionaryReader reader)
175 ThrowXmlException(reader, SR_.XmlProcessingInstructionNotSupported);
178 static public void ThrowInvalidXml(XmlDictionaryReader reader, byte b)
180 ThrowXmlException(reader, SR_.XmlInvalidXmlByte, b.ToString("X2", CultureInfo.InvariantCulture));
183 static public void ThrowUnexpectedEndOfFile(XmlDictionaryReader reader)
185 ThrowXmlException(reader, SR_.XmlUnexpectedEndOfFile, ((XmlBaseReader)reader).GetOpenElements());
188 static public void ThrowUnexpectedEndElement(XmlDictionaryReader reader)
190 ThrowXmlException(reader, SR_.XmlUnexpectedEndElement);
193 static public void ThrowTokenExpected(XmlDictionaryReader reader, string expected, char found)
195 ThrowXmlException(reader, SR_.XmlTokenExpected, expected, found.ToString());
198 static public void ThrowTokenExpected(XmlDictionaryReader reader, string expected, string found)
200 ThrowXmlException(reader, SR_.XmlTokenExpected, expected, found);
203 static public void ThrowInvalidCharRef(XmlDictionaryReader reader)
205 ThrowXmlException(reader, SR_.XmlInvalidCharRef);
208 static public void ThrowTagMismatch(XmlDictionaryReader reader, string expectedPrefix, string expectedLocalName, string foundPrefix, string foundLocalName)
210 ThrowXmlException(reader, SR_.XmlTagMismatch, GetName(expectedPrefix, expectedLocalName), GetName(foundPrefix, foundLocalName));
213 static public void ThrowDuplicateXmlnsAttribute(XmlDictionaryReader reader, string localName, string ns)
216 if (localName.Length == 0)
219 name = "xmlns:" + localName;
220 ThrowXmlException(reader, SR_.XmlDuplicateAttribute, name, name, ns);
223 static public void ThrowDuplicateAttribute(XmlDictionaryReader reader, string prefix1, string prefix2, string localName, string ns)
225 ThrowXmlException(reader, SR_.XmlDuplicateAttribute, GetName(prefix1, localName), GetName(prefix2, localName), ns);
228 static public void ThrowInvalidBinaryFormat(XmlDictionaryReader reader)
230 ThrowXmlException(reader, SR_.XmlInvalidFormat);
233 static public void ThrowInvalidRootData(XmlDictionaryReader reader)
235 ThrowXmlException(reader, SR_.XmlInvalidRootData);
238 static public void ThrowMultipleRootElements(XmlDictionaryReader reader)
240 ThrowXmlException(reader, SR_.XmlMultipleRootElements);
243 static public void ThrowDeclarationNotFirst(XmlDictionaryReader reader)
245 ThrowXmlException(reader, SR_.XmlDeclNotFirst);
248 static public void ThrowConversionOverflow(XmlDictionaryReader reader, string value, string type)
250 ThrowXmlException(reader, SR_.XmlConversionOverflow, value, type);
253 static public void ThrowXmlDictionaryStringIDOutOfRange(XmlDictionaryReader reader)
255 ThrowXmlException(reader, SR_.XmlDictionaryStringIDRange, XmlDictionaryString.MinKey.ToString(NumberFormatInfo.CurrentInfo), XmlDictionaryString.MaxKey.ToString(NumberFormatInfo.CurrentInfo));
258 static public void ThrowXmlDictionaryStringIDUndefinedStatic(XmlDictionaryReader reader, int key)
260 ThrowXmlException(reader, SR_.XmlDictionaryStringIDUndefinedStatic, key.ToString(NumberFormatInfo.CurrentInfo));
263 static public void ThrowXmlDictionaryStringIDUndefinedSession(XmlDictionaryReader reader, int key)
265 ThrowXmlException(reader, SR_.XmlDictionaryStringIDUndefinedSession, key.ToString(NumberFormatInfo.CurrentInfo));
268 static public void ThrowEmptyNamespace(XmlDictionaryReader reader)
270 ThrowXmlException(reader, SR_.XmlEmptyNamespaceRequiresNullPrefix);
273 static public XmlException CreateConversionException(string value, string type, Exception exception)
275 return new XmlException(SR_.GetString(SR_.XmlInvalidConversion, value, type), exception);
278 static public XmlException CreateEncodingException(byte[] buffer, int offset, int count, Exception exception)
280 return CreateEncodingException(new System.Text.UTF8Encoding(false, false).GetString(buffer, offset, count), exception);
283 static public XmlException CreateEncodingException(string value, Exception exception)
285 return new XmlException(SR_.GetString(SR_.XmlInvalidUTF8Bytes, value), exception);