forked from phusband/PiaNO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PiaNode.cs
154 lines (121 loc) · 3.92 KB
/
PiaNode.cs
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
//
// Copyright © 2014 Parrish Husband ([email protected])
// The MIT License (MIT) - See LICENSE.txt for further details.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using PiaNO.Serialization;
namespace PiaNO
{
public class PiaNode : ICloneable, IEquatable<PiaNode>, IEnumerable<PiaNode>
{
#region Properties
private IList<PiaNode> _childNodes;
protected internal IList<PiaNode> ChildNodes
{
get { return _childNodes ?? (_childNodes = new List<PiaNode>()); }
set { _childNodes = value; }
}
protected internal bool HasChildNodes
{
get { return ChildNodes != null && ChildNodes.Count > 0; }
}
protected internal string InnerData
{
get { return PiaSerializer._serializeNode(this); }
}
protected internal string NodeName { get; set; }
protected internal PiaFile Owner { get; set; }
protected internal PiaNode Parent { get; set; }
protected internal Dictionary<string, string> Values
{
get { return _values ?? (_values = new Dictionary<string, string>()); }
set { _values = value; }
}
private Dictionary<string, string> _values;
#endregion
#region Constructors
protected internal PiaNode()
{
ChildNodes = new List<PiaNode>();
Values = new Dictionary<string, string>();
}
protected internal PiaNode(string innerData)
{
ChildNodes = new List<PiaNode>();
Values = new Dictionary<string, string>();
PiaSerializer._deserializeNode(this, innerData);
}
#endregion
#region Methods
protected internal string GetValue(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
if (!Values.ContainsKey(key))
Values.Add(key, string.Empty);
return Values[key];
}
protected internal void SetValue(string key, string value)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
if (!Values.ContainsKey(key))
Values.Add(key, value);
else
Values[key] = value;
}
protected internal static Color? GetColor(string input)
{
var colorVal = int.Parse(input);
if (colorVal == -1)
return null;
return Color.FromArgb(colorVal);
}
public virtual PiaNode this[string name]
{
get
{
if (ChildNodes.Count == 0)
return null;
//throw new ArgumentOutOfRangeException();
return ChildNodes.FirstOrDefault(n => n.NodeName.Equals(name,
StringComparison.InvariantCultureIgnoreCase));
}
}
public override string ToString()
{
return this.NodeName;
}
#endregion
#region ICloneable
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
#region IEquatable
public bool Equals(PiaNode other)
{
if (this == null && other == null)
return true;
if (this == null || other == null)
return false;
return this.NodeName.Equals(other.NodeName, StringComparison.InvariantCultureIgnoreCase);
}
#endregion
#region IEnumerable
public IEnumerator<PiaNode> GetEnumerator()
{
return ChildNodes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}