Ejemplo n.º 1
0
        public override string MakeConstant(CsCodeWriter code, string value)
        {
            if (!String.IsNullOrEmpty(value))
                throw new NotSupportedException("Complex types can not have default values.");

            return String.Format("{0}.DefaultInstance", CsCodeWriter.Global + _type.ImplementationName);
        }
        public override void DeclareStaticData(CsCodeWriter code)
        {
            if (code.Language != _rule.Language)
                return;

            Match m;
            if (!String.IsNullOrEmpty(_rule.MethodBody) &&
                (m = new Regex(@"^(?<Name>[a-zA-Z][\w_]*)\(value\)$").Match(_rule.Code)).Success)
            {
                using (code.WriteBlock("private static bool {0}({1} value)", m.Groups["Name"].Value, _field.GetStorageType(code)))
                using (StringReader rdr = new StringReader(_rule.MethodBody))
                {
                    string line;
                    while(null != (line = rdr.ReadLine()))
                    {
                        line = line.Trim();
                        if (line.Length == 0)
                            continue;
                        if (line.StartsWith("}"))
                            code.Indent--;
                        code.WriteLine(line);
                        if (line.EndsWith("{"))
                            code.Indent--;
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public override string FromXmlString(CsCodeWriter code, string name)
 {
     return String.Format("new {0}({1})",
         GetPublicType(code),
         _generator.ValueField.FromXmlString(code, name)
         );
 }
 public override void WriteChecks(CsCodeWriter code)
 {
     if (_rule.MinValue != null)
     {
         using (code.WriteBlock("if (value.CompareTo({0}) < 0)", code.MakeConstant(_field.FieldType, _rule.MinValue)))
         {
             code.WriteLine(
                 "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{2}, " +
                 "{0}NClassify.Library.ResourceMessages.MustBeGreaterThan, TypeFields.{2}, {1}));",
                 CsCodeWriter.Global, code.MakeConstant(_field.FieldType, _rule.MinValue), _field.PropertyName);
             code.WriteLine("return false;");
         }
     }
     if (_rule.MaxValue != null)
     {
         using (code.WriteBlock("if (value.CompareTo({0}) < 0)", code.MakeConstant(_field.FieldType, _rule.MaxValue)))
         {
             code.WriteLine(
                 "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{2}, " +
                 "{0}NClassify.Library.ResourceMessages.MustBeLessThan, TypeFields.{2}, {1}));",
                 CsCodeWriter.Global, code.MakeConstant(_field.FieldType, _rule.MaxValue), _field.PropertyName);
             code.WriteLine("return false;");
         }
     }
 }
Ejemplo n.º 5
0
 public override void MakeReadOnly(CsCodeWriter code, string value)
 {
     if (_type.ImplementationName != _type.QualifiedName)
     {
         code.WriteLine("{0} = ({0} as {1}{2}) ?? new {1}{2}({0});", value, CsCodeWriter.Global, _type.ImplementationName);
         code.WriteLine("(({1}{2}){0}).MakeReadOnly();", value, CsCodeWriter.Global, _type.ImplementationName);
     }
     else
         code.WriteLine("{0}.MakeReadOnly();", value);
 }
 public override void WriteChecks(CsCodeWriter code)
 {
     using (code.WriteBlock("if (object.ReferenceEquals(null, value))"))
     {
         code.WriteLine(
             "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{1}, " +
             "{0}NClassify.Library.ResourceMessages.MustNotBeNull, TypeFields.{1}));",
             CsCodeWriter.Global, _field.PropertyName);
         code.WriteLine("return false;");
     }
 }
 public override void WriteChecks(CsCodeWriter code)
 {
     using (code.WriteBlock("if (!__valid_{0}.IsMatch(value.ToString()))", _field.CamelName))
     {
         code.WriteLine(
             "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{2}, " +
             "{0}NClassify.Library.ResourceMessages.MustMatchFormat, TypeFields.{2}, {1}));",
             CsCodeWriter.Global, code.MakeConstant(FieldType.String, _rule.Pattern), _field.PropertyName);
         code.WriteLine("return false;");
     }
 }
 public override void WriteChecks(CsCodeWriter code)
 {
     if (code.Language != _rule.Language)
         return;
     
     using (code.WriteBlock("if (!({0}))", _rule.Code))
     {
         code.WriteLine("if (onError != null) " +
             "onError(new {0}NClassify.Library.ValidationError(TypeFields.{1}, {0}NClassify.Library.ResourceMessages.InvalidField, TypeFields.{1}));",
             CsCodeWriter.Global, _field.PropertyName);
         code.WriteLine("return false;");
     }
 }
        public override void WriteChecks(CsCodeWriter code)
        {
            if (_field.FieldType != FieldType.String)
                throw new ApplicationException("The possible values constraints only applies to fields of type String.");

            using (code.WriteBlock("if (global::System.Array.BinarySearch(__in_{0}, value) < 0)", _field.CamelName))
            {
                code.WriteLine(
                    "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{2}, " +
                    "{0}NClassify.Library.ResourceMessages.MustBeOneOf, TypeFields.{2}, string.Join(\", \", __in_{1})));",
                    CsCodeWriter.Global, _field.CamelName, _field.PropertyName);
                code.WriteLine("return false;");
            }
        }
        public override void DeclareStaticData(CsCodeWriter code)
        {
            code.Write("private static readonly {0}[] __in_{1} = new {0}[] {{", _field.GetStorageType(code), _field.CamelName);
            int ix = 0;
            string[] list = (string[]) _rule.Values.Clone();
            Array.Sort(list);

            foreach (string possible in _rule.Values)
            {
                if (ix++ > 0) code.Write(", ");
                code.Write(code.MakeConstant(_field.FieldType, possible));
            }
            code.WriteLine("};");
        }
 public override void DeclareStaticData(CsCodeWriter code)
 {
     code.WriteLine("private static readonly " +
         CsCodeWriter.Global + "System.Text.RegularExpressions.Regex __valid_{0} = " + "new " +
         CsCodeWriter.Global + "System.Text.RegularExpressions.Regex({1}, {2});",
         _field.CamelName, code.MakeConstant(FieldType.String, _rule.Pattern), 
             (_rule.Multiline 
             ? CsCodeWriter.Global + "System.Text.RegularExpressions.RegexOptions.Multiline"
             : CsCodeWriter.Global + "System.Text.RegularExpressions.RegexOptions.Singleline")
             +
             (_rule.IgnoreCase 
             ? " | " + CsCodeWriter.Global + "System.Text.RegularExpressions.RegexOptions.IgnoreCase"
             : "")
         );
 }
 public override void WriteChecks(CsCodeWriter code)
 {
     using (code.WriteBlock("if (!value.IsValid())"))
     {
         code.WriteLine("if (onError == null)");
         code.WriteLineIndent("return false;");
         code.WriteLine(
             "{0}System.Collections.Generic.List<{0}NClassify.Library.ValidationError> errors = " +
             "new {0}System.Collections.Generic.List<{0}NClassify.Library.ValidationError>();",
             CsCodeWriter.Global
             );
         code.WriteLine("value.GetBrokenRules(errors.Add);");
         code.WriteLine("onError(new {0}NClassify.Library.ValidationError(TypeFields.{1}, errors));",
             CsCodeWriter.Global, _field.PropertyName);
         code.WriteLine("return false;");
     }
 }
        public override void WriteChecks(CsCodeWriter code)
        {
            string accessor = ".Length";
            switch (_field.FieldType)
            {
                case FieldType.Bytes:
                case FieldType.String:
                    break;
                    //case FieldType.Uri:
                    //    accessor = ".AbsoluteUri.Length";
                    //    break;
                default:
                    throw new ArgumentOutOfRangeException("Length constraint does not apply to fields of type " +
                                                          _field.FieldType);
            }

            if (_rule.MinLength > 0)
            {
                using (code.WriteBlock("if (value{0} < {1})", accessor, _rule.MinLength))
                {
                    code.WriteLine(
                        "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{2}, " +
                        "{0}NClassify.Library.ResourceMessages.MustBeLongerThan, TypeFields.{2}, {1}));",
                        CsCodeWriter.Global, _rule.MinLength, _field.PropertyName);
                    code.WriteLine("return false;");
                }
            }
            if (_rule.MaxLength < uint.MaxValue)
            {
                using (code.WriteBlock("if (value{0} > {1})", accessor, _rule.MaxLength))
                {
                    code.WriteLine(
                        "if (onError != null) onError(new {0}NClassify.Library.ValidationError(TypeFields.{2}, " +
                        "{0}NClassify.Library.ResourceMessages.MustBeShorterThan, TypeFields.{2}, {1}));",
                        CsCodeWriter.Global, _rule.MaxLength, _field.PropertyName);
                    code.WriteLine("return false;");
                }
            }
        }
Ejemplo n.º 14
0
        public override string MakeConstant(CsCodeWriter code, string value)
        {
            if (String.IsNullOrEmpty(value))
                return String.Format("default({0})", GetPublicType(code));

            uint tmp;
            if(uint.TryParse(value, out tmp))
            {
                string name = _enum.Values.Where(e => e.Value == tmp).Select(e => e.Name).FirstOrDefault();
                if (!String.IsNullOrEmpty(name))
                    value = name;
            }

            value = CodeWriter.ToPascalCase(value);
            EnumType.Item item = _enum.Values.FirstOrDefault(e => CodeWriter.ToPascalCase(e.Name) == value);

            if (item == null)
                throw new ApplicationException(
                    String.Format("Unknown default value '{0}' for enumeration {1}", value,
                                  _enum.QualifiedName));

            return String.Format("{0}.{1}", GetPublicType(code), CodeWriter.ToPascalCase(item.Name));
        }
Ejemplo n.º 15
0
 public override string FromXmlString(CsCodeWriter code, string name)
 {
     return String.Format("({1}){0}System.Enum.Parse(typeof({1}), {2}, false)", CsCodeWriter.Global, GetStorageType(code), name);
 }
Ejemplo n.º 16
0
 public override string ToXmlString(CsCodeWriter code, string name)
 {
     if (XmlOptions.Format != null)
         return base.ToXmlString(code, name);
     return String.Format("{0}.ToString()", name);
 }
Ejemplo n.º 17
0
 public override void WriteClone(CsCodeWriter code)
 {
     code.WriteLine("value.{0} = ({1})value.{0}.Clone();", FieldBackingName, GetStorageType(code));
 }
Ejemplo n.º 18
0
 public override void WriteCopy(CsCodeWriter code, string other)
 {
     if(_generator.IsMessage)
     {
         code.WriteLine("foreach ({0} item in {1}.{2})", _generator.GetPublicType(code), other, PropertyName);
         code.WriteLineIndent("{0}.Add(({1})item.Clone());", FieldBackingName, _generator.GetStorageType(code));
     }
     else
         code.WriteLine("{0}.AddRange({1}.{2});", FieldBackingName, other, PropertyName);
 }
Ejemplo n.º 19
0
 public override void WriteClone(CsCodeWriter code)
 {
     code.WriteLine("value.{0} = value.{0}.Clone();", FieldBackingName);
 }
Ejemplo n.º 20
0
 public override void WriteXmlOutput(CsCodeWriter code, string name)
 {
     code.WriteLine("{0}.WriteXml(\"{1}\", writer);", name, XmlOptions.XmlName);
 }
Ejemplo n.º 21
0
 public override void MakeReadOnly(CsCodeWriter code, string value)
 {
     code.WriteLine("{0}.MakeReadOnly();", value);
 }
Ejemplo n.º 22
0
 public override void WriteXmlOutput(CsCodeWriter code, string name)
 {
     using (code.WriteBlock("foreach ({0} item in {1})", _generator.GetPublicType(code), name))
         _generator.WriteXmlOutput(code, "item");
 }
Ejemplo n.º 23
0
        public override void ReadXmlMessage(CsCodeWriter code)
        {
            if (_type.ImplementationName != _type.QualifiedName)
                code.WriteLine("{0}{1} value = ({2} as {0}{1}) ?? new {0}{1}({2});",
                    CsCodeWriter.Global, _type.ImplementationName, FieldBackingName);
            else
                code.WriteLine("{0}{1} value = {2};", 
                    CsCodeWriter.Global, _type.ImplementationName, FieldBackingName);

            code.WriteLine("if (value.IsReadOnly())");
            code.WriteLineIndent("value = object.ReferenceEquals(value, {0}{1}.DefaultInstance) ? new {0}{1}() : new {0}{1}({2});",
                CsCodeWriter.Global, _type.ImplementationName, FieldBackingName);

            code.WriteLine("value.ReadXml(reader.LocalName, reader);");
            code.WriteLine("{0} = value;", FieldBackingName);
            if (HasBackingName != null)
                code.WriteLine(HasBackingName + " = true;");
        }
Ejemplo n.º 24
0
        public override void ReadXmlMessage(CsCodeWriter code)
        {
            if (_generator.IsMessage)
                code.WriteLine("{0} child = new {0}();", ((ComplexFieldGenerator)_generator).GetImplementationType(code));
            else
                code.WriteLine("{0} child = {1};", _generator.GetPublicType(code), _generator.MakeConstant(code, null));

            code.WriteLine("(({0}NClassify.Library.IBuilder)child).ReadXml(reader.LocalName, reader);", CsCodeWriter.Global);
            code.WriteLine("{0}.Add(child);", FieldBackingName);
        }
Ejemplo n.º 25
0
 public override string FromXmlString(CsCodeWriter code, string name)
 {
     return name;
 }
Ejemplo n.º 26
0
 public override void ReadXmlValue(CsCodeWriter code, string value)
 {
     code.WriteLine("{0}.Add({1});", FieldBackingName, _generator.FromXmlString(code, value));
 }
Ejemplo n.º 27
0
 public override void WriteValidation(CsCodeWriter code)
 {
     if (_generator.HasValidator)
     {
         using(code.WriteBlock("foreach ({0} item in {1})", _generator.GetPublicType(code), FieldBackingName))
             code.WriteLine("if (!IsValid{0}(item, onError)) errorCount++;", _generator.PropertyName);
     }
 }
Ejemplo n.º 28
0
 public string GetImplementationType(CsCodeWriter code)
 {
     return CsCodeWriter.Global + _type.ImplementationName;
 }
Ejemplo n.º 29
0
        public override void DeclareTypes(CsCodeWriter code)
        {
            string collection = GetStorageType(code);
            string itemType = _generator.GetPublicType(code);
            
            using (code.CodeRegion(collection))
            using (code.DeclareClass(
                new CodeItem(collection) { Access = FieldAccess.Private },
                new[]
                    {
                        CsCodeWriter.Global + "System.Collections.Generic.IList<" + _generator.GetPublicType(code) + ">",
                        CsCodeWriter.Global + "System.ICloneable",
                    }))
            {
                using(code.WriteBlock("private static T AssertNotNull<T>(T value) where T : class"))
                {
                    code.WriteLine("if (null == value) throw new {0}System.ArgumentNullException({1});",
                        CsCodeWriter.Global, code.MakeString(PropertyName)); 
                    code.WriteLine("return value;");
                }

                string value = _generator.IsNullable ? "AssertNotNull(value)" : "value";
                code.WriteLine("private bool _readOnly;");
                code.WriteLine("private readonly {0}System.Collections.Generic.List<{1}> _contents;", CsCodeWriter.Global, itemType);

                using(code.WriteBlock("public {0}()", collection))
                {
                    code.WriteLine("_readOnly = false;");
                    code.WriteLine("_contents = new {0}System.Collections.Generic.List<{1}>();", CsCodeWriter.Global, itemType);
                }
                using (code.WriteBlock("public {0}({1}System.Collections.Generic.IList<{2}> contents, bool clone)", collection, CsCodeWriter.Global, itemType))
                {
                    code.WriteLine("_readOnly = false;");
                    if (!_generator.IsNullable)
                    {
                        code.WriteLine("_contents = new {0}System.Collections.Generic.List<{1}>(AssertNotNull(contents));",
                            CsCodeWriter.Global, itemType);
                    }
                    else
                    {
                        code.WriteLine("_contents = new {0}System.Collections.Generic.List<{1}>(AssertNotNull(contents).Count);",
                            CsCodeWriter.Global, itemType);
                        using (code.WriteBlock("foreach ({0} item in contents)", _generator.GetPublicType(code)))
                        {
                            if (_generator.IsMessage)
                            {
                                code.WriteLine("if (clone)");
                                code.WriteLineIndent("_contents.Add(({0})AssertNotNull(item).Clone());", itemType);
                                code.WriteLine("else");
                            }
                            code.WriteLine("_contents.Add(AssertNotNull(item));");
                        }
                    }
                }
                _generator.Constraints.ForAll(x => x.WriteMember(code));
                using (code.WriteBlock("public void MakeReadOnly()"))
                {
                    code.WriteLine("if (_readOnly) return;");
                    code.WriteLine("_readOnly = true;");
                    if (_generator.IsMessage)
                        using (code.WriteBlock("for (int i=0; i < _contents.Count; i++)"))
                        {
                            _generator.MakeReadOnly(code, "_contents[i]");
                        }
                }
                using (code.WriteBlock("private {0}System.Collections.Generic.List<{1}> Modify", CsCodeWriter.Global, itemType))
                    code.WriteLine("get {{ if (!IsReadOnly) return _contents; throw new {0}System.InvalidOperationException(); }}", CsCodeWriter.Global);
                using (code.WriteBlock("public {0} this[int index]", itemType))
                {
                    code.WriteLine("get { return _contents[index]; }");
                    code.WriteLine("set {{ Modify[index] = {0}; }}", value);
                }
                code.WriteLine("public int Count { get { return _contents.Count; } }");
                code.WriteLine("public bool IsReadOnly { get { return _readOnly; } }");
                code.WriteLine("public void Add({0} value) {{ Modify.Add({1}); }}", itemType, value);
                using (code.WriteBlock("public void AddRange({1}System.Collections.Generic.ICollection<{0}> value)", itemType, CsCodeWriter.Global))
                {
                    if (_generator.IsNullable)
                        code.WriteLine("foreach ({0} item in AssertNotNull(value)) AssertNotNull(item);", _generator.GetPublicType(code));
                    code.WriteLine("Modify.AddRange(AssertNotNull(value));");
                }
                code.WriteLine("public void Insert(int index, {0} value) {{ Modify.Insert(index, {1}); }}", itemType, value);
                code.WriteLine("public bool Remove({0} item) {{ return Modify.Remove(item); }}", itemType);
                code.WriteLine("public void RemoveAt(int index) { Modify.RemoveAt(index); }");
                code.WriteLine("public void Clear() { Modify.Clear(); }");
                code.WriteLine("public bool Contains({0} item) {{ return _contents.Contains(item); }}", itemType);
                code.WriteLine("public int IndexOf({0} item) {{ return _contents.IndexOf(item); }}", itemType);
                code.WriteLine("public void CopyTo({0}[] array, int arrayIndex) {{ _contents.CopyTo(array, arrayIndex); }}", itemType);
                code.WriteLine("object {0}System.ICloneable.Clone() {{ return Clone(); }}", CsCodeWriter.Global);
                using (code.WriteBlock("public {0} Clone()", collection))
                {
                    code.WriteLine("return _readOnly ? this : new {0}(this, true);", collection);
                }
                code.WriteLine("public {0}System.Collections.Generic.IEnumerator<{1}> GetEnumerator()", CsCodeWriter.Global, itemType);
                code.WriteLine("{ return _contents.GetEnumerator(); }");
                code.WriteLine("{0}System.Collections.IEnumerator {0}System.Collections.IEnumerable.GetEnumerator()", CsCodeWriter.Global);
                code.WriteLine("{{ return (({0}System.Collections.IEnumerable)_contents).GetEnumerator(); }}", CsCodeWriter.Global);
            }
        }
Ejemplo n.º 30
0
 public override string MakeConstant(CsCodeWriter code, string value)
 {
     if (!String.IsNullOrEmpty(value))
         throw new NotSupportedException("Unable to define a default value for an array.");
     return "new " + GetStorageType(code) + "()";
 }