// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. // // Copyright (c) Microsoft Corporation. All rights reserved // // // Abstract: // // This file contains helper methods that provide a data-binding friendly way to access and parse bidi response data. using System; using System.Collections.Generic; using System.Windows.Media; using System.Xml; namespace Microsoft.Samples.Printing.PrinterExtension.Helpers { /// /// Provide a data-binding friendly way to access and parse bidi response data. /// public class BidiHelper { /// /// Parse the bidi response. /// /// Bidi response XML data. public BidiHelper(string bidiResponse) { BidiResponseParser parser = new BidiResponseParser(bidiResponse); InkLevelC = parser.GetInkLevel(Colors.Cyan); InkLevelM = parser.GetInkLevel(Colors.Magenta); InkLevelY = parser.GetInkLevel(Colors.Yellow); InkLevelK = parser.GetInkLevel(Colors.Black); } /// /// Get the Cyan ink level. /// public double InkLevelC { get; private set; } /// /// Get the Magenta ink level. /// public double InkLevelM { get; private set; } /// /// Get the Yellow ink level. /// public double InkLevelY { get; private set; } /// /// Get the Black ink level. /// public double InkLevelK { get; private set; } } /// /// This class parses bidi response xml data and provides wrapper methods that operate upon the xml. /// internal class BidiResponseParser { /// /// Parse the bidi response. /// /// Bidi response XML data. internal BidiResponseParser(string bidiResponse) { bidiData = new XmlDocument(); bidiData.LoadXml(bidiResponse); namespaceManager = new XmlNamespaceManager(bidiData.NameTable); namespaceManager.AddNamespace("bidi", "http://schemas.microsoft.com/windows/2005/03/printing/bidi"); } /// /// Get the ink level for a given color. /// /// Color /// Ink level percentage internal double GetInkLevel(Color color) { XmlElement root = bidiData.DocumentElement; XmlNode inkNode = root.SelectSingleNode(CreateInkXPathQuery(color), namespaceManager); return double.Parse(inkNode.FirstChild.Value) / 100; } /// /// Create an XPath query that retrieves the ink level from a standard bidi response. /// /// /// private static string CreateInkXPathQuery(Color color) { string colorName = null; if (color.Equals(Colors.Black)) { colorName = "Black"; } else if (color.Equals(Colors.Red)) { colorName = "Red"; } else if (color.Equals(Colors.Green)) { colorName = "Green"; } else if (color.Equals(Colors.Blue)) { colorName = "Blue"; } else if (color.Equals(Colors.Cyan)) { colorName = "Cyan"; } else if (color.Equals(Colors.Magenta)) { colorName = "Magenta"; } else if (color.Equals(Colors.Yellow)) { colorName = "Yellow"; } else { throw new ArgumentException("Unsupported color"); } return "/bidi:Get/Query/Schema[@name='\\Printer.Consumables." + colorName + "Ink" + ":Level']/BIDI_INT"; } private XmlDocument bidiData; private XmlNamespaceManager namespaceManager; } }