Пример #1
0
        public ResourceTypeSettings Validate(CodeDomProvider codeDomProvider)
        {
            var settings = Clone( );

            settings.ClassName       = codeDomProvider.ValidateIdentifier(settings.ClassName);
            settings.Namespace       = codeDomProvider.ValidateIdentifier(settings.Namespace, true);
            settings.AccessModifiers = codeDomProvider.ValidateAccessModifiers(settings.AccessModifiers);

            return(settings);
        }
Пример #2
0
        public static string ValidateBaseName(this CodeDomProvider codeDomProvider, string baseName)
        {
            var validatedBaseName = baseName;

            if (!codeDomProvider.IsValidIdentifier(validatedBaseName))
            {
                var validBaseName = codeDomProvider.ValidateIdentifier(validatedBaseName);
                if (validBaseName != null)
                {
                    validatedBaseName = validBaseName;
                }
            }

            if (!codeDomProvider.IsValidIdentifier(validatedBaseName))
            {
                throw new ArgumentException(string.Format(InvalidIdentifier, validatedBaseName));
            }

            return(validatedBaseName);
        }
Пример #3
0
        public IList <ResourceMapping> Map(CodeTypeDeclaration @class, IList <IResource> resourceSet)
        {
            var members = new HashSet <string> ( );

            foreach (CodeTypeMember member in @class.Members)
            {
                members.Add(member.Name);
            }

            var map = new SortedList <string, ResourceMapping> (resourceSet.Count, StringComparer.InvariantCultureIgnoreCase);

            foreach (var resource in resourceSet)
            {
                var name     = resource.Name;
                var property = name;

                if (members.Contains(name))
                {
                    AddError(resource, Format(PropertyAlreadyExists, name));
                    continue;
                }

                if (resource.Type == TypeNames.Void)
                {
                    AddError(resource, Format(InvalidPropertyType, resource.Type, name));
                    continue;
                }

                var isWinFormsLocalizableResource = name.Length > 0 && name [0] == '$' ||
                                                    name.Length > 1 && name [0] == '>' &&
                                                    name [1] == '>';
                if (isWinFormsLocalizableResource)
                {
                    AddWarning(resource, Format(SkippingWinFormsResource, name));
                    continue;
                }

                var isBaseName = ResourceNamingStrategy == null ||
                                 ResourceNamingStrategy.ParseArguments(PluralRules.Invariant, name, out _) == name;
                if (!isBaseName)
                {
                    continue;
                }

                if (!CodeDomProvider.IsValidIdentifier(property))
                {
                    property = CodeDomProvider.ValidateIdentifier(property);

                    if (property == null)
                    {
                        AddError(resource, Format(CannotCreateResourceProperty, name));
                        continue;
                    }
                }

                if (map.TryGetValue(property, out var duplicate))
                {
                    AddError(duplicate.Resource, Format(CannotCreateResourceProperty, duplicate.Resource.Name));
                    AddError(resource, Format(CannotCreateResourceProperty, name));

                    map.Remove(property);
                    continue;
                }

                map.Add(property, new ResourceMapping(resource)
                {
                    Property = property
                });
                members.Add(property);
            }

            foreach (var mapping in map.Values)
            {
                mapping.NumberOfArguments = GetNumberOfArguments(mapping.Resource);
                if (mapping.NumberOfArguments <= 0)
                {
                    continue;
                }

                var methodName = mapping.Property + FormatMethodSuffix;
                var isUnique   = !members.Contains(methodName);
                if (isUnique && CodeDomProvider.IsValidIdentifier(methodName))
                {
                    mapping.FormatMethod = methodName;
                    members.Add(methodName);
                }
                else
                {
                    AddError(mapping.Resource, Format(CannotCreateFormatMethod, methodName, mapping.Resource.Name));
                }
            }

            return(map.Values.ToList( ));
        }
Пример #4
0
 public void ValidateIdentifier(string value)
 {
     provider.ValidateIdentifier(value);
 }