blob: c975ee7c08ca6c202406e0be38ee80d640408de3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// 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
{
/// <summary>
/// Provide a data-binding friendly way to access and parse bidi response data.
/// </summary>
public class BidiHelper
{
/// <summary>
/// Parse the bidi response.
/// </summary>
/// <param name="bidiResponse">Bidi response XML data.</param>
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);
}
/// <summary>
/// Get the Cyan ink level.
/// </summary>
public double InkLevelC
{
get;
private set;
}
/// <summary>
/// Get the Magenta ink level.
/// </summary>
public double InkLevelM
{
get;
private set;
}
/// <summary>
/// Get the Yellow ink level.
/// </summary>
public double InkLevelY
{
get;
private set;
}
/// <summary>
/// Get the Black ink level.
/// </summary>
public double InkLevelK
{
get;
private set;
}
}
/// <summary>
/// This class parses bidi response xml data and provides wrapper methods that operate upon the xml.
/// </summary>
internal class BidiResponseParser
{
/// <summary>
/// Parse the bidi response.
/// </summary>
/// <param name="bidiResponse">Bidi response XML data.</param>
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");
}
/// <summary>
/// Get the ink level for a given color.
/// </summary>
/// <param name="color">Color</param>
/// <returns>Ink level percentage</returns>
internal double GetInkLevel(Color color)
{
XmlElement root = bidiData.DocumentElement;
XmlNode inkNode = root.SelectSingleNode(CreateInkXPathQuery(color), namespaceManager);
return double.Parse(inkNode.FirstChild.Value) / 100;
}
/// <summary>
/// Create an XPath query that retrieves the ink level from a standard bidi response.
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
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;
}
}
|