XmlExceptionHelper.cs 12 KB

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