SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IXmlReadWriteWrapper.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Xml;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
21 
22 namespace erminas.SmartAPI.CMS
23 {
24  internal interface IXmlReadWriteWrapper
25  {
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);
34  }
35 
36  internal class XmlReadWriteWrapper : IXmlReadWriteWrapper
37  {
38  private readonly IDictionary<string, string> _basisOverrides;
39  private readonly XmlElement _readElement;
40  private readonly IDictionary<string, string> _writtenValues = new Dictionary<string, string>();
41 
42  public XmlReadWriteWrapper(XmlElement readElement, IDictionary<string, string> basicOverrides)
43  {
44  _readElement = readElement;
45  _basisOverrides = basicOverrides;
46  }
47 
48  public string GetAttributeValue(string attributeName)
49  {
50  string value;
51  if (_writtenValues.TryGetValue(attributeName, out value) ||
52  _basisOverrides.TryGetValue(attributeName, out value))
53  {
54  return value;
55  }
56 
57  return _readElement.GetAttributeValue(attributeName);
58  }
59 
60  public bool? GetBoolAttributeValue(string attributeName)
61  {
62  var value = GetIntAttributeValue(attributeName);
63  if (value == null)
64  {
65  return null;
66  }
67  if (value == 1)
68  {
69  return true;
70  }
71  if (value == 0)
72  {
73  return false;
74  }
75  throw new SmartAPIException((ServerLogin) null,
76  string.Format(
77  "Could not convert value '{0}' of attribute '{1}' to a boolean value", value,
78  attributeName));
79  }
80 
81  public Guid GetGuid(string attributeName)
82  {
83  var strValue = GetAttributeValue(attributeName);
84  return Guid.Parse(strValue);
85  }
86 
87  public int? GetIntAttributeValue(string attributeName)
88  {
89  var strValue = GetAttributeValue(attributeName);
90  return string.IsNullOrEmpty(strValue) ? null : (int?) int.Parse(strValue);
91  }
92 
93  public bool IsAttributeSet(ISessionObject session, string attributeName)
94  {
95  var strValue = GetAttributeValue(attributeName);
96 
97  return !string.IsNullOrEmpty(strValue) && strValue != ("#" + session.Session.SessionKey);
98  }
99 
100  public XmlElement MergedElement
101  {
102  get
103  {
104  var mergedElement = (XmlElement) _readElement.Clone();
105  foreach (var curEntry in _basisOverrides)
106  {
107  mergedElement.SetAttributeValue(curEntry.Key, curEntry.Value);
108  }
109  foreach (var curEntry in _writtenValues)
110  {
111  mergedElement.SetAttributeValue(curEntry.Key, curEntry.Value);
112  }
113 
114  return mergedElement;
115  }
116  }
117 
118  public void SetAttributeValue(string attributeName, string value)
119  {
120  _writtenValues[attributeName] = value;
121  }
122 
123  public bool TryGetGuid(string attributeName, out Guid guid)
124  {
125  var strValue = GetAttributeValue(attributeName);
126  return Guid.TryParse(strValue, out guid);
127  }
128 
129  public IDictionary<string, string> WrittenValues
130  {
131  get { return _writtenValues; }
132  }
133  }
134 }