ResolutionConfigurationHandler.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Xml;
  5. namespace Mainsoft.Drawing.Configuration
  6. {
  7. /// <summary>
  8. /// Summary description for MetadataConfigurationHandler.
  9. /// </summary>
  10. public class ResolutionConfigurationHandler : IConfigurationSectionHandler
  11. {
  12. public ResolutionConfigurationHandler()
  13. {
  14. //
  15. // TODO: Add constructor logic here
  16. //
  17. }
  18. public virtual object Create (object parent, object configContext, XmlNode section) {
  19. if (section.Attributes != null && section.Attributes.Count != 0)
  20. HandlersUtil.ThrowException ("Unrecognized attribute", section);
  21. ResolutionConfigurationCollection col =
  22. new ResolutionConfigurationCollection(parent as ResolutionConfigurationCollection);
  23. XmlNodeList imageFormats = section.ChildNodes;
  24. foreach (XmlNode child in imageFormats) {
  25. XmlNodeType ntype = child.NodeType;
  26. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  27. continue;
  28. if (ntype != XmlNodeType.Element)
  29. HandlersUtil.ThrowException ("Only elements allowed", child);
  30. string imageFormatName = HandlersUtil.ExtractAttributeValue ("name", child, false, false);
  31. string xResPath = HandlersUtil.ExtractNodeValue(child["xresolution"]);
  32. string yResPath = HandlersUtil.ExtractNodeValue(child["yresolution"]);
  33. string unitsType = HandlersUtil.ExtractNodeValue(child["unitstype"], false, true);
  34. string xResDefault = HandlersUtil.ExtractAttributeValue ("default", child["xresolution"]);
  35. string yResDefault = HandlersUtil.ExtractAttributeValue ("default", child["yresolution"]);
  36. string unitsTypeDefault = HandlersUtil.ExtractAttributeValue ("default", child["unitstype"], true);
  37. Hashtable unitScale = new Hashtable(3);
  38. XmlNodeList unitScaleNodes = child.SelectNodes("unitscale");
  39. foreach (XmlNode unitScaleNode in unitScaleNodes) {
  40. unitScale.Add(
  41. HandlersUtil.ExtractAttributeValue ("value", unitScaleNode),
  42. HandlersUtil.ExtractNodeValue(unitScaleNode) );
  43. }
  44. ResolutionConfiguration resConf = new ResolutionConfiguration(
  45. imageFormatName,
  46. xResPath, yResPath, unitsType,
  47. xResDefault, yResDefault, unitsTypeDefault,
  48. unitScale);
  49. col.Add(resConf);
  50. }
  51. col.Sort();
  52. return col;
  53. }
  54. }
  55. internal sealed class HandlersUtil {
  56. private HandlersUtil () {
  57. }
  58. static internal string ExtractNodeValue(XmlNode node, bool optional, bool allowEmpty) {
  59. if (node == null) {
  60. if (optional)
  61. return null;
  62. ThrowException ("Required node not found", node);
  63. }
  64. string nodeValue = node.InnerText;
  65. if (!allowEmpty && nodeValue == String.Empty) {
  66. string opt = optional ? "Optional" : "Required";
  67. ThrowException (opt + " node is empty", node);
  68. }
  69. return nodeValue;
  70. }
  71. static internal string ExtractNodeValue(XmlNode node, bool optional) {
  72. return ExtractNodeValue(node, false, false);
  73. }
  74. static internal string ExtractNodeValue(XmlNode node) {
  75. return ExtractNodeValue(node, false);
  76. }
  77. static internal string ExtractAttributeValue (string attKey, XmlNode node) {
  78. return ExtractAttributeValue (attKey, node, false);
  79. }
  80. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional) {
  81. return ExtractAttributeValue (attKey, node, optional, false);
  82. }
  83. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional,
  84. bool allowEmpty) {
  85. if (node.Attributes == null) {
  86. if (optional)
  87. return null;
  88. ThrowException ("Required attribute not found: " + attKey, node);
  89. }
  90. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  91. if (att == null) {
  92. if (optional)
  93. return null;
  94. ThrowException ("Required attribute not found: " + attKey, node);
  95. }
  96. string value = att.Value;
  97. if (!allowEmpty && value == String.Empty) {
  98. string opt = optional ? "Optional" : "Required";
  99. ThrowException (opt + " attribute is empty: " + attKey, node);
  100. }
  101. return value;
  102. }
  103. static internal void ThrowException (string msg, XmlNode node) {
  104. if (node != null && node.Name != String.Empty)
  105. msg = msg + " (node name: " + node.Name + ") ";
  106. throw new ConfigurationException (msg, node);
  107. }
  108. }
  109. }