Beispiel #1
0
        void GenerateCode(string typeName, MimeTextMatchCollection matches)
        {
            CodeIdentifiers     members   = new CodeIdentifiers();
            CodeTypeDeclaration codeClass = WebCodeGenerator.AddClass(ImportContext.CodeNamespace, typeName, string.Empty, new string[0], null, CodeFlags.IsPublic);

            string[] fieldTypeNames = new string[matches.Count];
            for (int i = 0; i < matches.Count; i++)
            {
                MimeTextMatch match = matches[i];
                string        name  = members.AddUnique(CodeIdentifier.MakeValid(match.Name.Length == 0 ? methodName + "Match" : match.Name), match);
                CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();
                if (match.Pattern.Length == 0)
                {
                    throw new ArgumentException(Res.GetString(Res.WebTextMatchMissingPattern));
                }

                CodeExpression pattern       = new CodePrimitiveExpression(match.Pattern);
                int            numPropValues = 0;
                if (match.Group != 1)
                {
                    numPropValues++;
                }
                if (match.Capture != 0)
                {
                    numPropValues++;
                }
                if (match.IgnoreCase)
                {
                    numPropValues++;
                }
                if (match.Repeats != 1 && match.Repeats != int.MaxValue)
                {
                    numPropValues++;
                }
                CodeExpression[] propertyValues = new CodeExpression[numPropValues];
                string[]         propertyNames  = new string[propertyValues.Length];
                numPropValues = 0;
                if (match.Group != 1)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.Group);
                    propertyNames[numPropValues]  = "Group";
                    numPropValues++;
                }
                if (match.Capture != 0)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.Capture);
                    propertyNames[numPropValues]  = "Capture";
                    numPropValues++;
                }
                if (match.IgnoreCase)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.IgnoreCase);
                    propertyNames[numPropValues]  = "IgnoreCase";
                    numPropValues++;
                }
                if (match.Repeats != 1 && match.Repeats != int.MaxValue)
                {
                    propertyValues[numPropValues] = new CodePrimitiveExpression(match.Repeats);
                    propertyNames[numPropValues]  = "MaxRepeats";
                    numPropValues++;
                }
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(MatchAttribute), new CodeExpression[] { pattern }, propertyNames, propertyValues);

                string fieldTypeName;
                if (match.Matches.Count > 0)
                {
                    fieldTypeName     = ImportContext.ClassNames.AddUnique(CodeIdentifier.MakeValid(match.Type.Length == 0 ? name : match.Type), match);
                    fieldTypeNames[i] = fieldTypeName;
                }
                else
                {
                    fieldTypeName = typeof(string).FullName;
                }
                if (match.Repeats != 1)
                {
                    fieldTypeName += "[]";
                }

                CodeMemberField field = WebCodeGenerator.AddField(codeClass, fieldTypeName, name, null, metadata, CodeFlags.IsPublic);

                if (match.Matches.Count == 0 && match.Type.Length > 0)
                {
                    ImportContext.Warnings |= ServiceDescriptionImportWarnings.OptionalExtensionsIgnored;
                    ProtocolImporter.AddWarningComment(field.Comments, Res.GetString(Res.WebTextMatchIgnoredTypeWarning));
                }
            }

            for (int i = 0; i < fieldTypeNames.Length; i++)
            {
                string fieldTypeName = fieldTypeNames[i];
                if (fieldTypeName != null)
                {
                    GenerateCode(fieldTypeName, matches[i].Matches);
                }
            }
        }