public bool Validate(TypeMoniker parameterType, FacetMoniker moniker) { var sourceType = ExtraDataType == null ? parameterType.AssemblyQualifiedName : ExtraDataType.AssemblyQualifiedName; var data = moniker[sourceType, Key]; if (data == null) { return(AllowMissing); } if ((RegularExpression == null) == (ValidValues == null)) { throw new InvalidOperationException("One and only one of ValidValues or RegularExpression must be non-null"); } if (ValidValues != null) { return(ValidValues.Contains(data, StringComparer.CurrentCultureIgnoreCase)); } return(Regex.IsMatch(data, RegularExpression, RegexOptions.IgnoreCase)); }
public override int GetHashCode() { unchecked { return((Range.GetHashCode() * 397) ^ FacetMoniker.GetHashCode()); } }
internal LoaderAmbientFacet(LoaderExtension extension, Guid ambientToken, IFacet facet, FacetMoniker moniker) : base(extension) { AmbientToken = ambientToken; IFacet = facet; Moniker = moniker; }
static ParseResult CreateTextFacetResult(ParseInput input, ParseRange range) { var textForRange = input.GetTextForRange(range).Trim(); var moniker = new FacetMoniker(typeof(EngineFacetsFactory), typeof(TextFacet), textForRange, textForRange); return(new ParseResult(input, range, moniker, ParseResult.ExactMatchRelevance)); }
public ParseResult(ParseInput input, ParseRange range, FacetMoniker facetMoniker, double relevance, bool isSuggestion = false) { if (input == null) { throw new ArgumentNullException("input"); } if (range == null) { throw new ArgumentNullException("range"); } if (facetMoniker == null) { throw new ArgumentNullException("facetMoniker"); } if (relevance < 0 || relevance > 1) { throw new ArgumentOutOfRangeException("relevance", "must be between 0 and 1, inclusive"); } Input = input; Range = range; CompressedRange = input.ToCompressed(Range); FacetMoniker = facetMoniker; Relevance = relevance; IsSuggestion = isSuggestion; }
public override string ToString() { if (!IsSpecified) { return("Unspecified"); } return(FacetMoniker.ToString()); }
public override int GetHashCode() { if (!IsSpecified) { return(0); } return(FacetMoniker.GetHashCode()); }
public override IFacet CreateFacet(FacetMoniker moniker) { switch (moniker.FactoryData) { case "downloads": return(new FileSystemItemFacet(@"c:\users\ben\downloads", "Firefox Downloads Folder")); } return(null); }
public static LoaderFacetFactory GetFactory(this FacetMoniker moniker) { if (moniker.IsAmbient) { return(null); } return(Loader.FacetFactories .Where(x => x.Type == moniker.FactoryType) .SingleOrDefault()); }
internal IFacet CreateFacet(FacetMoniker moniker) { var facet = Factory.CreateFacet(moniker); if (facet != null) { facet.Initialize(Extension.Hooks); } return(facet); }
static string SerializeExtraData(FacetMoniker moniker) { var join = from ed in moniker.ExtraData let ekey = Uri.EscapeDataString(ed.Key) let evalue = Uri.EscapeDataString(ed.Value) let facetType = Loader.GetFacetInfo(ed.FacetType) where facetType != null select String.Format("{0}={1}={2}", facetType.DatabaseId, ekey, evalue); return(@join.JoinStrings("&")); }
public override IFacet CreateFacet(FacetMoniker moniker) { try { var ci = ContactInfo.FromXml(moniker.FactoryData); return(new ContactFacet(ci.DisplayName, ci.Email)); } catch (Exception) { return(null); } }
public override IFacet CreateFacet(FacetMoniker moniker) { try { var builder = DateTimeBuilder.FromXml(moniker.FactoryData); return(new DateTimeFacet(builder.DateTime, builder.Type)); } catch (Exception) { } return(null); }
public override bool CanCreateFacet(FacetMoniker moniker) { try { DateTimeBuilder.FromXml(moniker.FactoryData); return(true); } catch (Exception) { } return(false); }
public static IFacet CreateFacet(this FacetMoniker moniker) { if (moniker.IsAmbient) { return(Loader.AmbientFacets .Where(x => x.AmbientToken == moniker.AmbientToken.Value) .Select(x => x.IFacet) .SingleOrDefault()); } var factory = moniker.GetFactory(); return(factory == null ? null : factory.CreateFacet(moniker)); }
public override bool CanCreateFacet(FacetMoniker moniker) { try { // TODO: this is not fast ContactInfo.FromXml(moniker.FactoryData); return(true); } catch (Exception) { } return(false); }
internal static double ScoreSimilarity(FacetMoniker left, FacetMoniker right) { var score = 0.0; if (right.IsAmbient != left.IsAmbient) { return(score); } if (right.FacetType == left.FacetType) { score += 0.5; } if (right.IsAmbient) { // both ambient if (right.AmbientToken == left.AmbientToken) { score += 0.5; } return(score); } if (right.FactoryType == left.FactoryType) { score += 0.25; } const double closenessMax = 0.25; if (left.FactoryData == right.FactoryData) { score += closenessMax; } else { var dist = (double)StringUtil.LevenshteinDistance(left.FactoryData, right.FactoryData); var closenessScore = closenessMax * (dist / Math.Max(right.FactoryData.Length, left.FactoryData.Length)); score += closenessScore; } return(score); }
public FacetUsage(FacetMoniker moniker, Command command, int ordinal, DateTime at, string matchedText) { if (moniker == null) { throw new ArgumentNullException("moniker"); } if (matchedText == null) { throw new ArgumentNullException("matchedText"); } Moniker = moniker; Command = command; At = at; MatchedText = matchedText; Ordinal = ordinal; }
protected override IEnumerable <FacetMoniker> EnumerateIndexImpl() { var userItems = Directory.EnumerateFileSystemEntries(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "*.lnk", SearchOption.AllDirectories); var commonItems = Directory.EnumerateFileSystemEntries(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu), "*.lnk", SearchOption.AllDirectories); foreach (var path in userItems.Concat(commonItems)) { //Icon icon; //string displayName; var moniker = new FacetMoniker(GetType(), typeof(FileSystemItemFacet), path, Path.GetFileNameWithoutExtension(path), sourceName: "Start Menu", extraData: FacetExtraData.BeginWith(typeof(IFileSystemItemFacet), "Type", "Program"), iconPath: null); yield return(moniker); } }
public static FacetMoniker MaterializeMoniker(IDataRecord record) { if (record == null) { throw new ArgumentNullException("record"); } if (record.IsDBNull("FactoryTypeId")) { // ambient facet var ambient = Loader.GetAmbientFacet(Guid.Parse((string)record["FactoryData"])); return(ambient == null ? null : ambient.Moniker); } var facetType = Loader.GetFacetInfo((int)record["FacetTypeId"]); var factory = Loader.GetFactoryInfo((int)record["FactoryTypeId"]); if (facetType == null || factory == null) { return(null); } var edCol = record["ExtraData"]; var extraData = edCol == DBNull.Value ? null : DeserializeExtraData((string)edCol); var moniker = new FacetMoniker(factory.Type, facetType.Type, (string)record["FactoryData"], (string)record["DisplayName"], record.ValOrDefault <DateTime?>("DateTime"), record.ValOrDefault <string>("Source"), null, extraData); return(factory.Factory.CanCreateFacet(moniker) ? moniker : null); }
ParseResult CreateParseResult(ParseInput input, ParseRange range, bool isSuggestion, string path, double relevance, bool isDirectory) { var type = "Folder"; if (!isDirectory) { switch (Path.GetExtension(path).ToLower()) { case ".lnk": case ".exe": type = "Program"; break; default: type = "File"; break; } } var moniker = new FacetMoniker(GetType(), typeof(FileSystemItemFacet), path, path, extraData: FacetExtraData.BeginWith(typeof(IFileSystemItemFacet), "Type", type), iconPath: null); return(new ParseResult(input, range, moniker, relevance, isSuggestion)); }
public override IFacet CreateFacet(FacetMoniker moniker) { return(new FileSystemItemFacet(moniker.FactoryData, moniker.DisplayName)); }
public override bool CanCreateFacet(FacetMoniker moniker) { return(true); }
public static void SetFacetMonikerAlias(FacetMoniker moniker, string alias) { FacetIndex.SaveFacetMoniker(moniker, alias); }
internal CommandArgument(FacetMoniker facetMoniker, IEnumerable <FacetMoniker> orderedSuggestions = null) { Source = CommandArgumentSource.Suggested; FacetMoniker = facetMoniker; InitSuggestions(orderedSuggestions); }
public bool IsUsableAsArgument(FacetMoniker moniker) { return(TypeDescriptor.Get(moniker.FacetType).Implements(Type) && _filters.All(x => x.Validate(Type, moniker))); }
internal IFacet CreateFacet() { return(IsSpecified ? FacetMoniker.CreateFacet() : null); }
public static int?SaveFacetMoniker(FacetMoniker moniker, string alias = DontSetAlias, SqlCeConnection connection = null, SqlCeTransaction trans = null) { var localConnection = connection ?? DatabaseUtil.GetConnection(); try { var facetTypeId = Loader.GetFacetInfo(moniker.FacetType).DatabaseId; int? factoryTypeId = null; string factoryData; if (!moniker.IsAmbient) { factoryTypeId = Loader.GetFactoryInfo(moniker.FactoryType).DatabaseId; factoryData = moniker.FactoryData; } else { // we misuse the factorydata column for ambient facets because it simplifies // storage/retrieval factoryData = moniker.AmbientToken.ToString(); } using (var cmd = new SqlCeCommand("FacetMonikers", localConnection, trans)) { cmd.IndexName = "UQ_FacetMonikers"; cmd.CommandType = CommandType.TableDirect; using (var rs = cmd.ExecuteResultSet(ResultSetOptions.Updatable | ResultSetOptions.Scrollable)) { if (rs.Seek(DbSeekOptions.FirstEqual, factoryTypeId, facetTypeId, moniker.HashString) && rs.Read()) { if (alias != DontSetAlias) { var aliasOrdinal = rs.GetOrdinal("Alias"); if (rs.IsDBNull(aliasOrdinal) != (alias == null) || rs[aliasOrdinal] as string != alias) { rs.SetString(aliasOrdinal, alias); rs.Update(); } } return((int)rs["Id"]); } var record = rs.CreateRecord(); record["FacetTypeId"] = facetTypeId; record["FactoryTypeId"] = factoryTypeId; record["DisplayName"] = moniker.DisplayName; record["FactoryDataHash"] = moniker.HashString; record["FactoryData"] = factoryData; record["Source"] = moniker.Source; record["Alias"] = alias; if (moniker.DateTime != null) { record["DateTime"] = moniker.DateTime.Value; } if (moniker.ExtraData != null) { record["ExtraData"] = SerializeExtraData(moniker); } rs.Insert(record, DbInsertOptions.PositionOnInsertedRow); return((int)rs["Id"]); } } } finally { if (connection == null) { localConnection.Dispose(); } } }
protected override IEnumerable <ParseResult> ParseImpl(ParseInput input, ParseMode mode, IList <Type> facetTypes) { var builder = new DateTimeBuilder(); ParseInputTerm termMin = null; ParseInputTerm termMax = null; ParseInputTerm termCurrent = null; Func <bool> updateTermIndexes = () => { if (termMin == null) { termMin = termCurrent; } if (termMax != null && termMax != termCurrent && termMax.Ordinal != termCurrent.Ordinal - 1) { return(false); } termMax = termCurrent; return(true); }; Func <Group, Action <string>, bool> parseGroup = (grp, parse) => { if (grp.Success) { if (!updateTermIndexes()) { return(false); } parse(grp.Value); } return(true); }; var gotAmPm = false; foreach (var t in input.Terms) { termCurrent = t; foreach (var match in from r in s_regexes let m = r.Match(termCurrent.Text) where m.Success select m) { if (!builder.Hour.HasValue && !parseGroup(match.Groups["hour"], val => builder.Hour = int.Parse(val))) { break; } if (!builder.Minute.HasValue && !parseGroup(match.Groups["minute"], val => builder.Minute = int.Parse(val))) { break; } if (!gotAmPm && !parseGroup(match.Groups["ampm"], val => { gotAmPm = true; builder.Hour += char.ToLower(val[0]) == 'p' ? 12 : 0; })) { break; } //var hourminute = match.Groups["hourminute"]; //if (hourminute.Success) //{} if (!parseGroup(match.Groups["relativeday"], val => { switch (val) { case "yesterday": builder.SetDayFrom(DateTime.Today.AddDays(-1)); break; case "today": builder.SetDayFrom(DateTime.Today); break; case "tomorrow": builder.SetDayFrom(DateTime.Today.AddDays(1)); break; } })) { break; } } } if (builder.Type != 0) { var moniker = new FacetMoniker(GetType(), typeof(DateTimeFacet), builder.ToXml(), builder.ToString(), extraData: FacetExtraData.BeginWith(typeof(IDateTimeFacet), "Type", builder.Type.ToString()), iconPath: null); var parseResult = new ParseResult(input, input.GetTermsRange(termMin, termMax), moniker, 1.0); return(new[] { parseResult }); } return(null); }
public FacetMonikerInfo(FacetMoniker moniker) { Moniker = moniker; }