Exemplo 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>
 /// Returns tags search results.
 /// </summary>
 /// <param name="query">Search query.</param>
 /// <param name="resultsCollector">Results collector containing search results.</param>
 /// <returns>Search results enumeration, or null if search type is not supported.</returns>
 private IEnumerable <ISearchHit> TagsSearchResults(Query query, TopScoreDocCollector resultsCollector)
 {
     foreach (var doc in resultsCollector.TopDocs().ScoreDocs)
     {
         if (FileStorageProviders.Exists(mSearcher.Doc(doc.Doc).GetField(LuceneIndexBuilder.FIELD_PATH).StringValue))
         {
             yield return(new LuceneSearchHit(query, mSearcher.Doc(doc.Doc), doc.Score, CaseSensitive, SearchType.Tags));
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Registers all or certain types from the specified plugin assembly.
 /// </summary>
 /// <param name="pluginAssembly">Plugin assembly.</param>
 /// <param name="pluginType">Type of pluign to be registered. If null, all supported types are registered.</param>
 private static void RegisterTypesFromPluginAssembly(Assembly pluginAssembly, Type pluginType = null)
 {
     foreach (var exportedType in pluginAssembly.GetExportedTypes())
     {
         try
         {
             if ((pluginType == null || typeof(Interfaces.IFileAction) == pluginType) && typeof(Interfaces.IFileAction).IsAssignableFrom(exportedType))
             {
                 FileActions.RegisterFileAction(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.IFileCatalogueEnumerator) == pluginType) && typeof(Interfaces.IFileCatalogueEnumerator).IsAssignableFrom(exportedType))
             {
                 FileCatalogueEnumerators.RegisterFileCatalogueEnumerator(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.IFileEnumerator) == pluginType) && typeof(Interfaces.IFileEnumerator).IsAssignableFrom(exportedType))
             {
                 FileEnumerators.RegisterFileEnumerator(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.IFileFilter) == pluginType) && typeof(Interfaces.IFileFilter).IsAssignableFrom(exportedType))
             {
                 FileFilters.RegisterFileFilter(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.IHighlighter) == pluginType) && typeof(Interfaces.IHighlighter).IsAssignableFrom(exportedType))
             {
                 FileHighlighters.RegisterFileHighlighter(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.ITokenizer) == pluginType) && typeof(Interfaces.ITokenizer).IsAssignableFrom(exportedType))
             {
                 FileTokenizers.RegisterFileTokenizer(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.IFileStorageProvider) == pluginType) && typeof(Interfaces.IFileStorageProvider).IsAssignableFrom(exportedType))
             {
                 FileStorageProviders.RegisterFileStorageProvider(exportedType);
             }
             else
             if ((pluginType == null || typeof(Interfaces.ITextSearcher) == pluginType) && typeof(Interfaces.ITextSearcher).IsAssignableFrom(exportedType))
             {
                 TextSearchers.RegisterTextSearcher(exportedType);
             }
         }
         catch
         {
         }
     }
 }
        /// <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);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new document.
        /// </summary>
        /// <param name="filePath">Path of the document.</param>
        /// <param name="fileReader">File reader to be used for reading the document.</param>
        /// <param name="document">Document to be initialized.</param>
        private static void InitializeDocument(Lucene.Net.Documents.Document document, string filePath, System.IO.Stream contentsStream, System.IO.Stream caseInsensitiveContentsStream, System.IO.Stream tagsStream, System.IO.Stream caseInsensitiveTagStream, out System.IO.TextReader contentsReader, out System.IO.TextReader caseInsensitiveContentsReader, out System.IO.TextReader tagsReader, out System.IO.TextReader caseInsensitiveTagsReader)
        {
            contentsReader = new System.IO.StreamReader(contentsStream, Encoding.Default, true);
            tagsReader     = new System.IO.StreamReader(tagsStream, Encoding.Default, true);
            caseInsensitiveContentsReader = new System.IO.StreamReader(caseInsensitiveContentsStream, Encoding.Default, true);
            caseInsensitiveTagsReader     = new System.IO.StreamReader(caseInsensitiveTagStream, Encoding.Default, true);

            document.Add(new Lucene.Net.Documents.Field(FIELD_PATH, filePath, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED_NO_NORMS));
            document.Add(new Lucene.Net.Documents.Field(FIELD_EXTENSION, Path.GetExtension(filePath).ToLower(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED_NO_NORMS));
            document.Add(new Lucene.Net.Documents.NumericField(FIELD_LAST_MODIFIED).SetLongValue(FileStorageProviders.GetLastModificationTimeStamp(filePath)));
            document.Add(new Lucene.Net.Documents.NumericField(FIELD_SIZE).SetLongValue(FileStorageProviders.GetSize(filePath)));
            document.Add(new Lucene.Net.Documents.Field(FIELD_CONTENTS, contentsReader, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
            document.Add(new Lucene.Net.Documents.Field(FIELD_TAGS, tagsReader, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
            document.Add(new Lucene.Net.Documents.Field(FIELD_CONTENTS + FIELD_CASEINSENSITIVE, caseInsensitiveContentsReader, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
            document.Add(new Lucene.Net.Documents.Field(FIELD_TAGS + FIELD_CASEINSENSITIVE, caseInsensitiveTagsReader, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
        }