XmlExceptionHelper.cs 12 KB

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