using System;
using System.Collections.Generic;
using System.Text;
namespace ScrewTurn.Wiki.SearchEngine {
///
/// Contains search parameters.
///
public class SearchParameters {
private string query;
private string[] documentTypeTags;
private SearchOptions options;
///
/// Initializes a new instance of the class.
///
/// The search query.
/// The document type tags to include in the search, or null.
/// The search options.
/// If or one of the elements of (when the array is not null) are null.
/// If or one of the elements of (when the array is not null) are empty.
public SearchParameters(string query, string[] documentTypeTags, SearchOptions options) {
if(query == null) throw new ArgumentNullException("query");
if(query.Length == 0) throw new ArgumentException("Query cannot be empty", "query");
if(documentTypeTags != null) {
if(documentTypeTags.Length == 0) throw new ArgumentException("DocumentTypeTags cannot be empty", "documentTypeTags");
foreach(string dtt in documentTypeTags) {
if(dtt == null) throw new ArgumentNullException("documentTypeTags");
if(dtt.Length == 0) throw new ArgumentException("DocumentTypeTag cannot be empty", "documentTypeTag");
}
}
this.query = PrepareQuery(query);
this.documentTypeTags = documentTypeTags;
this.options = options;
}
///
/// Initializes a new instance of the class.
///
/// The search query.
public SearchParameters(string query)
: this(query, null, SearchOptions.AtLeastOneWord) { }
///
/// Initializes a new instance of the class.
///
/// The search query.
/// The document type tags to include in the search, or null.
public SearchParameters(string query, params string[] documentTypeTags)
: this(query, documentTypeTags, SearchOptions.AtLeastOneWord) { }
///
/// Initializes a new instance of the class.
///
/// The search query.
/// The search options.
public SearchParameters(string query, SearchOptions options)
: this(query, null, options) { }
///
/// Prepares a query for searching.
///
/// The query.
/// The prepared query.
private static string PrepareQuery(string query) {
StringBuilder sb = new StringBuilder(query.Length);
// This behavior is slightly different from RemoveDiacriticsAndPunctuation
foreach(char c in query) {
if(!ScrewTurn.Wiki.SearchEngine.Tools.IsSplitChar(c)) sb.Append(c);
else sb.Append(" ");
}
string normalized = Tools.RemoveDiacriticsAndPunctuation(sb.ToString(), false);
return normalized;
}
///
/// Gets or sets the query.
///
public string Query {
get { return query; }
set { query = PrepareQuery(value); }
}
///
/// Gets or sets the document type tags to include in the search, or null.
///
public string[] DocumentTypeTags {
get { return documentTypeTags; }
set { documentTypeTags = value; }
}
///
/// Gets or sets the search options.
///
public SearchOptions Options {
get { return options; }
set { options = value; }
}
}
}