xn/Transformation/ContextFunctions.cs

77 lines
1.8 KiB
C#

using System;
using System.IO;
using System.Text;
using System.Xml;
using Sprache;
using XNeedle.Parser;
using XNeedle.Parser.Elements;
namespace XNeedle.Transformation
{
public class ContextFunctions
{
public static string hex(Context ctx, double value)
{
uint v = (uint)value;
return $"#x{v:X8}";
}
public static int GetLevel(XmlNode node)
{
int level = 0;
XmlNode? currentNode = node;
while (null != (currentNode = currentNode.ParentNode))
level++;
return level;
}
public static double crc(Context ctx)
{
if (ctx.Node is null)
{
return 0;
}
int crc = 0xAFFE;
return (double)crc;
}
public static string text(Context ctx)
{
if (ctx.Node is null) return "";
return ctx.Node.Value;
}
public static string text(Context ctx, string txt)
{
if (ctx.Node is null) return "";
ctx.Node.SetValue(txt);
return txt;
}
public static string attr(Context ctx, string name, string value)
{
if (ctx.Node is null || !ctx.Node.HasAttributes) return "";
var attr = ctx.Node.Attribute(name);
if (attr != null)
{
attr.Value = value;
}
else
{
ctx.Node.SetAttributeValue(name, value);
}
return "";
}
public static string attr(Context ctx, string name)
{
var attr = ctx.Node?.Attribute(name);
if (attr != null)
{
return attr.Value;
}
return "";
}
}
}