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;
3using System.Text;
4using System.Xml;
8
10{
15 {
16 private readonly string encapsulatingNamespace;
17 private readonly string language = null;
18 private string normalizedXml = null;
19
23 public XmlLiteral()
24 : base()
25 {
26 }
27
34 public XmlLiteral(IEnumerable Value, string EncapsulatingNamespace, string Language)
35 : base(Value, ToString(Value))
36 {
37 this.encapsulatingNamespace = EncapsulatingNamespace;
38 this.language = Language;
39 }
40
41 private static string ToString(IEnumerable List)
42 {
43 StringBuilder sb = new StringBuilder();
44
45 foreach (XmlNode N in List)
46 sb.Append(N.OuterXml);
47
48 return sb.ToString();
49 }
50
58 public XmlLiteral(XmlNodeList Value, string EncapsulatingNamespace, string StringValue, string Language)
59 : base(Value, StringValue)
60 {
61 this.encapsulatingNamespace = EncapsulatingNamespace;
62 this.language = Language;
63 }
64
68 public static readonly string TypeUri = Rdf.XMLLiteral.OriginalString;
69
73 public override string StringType => TypeUri;
74
80 public override Grade Supports(Type ValueType)
81 {
82 return Grade.NotAtAll;
83 }
84
90 public override ISemanticLiteral Encapsulate(object Value)
91 {
92 return new StringLiteral(Value?.ToString() ?? string.Empty);
93 }
94
102 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
103 {
104 try
105 {
106 XmlDocument Doc = XML.ParseXml(Value, true);
107
108 return new XmlLiteral(Doc.ChildNodes, string.Empty, Value, Language);
109 }
110 catch (Exception)
111 {
112 return new CustomLiteral(Value, DataType, Language);
113 }
114 }
115
119 public string NormalizedXml
120 {
121 get
122 {
123 if (this.normalizedXml is null)
124 {
126 XML.NormalizeXml((XmlNodeList)this.Value, false, this.encapsulatingNamespace, State);
127 this.normalizedXml = State.ToString();
128 }
129
130 return this.normalizedXml;
131 }
132 }
133
135 public override bool Equals(object obj)
136 {
137 string s2;
138
139 if (obj is XmlLiteral Typed)
140 {
141 if (this.language != Typed.language)
142 return false;
143
144 s2 = Typed.NormalizedXml;
145 }
146 else if (obj is CustomLiteral Custom && Custom.StringType == TypeUri)
147 {
148 if (this.language != Custom.Language)
149 return false;
150
151 try
152 {
153 XmlDocument Doc = XML.ParseXml("<root>" + Custom.StringValue + "</root>", true);
154
155 s2 = XML.NormalizeXml(Doc.DocumentElement.ChildNodes, false);
156 }
157 catch (Exception)
158 {
159 return false;
160 }
161 }
162 else
163 return false;
164
165 string s1 = this.NormalizedXml;
166
167 return s1 == s2;
168 }
169
171 public override int GetHashCode()
172 {
173 int Result = this.NormalizedXml.GetHashCode();
174 Result ^= Result << 5 ^ (this.language?.GetHashCode() ?? 0);
175 return Result;
176 }
177
179 public override string ToString()
180 {
181 StringBuilder sb = new StringBuilder();
182
183 sb.Append('"');
184 sb.Append(JSON.Encode(this.StringValue));
185 sb.Append('"');
186
187 if (!string.IsNullOrEmpty(this.language))
188 {
189 sb.Append('@');
190 sb.Append(this.language);
191 }
192
193 sb.Append("^^<");
194 sb.Append(this.StringType);
195 sb.Append('>');
196
197 return sb.ToString();
198 }
199
200 }
201}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
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:102
static readonly string TypeUri
http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral
Definition: XmlLiteral.cs:68
override Grade Supports(Type ValueType)
How well the type supports a given value type.
Definition: XmlLiteral.cs:80
XmlLiteral(XmlNodeList Value, string EncapsulatingNamespace, string StringValue, string Language)
Represents a Uri literal.
Definition: XmlLiteral.cs:58
XmlLiteral()
Represents an XML literal.
Definition: XmlLiteral.cs:23
override ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
Definition: XmlLiteral.cs:90
XmlLiteral(IEnumerable Value, string EncapsulatingNamespace, string Language)
Represents an XML literal.
Definition: XmlLiteral.cs:34
Abstract base class for semantic literal values.
string StringValue
String representation of value.
static readonly Uri XMLLiteral
URI representing rdf:XMLLiteral
Definition: Rdf.cs:117
Helps with common XML-related tasks.
Definition: XML.cs:21
static string NormalizeXml(string Xml)
Normalizes XML in string form.
Definition: XML.cs:1561
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Current state of XML normalization process.
Interface for semantic literals.
Grade
Grade enumeration
Definition: Grade.cs:7