Funny regular expression experience


I was working on a regex to check if all the symbols in the string are from the range of allowed ones.

My initial regex, which  was working was:

 C |   copy code | ?  
1
^[a-zA-Z0-9_-]*$

Later I decided to allow spaces and dots and wrote a new regex:

 C |   copy code | ?  
1
^[a-zA-Z0-9_-\. ]*$

This one was not working! I’ve spent quite a time until I figured out that the peace “_-\.” was treated the same way as “a-z”. It assumed I am providing a scope. My issue was fixed after I escaped “-” and my final regex looked like this:

 C |   copy code | ?  
1
^[a-zA-Z0-9_\-\. ]*$

c# .net code snippet to cache xslt objects


XslCompiledTransform class is used to perform xslt transformation. Construction of the instance of the object of this type takes some time as it has to load, parse and compile xslt file and all the includes. You have to think about caching of the XslCompiledTransform object instance if your application uses xslt transformation based on the same xslt file more than once.

 C |   copy code | ?  
01
var xslFilePath = "path\\to\\your\\xsl\\file";
02
ObjectCache cache = MemoryCache.Default; // I used MemoryCache - there are more possibilities
03
//Check if there is something in the cache. Cache key is xslt file path
04
var xsl = cache[xslFilePath] as XslCompiledTransform;
05
if (xsl == null)
06
{
07
    // If nothing construct the XslCompiledTransform objects instance and cache it
08
    xsl = new XslCompiledTransform();
09
    var xsltSettings = new XsltSettings(false, true);
10
    xsl.Load(xslFilePath, xsltSettings, new XmlUrlResolver());
11
    var policy = new CacheItemPolicy();
12
    List paths = null;
13
    // retrieve all the dependent file paths
14
    ConstructCacheItemPolicy(xslFilePath,ref paths);
15
    policy.ChangeMonitors.Add(new HostFileChangeMonitor(paths));
16
    cache.Set(xslFilePath,xsl,policy);
17
}
18
var ms = new MemoryStream();
19
xsl.Transform(xInput, null, ms);
20
ms.Position = 0;
21
var result = ms;

A function to collect all the dependent xslt includes for the cache dependency

 C |   copy code | ?  
01
private static void ConstructCacheItemPolicy(string xslFilePath, ref List<string> paths )</string>
02
{
03
    // Add current xslt file path into our result set
04
    if (paths == null) paths = new List<string>();
05
    if (!paths.Contains(xslFilePath)) paths.Add(xslFilePath);
06
    var xmlDocument=new XmlDocument();
07
    // load xslt file into XmlDocument
08
    xmlDocument.Load(xslFilePath);
09
    // Take care of xml namespaces
10
    var xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
11
    xmlNamespaceManager.AddNamespace("xsl","http://www.w3.org/1999/XSL/Transform");
12
    // Select includes
13
    var xNL = xmlDocument.SelectNodes("//xsl:include",xmlNamespaceManager);
14
    if (xNL != null)
15
    // iterated through the includes and perform recursive iterations to collect everything what is in scope
16
    foreach (XmlElement xE in xNL)
17
    {
18
        var pathRoot = Path.GetDirectoryName(xslFilePath);
19
        if (pathRoot != null)
20
        {
21
            var xslIncludeFilePath = Path.Combine(pathRoot, xE.GetAttribute("href"));
22
             ConstructCacheItemPolicy(xslIncludeFilePath, ref paths);
23
        }
24
    }
25
}

java, *.jar to c# .NET


 

Use tools from ikvm.net to generate .NET libraries out of java code.