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(string commandName,
30 string hresultText = parameters;
31 if (hresultText.Equals(String.Empty))
33 serviceOutput.WriteLine("Please provide a valid HRESULT value.");
37 NumberParser np = new NumberParser();
38 long hresult = np.Parse(hresultText);
41 serviceOutput.WriteLine(String.Format("{0} is not a valid HRESULT value.",
46 string description = GetHresultDescription(hresult);
47 if (description != null)
49 serviceOutput.WriteLine(String.Format("{0} is {1}.",
55 serviceOutput.WriteLine(String.Format("I don't know about HRESULT {0}.",
62 return "!hresult <value>";
65 private string GetHresultDescription(long hresult)
67 XmlElement root = hresultXmlDocument.DocumentElement;
68 XmlNode node = root.SelectSingleNode(String.Format("Hresult[@value='{0}']",
69 hresult.ToString("X8")));
72 XmlAttribute text = node.Attributes["text"];
74 throw new Exception("Node has no text attribute.");