Unpack iOS app receipt in c# and php - c#

I have Xamarin iOS app with in-app products. I get base64 encoded app receipt:
NSUrl receiptURL = NSBundle.MainBundle.AppStoreReceiptUrl;
String receiptData = receipt.GetBase64EncodedString(0);
According to Apple docs 7.0+ app receipt is packed in PKCS7 container using ASN1. When I send it to apple server it returns receipt in JSON. But I want to parse it locally to know what iaps user does already have. I don't need validation, as Apple does it remotely, I just need to get receipts for purchased inaps.
So far, as an investigation, I've parsed it in php with phpseclib, manually (no idea how to do this programatically) located receipt and parsed it as well.
$asn_parser = new File_ASN1();
//parse the receipt binary string
$pkcs7 = $asn_parser->decodeBER(base64_decode($f));
//print_r($pkcs7);
$payload_sequence = $pkcs7[0]['content'][1]['content'][0]['content'][2]['content'];
$pld = $asn_parser->decodeBER($payload_sequence[1]['content'][0]['content']);
//print_r($pld);
$prd = $asn_parser->decodeBER($pld[0]['content'][21]['content'][2]['content']);
print_r($prd);
But even this way I've got a mess of attributes, each looks like:
Array
(
[start] => 271
[headerlength] => 2
[type] => 4
[content] => 2016-08-22T13:22:00Z
[length] => 24
)
It is not human readable, I need something like (output with print_r of returned by Apple) :
[receipt] => Array
(
[receipt_type] => ProductionSandbox
[adam_id] => 0
[app_item_id] => 0
[bundle_id] => com.my.test.app.iOS
...
[in_app] => Array
(
[0] => Array
(
[quantity] => 1
[product_id] => test_iap_1
[transaction_id] => 1000000230806171
...
[is_trial_period] => false
)
)
)
Things seem too complicated, I hardly believe unpacking receipt is so complex. Does anybody know how to manage this? I've found this post but library is written in objective-C which is not applicable to my current environment. I'd say sources of this lib are frightened me: so much complex code just to unpack standartized container. I mean working with json, bson, etc. is very easy, but not asn1.

Finally I've unpacked it using Liping Dai LCLib (lipingshare.com). Asn1Parser returns DOM-like tree with root node - very handy lib.
public class AppleAppReceipt
{
public class AppleInAppPurchaseReceipt
{
public int Quantity;
public string ProductIdentifier;
public string TransactionIdentifier;
public DateTime PurchaseDate;
public string OriginalTransactionIdentifier;
public DateTime OriginalPurchaseDate;
public DateTime SubscriptionExpirationDate;
public DateTime CancellationDate;
public int WebOrderLineItemID;
}
const int AppReceiptASN1TypeBundleIdentifier = 2;
const int AppReceiptASN1TypeAppVersion = 3;
const int AppReceiptASN1TypeOpaqueValue = 4;
const int AppReceiptASN1TypeHash = 5;
const int AppReceiptASN1TypeReceiptCreationDate = 12;
const int AppReceiptASN1TypeInAppPurchaseReceipt = 17;
const int AppReceiptASN1TypeOriginalAppVersion = 19;
const int AppReceiptASN1TypeReceiptExpirationDate = 21;
const int AppReceiptASN1TypeQuantity = 1701;
const int AppReceiptASN1TypeProductIdentifier = 1702;
const int AppReceiptASN1TypeTransactionIdentifier = 1703;
const int AppReceiptASN1TypePurchaseDate = 1704;
const int AppReceiptASN1TypeOriginalTransactionIdentifier = 1705;
const int AppReceiptASN1TypeOriginalPurchaseDate = 1706;
const int AppReceiptASN1TypeSubscriptionExpirationDate = 1708;
const int AppReceiptASN1TypeWebOrderLineItemID = 1711;
const int AppReceiptASN1TypeCancellationDate = 1712;
public string BundleIdentifier;
public string AppVersion;
public string OriginalAppVersion; //какую покупали
public DateTime ReceiptCreationDate;
public Dictionary<string, AppleInAppPurchaseReceipt> PurchaseReceipts;
public bool parseAsn1Data(byte[] val)
{
if (val == null)
return false;
Asn1Parser p = new Asn1Parser();
var stream = new MemoryStream(val);
try
{
p.LoadData(stream);
}
catch (Exception e)
{
return false;
}
Asn1Node root = p.RootNode;
if (root == null)
return false;
PurchaseReceipts = new Dictionary<string, AppleInAppPurchaseReceipt>();
parseNodeRecursive(root);
return !string.IsNullOrEmpty(BundleIdentifier);
}
private static string getStringFromSubNode(Asn1Node nn)
{
string dataStr = null;
if ((nn.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.OCTET_STRING && nn.ChildNodeCount > 0)
{
Asn1Node n = nn.GetChildNode(0);
switch (n.Tag & Asn1Tag.TAG_MASK)
{
case Asn1Tag.PRINTABLE_STRING:
case Asn1Tag.IA5_STRING:
case Asn1Tag.UNIVERSAL_STRING:
case Asn1Tag.VISIBLE_STRING:
case Asn1Tag.NUMERIC_STRING:
case Asn1Tag.UTC_TIME:
case Asn1Tag.UTF8_STRING:
case Asn1Tag.BMPSTRING:
case Asn1Tag.GENERAL_STRING:
case Asn1Tag.GENERALIZED_TIME:
{
if ((n.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.UTF8_STRING)
{
UTF8Encoding unicode = new UTF8Encoding();
dataStr = unicode.GetString(n.Data);
}
else
{
dataStr = Asn1Util.BytesToString(n.Data);
}
}
break;
}
}
return dataStr;
}
private static DateTime getDateTimeFromSubNode(Asn1Node nn)
{
string dataStr = getStringFromSubNode(nn);
if (string.IsNullOrEmpty(dataStr))
return DateTime.MinValue;
DateTime retval = DateTime.MaxValue;
try
{
retval = DateTime.Parse(dataStr);
}
catch (Exception e)
{
}
return retval;
}
private static int getIntegerFromSubNode(Asn1Node nn)
{
int retval = -1;
if ((nn.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.OCTET_STRING && nn.ChildNodeCount > 0)
{
Asn1Node n = nn.GetChildNode(0);
if ((n.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.INTEGER)
retval = (int)Asn1Util.BytesToLong(n.Data);
}
return retval;
}
private void parseNodeRecursive(Asn1Node tNode)
{
bool processed_node = false;
if ((tNode.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.SEQUENCE && tNode.ChildNodeCount == 3)
{
Asn1Node node1 = tNode.GetChildNode(0);
Asn1Node node2 = tNode.GetChildNode(1);
Asn1Node node3 = tNode.GetChildNode(2);
if ((node1.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.INTEGER && (node2.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.INTEGER &&
(node3.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.OCTET_STRING)
{
processed_node = true;
int type = (int)Asn1Util.BytesToLong(node1.Data);
switch (type)
{
case AppReceiptASN1TypeBundleIdentifier:
BundleIdentifier = getStringFromSubNode(node3);
break;
case AppReceiptASN1TypeAppVersion:
AppVersion = getStringFromSubNode(node3);
break;
case AppReceiptASN1TypeOpaqueValue:
break;
case AppReceiptASN1TypeHash:
break;
case AppReceiptASN1TypeOriginalAppVersion:
OriginalAppVersion = getStringFromSubNode(node3);
break;
case AppReceiptASN1TypeReceiptExpirationDate:
break;
case AppReceiptASN1TypeReceiptCreationDate:
ReceiptCreationDate = getDateTimeFromSubNode(node3);
break;
case AppReceiptASN1TypeInAppPurchaseReceipt:
{
if (node3.ChildNodeCount > 0)
{
Asn1Node node31 = node3.GetChildNode(0);
if ((node31.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.SET && node31.ChildNodeCount > 0)
{
AppleInAppPurchaseReceipt receipt = new AppleInAppPurchaseReceipt();
for (int i = 0; i < node31.ChildNodeCount; i++)
{
Asn1Node node311 = node31.GetChildNode(i);
if ((node311.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.SEQUENCE && node311.ChildNodeCount == 3)
{
Asn1Node node3111 = node311.GetChildNode(0);
Asn1Node node3112 = node311.GetChildNode(1);
Asn1Node node3113 = node311.GetChildNode(2);
if ((node3111.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.INTEGER && (node3112.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.INTEGER &&
(node3113.Tag & Asn1Tag.TAG_MASK) == Asn1Tag.OCTET_STRING)
{
int type1 = (int)Asn1Util.BytesToLong(node3111.Data);
switch (type1)
{
case AppReceiptASN1TypeQuantity:
receipt.Quantity = getIntegerFromSubNode(node3113);
break;
case AppReceiptASN1TypeProductIdentifier:
receipt.ProductIdentifier = getStringFromSubNode(node3113);
break;
case AppReceiptASN1TypeTransactionIdentifier:
receipt.TransactionIdentifier = getStringFromSubNode(node3113);
break;
case AppReceiptASN1TypePurchaseDate:
receipt.PurchaseDate = getDateTimeFromSubNode(node3113);
break;
case AppReceiptASN1TypeOriginalTransactionIdentifier:
receipt.OriginalTransactionIdentifier = getStringFromSubNode(node3113);
break;
case AppReceiptASN1TypeOriginalPurchaseDate:
receipt.OriginalPurchaseDate = getDateTimeFromSubNode(node3113);
break;
case AppReceiptASN1TypeSubscriptionExpirationDate:
receipt.SubscriptionExpirationDate = getDateTimeFromSubNode(node3113);
break;
case AppReceiptASN1TypeWebOrderLineItemID:
receipt.WebOrderLineItemID = getIntegerFromSubNode(node3113);
break;
case AppReceiptASN1TypeCancellationDate:
receipt.CancellationDate = getDateTimeFromSubNode(node3113);
break;
}
}
}
}
if (!string.IsNullOrEmpty(receipt.ProductIdentifier))
PurchaseReceipts.Add(receipt.ProductIdentifier, receipt);
}
}
}
break;
default:
processed_node = false;
break;
}
}
}
if (!processed_node)
{
for (int i = 0; i < tNode.ChildNodeCount; i++)
{
Asn1Node chld = tNode.GetChildNode(i);
if (chld != null)
parseNodeRecursive(chld);
}
}
}
}
And usage:
public void printAppReceipt()
{
NSUrl receiptURL = NSBundle.MainBundle.AppStoreReceiptUrl;
if (receiptURL != null)
{
Console.WriteLine("receiptUrl='" + receiptURL + "'");
NSData receipt = NSData.FromUrl(receiptURL);
if (receipt != null)
{
byte[] rbytes = receipt.ToArray();
AppleAppReceipt apprec = new AppleAppReceipt();
if (apprec.parseAsn1Data(rbytes))
{
Console.WriteLine("Received receipt for " + apprec.BundleIdentifier + " with " + apprec.PurchaseReceipts.Count +
" products");
Console.WriteLine(JsonConvert.SerializeObject(apprec,Formatting.Indented));
}
}
else
Console.WriteLine("receipt == null");
}
}

Related

Trying to detect which html tags are affecting an element in a string

I am trying to write a markup editor in c#, part of is is to detect which tags are affecting where the caret currently is.
Examples:
<b>Tes|ting</b><u><i>Testing</i>Testing</u> (caret '|' at index 6)
Answer: [b]
<b>Testing<u><i>Test|ing</i>Tes</b>ting</u> (caret '|' at index 20)
Answer: [b, u, i]
Here is the code I have currently, inside "GetTags()" I split the string into 2 queues representing what tags are in front and behind where the caret is. I played around with ways to pop the elements from both queues to try to solve the problem but I keep getting wrong results. I think im on the right track creating 2 data structures but I don't know if Queue<> is what I need.
public class Tag
{
public enum TagType
{
bold,
italic,
underline,
}
public TagType type;
public bool opening;
public Tag(TagType type, bool opening)
{
this.type = type;
this.opening = opening;
}
public static Tag GetTagFromString(string str)
{
switch (str)
{
case "<b>": return new Tag(TagType.bold, true);
case "</b>": return new Tag(TagType.bold, false);
case "<i>": return new Tag(TagType.italic, true);
case "</i>": return new Tag(TagType.italic, false);
case "<u>": return new Tag(TagType.underline, true);
case "</u>": return new Tag(TagType.underline, false);
}
return null;
}
public static List<TagType> GetTags(string str, int index)
{
Queue<Tag> backQueue = new Queue<Tag>();
Queue<Tag> forwardQueue = new Queue<Tag>();
// populate the back queue
int i = index;
while (true)
{
int lastOpening = str.LastIndexOf('<', i);
int lastClosing = str.LastIndexOf('>', i);
if (lastOpening != -1 && lastClosing != -1)
{
string tagStr = str.Substring(lastOpening, lastClosing - lastOpening + 1);
Tag tag = GetTagFromString(tagStr);
backQueue.Enqueue(tag);
i = lastOpening - 1;
if (i < 0)
break;
}
else
break;
}
// populate the front stack
i = index;
while (true)
{
int nextOpening = str.IndexOf('<', i);
int nextClosing = str.IndexOf('>', i);
if (nextOpening != -1 && nextClosing != -1)
{
string tagStr = str.Substring(nextOpening, nextClosing - nextOpening + 1);
Tag tag = GetTagFromString(tagStr);
forwardQueue.Enqueue(tag);
i = nextClosing + 1;
}
else
break;
}
List<TagType> tags = new List<TagType>();
// populate 'tags' list with the tags affecting the index here
return tags;
}
public override string ToString()
{
string str = "<";
if (!opening)
str += "/";
switch (type)
{
case TagType.bold:
str += "b";
break;
case TagType.italic:
str += "i";
break;
case TagType.underline:
str += "u";
break;
}
str += ">";
return str;
}
}
Would love any input on how I could solve this and would greatly appreciate any issues anyone has with the code that I've provided.

C# Simple Passing Parameters Issue

I know I sound like a bad programmer right now - but I'm new and I can't figure out how to use this reference thing and pass parameters, I mean I know how to do it - but at the same time - this isn't working and I don't know why.
static void Main(string[] args) {
DealCard(ref card);
Console.WriteLine();
Console.ReadLine();
}
private static void DealCard(string card) {
string finalNum = "";
string finalSuite = "";
bool diffCard = false;
do {
Random cardPicker = new Random();
int cardSuite = cardPicker.Next(1, 5);
if (cardSuite == 1) {
finalSuite = "Hearts";
} else if (cardSuite == 2) {
finalSuite = "Spades";
} else if (cardSuite == 3) {
finalSuite = "Clubs";
} else if (cardSuite == 4) {
finalSuite = "Diamonds";
}
int cardNum = cardPicker.Next(1, 14);
if (cardNum == 1) {
finalNum = "Ace";
} else if (cardNum == 2) {
finalNum = "Two";
} else if (cardNum == 3) {
finalNum = "Thre";
} else if (cardNum == 4) {
finalNum = "Four";
} else if (cardNum == 5) {
finalNum = "Five";
} else if (cardNum == 6) {
finalNum = "Six";
} else if (cardNum == 7) {
finalNum = "Seven";
} else if (cardNum == 8) {
finalNum = "Eight";
} else if (cardNum == 9) {
finalNum = "Nine";
} else if (cardNum == 10) {
finalNum = "Ten";
} else if (cardNum == 11) {
finalNum = "Jack";
} else if (cardNum == 12) {
finalNum = "Queen";
} else if (cardNum == 13) {
finalNum = "King";
}
string newCard = finalNum + " of " + finalSuite;
if (newCard != card) {
card = finalNum + " of " + finalSuite;
diffCard = true;
} else {
}
card = newCard;
} while (diffCard == false);
}
Yes I know that massive 'if' is an eyesore.
Yes I know I could accomplish this in less than half the lines.
Yes I know it's a simple question.
Yes I know I'm bad, but I'd like to humbly request that anyone helps me to stop losing hair over this.
You have to declare your method like this:
private static void DealCard(ref string card)
Basically the method has to accept a ref parameter.
Here is documentation to support the answer:
Value Type Parameters
Reference Type Parameters
Your code can be like this
public class Program
{
public static void Main(string[] args) {
string card = "";
DealCard(ref card);
}
private static void DealCard(ref string card)
{
string finalNum = "";
string finalSuite = "";
bool diffCard = false;
do {
Random cardPicker = new Random();
int cardSuite = cardPicker.Next(1, 5);
string[] suite = new String[]{"Hearts","Spades", "Clubs", "Diaminds"};
int cardNum = cardPicker.Next(1, 3);
string[] numbers = new String[]{"one","two","three", "four"};
string newCard = numbers[cardNum] + " of " + suite[cardSuite];
if (newCard != card) {
card = finalNum + " of " + finalSuite;
diffCard = true;
} else {
}
card = newCard;
Console.WriteLine(newCard);
} while (diffCard == false);
}
}

Write a test to find all types used in the instantiation of a class

I have some code like:
new ValidationFailure<AddSystemUserDto>
This is in various places in my application service layer, I want to find all the different "Dto" types that have been used when newing up a ValidationFailure across the code, is this possible using reflection? Without having to run each application service method?
Yes.
I use a little helper class for these kinds of tasks, that I conveniently call Decompiler. Basically it transforms a byte[] of .NET code to opcodes:
public class ILInstruction
{
public ILInstruction(int offset, OpCode code, object operand)
{
this.Offset = offset;
this.Code = code;
this.Operand = operand;
}
// Fields
public int Offset { get; private set; }
public OpCode Code { get; private set; }
public object Operand { get; private set; }
}
internal class Decompiler
{
private Decompiler() { }
static Decompiler()
{
InitDecompiler();
}
private static OpCode[] singleByteOpcodes;
private static OpCode[] multiByteOpcodes;
private static void InitDecompiler()
{
singleByteOpcodes = new OpCode[0x100];
multiByteOpcodes = new OpCode[0x100];
FieldInfo[] infoArray1 = typeof(OpCodes).GetFields();
for (int num1 = 0; num1 < infoArray1.Length; num1++)
{
FieldInfo info1 = infoArray1[num1];
if (info1.FieldType == typeof(OpCode))
{
OpCode code1 = (OpCode)info1.GetValue(null);
ushort num2 = (ushort)code1.Value;
if (num2 < 0x100)
{
singleByteOpcodes[(int)num2] = code1;
}
else
{
if ((num2 & 0xff00) != 0xfe00)
{
throw new Exception("Invalid opcode: " + num2.ToString());
}
multiByteOpcodes[num2 & 0xff] = code1;
}
}
}
}
public static IEnumerable<ILInstruction> Decompile(MethodBase mi, byte[] ildata)
{
Module module = mi.Module;
ByteReader reader = new ByteReader(ildata);
while (!reader.Eof)
{
OpCode code = OpCodes.Nop;
int offset = reader.Position;
ushort b = reader.ReadByte();
if (b != 0xfe)
{
code = singleByteOpcodes[b];
}
else
{
b = reader.ReadByte();
code = multiByteOpcodes[b];
b |= (ushort)(0xfe00);
}
object operand = null;
switch (code.OperandType)
{
case OperandType.InlineBrTarget:
operand = reader.ReadInt32() + reader.Position;
break;
case OperandType.InlineField:
if (mi is ConstructorInfo)
{
operand = module.ResolveField(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), Type.EmptyTypes);
}
else
{
operand = module.ResolveField(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), mi.GetGenericArguments());
}
break;
case OperandType.InlineI:
operand = reader.ReadInt32();
break;
case OperandType.InlineI8:
operand = reader.ReadInt64();
break;
case OperandType.InlineMethod:
try
{
if (mi is ConstructorInfo)
{
operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), Type.EmptyTypes);
}
else
{
operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), mi.GetGenericArguments());
}
}
catch
{
operand = null;
}
break;
case OperandType.InlineNone:
break;
case OperandType.InlineR:
operand = reader.ReadDouble();
break;
case OperandType.InlineSig:
operand = module.ResolveSignature(reader.ReadInt32());
break;
case OperandType.InlineString:
operand = module.ResolveString(reader.ReadInt32());
break;
case OperandType.InlineSwitch:
int count = reader.ReadInt32();
int[] targetOffsets = new int[count];
for (int i = 0; i < count; ++i)
{
targetOffsets[i] = reader.ReadInt32();
}
int pos = reader.Position;
for (int i = 0; i < count; ++i)
{
targetOffsets[i] += pos;
}
operand = targetOffsets;
break;
case OperandType.InlineTok:
case OperandType.InlineType:
try
{
if (mi is ConstructorInfo)
{
operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), Type.EmptyTypes);
}
else
{
operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), mi.GetGenericArguments());
}
}
catch
{
operand = null;
}
break;
case OperandType.InlineVar:
operand = reader.ReadUInt16();
break;
case OperandType.ShortInlineBrTarget:
operand = reader.ReadSByte() + reader.Position;
break;
case OperandType.ShortInlineI:
operand = reader.ReadSByte();
break;
case OperandType.ShortInlineR:
operand = reader.ReadSingle();
break;
case OperandType.ShortInlineVar:
operand = reader.ReadByte();
break;
default:
throw new Exception("Unknown instruction operand; cannot continue. Operand type: " + code.OperandType);
}
yield return new ILInstruction(offset, code, operand);
}
}
}
From this point, the rest is a matter of finding all types that you use when calling constructors (Note: untested code, there's probably something wrong :-):
IEnumerable<Type> FindDtoTypes()
{
foreach (var item in GetType().Assembly.GetTypes())
{
foreach (var member in type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
{
var meth = member as MethodBase;
if (meth != null && meth.GetMethodBody() != null)
{
var code = meth.GetMethodBody().GetILAsByteArray();
foreach (var instr in Decompile(meth, code))
{
var oper = instr.Operand as MethodBase;
if (oper != null && oper.IsConstructor &&
oper.DeclaringType.IsGenericType)
{
var dtoType = mb.DeclaringType.GetGenericArguments().First();
if (dtoType.IsAssignableFrom(oper.DeclaringType))
yield return oper.DeclaringType;
}
}
}
}
}
}

String To Pathgeometry Converter for silverlight 5

I have extended a togglebutton control in Silverlight 5 and have created a Dependency property to Hold PathGeometry data. Expectedly the PathGeometry data is not being read. I'm not sure if its the converter or the setup. Here is all my code.
ExtentedRadTogglebutton.cs
public static readonly DependencyProperty ButtonIconPathDataProperty = DependencyProperty.Register(
"ButtonIconPathData",
typeof(PathGeometry),
typeof(ExtendedRadToggleButton),
new PropertyMetadata(new PathGeometry(), null));
Customised_RadToggleButtonControl.cs
if (!string.IsNullOrEmpty(settings.CusNav_L2_HomeButton_IconPathData))
{
StringToPathGeometryConverter cvSTGeo = new StringToPathGeometryConverter();
var iconPathData = cvSTGeo.Convert(settings.CusNav_L2_HomeButton_IconPathData,
null, null, null);
_radToggleButton.SetValue(ExtendedRadToggleButton.ButtonIconPathDataProperty, iconPathData);
}
XAML. resource
<Path x:Name="arrow"
Data="{TemplateBinding ButtonIconPathData}"/>
Converter
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MyClub.SilverlightClient.Converter
{
public class StringToPathGeometryConverter : IValueConverter
{
#region Const & Private Variables
const bool AllowSign = true;
const bool AllowComma = true;
const bool IsFilled = true;
const bool IsClosed = true;
IFormatProvider _formatProvider;
PathFigure _figure = null; // Figure object, which will accept parsed segments
string _pathString; // Input string to be parsed
int _pathLength;
int _curIndex; // Location to read next character from
bool _figureStarted; // StartFigure is effective
Point _lastStart; // Last figure starting point
Point _lastPoint; // Last point
Point _secondLastPoint; // The point before last point
char _token; // Non whitespace character returned by ReadToken
#endregion
#region Public Functionality
/// <summary>
/// Main conversion routine - converts string path data definition to PathGeometry object
/// </summary>
/// <param name="path">String with path data definition</param>
/// <returns>PathGeometry object created from string definition</returns>
public PathGeometry Convert(string path)
{
if (null == path)
throw new ArgumentException("Path string cannot be null!");
if (path.Length == 0)
throw new ArgumentException("Path string cannot be empty!");
return parse(path);
}
/// <summary>
/// Main back conversion routine - converts PathGeometry object to its string equivalent
/// </summary>
/// <param name="geometry">Path Geometry object</param>
/// <returns>String equivalent to PathGeometry contents</returns>
public string ConvertBack(PathGeometry geometry)
{
if (null == geometry)
throw new ArgumentException("Path Geometry cannot be null!");
return parseBack(geometry);
}
#endregion
#region Private Functionality
/// <summary>
/// Main parser routine, which loops over each char in received string, and performs actions according to command/parameter being passed
/// </summary>
/// <param name="path">String with path data definition</param>
/// <returns>PathGeometry object created from string definition</returns>
private PathGeometry parse(string path)
{
PathGeometry _pathGeometry = null;
_formatProvider = CultureInfo.InvariantCulture;
_pathString = path;
_pathLength = path.Length;
_curIndex = 0;
_secondLastPoint = new Point(0, 0);
_lastPoint = new Point(0, 0);
_lastStart = new Point(0, 0);
_figureStarted = false;
bool first = true;
char last_cmd = ' ';
while (ReadToken()) // Empty path is allowed in XAML
{
char cmd = _token;
if (first)
{
if ((cmd != 'M') && (cmd != 'm') && (cmd != 'f') && (cmd != 'F')) // Path starts with M|m
{
ThrowBadToken();
}
first = false;
}
switch (cmd)
{
case 'f':
case 'F':
_pathGeometry = new PathGeometry();
double _num = ReadNumber(!AllowComma);
_pathGeometry.FillRule = _num == 0 ? FillRule.EvenOdd : FillRule.Nonzero;
break;
case 'm':
case 'M':
// XAML allows multiple points after M/m
_lastPoint = ReadPoint(cmd, !AllowComma);
_figure = new PathFigure();
_figure.StartPoint = _lastPoint;
_figure.IsFilled = IsFilled;
_figure.IsClosed = !IsClosed;
//context.BeginFigure(_lastPoint, IsFilled, !IsClosed);
_figureStarted = true;
_lastStart = _lastPoint;
last_cmd = 'M';
while (IsNumber(AllowComma))
{
_lastPoint = ReadPoint(cmd, !AllowComma);
LineSegment _lineSegment = new LineSegment();
_lineSegment.Point = _lastPoint;
_figure.Segments.Add(_lineSegment);
//context.LineTo(_lastPoint, IsStroked, !IsSmoothJoin);
last_cmd = 'L';
}
break;
case 'l':
case 'L':
case 'h':
case 'H':
case 'v':
case 'V':
EnsureFigure();
do
{
switch (cmd)
{
case 'l': _lastPoint = ReadPoint(cmd, !AllowComma); break;
case 'L': _lastPoint = ReadPoint(cmd, !AllowComma); break;
case 'h': _lastPoint.X += ReadNumber(!AllowComma); break;
case 'H': _lastPoint.X = ReadNumber(!AllowComma); break;
case 'v': _lastPoint.Y += ReadNumber(!AllowComma); break;
case 'V': _lastPoint.Y = ReadNumber(!AllowComma); break;
}
LineSegment _lineSegment = new LineSegment();
_lineSegment.Point = _lastPoint;
_figure.Segments.Add(_lineSegment);
//context.LineTo(_lastPoint, IsStroked, !IsSmoothJoin);
}
while (IsNumber(AllowComma));
last_cmd = 'L';
break;
case 'c':
case 'C': // cubic Bezier
case 's':
case 'S': // smooth cublic Bezier
EnsureFigure();
do
{
Point p;
if ((cmd == 's') || (cmd == 'S'))
{
if (last_cmd == 'C')
{
p = Reflect();
}
else
{
p = _lastPoint;
}
_secondLastPoint = ReadPoint(cmd, !AllowComma);
}
else
{
p = ReadPoint(cmd, !AllowComma);
_secondLastPoint = ReadPoint(cmd, AllowComma);
}
_lastPoint = ReadPoint(cmd, AllowComma);
BezierSegment _bizierSegment = new BezierSegment();
_bizierSegment.Point1 = p;
_bizierSegment.Point2 = _secondLastPoint;
_bizierSegment.Point3 = _lastPoint;
_figure.Segments.Add(_bizierSegment);
//context.BezierTo(p, _secondLastPoint, _lastPoint, IsStroked, !IsSmoothJoin);
last_cmd = 'C';
}
while (IsNumber(AllowComma));
break;
case 'q':
case 'Q': // quadratic Bezier
case 't':
case 'T': // smooth quadratic Bezier
EnsureFigure();
do
{
if ((cmd == 't') || (cmd == 'T'))
{
if (last_cmd == 'Q')
{
_secondLastPoint = Reflect();
}
else
{
_secondLastPoint = _lastPoint;
}
_lastPoint = ReadPoint(cmd, !AllowComma);
}
else
{
_secondLastPoint = ReadPoint(cmd, !AllowComma);
_lastPoint = ReadPoint(cmd, AllowComma);
}
QuadraticBezierSegment _quadraticBezierSegment = new QuadraticBezierSegment();
_quadraticBezierSegment.Point1 = _secondLastPoint;
_quadraticBezierSegment.Point2 = _lastPoint;
_figure.Segments.Add(_quadraticBezierSegment);
//context.QuadraticBezierTo(_secondLastPoint, _lastPoint, IsStroked, !IsSmoothJoin);
last_cmd = 'Q';
}
while (IsNumber(AllowComma));
break;
case 'a':
case 'A':
EnsureFigure();
do
{
// A 3,4 5, 0, 0, 6,7
double w = ReadNumber(!AllowComma);
double h = ReadNumber(AllowComma);
double rotation = ReadNumber(AllowComma);
bool large = ReadBool();
bool sweep = ReadBool();
_lastPoint = ReadPoint(cmd, AllowComma);
ArcSegment _arcSegment = new ArcSegment();
_arcSegment.Point = _lastPoint;
_arcSegment.Size = new Size(w, h);
_arcSegment.RotationAngle = rotation;
_arcSegment.IsLargeArc = large;
_arcSegment.SweepDirection = sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;
_figure.Segments.Add(_arcSegment);
//context.ArcTo(
// _lastPoint,
// new Size(w, h),
// rotation,
// large,
// sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
// IsStroked,
// !IsSmoothJoin
// );
}
while (IsNumber(AllowComma));
last_cmd = 'A';
break;
case 'z':
case 'Z':
EnsureFigure();
_figure.IsClosed = IsClosed;
//context.SetClosedState(IsClosed);
_figureStarted = false;
last_cmd = 'Z';
_lastPoint = _lastStart; // Set reference point to be first point of current figure
break;
default:
ThrowBadToken();
break;
}
if (null != _figure)
{
if (_figure.IsClosed)
{
if (null == _pathGeometry)
_pathGeometry = new PathGeometry();
_pathGeometry.Figures.Add(_figure);
_figure = null;
first = true;
}
}
}
if (null != _figure)
{
if (null == _pathGeometry)
_pathGeometry = new PathGeometry();
if (!_pathGeometry.Figures.Contains(_figure))
_pathGeometry.Figures.Add(_figure);
}
return _pathGeometry;
}
void SkipDigits(bool signAllowed)
{
// Allow for a sign
if (signAllowed && More() && ((_pathString[_curIndex] == '-') || _pathString[_curIndex] == '+'))
{
_curIndex++;
}
while (More() && (_pathString[_curIndex] >= '0') && (_pathString[_curIndex] <= '9'))
{
_curIndex++;
}
}
bool ReadBool()
{
SkipWhiteSpace(AllowComma);
if (More())
{
_token = _pathString[_curIndex++];
if (_token == '0')
{
return false;
}
else if (_token == '1')
{
return true;
}
}
ThrowBadToken();
return false;
}
private Point Reflect()
{
return new Point(2 * _lastPoint.X - _secondLastPoint.X,
2 * _lastPoint.Y - _secondLastPoint.Y);
}
private void EnsureFigure()
{
if (!_figureStarted)
{
_figure = new PathFigure();
_figure.StartPoint = _lastStart;
//_context.BeginFigure(_lastStart, IsFilled, !IsClosed);
_figureStarted = true;
}
}
double ReadNumber(bool allowComma)
{
if (!IsNumber(allowComma))
{
ThrowBadToken();
}
bool simple = true;
int start = _curIndex;
//
// Allow for a sign
//
// There are numbers that cannot be preceded with a sign, for instance, -NaN, but it's
// fine to ignore that at this point, since the CLR parser will catch this later.
//
if (More() && ((_pathString[_curIndex] == '-') || _pathString[_curIndex] == '+'))
{
_curIndex++;
}
// Check for Infinity (or -Infinity).
if (More() && (_pathString[_curIndex] == 'I'))
{
//
// Don't bother reading the characters, as the CLR parser will
// do this for us later.
//
_curIndex = Math.Min(_curIndex + 8, _pathLength); // "Infinity" has 8 characters
simple = false;
}
// Check for NaN
else if (More() && (_pathString[_curIndex] == 'N'))
{
//
// Don't bother reading the characters, as the CLR parser will
// do this for us later.
//
_curIndex = Math.Min(_curIndex + 3, _pathLength); // "NaN" has 3 characters
simple = false;
}
else
{
SkipDigits(!AllowSign);
// Optional period, followed by more digits
if (More() && (_pathString[_curIndex] == '.'))
{
simple = false;
_curIndex++;
SkipDigits(!AllowSign);
}
// Exponent
if (More() && ((_pathString[_curIndex] == 'E') || (_pathString[_curIndex] == 'e')))
{
simple = false;
_curIndex++;
SkipDigits(AllowSign);
}
}
if (simple && (_curIndex <= (start + 8))) // 32-bit integer
{
int sign = 1;
if (_pathString[start] == '+')
{
start++;
}
else if (_pathString[start] == '-')
{
start++;
sign = -1;
}
int value = 0;
while (start < _curIndex)
{
value = value * 10 + (_pathString[start] - '0');
start++;
}
return value * sign;
}
else
{
string subString = _pathString.Substring(start, _curIndex - start);
try
{
return System.Convert.ToDouble(subString, _formatProvider);
}
catch (FormatException except)
{
throw new FormatException(string.Format("Unexpected character in path '{0}' at position {1}", _pathString, _curIndex - 1), except);
}
}
}
private bool IsNumber(bool allowComma)
{
bool commaMet = SkipWhiteSpace(allowComma);
if (More())
{
_token = _pathString[_curIndex];
// Valid start of a number
if ((_token == '.') || (_token == '-') || (_token == '+') || ((_token >= '0') && (_token <= '9'))
|| (_token == 'I') // Infinity
|| (_token == 'N')) // NaN
{
return true;
}
}
if (commaMet) // Only allowed between numbers
{
ThrowBadToken();
}
return false;
}
private Point ReadPoint(char cmd, bool allowcomma)
{
double x = ReadNumber(allowcomma);
double y = ReadNumber(AllowComma);
if (cmd >= 'a') // 'A' < 'a'. lower case for relative
{
x += _lastPoint.X;
y += _lastPoint.Y;
}
return new Point(x, y);
}
private bool ReadToken()
{
SkipWhiteSpace(!AllowComma);
// Check for end of string
if (More())
{
_token = _pathString[_curIndex++];
return true;
}
else
{
return false;
}
}
bool More()
{
return _curIndex < _pathLength;
}
// Skip white space, one comma if allowed
private bool SkipWhiteSpace(bool allowComma)
{
bool commaMet = false;
while (More())
{
char ch = _pathString[_curIndex];
switch (ch)
{
case ' ':
case '\n':
case '\r':
case '\t': // SVG whitespace
break;
case ',':
if (allowComma)
{
commaMet = true;
allowComma = false; // one comma only
}
else
{
ThrowBadToken();
}
break;
default:
// Avoid calling IsWhiteSpace for ch in (' ' .. 'z']
if (((ch > ' ') && (ch <= 'z')) || !Char.IsWhiteSpace(ch))
{
return commaMet;
}
break;
}
_curIndex++;
}
return commaMet;
}
private void ThrowBadToken()
{
throw new FormatException(string.Format("Unexpected character in path '{0}' at position {1}", _pathString, _curIndex - 1));
}
static internal char GetNumericListSeparator(IFormatProvider provider)
{
char numericSeparator = ',';
// Get the NumberFormatInfo out of the provider, if possible
// If the IFormatProvider doesn't not contain a NumberFormatInfo, then
// this method returns the current culture's NumberFormatInfo.
NumberFormatInfo numberFormat = NumberFormatInfo.GetInstance(provider);
// Is the decimal separator is the same as the list separator?
// If so, we use the ";".
if ((numberFormat.NumberDecimalSeparator.Length > 0) && (numericSeparator == numberFormat.NumberDecimalSeparator[0]))
{
numericSeparator = ';';
}
return numericSeparator;
}
private string parseBack(PathGeometry geometry)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
IFormatProvider provider = new System.Globalization.CultureInfo("en-us");
string format = null;
sb.Append("F" + (geometry.FillRule == FillRule.EvenOdd ? "0" : "1") + " ");
foreach (PathFigure figure in geometry.Figures)
{
sb.Append("M " + ((IFormattable)figure.StartPoint).ToString(format, provider) + " ");
foreach (PathSegment segment in figure.Segments)
{
char separator = GetNumericListSeparator(provider);
if (segment.GetType() == typeof(LineSegment))
{
LineSegment _lineSegment = segment as LineSegment;
sb.Append("L " + ((IFormattable)_lineSegment.Point).ToString(format, provider) + " ");
}
else if (segment.GetType() == typeof(BezierSegment))
{
BezierSegment _bezierSegment = segment as BezierSegment;
sb.Append(String.Format(provider,
"C{1:" + format + "}{0}{2:" + format + "}{0}{3:" + format + "} ",
separator,
_bezierSegment.Point1,
_bezierSegment.Point2,
_bezierSegment.Point3
));
}
else if (segment.GetType() == typeof(QuadraticBezierSegment))
{
QuadraticBezierSegment _quadraticBezierSegment = segment as QuadraticBezierSegment;
sb.Append(String.Format(provider,
"Q{1:" + format + "}{0}{2:" + format + "} ",
separator,
_quadraticBezierSegment.Point1,
_quadraticBezierSegment.Point2));
}
else if (segment.GetType() == typeof(ArcSegment))
{
ArcSegment _arcSegment = segment as ArcSegment;
sb.Append(String.Format(provider,
"A{1:" + format + "}{0}{2:" + format + "}{0}{3}{0}{4}{0}{5:" + format + "} ",
separator,
_arcSegment.Size,
_arcSegment.RotationAngle,
_arcSegment.IsLargeArc ? "1" : "0",
_arcSegment.SweepDirection == SweepDirection.Clockwise ? "1" : "0",
_arcSegment.Point));
}
}
if (figure.IsClosed)
sb.Append("Z");
}
return sb.ToString();
}
#endregion
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string path = value as string;
if (null != path)
return Convert(path);
else
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
PathGeometry geometry = value as PathGeometry;
if (null != geometry)
return ConvertBack(geometry);
else
return default(string);
}
#endregion
}
}

What C# template engine that has clean separation between HTML and control code?

What C# template engine
that uses 'pure' HTML having only text and markers
sans any control flow like if, while, loop or expressions,
separating html from control code ?
Below is the example phone book list code,
expressing how this should be done:
string html=#"
<html><head><title>#title</title></head>
<body>
<table>
<tr>
<td> id</td> <td> name</td> <td> sex</td> <td>phones</td>
</tr><!--#contacts:-->
<tr>
<td>#id</td> <td>#name</td> <td>#sex</td>
<td>
<!--#phones:-->#phone <br/>
<!--:#phones-->
</td>
</tr><!--:#contacts-->
</table>
</body>
</html>";
var contacts = from c in db.contacts select c;
Marker m = new Marker(html);
Filler t = m.Mark("title");
t.Set("Phone book");
Filler c = m.Mark("contacts", "id,name,sex");
// **foreach** expressed in code, not in html
foreach(var contact in contacts) {
int id = contact.id;
c.Add(id, contact.name, contact.sex);
Filler p = c.Mark("phones", "phone");
var phones = from ph in db.phones
where ph.id == id
select new {ph.phone};
if (phones.Any()) {
foreach(var ph in phones) {
p.Add(ph);
}
} else {
fp.Clear();
}
}
Console.Out.WriteLine(m.Get());
Use this code:
Templet.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace templaten.com.Templaten
{
public class tRange
{
public int head, toe;
public tRange(int _head, int _toe)
{
head = _head;
toe = _toe;
}
}
public enum AType
{
VALUE = 0,
NAME = 1,
OPEN = 2,
CLOSE = 3,
GROUP = 4
}
public class Atom
{
private AType kin;
private string tag;
private object data;
private List<Atom> bag;
public Atom(string _tag = "",
AType _kin = AType.VALUE,
object _data = null)
{
tag = _tag;
if (String.IsNullOrEmpty(_tag))
_kin = AType.GROUP;
kin = _kin;
if (_kin == AType.GROUP)
bag = new List<Atom>();
else
bag = null;
data = _data;
}
public AType Kin
{
get { return kin; }
}
public string Tag
{
get { return tag; }
set { tag = value; }
}
public List<Atom> Bag
{
get { return bag; }
}
public object Data
{
get { return data; }
set { data = value; }
}
public int Add(string _tag = "",
AType _kin = AType.VALUE,
object _data = null)
{
if (bag != null)
{
bag.Add(new Atom(_tag, _kin, _data));
return bag.Count - 1;
}
else
{
return -1;
}
}
}
public class Templet
{
private string content;
string namepat = "\\w+";
string justName = "(\\w+)";
string namePre = "#";
string namePost = "";
string comment0 = "\\<!--\\s*";
string comment1 = "\\s*--\\>";
private Atom tokens; // parsed contents
private Dictionary<string, int> iNames; // name index
private Dictionary<string, tRange> iGroups; // groups index
private Atom buffer; // output buffer
private Dictionary<string, int> _iname; // output name index
private Dictionary<string, tRange> _igroup; // output index
public Templet(string Content = null)
{
Init(Content);
}
private int[] mark(string[] names, string group)
{
if (names == null || names.Length < 1) return null;
tRange t = new tRange(0, buffer.Bag.Count - 1);
if (group != null)
{
if (!_igroup.ContainsKey(group)) return null;
t = _igroup[group];
}
int[] marks = new int[names.Length];
for (int i = 0; i < marks.Length; i++)
marks[i] = -1;
for (int i = t.head; i <= t.toe; i++)
{
if (buffer.Bag[i].Kin == AType.NAME)
{
for (int j = 0; j < names.Length; j++)
{
if (String.Compare(
names[j],
buffer.Bag[i].Tag,
true) == 0)
{
marks[j] = i;
break;
}
}
}
}
return marks;
}
public Filler Mark(string group, string names)
{
Filler f = new Filler(this, names);
f.di = mark(f.names, group);
f.Group = group;
tRange t = null;
if (_igroup.ContainsKey(group)) t = _igroup[group];
f.Range = t;
return f;
}
public Filler Mark(string names)
{
Filler f = new Filler(this, names);
f.di = mark(f.names, null);
f.Group = "";
f.Range = null;
return f;
}
public void Set(int[] locations, object[] x)
{
int j = Math.Min(x.Length, locations.Length);
for (int i = 0; i < j; i++)
{
int l = locations[i];
if ((l >= 0) && (buffer.Bag[l] != null))
buffer.Bag[l].Data = x[i];
}
}
public void New(string group, int seq = 0)
{
// place new group copied from old group just below it
if (!( iGroups.ContainsKey(group)
&& _igroup.ContainsKey(group)
&& seq > 0)) return;
tRange newT = null;
tRange t = iGroups[group];
int beginRange = _igroup[group].toe + 1;
for (int i = t.head; i <= t.toe; i++)
{
buffer.Bag.Insert(beginRange,
new Atom(tokens.Bag[i].Tag,
tokens.Bag[i].Kin,
tokens.Bag[i].Data));
beginRange++;
}
newT = new tRange(t.toe + 1, t.toe + (t.toe - t.head + 1));
// rename past group
string pastGroup = group + "_" + seq;
t = _igroup[group];
buffer.Bag[t.head].Tag = pastGroup;
buffer.Bag[t.toe].Tag = pastGroup;
_igroup[pastGroup] = t;
// change group indexes
_igroup[group] = newT;
}
public void ReMark(Filler f, string group)
{
if (!_igroup.ContainsKey(group)) return;
Map(buffer, _iname, _igroup);
f.di = mark(f.names, group);
f.Range = _igroup[group];
}
private static void Indexing(string aname,
AType kin,
int i,
Dictionary<string, int> dd,
Dictionary<string, tRange> gg)
{
switch (kin)
{
case AType.NAME: // index all names
dd[aname] = i;
break;
case AType.OPEN: // index all groups
if (!gg.ContainsKey(aname))
gg[aname] = new tRange(i, -1);
else
gg[aname].head = i;
break;
case AType.CLOSE:
if (!gg.ContainsKey(aname))
gg[aname] = new tRange(-1, i);
else
gg[aname].toe = i;
break;
default:
break;
}
}
private static void Map(Atom oo,
Dictionary<string, int> dd,
Dictionary<string, tRange> gg)
{
for (int i = 0; i < oo.Bag.Count; i++)
{
string aname = oo.Bag[i].Tag;
Indexing(oo.Bag[i].Tag, oo.Bag[i].Kin, i, dd, gg);
}
}
public void Init(string Content = null)
{
content = Content;
tokens = new Atom("", AType.GROUP);
iNames = new Dictionary<string, int>();
iGroups = new Dictionary<string, tRange>();
// parse content into tokens
string namePattern = namePre + namepat + namePost;
string patterns =
"(?<var>" + namePattern + ")|" +
"(?<head>" + comment0 + namePattern + ":" + comment1 + ")|" +
"(?<toe>" + comment0 + ":" + namePattern + comment1 + ")";
Regex jn = new Regex(justName, RegexOptions.Compiled);
Regex r = new Regex(patterns, RegexOptions.Compiled);
MatchCollection ms = r.Matches(content);
int pre = 0;
foreach (Match m in ms)
{
tokens.Add(content.Substring(pre, m.Index - pre));
int idx = -1;
if (m.Groups.Count >= 3)
{
string aname = "";
MatchCollection x = jn.Matches(m.Value);
if (x.Count > 0 && x[0].Groups.Count > 1)
aname = x[0].Groups[1].ToString();
AType t = AType.VALUE;
if (m.Groups[1].Length > 0) t = AType.NAME;
if (m.Groups[2].Length > 0) t = AType.OPEN;
if (m.Groups[3].Length > 0) t = AType.CLOSE;
if (aname.Length > 0)
{
tokens.Add(aname, t);
idx = tokens.Bag.Count - 1;
}
Indexing(aname, t, idx, iNames, iGroups);
}
pre = m.Index + m.Length;
}
if (pre < content.Length)
tokens.Add(content.Substring(pre, content.Length - pre));
// copy tokens into buffer
buffer = new Atom("", AType.GROUP);
for (int i = 0; i < tokens.Bag.Count; i++)
buffer.Add(tokens.Bag[i].Tag, tokens.Bag[i].Kin);
// initialize index of output names
_iname = new Dictionary<string, int>();
foreach (string k in iNames.Keys)
_iname[k] = iNames[k];
// initialize index of output groups
_igroup = new Dictionary<string, tRange>();
foreach (string k in iGroups.Keys)
{
tRange t = iGroups[k];
_igroup[k] = new tRange(t.head, t.toe);
}
}
public string Get()
{
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < buffer.Bag.Count; i++)
{
switch (buffer.Bag[i].Kin)
{
case AType.VALUE:
sb.Append(buffer.Bag[i].Tag);
break;
case AType.NAME:
sb.Append(buffer.Bag[i].Data);
break;
case AType.OPEN:
case AType.CLOSE:
break;
default: break;
}
}
return sb.ToString();
}
}
public class Filler
{
private Templet t = null;
public int[] di;
public string[] names;
public string Group { get; set; }
public tRange Range { get; set; }
private int seq = 0;
public Filler(Templet tl, string markers = null)
{
t = tl;
if (markers != null)
names = markers.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries);
else
names = null;
}
public void init(int length)
{
di = new int[length];
for (int i = 0; i < length; i++)
di[i] = -1;
seq = 0;
Group = "";
Range = null;
}
// clear contents inside marked object or group
public void Clear()
{
object[] x = new object[di.Length];
for (int i = 0; i < di.Length; i++)
x[i] = null;
t.Set(di, x);
}
// set value for marked object,
// or add row to group and set value to columns
public void Set(params object[] x)
{
t.Set(di, x);
}
public void Add(params object[] x)
{
if (Group.Length > 0)
{
t.New(Group, seq);
++seq;
t.ReMark(this, Group);
}
t.Set(di, x);
}
}
}
Testing program
Program.cs
Templet m = new Templet(html);
Filler f= m.Mark("title");
f.Set("Phone book");
Filler fcontacts = m.Mark("contacts", "id,name,sex,phone");
fcontacts.Add(1, "Akhmad", "M", "123456");
fcontacts.Add(2, "Barry", "M", "234567");
fcontacts.Add(1, "Charles", "M", "345678");
Console.Out.WriteLine(m.Get());
Still can't do nested loop- yet.
Just use ASP.NET. Whether you use webforms or MVC, it's super easy to have C# in your .cs files, and HTML in your .aspx files.
As with anything in programming, it's 99% up to you to do things right. Flexible UI engines aren't going to enforce that you follow good coding practices.
In principle most any template engine you choose can separate HTML from control logic with the proper architecture. using an MVC (Or MVVM) pattern, if you construct your model in such a way that the controller contains the if/then logic instead of the view you can eliminate it from the view.
That said, the syntax you use is very close to Razor syntax which is easily available for ASP.NET MVC through NuGet packages.
I totally hear you. I built SharpFusion, which has some other stuff in it but if you look for the template.cs file you will see the handler that parses a HTML file and simply replaces out tokens with values that you've made in c#.
Because no XML parsing is done like ASP.NET the framework loads much faster than even an MVC site.
Another alternative is ServiceStack.

Categories

Resources