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