Ejemplo n.º 1
0
        private static ReadOnlyDictionary <string, string> LoadStrings(string fileName)
        {
            XElement strings = XElementUtils.Load(fileName);

            var stringDictionary = new Dictionary <string, string>();

            foreach (XElement stringElement in strings.Elements())
            {
                XAttribute keyAttribute   = stringElement.Attribute("key");
                XAttribute valueAttribute = stringElement.Attribute("value");

                if (keyAttribute != null && valueAttribute != null)
                {
                    if (stringDictionary.ContainsKey(keyAttribute.Value))
                    {
                        throw new InvalidOperationException("Duplicate string resource key '{0}' in XML file '{1}'"
                                                            .FormatWith(keyAttribute.Value, Path.GetFileName(fileName)));
                    }

                    stringDictionary.Add(keyAttribute.Value, valueAttribute.Value);
                }
                else
                {
                    Log.LogError(LogTitle, "Invalid entry '{0}' in file '{1}'"
                                 .FormatWith(stringElement.ToString(SaveOptions.DisableFormatting), Path.GetFileName(fileName)));
                }
            }

            return(new ReadOnlyDictionary <string, string>(stringDictionary));
        }
        /// <summary>
        /// Returns strings for the specified culture or null if no strings exists.
        /// </summary>
        private Dictionary <string, string> GetStringsForCulture(CultureInfo cultureInfo)
        {
            Dictionary <string, string> stringDictionary;

            using (_resourceLocker.Locker)
            {
                if (_resourceLocker.Resources.CultureFileLastUpdated.ContainsKey(cultureInfo))
                {
                    string unresolvedFileName = _resourceLocker.Resources.CultureToFileLookup[cultureInfo];
                    string resolvedFileName   = PathUtil.Resolve(unresolvedFileName);

                    DateTime lastWrite             = C1File.GetLastWriteTime(resolvedFileName);
                    double   secondsSinceLastWrite = (DateTime.Now - lastWrite).TotalSeconds;

                    if (secondsSinceLastWrite < 300 || lastWrite > _resourceLocker.Resources.CultureFileLastUpdated[cultureInfo])
                    {
                        _resourceLocker.Resources.CultureToTranslation.Remove(cultureInfo);
                        _resourceLocker.Resources.CultureFileLastUpdated[cultureInfo] = lastWrite;
                    }
                }

                if (_resourceLocker.Resources.CultureToTranslation.TryGetValue(cultureInfo, out stringDictionary) == false)
                {
                    string unresolvedFileName;
                    if (_resourceLocker.Resources.CultureToFileLookup.TryGetValue(cultureInfo, out unresolvedFileName) == false)
                    {
                        return(null);
                    }

                    string   resolvedFileName = PathUtil.Resolve(unresolvedFileName);
                    XElement strings          = XElementUtils.Load(resolvedFileName);

                    stringDictionary = new Dictionary <string, string>();

                    foreach (XElement stringElement in strings.Elements())
                    {
                        XAttribute keyAttribute   = stringElement.Attribute("key");
                        XAttribute valueAttribute = stringElement.Attribute("value");

                        if (keyAttribute != null && valueAttribute != null)
                        {
                            if (stringDictionary.ContainsKey(keyAttribute.Value))
                            {
                                throw new InvalidOperationException(string.Format("Duplicate string resource key '{0}' in XML file '{1}'", keyAttribute.Value, unresolvedFileName));
                            }
                            stringDictionary.Add(keyAttribute.Value, valueAttribute.Value);
                        }
                        else
                        {
                            LoggingService.LogError("XmlStringResourceProvider", string.Format("Invalid entry '{0}' in file '{1}'", stringElement.ToString(SaveOptions.DisableFormatting), unresolvedFileName));
                        }
                    }

                    _resourceLocker.Resources.CultureToTranslation.Add(cultureInfo, stringDictionary);
                }
            }

            return(stringDictionary);
        }
        private static XElement GetPredefinedResizingOptions()
        {
            XElement xel = HttpRuntime.Cache.Get("ResizedImageKeys") as XElement;

            //If it's not there, load the xml document and then add it to the cache
            if (xel == null)
            {
                if (_resizedImageKeysFilePath == null)
                {
                    _resizedImageKeysFilePath = PathUtil.Resolve(ResizedImageKeys);
                }

                if (!C1File.Exists(_resizedImageKeysFilePath))
                {
                    string directoryPath = Path.GetDirectoryName(_resizedImageKeysFilePath);
                    if (!C1Directory.Exists(directoryPath))
                    {
                        C1Directory.CreateDirectory(directoryPath);
                    }

                    var config = new XElement("ResizedImages",
                                              new XElement("image",
                                                           new XAttribute("name", "thumbnail"),
                                                           new XAttribute("maxwidth", "100"),
                                                           new XAttribute("maxheight", "100")),
                                              new XElement("image",
                                                           new XAttribute("name", "normal"),
                                                           new XAttribute("maxwidth", "200")),
                                              new XElement("image",
                                                           new XAttribute("name", "large"),
                                                           new XAttribute("maxheight", "300"))
                                              );

                    config.SaveToPath(_resizedImageKeysFilePath);
                }

                xel = XElementUtils.Load(_resizedImageKeysFilePath);
                var cd = new CacheDependency(_resizedImageKeysFilePath);
                var cacheExpirationTimeSpan = new TimeSpan(24, 0, 0);
                HttpRuntime.Cache.Add("ResizedImageKeys", xel, cd, Cache.NoAbsoluteExpiration, cacheExpirationTimeSpan, CacheItemPriority.Default, null);
            }

            return(xel);
        }
Ejemplo n.º 4
0
        public override object Execute(ParameterList parameters, FunctionContextContainer context)
        {
            string url = parameters.GetParameter <string>("Url");

            bool cachingEnabled = false;

            int cachePeriod;

            if (parameters.TryGetParameter("CacheTime", out cachePeriod))
            {
                cachingEnabled = cachePeriod > 0;
            }

            string cacheKey = null;

            if (cachingEnabled)
            {
                cacheKey = typeof(LoadUrlFunction).FullName + "|" + url;
                var cachedValue = HttpRuntime.Cache.Get(cacheKey) as XElement;
                if (cachedValue != null)
                {
                    return(cachedValue);
                }
            }

            using (TimerProfilerFacade.CreateTimerProfiler(url))
            {
                XElement value = XElementUtils.Load(url);

                if (cachingEnabled)
                {
                    HttpRuntime.Cache.Add(cacheKey, value, null, DateTime.Now.AddSeconds(cachePeriod),
                                          Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                }

                return(value);
            }
        }
Ejemplo n.º 5
0
        private void DeserializeMessagesFromFileSystem()
        {
            lock (_lock)
            {
                if (!C1File.Exists(MessageQueueFilePath))
                {
                    return;
                }

                XElement serializedMessages;

                try
                {
                    serializedMessages = XElementUtils.Load(MessageQueueFilePath);
                }
                catch (Exception)
                {
                    return;
                }

                IXmlSerializer xmlSerializer = GetMessageListXmlSerializer();

                _elements = xmlSerializer.Deserialize(serializedMessages) as List <ConsoleMessageQueueElement>;
                if (_elements == null)
                {
                    _elements = new List <ConsoleMessageQueueElement>();
                }

                CleanOutOldMessages(_elements);

                if (_elements.Any())
                {
                    _queueItemCounter = _elements.Max(f => f.QueueItemNumber);
                }
            }
        }