4 namespace TechBot.Library
6 public class HresultCommand : BaseCommand, ICommand
8 private IServiceOutput serviceOutput;
9 private string hresultXml;
10 private XmlDocument hresultXmlDocument;
12 public HresultCommand(IServiceOutput serviceOutput,
15 this.serviceOutput = serviceOutput;
16 this.hresultXml = hresultXml;
17 hresultXmlDocument = new XmlDocument();
18 hresultXmlDocument.Load(hresultXml);
21 public bool CanHandle(string commandName)
23 return CanHandle(commandName,
24 new string[] { "hresult" });
27 public void Handle(MessageContext context,
31 string hresultText = parameters;
32 if (hresultText.Equals(String.Empty))
34 serviceOutput.WriteLine(context,
35 "Please provide a valid HRESULT value.");
39 NumberParser np = new NumberParser();
40 long hresult = np.Parse(hresultText);
43 serviceOutput.WriteLine(context,
44 String.Format("{0} is not a valid HRESULT value.",
49 string description = GetHresultDescription(hresult);
50 if (description != null)
52 serviceOutput.WriteLine(context,
53 String.Format("{0} is {1}.",
59 serviceOutput.WriteLine(context,
60 String.Format("I don't know about HRESULT {0}.",
67 return "!hresult <value>";
70 public string GetHresultDescription(long hresult)
72 XmlElement root = hresultXmlDocument.DocumentElement;
73 XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
74 hresult.ToString("X8")));
77 XmlAttribute text = node.Attributes["text"];
79 throw new Exception("Node has no text attribute.");