예제 #1
0
파일: FileUtility.cs 프로젝트: sjyfok/cpp
        static FileOperationResult ObservedLoadHandleException(Exception e, FileOperationDelegate loadFile, FileName fileName, string message, FileErrorPolicy policy)
        {
            message = message + Environment.NewLine + Environment.NewLine + e.Message;
            var messageService = ServiceSingleton.GetRequiredService <IMessageService>();

            switch (policy)
            {
            case FileErrorPolicy.Inform:
                messageService.InformSaveError(fileName, message, "${res:FileUtilityService.ErrorWhileLoading}", e);
                break;

            case FileErrorPolicy.ProvideAlternative:
                ChooseSaveErrorResult r = messageService.ChooseSaveError(fileName, message, "${res:FileUtilityService.ErrorWhileLoading}", e, false);
                if (r.IsRetry)
                {
                    return(ObservedLoad(loadFile, fileName, message, policy));
                }
                else if (r.IsIgnore)
                {
                    return(FileOperationResult.Failed);
                }
                break;
            }
            return(FileOperationResult.Failed);
        }
예제 #2
0
파일: FileUtility.cs 프로젝트: sjyfok/cpp
 public static bool TestFileExists(string filename)
 {
     if (!File.Exists(filename))
     {
         var messageService = ServiceSingleton.GetRequiredService <IMessageService>();
         messageService.ShowWarning(StringParser.Parse("${res:Fileutility.CantFindFileError}", new StringTagPair("FILE", filename)));
         return(false);
     }
     return(true);
 }
예제 #3
0
        /// <summary>
        /// Allow special syntax to retrieve property values:
        /// ${property:PropertyName}
        /// ${property:PropertyName??DefaultValue}
        /// ${property:ContainerName/PropertyName}
        /// ${property:ContainerName/PropertyName??DefaultValue}
        /// A container is a Properties instance stored in the PropertyService. This is
        /// used by many AddIns to group all their properties into one container.
        /// </summary>
        static string GetProperty(string propertyName)
        {
            string defaultValue = "";
            int    pos          = propertyName.LastIndexOf("??", StringComparison.Ordinal);

            if (pos >= 0)
            {
                defaultValue = propertyName.Substring(pos + 2);
                propertyName = propertyName.Substring(0, pos);
            }
            Properties properties = ServiceSingleton.GetRequiredService <IPropertyService>().MainPropertiesContainer;

            pos = propertyName.IndexOf('/');
            while (pos >= 0)
            {
                properties   = properties.NestedProperties(propertyName.Substring(0, pos));
                propertyName = propertyName.Substring(pos + 1);
                pos          = propertyName.IndexOf('/');
            }
            return(properties.Get(propertyName, defaultValue));
        }
예제 #4
0
        public static string GetValue(string propertyName,
                                      params StringTagPair[] customTags)
        {
            if (propertyName == null)
            {
                throw new ArgumentNullException("propertyName");
            }
            if (propertyName == "$")
            {
                return("$");
            }
            if (customTags != null)
            {
                foreach (StringTagPair pair in customTags)
                {
                    if (propertyName.Equals(pair.Tag, StringComparison.OrdinalIgnoreCase))
                    {
                        return(pair.Value);
                    }
                }
            }
            int k = propertyName.IndexOf(':');

            if (k <= 0)
            {
                if (propertyName.Equals("DATE", StringComparison.OrdinalIgnoreCase))
                {
                    return(DateTime.Today.ToShortDateString());
                }
                if (propertyName.Equals("TIME", StringComparison.OrdinalIgnoreCase))
                {
                    return(DateTime.Now.ToShortTimeString());
                }
                if (propertyName.Equals("ProductName", StringComparison.OrdinalIgnoreCase))
                {
                    return(ServiceSingleton.GetRequiredService <IMessageService>().ProductName);
                }
                if (propertyName.Equals("GUID", StringComparison.OrdinalIgnoreCase))
                {
                    return(Guid.NewGuid().ToString().ToUpperInvariant());
                }
                if (propertyName.Equals("USER", StringComparison.OrdinalIgnoreCase))
                {
                    return(Environment.UserName);
                }
                if (propertyName.Equals("Version", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);// RevisionClass.FullVersion;
                }
                if (propertyName.Equals("CONFIGDIRECTORY", StringComparison.OrdinalIgnoreCase))
                {
                    return(ServiceSingleton.GetRequiredService <IPropertyService>().ConfigDirectory);
                }
                foreach (IStringTagProvider provider in stringTagProviders)
                {
                    string result = provider.ProvideString(propertyName, customTags);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                return(null);
            }
            else
            {
                // it's a prefixed property


                // res: properties are quite common, so optimize by testing for them first
                // before allocaing the prefix/propertyName strings
                // All other prefixed properties {prefix:Key} shoulg get handled in the switch below.
                if (propertyName.StartsWith("res:", StringComparison.OrdinalIgnoreCase))
                {
                    var resourceService = (IResourceService)ServiceSingleton.ServiceProvider.GetService(typeof(IResourceService));
                    if (resourceService == null)
                    {
                        return(null);
                    }
                    try
                    {
                        return(Parse(resourceService.GetString(propertyName.Substring(4)), customTags));
                    }
                    catch (ResourceNotFoundException)
                    {
                        return(null);
                    }
                }

                string prefix = propertyName.Substring(0, k);
                propertyName = propertyName.Substring(k + 1);
                switch (prefix.ToUpperInvariant())
                {
                case "SDKTOOLPATH":
                    return(FileUtility.GetSdkPath(propertyName));

                //case "ADDINPATH":
                //    foreach (var addIn in ServiceSingleton.GetRequiredService<IAddInTree>().AddIns)
                //    {
                //        if (addIn.Manifest.Identities.ContainsKey(propertyName))
                //        {
                //            return System.IO.Path.GetDirectoryName(addIn.FileName);
                //        }
                //    }
                //    return null;
                case "DATE":
                    try
                    {
                        return(DateTime.Now.ToString(propertyName, CultureInfo.CurrentCulture));
                    }
                    catch (Exception ex)
                    {
                        return(ex.Message);
                    }

                case "ENV":
                    return(Environment.GetEnvironmentVariable(propertyName));

                case "PROPERTY":
                    return(GetProperty(propertyName));

                default:
                    IStringTagProvider provider;
                    if (prefixedStringTagProviders.TryGetValue(prefix, out provider))
                    {
                        return(provider.ProvideString(propertyName, customTags));
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }