Esempio n. 1
0
        private bool GetStreamsForFile(string filePath, out System.IO.Stream contentStream, out System.IO.Stream caseInsensitiveContentStream, out System.IO.Stream tagStream, out System.IO.Stream caseInsensitiveTagStream)
        {
            contentStream = null;
            caseInsensitiveContentStream = null;
            tagStream = null;
            caseInsensitiveTagStream = null;

            System.IO.Stream[] streams = new System.IO.Stream[4];
            for (int i = 0; i < 4; ++i)
            {
                streams[i] = FileStorageProviders.GetFileStream(filePath);
                if (streams[i] == null)
                {
                    for (int j = 0; j < i; ++j)
                    {
                        if (streams[j] != null)
                        {
                            streams[j].Close();
                        }
                    }
                    return(false);
                }
            }

            contentStream = streams[0];
            tagStream     = streams[1];
            caseInsensitiveContentStream = streams[2];
            caseInsensitiveTagStream     = streams[3];
            return(true);
        }
        /// <summary>
        /// Determines a list of search query matches.
        /// </summary>
        /// <param name="searchType">Type of search.</param>
        /// <param name="filePath">File path of the document, which should be searched.</param>
        /// <param name="query">Query, whose matches should be returned.</param>
        /// <param name="filesAnalyzer">Files analyzer to be used.</param>
        /// <param name="contentsAnalyzer">Contents analyzer to be used.</param>
        /// <param name="tagsAnalyzer">Tags analyzer to be used.</param>
        /// <returns>Collection of search query match occurrences.</returns>
        public static ReadOnlyCollection <IOccurrence> DetermineOccurrences(string searchType, string filePath, Query query, Analyzer filesAnalyzer, Analyzer contentsAnalyzer, Analyzer tagsAnalyzer)
        {
            try
            {
                string originalText   = null;
                bool   createFragment = true;
                switch (searchType)
                {
                case SearchType.Files:
                {
                    createFragment = false;
                    originalText   = filePath;
                }
                break;

                default:
                {
                    createFragment = true;
                    // Use file storage provider registry to get read only access to the original file.
                    using (var fileStream = FileStorageProviders.GetFileStream(filePath))
                    {
                        if (fileStream != null)
                        {
                            using (var reader = new System.IO.StreamReader(fileStream, Encoding.Default, true))
                            {
                                // Read the whole text.
                                originalText = reader.ReadToEnd();
                                reader.Close();
                            }
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                break;
                }

                return(DetermineOccurrences(searchType, query, originalText, filesAnalyzer, contentsAnalyzer, tagsAnalyzer, createFragment));
            }
            catch
            {
                return(null);
            }
        }