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