Exemplo n.º 1
0
        protected List <SearchToken> ToTokens(string searchText, SearchTextFormat format)
        {
            var _searchTokenizerService = new SearchTokenizerService <TSearchHeadEnum>();

            _searchTokenizerService.OnSearchTokenKeyValuePair = OnSearchTokenKeyValuePair;

            if (searchText == null)
            {
                searchText = string.Empty;
            }

            // Allow searches for domain\fatuser
            searchText = searchText.Replace(@"\", @"\\");

            // strip special characters that would be interpreted in neo regex
            if (format == SearchTextFormat.Unstructured && !string.IsNullOrEmpty(searchText))
            {
                var allowedPattern = "[^-a-zA-Z0-9,_'|:!\" .\\\\]+";
                searchText = Regex.Replace(searchText, allowedPattern, "", RegexOptions.Compiled);
            }

            List <SearchToken> tokens;

            try
            {
                tokens = _searchTokenizerService.ToTokens(searchText);
            }
            catch (Exception e)
            {
                var message = $"Failed to convert '{searchText}' to tokens: " + e.Message;
                throw ArtemisException.Create(message, e);
            }


            // Add the wild cards for the user
            if (format == SearchTextFormat.Unstructured)
            {
                foreach (var token in tokens)
                {
                    var tokenAsString = token as SearchStringToken;

                    //Don't turn empty into wildcard - too many hits are useless
                    //if (token.Value == "*")
                    //{
                    //    token.Value = "";
                    //}

                    if (tokenAsString != null)
                    {
                        tokenAsString.Value = $"*{tokenAsString.Value}*";
                    }
                    var tokenAsNumber = token as SearchNumberToken;
                    if (tokenAsNumber != null)
                    {
                        tokenAsNumber.Value = $"*{tokenAsNumber.Value}*";
                    }
                }
            }
            return(tokens);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Create a new Exception
 /// </summary>
 /// <remarks>
 /// Only use this exception if no suitable alternative exists - eg: use InvalidArgumentException if it is a better fit
 /// </remarks>
 public static ArtemisException Create(string message, Exception innerException)
 {
     ArtemisException exception = new ArtemisException(message, innerException);
     return exception;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Create a new Exception
 /// </summary>
 /// <remarks>
 /// Only use this exception if no suitable alternative exists - eg: use InvalidArgumentException if it is a better fit
 /// </remarks>
 public static ArtemisException Create(string message)
 {
     ArtemisException exception = new ArtemisException(message);
     return exception;
 }
Exemplo n.º 4
0
        public List <SearchToken> ToTokens(string searchText)
        {
            Token token;
            var   tokenizer = new StringTokenizer(searchText);
            var   tokenList = new List <SearchToken>();

            do
            {
                token = tokenizer.Next();

                switch (token.Kind)
                {
                case TokenKind.NotOperator:
                    token = tokenizer.Next();
                    tokenList.Add(new AntiStringToken {
                        Value = token.Value
                    });
                    break;

                case TokenKind.AttributeOperator:
                    var attributeValue = token.Value.Substring(1);
                    tokenList.Add(new SearchAttributeToken {
                        Value = attributeValue
                    });
                    break;

                case TokenKind.KeyValueOperator:
                    Token operand     = tokenizer.Next();
                    var   keyAsString = token.Value.Remove(token.Value.Length - 1).ToLower(); // strip off the trailing colon (:)

                    TSearchKeyOperator keyAsEnum;

                    if (!Enum.TryParse(keyAsString, true, out keyAsEnum))
                    {
                        var allowedKeys = ((TSearchKeyOperator[])Enum.GetValues(typeof(TSearchKeyOperator))).ToList();
                        allowedKeys.RemoveAll(m => Convert.ToInt32(m) == 0);
                        var allowedKeyCsv = string.Join(", ", allowedKeys);
                        throw ArtemisException.Create($"{keyAsString} is not a recognised search key. Valid values are: {allowedKeyCsv}");
                    }

                    var keyValueTokenPair = new SearchKeyValuePairToken <TSearchKeyOperator> {
                        Key = keyAsEnum, Value = operand.Value
                    };

                    // Allow transformation of search input values
                    if (OnSearchTokenKeyValuePair != null)
                    {
                        OnSearchTokenKeyValuePair(operand, keyValueTokenPair);
                    }

                    tokenList.Add(keyValueTokenPair);
                    break;

                case TokenKind.Number:
                    tokenList.Add(new SearchNumberToken {
                        Value = token.Value
                    });
                    break;

                case TokenKind.Word:
                case TokenKind.QuotedString:
                    tokenList.Add(new SearchStringToken {
                        Value = token.Value
                    });
                    break;
                }
            }while (token.Kind != TokenKind.EOF);

            return(tokenList);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create a new Exception
        /// </summary>
        /// <remarks>
        /// Only use this exception if no suitable alternative exists - eg: use InvalidArgumentException if it is a better fit
        /// </remarks>
        public static ArtemisException Create(string message, Exception innerException)
        {
            ArtemisException exception = new ArtemisException(message, innerException);

            return(exception);
        }