17 using System.Collections.Generic;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
22 namespace erminas.SmartAPI.CMS
24 internal interface IXmlReadWriteWrapper
26 string GetAttributeValue(
string attributeName);
27 bool? GetBoolAttributeValue(
string attributeName);
28 Guid GetGuid(
string attributeName);
29 int? GetIntAttributeValue(
string attributeName);
30 bool IsAttributeSet(ISessionObject session,
string attributeName);
31 XmlElement MergedElement {
get; }
32 void SetAttributeValue(
string attributeName,
string value);
33 bool TryGetGuid(
string attributeName, out Guid guid);
36 internal class XmlReadWriteWrapper : IXmlReadWriteWrapper
38 private readonly IDictionary<string, string> _basisOverrides;
39 private readonly XmlElement _readElement;
40 private readonly IDictionary<string, string> _writtenValues =
new Dictionary<string, string>();
42 public XmlReadWriteWrapper(XmlElement readElement, IDictionary<string, string> basicOverrides)
44 _readElement = readElement;
45 _basisOverrides = basicOverrides;
48 public string GetAttributeValue(
string attributeName)
51 if (_writtenValues.TryGetValue(attributeName, out value) ||
52 _basisOverrides.TryGetValue(attributeName, out value))
57 return _readElement.GetAttributeValue(attributeName);
60 public bool? GetBoolAttributeValue(
string attributeName)
62 var value = GetIntAttributeValue(attributeName);
77 "Could not convert value '{0}' of attribute '{1}' to a boolean value", value,
81 public Guid GetGuid(
string attributeName)
83 var strValue = GetAttributeValue(attributeName);
84 return Guid.Parse(strValue);
87 public int? GetIntAttributeValue(
string attributeName)
89 var strValue = GetAttributeValue(attributeName);
90 return string.IsNullOrEmpty(strValue) ? null : (
int?)
int.Parse(strValue);
93 public bool IsAttributeSet(ISessionObject session,
string attributeName)
95 var strValue = GetAttributeValue(attributeName);
97 return !
string.IsNullOrEmpty(strValue) && strValue != (
"#" + session.Session.SessionKey);
100 public XmlElement MergedElement
104 var mergedElement = (XmlElement) _readElement.Clone();
105 foreach (var curEntry
in _basisOverrides)
107 mergedElement.SetAttributeValue(curEntry.Key, curEntry.Value);
109 foreach (var curEntry
in _writtenValues)
111 mergedElement.SetAttributeValue(curEntry.Key, curEntry.Value);
114 return mergedElement;
118 public void SetAttributeValue(
string attributeName,
string value)
120 _writtenValues[attributeName] = value;
123 public bool TryGetGuid(
string attributeName, out Guid guid)
125 var strValue = GetAttributeValue(attributeName);
126 return Guid.TryParse(strValue, out guid);
129 public IDictionary<string, string> WrittenValues
131 get {
return _writtenValues; }