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
|
using System;
using System.Collections;
using System.Configuration;
using System.Xml;
namespace Mainsoft.Drawing.Configuration
{
/// <summary>
/// Summary description for MetadataConfigurationHandler.
/// </summary>
public class ResolutionConfigurationHandler : IConfigurationSectionHandler
{
public ResolutionConfigurationHandler()
{
//
// TODO: Add constructor logic here
//
}
public virtual object Create (object parent, object configContext, XmlNode section) {
if (section.Attributes != null && section.Attributes.Count != 0)
HandlersUtil.ThrowException ("Unrecognized attribute", section);
ResolutionConfigurationCollection col =
new ResolutionConfigurationCollection(parent as ResolutionConfigurationCollection);
XmlNodeList imageFormats = section.ChildNodes;
foreach (XmlNode child in imageFormats) {
XmlNodeType ntype = child.NodeType;
if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
continue;
if (ntype != XmlNodeType.Element)
HandlersUtil.ThrowException ("Only elements allowed", child);
string imageFormatName = HandlersUtil.ExtractAttributeValue ("name", child, false, false);
string xResPath = HandlersUtil.ExtractNodeValue(child["xresolution"]);
string yResPath = HandlersUtil.ExtractNodeValue(child["yresolution"]);
string unitsType = HandlersUtil.ExtractNodeValue(child["unitstype"], false, true);
string xResDefault = HandlersUtil.ExtractAttributeValue ("default", child["xresolution"]);
string yResDefault = HandlersUtil.ExtractAttributeValue ("default", child["yresolution"]);
string unitsTypeDefault = HandlersUtil.ExtractAttributeValue ("default", child["unitstype"], true);
Hashtable unitScale = new Hashtable(3);
XmlNodeList unitScaleNodes = child.SelectNodes("unitscale");
foreach (XmlNode unitScaleNode in unitScaleNodes) {
unitScale.Add(
HandlersUtil.ExtractAttributeValue ("value", unitScaleNode),
HandlersUtil.ExtractNodeValue(unitScaleNode) );
}
ResolutionConfiguration resConf = new ResolutionConfiguration(
imageFormatName,
xResPath, yResPath, unitsType,
xResDefault, yResDefault, unitsTypeDefault,
unitScale);
col.Add(resConf);
}
col.Sort();
return col;
}
}
internal sealed class HandlersUtil {
private HandlersUtil () {
}
static internal string ExtractNodeValue(XmlNode node, bool optional, bool allowEmpty) {
if (node == null) {
if (optional)
return null;
ThrowException ("Required node not found", node);
}
string nodeValue = node.InnerText;
if (!allowEmpty && nodeValue == String.Empty) {
string opt = optional ? "Optional" : "Required";
ThrowException (opt + " node is empty", node);
}
return nodeValue;
}
static internal string ExtractNodeValue(XmlNode node, bool optional) {
return ExtractNodeValue(node, false, false);
}
static internal string ExtractNodeValue(XmlNode node) {
return ExtractNodeValue(node, false);
}
static internal string ExtractAttributeValue (string attKey, XmlNode node) {
return ExtractAttributeValue (attKey, node, false);
}
static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional) {
return ExtractAttributeValue (attKey, node, optional, false);
}
static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
bool allowEmpty) {
if (node.Attributes == null) {
if (optional)
return null;
ThrowException ("Required attribute not found: " + attKey, node);
}
XmlNode att = node.Attributes.RemoveNamedItem (attKey);
if (att == null) {
if (optional)
return null;
ThrowException ("Required attribute not found: " + attKey, node);
}
string value = att.Value;
if (!allowEmpty && value == String.Empty) {
string opt = optional ? "Optional" : "Required";
ThrowException (opt + " attribute is empty: " + attKey, node);
}
return value;
}
static internal void ThrowException (string msg, XmlNode node) {
if (node != null && node.Name != String.Empty)
msg = msg + " (node name: " + node.Name + ") ";
throw new ConfigurationException (msg, node);
}
}
}
|