Skip to content

Commit

Permalink
Fix regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Liu, Shabai authored and Liu, Shabai committed Jul 28, 2014
1 parent 7f6be0a commit a8cf745
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Source/Cvent.SchemaToPoco.Core/JsonSchemaToCodeUnit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.CodeDom;
using System.Text.RegularExpressions;
using Cvent.SchemaToPoco.Core.Types;
using Cvent.SchemaToPoco.Core.Util;
using Microsoft.CSharp;
Expand Down Expand Up @@ -88,6 +89,12 @@ public CodeCompileUnit Execute()
{
JsonSchema schema = i.Value;

// Sanitize inputs
if (!String.IsNullOrEmpty(schema.Description))
{
schema.Description = Regex.Unescape(schema.Description);
}

// If it is an enum
if (schema.Enum != null)
{
Expand Down
5 changes: 4 additions & 1 deletion Source/Cvent.SchemaToPoco.Core/Types/PropertyWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public PropertyWrapper(CodeMemberProperty p) : base(p)
/// <param name="schema">The JsonSchema.</param>
public void Populate(JsonSchema schema)
{
// Add description
if (schema.Description != null)
{
AddComment(schema.Description);
}

// Add required attribute
if (schema.Required != null && schema.Required.Value)
{
AddAttribute("Required");
Expand Down Expand Up @@ -85,7 +87,8 @@ public void Populate(JsonSchema schema)
if (!String.IsNullOrEmpty(schema.Pattern))
{
AddAttribute("RegularExpression",
new CodeAttributeArgument(new CodeSnippetExpression(string.Format("@\"{0}\"", schema.Pattern))));
new CodeAttributeArgument(new CodeSnippetExpression(string.Format(@"@""{0}""",
StringUtils.SanitizeRegex(schema.Pattern, true)))));
}
}

Expand Down
49 changes: 47 additions & 2 deletions Source/Cvent.SchemaToPoco.Core/Util/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Media;
using System.Text.RegularExpressions;

namespace Cvent.SchemaToPoco.Core.Util
{
Expand All @@ -8,6 +12,16 @@ namespace Cvent.SchemaToPoco.Core.Util
/// </summary>
public static class StringUtils
{
private static char[] _escapeChars = new[]
{
'w',
'W',
'd',
'D',
's',
'S'
};

/// <summary>
/// Capitalize the first letter in a string.
/// </summary>
Expand Down Expand Up @@ -63,10 +77,9 @@ public static string SanitizeIdentifier(string s)
/// <param name="s">The regex.</param>
/// <param name="literal">Whether or not to sanitize for use as a string literal.</param>
/// <returns>A sanitized regular expression</returns>
/// TODO
public static string SanitizeRegex(string s, bool literal)
{
return s;
return literal ? ToLiteral(s, true).Replace("\"", "\"\"") : Regex.Escape(s);
}

/// <summary>
Expand All @@ -85,5 +98,37 @@ public static string LoadUri(Uri uri)
}
return null;
}

/// <summary>
/// Convert a string to a literal string.
/// </summary>
/// <param name="input">The string.</param>
/// <param name="preserveEscapes">Whether or not to preserve regex escape sequences.</param>
/// <returns>An escaped string.</returns>
public static string ToLiteral(string input, bool preserveEscapes)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
string s = writer.ToString();

// Remove quotes from beginning and end
s = s.TrimStart(new[] { '"' }).TrimEnd(new[] { '"' });

// Preserve escape sequences
if (preserveEscapes)
{
foreach (char c in _escapeChars)
{
s = s.Replace(@"\\" + c, @"\" + c);
}
}

return s;
}
}
}
}
}
1 change: 1 addition & 0 deletions Source/Test/DataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class DefaultClassName

[Required]
[MinLength(2)]
[RegularExpression(@"^\\\""dev\""\'[a-c]\t$")]
public virtual string Foo
{
get { return _foo; }
Expand Down
17 changes: 8 additions & 9 deletions Source/Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using Newtonsoft.Json.Schema;
using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Schema;

namespace Test
{
internal class Program
{
private static void Main(string[] args)
{
string schemaJson = @"{
'$id' : 'http://jsonschema.net/examples/',
'properties': {
'A': {'$ref':'#B.json'}
}
}";

JsonSchema parsed = JsonSchema.Parse(schemaJson);
Regex r = new Regex(@"^\\\""dev\""\'[a-c]\s$");
string text = @"\""dev""'a ";
MatchCollection col = r.Matches(text);
Console.WriteLine(text);
Console.WriteLine("{0} matches found", col.Count);
}
}
}

0 comments on commit a8cf745

Please sign in to comment.