public IViewContent CreateContentForFile(OpenedFile file)
        {
            var codons = SD.DisplayBindingService.GetCodonsPerFileName(file.FileName);
            DisplayBindingDescriptor bestMatch = null;
            double    max           = double.NegativeInfinity;
            const int BUFFER_LENGTH = 4 * 1024;

            using (var stream = file.OpenRead()) {
                string mime = "text/plain";
                if (stream.Length > 0)
                {
                    stream.Position = 0;
                    mime            = MimeTypeDetection.FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH));
                }
                foreach (var codon in codons)
                {
                    stream.Position = 0;
                    double value = codon.Binding.AutoDetectFileContent(file.FileName, stream, mime);
                    if (value > max)
                    {
                        max       = value;
                        bestMatch = codon;
                    }
                }
            }

            if (bestMatch == null)
            {
                throw new InvalidOperationException();
            }

            return(bestMatch.Binding.CreateContentForFile(file));
        }
        public IViewContent CreateContentForFile(OpenedFile file)
        {
            var codons = SD.DisplayBindingService.GetCodonsPerFileName(file.FileName);
            DisplayBindingDescriptor bestMatch = null;
            double max = double.NegativeInfinity;
            const int BUFFER_LENGTH = 4 * 1024;

            using (var stream = file.OpenRead()) {
                string mime = "text/plain";
                if (stream.Length > 0) {
                    stream.Position = 0;
                    mime = MimeTypeDetection.FindMimeType(new BinaryReader(stream).ReadBytes(BUFFER_LENGTH));
                }
                foreach (var codon in codons) {
                    stream.Position = 0;
                    double value = codon.Binding.AutoDetectFileContent(file.FileName, stream, mime);
                    if (value > max) {
                        max = value;
                        bestMatch = codon;
                    }
                }
            }

            if (bestMatch == null)
                throw new InvalidOperationException();

            return bestMatch.Binding.CreateContentForFile(file);
        }
Пример #3
0
        public ITextSource GetFileContentForOpenFile(FileName fileName)
        {
            return(SD.MainThread.InvokeIfRequired(
                       delegate {
                OpenedFile file = this.GetOpenedFile(fileName);
                if (file != null)
                {
                    if (file.CurrentView != null)
                    {
                        IFileDocumentProvider provider = file.CurrentView.GetService <IFileDocumentProvider>();
                        if (provider != null)
                        {
                            IDocument document = provider.GetDocumentForFile(file);
                            if (document != null)
                            {
                                return document.CreateSnapshot();
                            }
                        }
                    }

                    using (Stream s = file.OpenRead()) {
                        // load file
                        return new StringTextSource(FileReader.ReadFileContent(s, DefaultFileEncoding));
                    }
                }
                return null;
            }));
        }