Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XSL.cs
1using System;
2using System.Reflection;
3using System.IO;
4using System.Text;
5using System.Xml;
6using System.Xml.Schema;
7using System.Xml.Xsl;
9
10namespace Waher.Content.Xsl
11{
15 public static class XSL
16 {
24 public static XmlSchema LoadSchema(string ResourceName)
25 {
26 return LoadSchema(ResourceName, Types.GetAssemblyForResource(ResourceName));
27 }
28
37 public static XmlSchema LoadSchema(string ResourceName, Assembly Assembly)
38 {
39 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
40 {
41 if (f is null)
42 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
43
44 return LoadSchema(f, ResourceName);
45 }
46 }
47
55 public static XmlSchema LoadSchema(byte[] Input, string ResourceName)
56 {
57 using (MemoryStream ms = new MemoryStream(Input))
58 {
59 return LoadSchema(ms, ResourceName);
60 }
61 }
62
70 public static XmlSchema LoadSchema(Stream Input, string ResourceName)
71 {
72 XmlValidator Validator = new XmlValidator(ResourceName);
73 XmlSchema Result = XmlSchema.Read(Input, Validator.ValidationCallback);
74
75 Validator.AssertNoError();
76
77 return Result;
78 }
79
86 public static XslCompiledTransform LoadTransform(string ResourceName)
87 {
88 return LoadTransform(ResourceName, Types.GetAssemblyForResource(ResourceName));
89 }
90
98 public static XslCompiledTransform LoadTransform(string ResourceName, Assembly Assembly)
99 {
100 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
101 {
102 if (f is null)
103 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
104
105 return LoadTransform(f);
106 }
107 }
108
114 public static XslCompiledTransform LoadTransform(Stream Input)
115 {
116 using (XmlReader r = XmlReader.Create(Input))
117 {
118 XslCompiledTransform Xslt = new XslCompiledTransform();
119 Xslt.Load(r);
120
121 return Xslt;
122 }
123 }
124
125 #region Validation
126
134 public static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
135 {
136 Validate(ObjectID, Xml, string.Empty, string.Empty, Schemas);
137 }
138
148 public static void Validate(string ObjectID, XmlDocument Xml, string ExpectedRootElement, string ExpectedRootElementNamespace,
149 params XmlSchema[] Schemas)
150 {
151 if (!string.IsNullOrEmpty(ExpectedRootElement))
152 {
153 if (Xml.DocumentElement is null)
154 throw new XmlSchemaException("Not root element.");
155
156 if (Xml.DocumentElement.LocalName != ExpectedRootElement)
157 {
158 throw new XmlSchemaException("Expected root element is " + ExpectedRootElement +
159 " but was " + Xml.DocumentElement.LocalName + ".");
160 }
161
162 if (Xml.DocumentElement.NamespaceURI != ExpectedRootElementNamespace)
163 {
164 throw new XmlSchemaException("Expected root element namespace is " + ExpectedRootElementNamespace +
165 " but was " + Xml.DocumentElement.NamespaceURI + ".");
166 }
167 }
168
169 lock (xmlSchemaSetSynchronization)
170 {
171 foreach (XmlSchema Schema in Schemas)
172 Xml.Schemas.Add(Schema);
173
174 XmlValidator Validator = new XmlValidator(ObjectID);
175 Xml.Validate(Validator.ValidationCallback);
176
177 Validator.AssertNoError();
178 }
179 }
180
185 private static readonly object xmlSchemaSetSynchronization = new object();
186
187 #endregion
188
189 #region Transformation
190
197 public static string Transform(string XML, XslCompiledTransform Transform)
198 {
199 StringBuilder Output = new StringBuilder();
200 TextWriter OutputText = new StringWriter(Output);
201 TextReader InputText = new StringReader(XML);
202 XmlReader XmlReader = XmlReader.Create(InputText);
203 XmlWriterSettings Settings = Content.Xml.XML.WriterSettings(false, true);
204 Settings.ConformanceLevel = ConformanceLevel.Auto;
205
206 XmlWriter XmlWriter = XmlWriter.Create(OutputText, Settings);
207
208 try
209 {
210 Transform.Transform(XmlReader, XmlWriter);
211 }
212 finally
213 {
214 XmlWriter.Flush();
215 OutputText.Flush();
216
217 XmlReader.Close();
218 InputText.Dispose();
219
220 XmlWriter.Close();
221 OutputText.Dispose();
222 }
223
224 return Output.ToString();
225 }
226
227 #endregion
228
229 }
230}
Static class managing loading of XSL resources stored as embedded resources or in content files.
Definition: XSL.cs:16
static XmlSchema LoadSchema(byte[] Input, string ResourceName)
Loads an XML schema from an array of bytes.
Definition: XSL.cs:55
static XmlSchema LoadSchema(string ResourceName, Assembly Assembly)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:37
static XmlSchema LoadSchema(Stream Input, string ResourceName)
Loads an XML schema from a stream.
Definition: XSL.cs:70
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:24
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:86
static XslCompiledTransform LoadTransform(string ResourceName, Assembly Assembly)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:98
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
Definition: XSL.cs:197
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:134
static XslCompiledTransform LoadTransform(Stream Input)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:114
static void Validate(string ObjectID, XmlDocument Xml, string ExpectedRootElement, string ExpectedRootElementNamespace, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:148
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static Assembly GetAssemblyForResource(string ResourceName)
Gets the assembly corresponding to a given resource name.
Definition: Types.cs:1787