Ejemplo n.º 1
0
        static XmlDoc Load(string fileName, string cachePath, bool allowRedirect)
        {
            LoggingService.Debug("Loading XmlDoc for " + fileName);
            XmlDoc doc;
            string cacheName = null;

            if (cachePath != null)
            {
                Directory.CreateDirectory(cachePath);
                cacheName = cachePath + "/" + Path.GetFileNameWithoutExtension(fileName)
                            + "." + fileName.GetHashCode().ToString("x") + ".dat";
                if (File.Exists(cacheName))
                {
                    doc = new XmlDoc();
                    if (doc.LoadFromBinary(cacheName, File.GetLastWriteTimeUtc(fileName)))
                    {
                        //LoggingService.Debug("XmlDoc: Load from cache successful");
                        return(doc);
                    }
                    else
                    {
                        doc.Dispose();
                        try {
                            File.Delete(cacheName);
                        } catch {}
                    }
                }
            }

            try {
                using (XmlTextReader xmlReader = new XmlTextReader(fileName)) {
                    xmlReader.MoveToContent();
                    if (allowRedirect && !string.IsNullOrEmpty(xmlReader.GetAttribute("redirect")))
                    {
                        string redirectionTarget = GetRedirectionTarget(xmlReader.GetAttribute("redirect"));
                        if (redirectionTarget != null)
                        {
                            LoggingService.Info("XmlDoc " + fileName + " is redirecting to " + redirectionTarget);
                            return(Load(redirectionTarget, cachePath, false));
                        }
                        else
                        {
                            LoggingService.Warn("XmlDoc " + fileName + " is redirecting to " + xmlReader.GetAttribute("redirect") + ", but that file was not found.");
                            return(new XmlDoc());
                        }
                    }
                    doc = Load(xmlReader);
                }
            } catch (XmlException ex) {
                LoggingService.Warn("Error loading XmlDoc " + fileName, ex);
                return(new XmlDoc());
            }

            if (cachePath != null && doc.xmlDescription.Count > cacheLength * 2)
            {
                LoggingService.Debug("XmlDoc: Creating cache for " + fileName);
                DateTime date = File.GetLastWriteTimeUtc(fileName);
                try {
                    doc.Save(cacheName, date);
                } catch (Exception ex) {
                    LoggingService.Error("Cannot write to cache file " + cacheName, ex);
                    return(doc);
                }
                doc.Dispose();
                doc = new XmlDoc();
                doc.LoadFromBinary(cacheName, date);
            }
            return(doc);
        }
Ejemplo n.º 2
0
        public ReflectionProjectContent(string assemblyFullName, string assemblyLocation, DomAssemblyName[] referencedAssemblies, ProjectContentRegistry registry)
        {
            if (assemblyFullName == null)
            {
                throw new ArgumentNullException("assemblyFullName");
            }
            if (assemblyLocation == null)
            {
                throw new ArgumentNullException("assemblyLocation");
            }
            if (registry == null)
            {
                throw new ArgumentNullException("registry");
            }

            this.registry                = registry;
            this.assemblyFullName        = assemblyFullName;
            this.assemblyName            = (assemblyFullName.IndexOf(',') > -1) ? assemblyFullName.Substring(0, assemblyFullName.IndexOf(',')) : assemblyFullName;
            this.referencedAssemblyNames = referencedAssemblies;
            this.assemblyLocation        = assemblyLocation;
            this.assemblyCompilationUnit = new DefaultCompilationUnit(this);

            try {
                assemblyFileLastWriteTime = File.GetLastWriteTimeUtc(assemblyLocation);
            } catch (Exception ex) {
                LoggingService.Warn(ex);
            }

            string fileName = null;

            if (assemblyLocation != typeof(object).Assembly.Location)
            {
                // First look in the assembly's directory.
                // mscorlib is the exception, because it is loaded from the runtime directory (C:\Windows\Microsoft.NET\Framework\v4.0.30319),
                // but that might contain an outdated version of mscorlib.xml (with less documented members than the mscorlib.xml in the Reference Assemblies)
                // (at least on my machine, lots of others don't seem to have the v4.0.30319\mscorlib.xml at all).
                fileName = XmlDoc.LookupLocalizedXmlDoc(assemblyLocation);
            }
            if (fileName == null)
            {
                // Not found -> look in other directories:
                foreach (string testDirectory in XmlDoc.XmlDocLookupDirectories)
                {
                    fileName = XmlDoc.LookupLocalizedXmlDoc(Path.Combine(testDirectory, Path.GetFileName(assemblyLocation)));
                    if (fileName != null)
                    {
                        break;
                    }
                }
            }

            if (fileName != null)
            {
                if (registry.persistence != null)
                {
                    this.XmlDoc = XmlDoc.Load(fileName, Path.Combine(registry.persistence.CacheDirectory, "XmlDoc"));
                }
                else
                {
                    this.XmlDoc = XmlDoc.Load(fileName, null);
                }
            }
        }