-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathExtensions.cs
More file actions
223 lines (188 loc) · 5.63 KB
/
Extensions.cs
File metadata and controls
223 lines (188 loc) · 5.63 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Waher.Content;
using Waher.Content.Xml;
using Waher.Runtime.Collections;
using Waher.Script.Abstraction.Elements;
using Waher.Script.Exceptions;
using Waher.Script.Model;
using Waher.Script.Objects.Matrices;
namespace Waher.Script.Xml
{
/// <summary>
/// Script extensions helping with convertsion of script results to XML.
/// </summary>
public static class Extensions
{
/// <summary>
/// Exports the matrix to XML
/// </summary>
/// <param name="M"><see cref="ObjectMatrix"/> to be exported to XML.</param>
/// <param name="Namespace">Namespace of collection</param>
/// <param name="CollectionName">Local name of collection element.</param>
/// <param name="RecordName">Local name of record element.</param>
/// <returns>XML string.</returns>
public static string ToXml(this ObjectMatrix M, string Namespace, string CollectionName, string RecordName)
{
StringBuilder Xml = new StringBuilder();
M.ToXml(Xml, Namespace, CollectionName, RecordName);
return Xml.ToString();
}
/// <summary>
/// Exports the matrix to XML
/// </summary>
/// <param name="M"><see cref="ObjectMatrix"/> to be exported to XML.</param>
/// <param name="Xml">XML Output</param>
/// <param name="Namespace">Namespace of collection</param>
/// <param name="CollectionName">Local name of collection element.</param>
/// <param name="RecordName">Local name of record element.</param>
/// <returns>XML string.</returns>
public static void ToXml(this ObjectMatrix M, StringBuilder Xml, string Namespace, string CollectionName, string RecordName)
{
int c = M.ColumnNames?.Length ?? 0;
Xml.Append('<');
Xml.Append(CollectionName);
Xml.Append(" xmlns='");
Xml.Append(Namespace);
Xml.Append("'>");
foreach (IElement Row in M.VectorElements)
{
if (Row is IVector RowVector)
{
int i = 0;
Xml.Append('<');
Xml.Append(RecordName);
foreach (IElement Element in RowVector.VectorElements)
{
Xml.Append(' ');
if (i < c)
Xml.Append(M.ColumnNames[i++]);
else
{
Xml.Append('c');
Xml.Append((++i).ToString());
}
Xml.Append("='");
Xml.Append(XML.Encode(Expression.ToString(Element.AssociatedObjectValue)));
Xml.Append('\'');
}
Xml.Append("/>");
}
}
Xml.Append("</");
Xml.Append(CollectionName);
Xml.Append('>');
}
/// <summary>
/// Exports the matrix to XML
/// </summary>
/// <param name="Expression">Expression to expirt.</param>
/// <returns>XML string.</returns>
public static string ToXml(this Expression Expression)
{
StringBuilder Xml = new StringBuilder();
Expression.ToXml(Xml);
return Xml.ToString();
}
/// <summary>
/// Exports the matrix to XML
/// </summary>
/// <param name="Expression">Expression to expirt.</param>
/// <param name="Xml">XML Output</param>
/// <returns>XML string.</returns>
public static void ToXml(this Expression Expression, StringBuilder Xml)
{
ToXml(Expression, new StringWriter(Xml));
}
/// <summary>
/// Exports the matrix to XML
/// </summary>
/// <param name="Expression">Expression to expirt.</param>
/// <param name="Xml">XML Output</param>
/// <returns>XML string.</returns>
public static void ToXml(this Expression Expression, TextWriter Xml)
{
XmlWriterSettings Settings = new XmlWriterSettings()
{
Indent = true,
IndentChars = "\t",
CheckCharacters = false,
OmitXmlDeclaration = true
};
using (XmlWriter w = XmlWriter.Create(Xml, Settings))
{
Expression.ToXml(w);
w.Flush();
}
}
/// <summary>
/// Exports the matrix to XML
/// </summary>
/// <param name="Expression">Expression to expirt.</param>
/// <param name="Xml">XML Output</param>
/// <returns>XML string.</returns>
public static void ToXml(this Expression Expression, XmlWriter Xml)
{
ChunkedList<ScriptNode> Stack = new ChunkedList<ScriptNode>();
int c = 0;
Stack.Add(null);
Expression.ForAll((ScriptNode Node, out ScriptNode NewNode, object Stata) =>
{
NewNode = null;
while (Stack.HasLastItem && Stack.LastItem != Node.Parent)
{
if (Node.Parent is null)
throw new InvalidOperationException("Parent node reference not set correctly on node.");
if (c == 0)
throw new ScriptException("Script tree not consistent.");
Xml.WriteEndElement();
c--;
Stack.RemoveLast();
}
Type T = Node.GetType();
Xml.WriteStartElement(T.FullName);
c++;
foreach (PropertyInfo PI in T.GetRuntimeProperties())
{
if (!PI.CanRead || !PI.GetMethod.IsPublic)
continue;
if (PI.PropertyType.IsArray)
continue;
if (PI.PropertyType == typeof(ScriptNode))
continue;
if (typeof(IEnumerable<ScriptNode>).IsAssignableFrom(PI.PropertyType.GetTypeInfo()))
continue;
switch (PI.Name)
{
case "IsAsynchronous":
case "DefaultVariableName":
case "Start":
case "Length":
case "Parent":
case "Expression":
case "AssociatedObjectValue":
case "AssociatedSet":
case "ChildElements":
case "IsScalar":
continue;
}
object Value = PI.GetValue(Node);
if (Value is string s)
Xml.WriteAttributeString(PI.Name, s);
else if (Value is bool b)
Xml.WriteAttributeString(PI.Name, CommonTypes.Encode(b));
else
Xml.WriteAttributeString(PI.Name, Expression.ToString(Value));
}
Stack.Add(Node);
return true;
}, null, SearchMethod.TreeOrder);
while (c-- > 0)
Xml.WriteEndElement();
}
}
}