开发者

.net dynamic text parser

开发者 https://www.devze.com 2023-03-03 02:09 出处:网络
thanks for reading. I am trying to write a little parser. What I am trying to do is the following. The database contains 3 tables. One with Persons (Name, LastName, Age), one with TextTemplates (\"Thi

thanks for reading. I am trying to write a little parser. What I am trying to do is the following. The database contains 3 tables. One with Persons (Name, LastName, Age), one with TextTemplates ("This document belongs to .") and one with TemplateElements (for example , ...). So the user is able to choose a TextTemplate, edit it and add more T开发者_开发知识库emplateElements. As he presses a button the system should generate PDF documents by replacing the TemplateElements with the corresponding properties of the persons out of the Persons table. The problem is to get the persons property that matches the TemplateElement. Of course I could write some:

foreach(element...){    
    if(element.Equals("<Name>"))  
      text.Replace("<Name>", Person.Name);  
    if(element.Equals("<LastName>"))  
      text.Replace("<LastName>", Person.LastName);  
}  

but I want to keep this as dynamic as possible. Properties and TemplateElements could change in the future. So the best solution would be to somehow get the corresponding property according to the actual element.

Would be very nice if one of you guys has a solution for this.

Thanks ;)


Have a look at these blog posts where some implementations of 'named formatters' are discussed:

  • http://haacked.com/archive/2009/01/04/fun-with-named-formats-string-parsing-and-edge-cases.aspx
  • http://haacked.com/archive/2009/01/14/named-formats-redux.aspx

Essentially, the idea is that you define an extension method on string that allows you to format a string based on a syntax like {PropertyName}

Example:

Person person = GetPerson();
string text = "Hello, {Name} {LastName}";
var evaluated = text.FormatWith(person);


So here is my result that does exactly what I need it to do ;)

    private string ReplaceTemplateElements(Person person, string inputText)  
    {
        //RegExp to get everything like <Name>, <LastName>...
        Regex regExp = new Regex(@"\<(\w*?)\>", RegexOptions.Compiled);
        //Saves properties and values of the person object
        Dictionary<string, string> templateElements = new Dictionary<string, string>();
        FieldInfo[] fieldInfo;
        Type type = typeof(Person);

        fieldInfo = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
        //Putting every property and value in the dictionary
        foreach (FieldInfo info in fieldInfo)
        {
            templateElements.Add(info.Name.TrimStart('_'), info.GetValue(person).ToString());
        }

        //Replacing the matching templateElements with the persons properties
        string result = regExp.Replace(inputText, delegate(Match match)
        {
            string key = match.Groups[1].Value;
            return templateElements[key];
        });
        return result;
    }

So with this I do not have to care about the properties of a Person. Adding or deleting a property will not effect the functionality of this method. It just looks for existing templateElements in the inputText and replaces them with the matching property of the Persons-Object (if there is a matching one ;) ). If there are any suggestions please tell me.

0

精彩评论

暂无评论...
验证码 换一张
取 消