public void NotSoSimpleWithDelimiters() { string s = "The lazy... I don't know "; StringTokenizer st = new StringTokenizer(s, " .", true); IEnumerator enumerator = st.GetEnumerator(); enumerator.MoveNext(); Assert.AreEqual("The", (string) enumerator.Current, "Can't get first token"); enumerator.MoveNext(); Assert.AreEqual(" ", (string) enumerator.Current, "Can't get first space"); enumerator.MoveNext(); Assert.AreEqual("lazy", (string) enumerator.Current, "Can't get second token"); enumerator.MoveNext(); Assert.AreEqual(".", (string) enumerator.Current, "Can't get first dot"); enumerator.MoveNext(); Assert.AreEqual(".", (string) enumerator.Current, "Can't get second dot"); enumerator.MoveNext(); Assert.AreEqual(".", (string) enumerator.Current, "Can't get third dot"); enumerator.MoveNext(); Assert.AreEqual(" ", (string) enumerator.Current, "Can't get second space"); enumerator.MoveNext(); Assert.AreEqual("I", (string) enumerator.Current, "Can't get third token"); enumerator.MoveNext(); Assert.AreEqual(" ", (string) enumerator.Current, "Can't get third space"); enumerator.MoveNext(); Assert.AreEqual("don't", (string) enumerator.Current, "Can't get fourth token"); enumerator.MoveNext(); Assert.AreEqual(" ", (string) enumerator.Current, "Can't get fourth space"); enumerator.MoveNext(); Assert.AreEqual("know", (string) enumerator.Current, "Can't get fifth token"); enumerator.MoveNext(); Assert.AreEqual(" ", (string) enumerator.Current, "Can't get last space"); Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens"); }
public void SimpleStringWithoutDelimiters() { string s = "Hello world!"; StringTokenizer st = new StringTokenizer(s); IEnumerator enumerator = st.GetEnumerator(); enumerator.MoveNext(); Assert.AreEqual("Hello", (string) enumerator.Current, "Can't get first token"); enumerator.MoveNext(); Assert.AreEqual("world!", (string) enumerator.Current, "Can't get second token"); Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens"); }
/// <summary> /// /// </summary> /// <param name="property"></param> /// <param name="delim"></param> /// <param name="properties"></param> /// <returns></returns> public static IDictionary ToDictionary( string property, string delim, IDictionary properties ) { IDictionary map = new Hashtable(); string propValue = ( string ) properties[ property ]; if( propValue != null ) { StringTokenizer tokens = new StringTokenizer( propValue, delim, false ); IEnumerator en = tokens.GetEnumerator(); while( en.MoveNext() ) { string key = ( string ) en.Current; string value = en.MoveNext() ? ( string ) en.Current : String.Empty; map[ key ] = value; } } return map; }
public static IDictionary<string, string> ToDictionary(string property, string delim, IDictionary<string, string> properties) { IDictionary<string, string> map = new Dictionary<string, string>(); if (properties.ContainsKey(property)) { string propValue = properties[property]; StringTokenizer tokens = new StringTokenizer(propValue, delim, false); IEnumerator<string> en = tokens.GetEnumerator(); while (en.MoveNext()) { string key = en.Current; string value = en.MoveNext() ? en.Current : String.Empty; map[key] = value; } } return map; }
public static IDictionary <string, string> ToDictionary(string property, string delim, IDictionary <string, string> properties) { IDictionary <string, string> map = new Dictionary <string, string>(); if (properties.ContainsKey(property)) { string propValue = properties[property]; StringTokenizer tokens = new StringTokenizer(propValue, delim, false); IEnumerator <string> en = tokens.GetEnumerator(); while (en.MoveNext()) { string key = en.Current; string value = en.MoveNext() ? en.Current : String.Empty; map[key] = value; } } return(map); }
/// <summary> /// /// </summary> /// <param name="property"></param> /// <param name="delim"></param> /// <param name="properties"></param> /// <returns></returns> public static IDictionary ToDictionary(string property, string delim, IDictionary properties) { IDictionary map = new Hashtable(); string propValue = ( string )properties[property]; if (propValue != null) { StringTokenizer tokens = new StringTokenizer(propValue, delim, false); IEnumerator en = tokens.GetEnumerator(); while (en.MoveNext()) { string key = ( string )en.Current; string value = en.MoveNext() ? ( string )en.Current : String.Empty; map[key] = value; } } return(map); }
public static IDictionary <string, string> ToDictionary(string property, string delim, IDictionary <string, string> properties) { IDictionary <string, string> map = new Dictionary <string, string>(); string propValue; if (properties.TryGetValue(property, out propValue)) { var tokens = new StringTokenizer(propValue, delim, false); using (var en = tokens.GetEnumerator()) { while (en.MoveNext()) { var key = en.Current; var value = en.MoveNext() ? en.Current : string.Empty; map[key] = value; } } } return(map); }
private string Replace(string message, object entity) { var tokens = new StringTokenizer(message, "#${}", true); var buf = new StringBuilder(100); var escaped = false; var el = false; var isMember = false; IEnumerator ie = tokens.GetEnumerator(); while (ie.MoveNext()) { string token = (string) ie.Current; if (!escaped && "#".Equals(token)) { el = true; } if(!el && ("$".Equals(token))) { isMember = true; } else if("}".Equals(token) && isMember) { isMember = false; } if (!el && "{".Equals(token)) { escaped = true; } else if (escaped && "}".Equals(token)) { escaped = false; } else if (!escaped) { if ("{".Equals(token)) { el = false; } if(!"$".Equals(token)) buf.Append(token); } else if(!isMember) { object variable; if (attributeParameters.TryGetValue(token.ToLowerInvariant(), out variable)) { buf.Append(variable); } else { string _string = null; try { _string = messageBundle != null ? messageBundle.GetString(token, culture) : null; } catch (MissingManifestResourceException) { //give a second chance with the default resource bundle } if (_string == null) { _string = defaultMessageBundle.GetString(token, culture); // in this case we don't catch the MissingManifestResourceException because // we are sure that we DefaultValidatorMessages.resx is an embedded resource } if (_string == null) { buf.Append('{').Append(token).Append('}'); } else { buf.Append(Replace(_string,entity)); } } } else { ReplaceValue(buf, entity, token); } } return buf.ToString(); }
public static string RenderWhereStringTemplate(string sqlWhereString, string placeholder, Dialect.Dialect dialect, SQLFunctionRegistry functionRegistry) { //TODO: make this a bit nicer string symbols = new StringBuilder() .Append("=><!+-*/()',|&`") .Append(ParserHelper.Whitespace) .Append(dialect.OpenQuote) .Append(dialect.CloseQuote) .ToString(); StringTokenizer tokens = new StringTokenizer(sqlWhereString, symbols, true); StringBuilder result = new StringBuilder(); bool quoted = false; bool quotedIdentifier = false; bool beforeTable = false; bool inFromClause = false; bool afterFromTable = false; IEnumerator tokensEnum = tokens.GetEnumerator(); bool hasMore = tokensEnum.MoveNext(); string nextToken = hasMore ? (string) tokensEnum.Current : null; while (hasMore) { string token = nextToken; string lcToken = token.ToLower(CultureInfo.InvariantCulture); hasMore = tokensEnum.MoveNext(); nextToken = hasMore ? (string) tokensEnum.Current : null; bool isQuoteCharacter = false; if (!quotedIdentifier && "'".Equals(token)) { quoted = !quoted; isQuoteCharacter = true; } if (!quoted) { bool isOpenQuote; if ("`".Equals(token)) { isOpenQuote = !quotedIdentifier; token = lcToken = isOpenQuote ? dialect.OpenQuote.ToString() : dialect.CloseQuote.ToString(); quotedIdentifier = isOpenQuote; isQuoteCharacter = true; } else if (!quotedIdentifier && (dialect.OpenQuote == token[0])) { isOpenQuote = true; quotedIdentifier = true; isQuoteCharacter = true; } else if (quotedIdentifier && (dialect.CloseQuote == token[0])) { quotedIdentifier = false; isQuoteCharacter = true; isOpenQuote = false; } else { isOpenQuote = false; } if (isOpenQuote) { result.Append(placeholder).Append('.'); } } bool quotedOrWhitespace = quoted || quotedIdentifier || isQuoteCharacter || char.IsWhiteSpace(token[0]); if (quotedOrWhitespace) { result.Append(token); } else if (beforeTable) { result.Append(token); beforeTable = false; afterFromTable = true; } else if (afterFromTable) { if (!"as".Equals(lcToken)) afterFromTable = false; result.Append(token); } else if (IsNamedParameter(token)) { result.Append(token); } else if ( IsIdentifier(token, dialect) && !IsFunctionOrKeyword(lcToken, nextToken, dialect, functionRegistry) ) { result.Append(placeholder) .Append('.') .Append(token); } else { if (BeforeTableKeywords.Contains(lcToken)) { beforeTable = true; inFromClause = true; } else if (inFromClause && ",".Equals(lcToken)) { beforeTable = true; } result.Append(token); } if ( //Yuck: inFromClause && Keywords.Contains(lcToken) && //"as" is not in Keywords !BeforeTableKeywords.Contains(lcToken) ) { inFromClause = false; } } return result.ToString(); }
public static string RenderOrderByStringTemplate(string sqlOrderByString, Dialect.Dialect dialect, SQLFunctionRegistry functionRegistry) { //TODO: make this a bit nicer string symbols = new StringBuilder() .Append("=><!+-*/()',|&`") .Append(ParserHelper.Whitespace) .Append(dialect.OpenQuote) .Append(dialect.CloseQuote) .ToString(); StringTokenizer tokens = new StringTokenizer(sqlOrderByString, symbols, true); StringBuilder result = new StringBuilder(); bool quoted = false; bool quotedIdentifier = false; IEnumerator<string> tokensEnum = tokens.GetEnumerator(); bool hasMore = tokensEnum.MoveNext(); string nextToken = hasMore ? tokensEnum.Current : null; while (hasMore) { string token = nextToken; string lcToken = token.ToLowerInvariant(); hasMore = tokensEnum.MoveNext(); nextToken = hasMore ? tokensEnum.Current : null; bool isQuoteCharacter = false; if (!quotedIdentifier && "'".Equals(token)) { quoted = !quoted; isQuoteCharacter = true; } if (!quoted) { bool isOpenQuote; if ("`".Equals(token)) { isOpenQuote = !quotedIdentifier; token = lcToken = isOpenQuote ? dialect.OpenQuote.ToString() : dialect.CloseQuote.ToString(); quotedIdentifier = isOpenQuote; isQuoteCharacter = true; } else if (!quotedIdentifier && (dialect.OpenQuote == token[0])) { isOpenQuote = true; quotedIdentifier = true; isQuoteCharacter = true; } else if (quotedIdentifier && (dialect.CloseQuote == token[0])) { quotedIdentifier = false; isQuoteCharacter = true; isOpenQuote = false; } else { isOpenQuote = false; } if (isOpenQuote) { result.Append(Placeholder).Append('.'); } } bool quotedOrWhitespace = quoted || quotedIdentifier || isQuoteCharacter || char.IsWhiteSpace(token[0]); if (quotedOrWhitespace) { result.Append(token); } else if ( IsIdentifier(token, dialect) && !IsFunctionOrKeyword(lcToken, nextToken, dialect, functionRegistry) ) { result.Append(Placeholder) .Append('.') .Append(token); } else { result.Append(token); } } return result.ToString(); }
public void OnlyDelimitersWithoutDelimiters() { string s = " .,,,"; StringTokenizer st = new StringTokenizer(s, " ,.", false); IEnumerator enumerator = st.GetEnumerator(); Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens"); }
public void OnlyDelimitersWithDelimiters() { string s = " .,,,"; StringTokenizer st = new StringTokenizer(s, " ,.", true); IEnumerator enumerator = st.GetEnumerator(); enumerator.MoveNext(); Assert.AreEqual(" ", (string) enumerator.Current, "Can't get first delimiter"); enumerator.MoveNext(); Assert.AreEqual(".", (string) enumerator.Current, "Can't get second delimiter"); enumerator.MoveNext(); Assert.AreEqual(",", (string) enumerator.Current, "Can't get third delimiter"); enumerator.MoveNext(); Assert.AreEqual(",", (string) enumerator.Current, "Can't get fourth delimiter"); enumerator.MoveNext(); Assert.AreEqual(",", (string) enumerator.Current, "Can't get fifth delimiter"); Assert.AreEqual(false, enumerator.MoveNext(), "Still thinking there are more tokens"); }
private IActor ResolveArgument(IActor resolvedActor, String expression, int index) { String argument = null; String whiteSpace = " ".Substring(0, index); String[] parameters = null; int argumentEndIndex = expression.IndexOf("->", index); if (argumentEndIndex == -1) { argumentEndIndex = expression.Length; } int parametersStartIndex = expression.IndexOf("(", index); int parametersEndIndex = -1; if ((parametersStartIndex != -1) && (parametersStartIndex < argumentEndIndex)) { argument = expression.Substring(index, (parametersStartIndex) - (index)).Trim(); parametersEndIndex = expression.IndexOf(")", parametersStartIndex + 1); if (parametersEndIndex > argumentEndIndex) { throw new SystemException("can't resolve assigner expression : couldn't find closing bracket for bracket on index '" + parametersStartIndex + "' in expression '" + expression + "'"); } // the next exception happens when a parameter contains a right bracket. String shouldBewhiteSpace = expression.Substring(parametersEndIndex + 1, (argumentEndIndex) - (parametersEndIndex + 1)); if (!"".Equals(shouldBewhiteSpace.Trim())) { throw new SystemException("can't resolve assigner expression : only whitespace allowed between closing bracket of the parameterlist of an argument and the end of the argument : closing bracket position '" + parametersEndIndex + "' in expression '" + expression + "'"); } String parametersText = expression.Substring(parametersStartIndex + 1, (parametersEndIndex) - (parametersStartIndex + 1)); ArrayList parameterList = new ArrayList(); StringTokenizer tokenizer = new StringTokenizer(parametersText, ","); IEnumerator tokenEnum = tokenizer.GetEnumerator(); while (tokenEnum.MoveNext()) { parameterList.Add(tokenEnum.Current.ToString().Trim()); } if (parameterList.Count > 0) { parameters = new String[parameterList.Count]; parameters = (String[])parameterList.ToArray(typeof(String)); } else { parameters = new String[0]; } } else { argument = expression.Substring(index, (argumentEndIndex) - (index)).Trim(); parameters = new String[0]; } if ("".Equals(argument)) { throw new SystemException("can't resolve assigner expression : can't resolve empty argument on index '" + index + "' for expression '" + expression + "'"); } String methodName = "ResolveArgument" + argument.Substring(0, 1).ToUpper() + argument.Substring(1); try { MethodInfo method = this.GetType().GetMethod(methodName, (Type[])RESOLVE_METHOD_ARGUMENT_TYPES); Object[] args = new Object[] { resolvedActor, parameters }; resolvedActor = (IActor)method.Invoke(this, (Object[])args); } catch (Exception t) { throw new SystemException("can't resolve assigner expression : couldn't resolve argument '" + argument + "' : " + t.Message, t); } if (argumentEndIndex != expression.Length) { if (argumentEndIndex < expression.Length) { argumentEndIndex = expression.IndexOf("->", argumentEndIndex) + 2; } resolvedActor = ResolveArgument(resolvedActor, expression, argumentEndIndex); } return resolvedActor; }
/// <summary> /// Takes the where condition provided in the mapping attribute and iterpolates the alias. /// </summary> /// <param name="whereSql">The "where" sql statement from the mapping attribute.</param> /// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with rendering the Sql.</param> /// <returns> /// A well formed "where" sql statement for the <see cref="Dialect.Dialect"/>. /// </returns> public static string RenderWhereStringTemplate( string whereSql, Dialect.Dialect dialect ) { //TODO: make this a bit nicer. StringTokenizer tokens = new StringTokenizer( whereSql, delimiters, true ); StringBuilder result = new StringBuilder( whereSql.Length + 10 ); bool quoted = false; bool afterFrom = false; bool afterFromTable = false; IEnumerator tokensEnum = tokens.GetEnumerator(); bool hasMore = tokensEnum.MoveNext(); string nextToken = hasMore ? ( string ) tokensEnum.Current : null; while( hasMore ) { string token = nextToken; hasMore = tokensEnum.MoveNext(); nextToken = hasMore ? ( string ) tokensEnum.Current : null; // the begin or end "'" has been found if( "'".Equals( token ) ) { quoted = !quoted; } if( quoted || char.IsWhiteSpace( token[ 0 ] ) ) { result.Append( token ); } else { bool isIdentifier = token[ 0 ] == '`' || ( // allow any identifier quoted with backtick Char.IsLetter( token[ 0 ] ) && // only recognizes identifiers beginning with a letter !Keywords.Contains( token.ToLower( System.Globalization.CultureInfo.InvariantCulture ) ) && token.IndexOf( '.' ) < 0 ); if( afterFrom ) { result.Append( token ); afterFrom = false; afterFromTable = true; } else if( afterFromTable ) { afterFromTable = false; result.Append( token ); } else if( isIdentifier && ( nextToken == null || !nextToken.Equals( "(" ) ) ) // not a function call { result.Append( Placeholder ) .Append( StringHelper.Dot ) .Append( Quote( token, dialect ) ); } else { if( "from".Equals( token.ToLower( System.Globalization.CultureInfo.InvariantCulture ) ) ) { afterFrom = true; } result.Append( token ); } } } return result.ToString(); }
/// <summary> /// Takes "order by" clause in the mapping attribute and iterpolates the alias /// </summary> /// <param name="orderBySql">The "order by" sql statement from the mapping.</param> /// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with rendering the Sql.</param> /// <returns> /// A well formed "order by" sql statement for the <see cref="Dialect.Dialect"/>. /// </returns> public static string RenderOrderByStringTemplate( string orderBySql, Dialect.Dialect dialect ) { //TODO: make this a bit nicer StringTokenizer tokens = new StringTokenizer( orderBySql, ",", false ); StringBuilder result = new StringBuilder( orderBySql.Length + 2 ); bool commaNeeded = false; IEnumerator tokenEnum = tokens.GetEnumerator(); while( tokenEnum.MoveNext() ) { string column = Quote( ( ( string ) tokenEnum.Current ).Trim(), dialect ); if( commaNeeded ) { result.Append( StringHelper.CommaSpace ); } commaNeeded = true; result.Append( Placeholder ) .Append( StringHelper.Dot ) .Append( column ); } return result.ToString(); }
private void InitParameterBookKeeping() { StringTokenizer st = new StringTokenizer( queryString, ParserHelper.HqlSeparators, true ); ISet result = new HashedSet(); IEnumerator enumer = st.GetEnumerator(); while( enumer.MoveNext() ) { string str = (string) enumer.Current; if ( str.StartsWith( ParserHelper.HqlVariablePrefix ) ) { result.Add( str.Substring( 1 ) ); } } actualNamedParameters = result; // TODO: This is weak as it doesn't take account of ? embedded in the SQL positionalParameterCount = StringHelper.CountUnquoted( queryString, StringHelper.SqlParameter.ToCharArray()[0] ); }