Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlLiteral.cs
1using System;
2using System.Collections;
3using System.Text;
4using System.Xml;
7
9{
14 {
15 private readonly string encapsulatingNamespace;
16 private readonly string language = null;
17 private string normalizedXml = null;
18
22 public XmlLiteral()
23 : base()
24 {
25 }
26
33 public XmlLiteral(IEnumerable Value, string EncapsulatingNamespace, string Language)
34 : base(Value, ToString(Value))
35 {
36 this.encapsulatingNamespace = EncapsulatingNamespace;
37 this.language = Language;
38 }
39
40 private static string ToString(IEnumerable List)
41 {
42 StringBuilder sb = new StringBuilder();
43
44 foreach (XmlNode N in List)
45 sb.Append(N.OuterXml);
46
47 return sb.ToString();
48 }
49
57 public XmlLiteral(XmlNodeList Value, string EncapsulatingNamespace, string StringValue, string Language)
58 : base(Value, StringValue)
59 {
60 this.encapsulatingNamespace = EncapsulatingNamespace;
61 this.language = Language;
62 }
63
67 public const string TypeUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral";
68
72 public override string StringType => TypeUri;
73
79 public override Grade Supports(Type ValueType)
80 {
81 return Grade.NotAtAll;
82 }
83
89 public override ISemanticLiteral Encapsulate(object Value)
90 {
91 return new StringLiteral(Value?.ToString() ?? string.Empty);
92 }
93
101 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
102 {
103 try
104 {
105 XmlDocument Doc = new XmlDocument()
106 {
107 PreserveWhitespace = true
108 };
109
110 Doc.LoadXml(Value);
111
112 return new XmlLiteral(Doc.ChildNodes, string.Empty, Value, Language);
113 }
114 catch (Exception)
115 {
116 return new CustomLiteral(Value, DataType, Language);
117 }
118 }
119
123 public string NormalizedXml
124 {
125 get
126 {
127 if (this.normalizedXml is null)
128 {
130 XML.NormalizeXml((XmlNodeList)this.Value, false, this.encapsulatingNamespace, State);
131 this.normalizedXml = State.ToString();
132 }
133
134 return this.normalizedXml;
135 }
136 }
137
139 public override bool Equals(object obj)
140 {
141 string s2;
142
143 if (obj is XmlLiteral Typed)
144 {
145 if (this.language != Typed.language)
146 return false;
147
148 s2 = Typed.NormalizedXml;
149 }
150 else if (obj is CustomLiteral Custom && Custom.StringType == TypeUri)
151 {
152 if (this.language != Custom.Language)
153 return false;
154
155 try
156 {
157 XmlDocument Doc = new XmlDocument()
158 {
159 PreserveWhitespace = true
160 };
161 Doc.LoadXml("<root>" + Custom.StringValue + "</root>");
162
163 s2 = XML.NormalizeXml(Doc.DocumentElement.ChildNodes, false);
164 }
165 catch (Exception)
166 {
167 return false;
168 }
169 }
170 else
171 return false;
172
173 string s1 = this.NormalizedXml;
174
175 return s1 == s2;
176 }
177
179 public override int GetHashCode()
180 {
181 int Result = this.NormalizedXml.GetHashCode();
182 Result ^= Result << 5 ^ (this.language?.GetHashCode() ?? 0);
183 return Result;
184 }
185
187 public override string ToString()
188 {
189 StringBuilder sb = new StringBuilder();
190
191 sb.Append('"');
192 sb.Append(JSON.Encode(this.StringValue));
193 sb.Append('"');
194
195 if (!string.IsNullOrEmpty(this.language))
196 {
197 sb.Append('@');
198 sb.Append(this.language);
199 }
200
201 sb.Append("^^<");
202 sb.Append(this.StringType);
203 sb.Append('>');
204
205 return sb.ToString();
206 }
207
208 }
209}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
override ISemanticLiteral Parse(string Value, string DataType, string Language)
Tries to parse a string value of the type supported by the class..
Definition: XmlLiteral.cs:101
override Grade Supports(Type ValueType)
How well the type supports a given value type.
Definition: XmlLiteral.cs:79
XmlLiteral(XmlNodeList Value, string EncapsulatingNamespace, string StringValue, string Language)
Represents a Uri literal.
Definition: XmlLiteral.cs:57
XmlLiteral()
Represents an XML literal.
Definition: XmlLiteral.cs:22
const string TypeUri
http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral
Definition: XmlLiteral.cs:67
override ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
Definition: XmlLiteral.cs:89
XmlLiteral(IEnumerable Value, string EncapsulatingNamespace, string Language)
Represents an XML literal.
Definition: XmlLiteral.cs:33
Abstract base class for semantic literal values.
string StringValue
String representation of value.
Helps with common XML-related tasks.
Definition: XML.cs:19
static string NormalizeXml(XmlNodeList Xml)
Normalizes a list of XML nodes.
Definition: XML.cs:1386
Current state of XML normalization process.
Interface for semantic literals.
Grade
Grade enumeration
Definition: Grade.cs:7