/** * Returns the number of matching characters, zero for no match. * <p> * This method is called to check for a match. * The parameter <code>pos</code> represents the current position to be * checked in the string <code>buffer</code> (a character array which must * not be changed). * The API guarantees that <code>pos</code> is a valid index for <code>buffer</code>. * <p> * The matching code may check one character or many. * It may check characters preceeding <code>pos</code> as well as those after. * <p> * It must return zero for no match, or a positive number if a match was found. * The number indicates the number of characters that matched. * * @param buffer the text content to match against, do not change * @param pos the starting position for the match, valid for buffer * @return the number of matching characters, zero for no match * @since 2.4 */ public int isMatch(char[] buffer, int pos) { return isMatch(buffer, pos, 0, buffer.length); }
/** * Sets the field delimiter matcher. * <p> * The delimitier is used to separate one token from another. * * @param delim the delimiter matcher to use * @return this, to enable chaining */ public StrTokenizer setDelimiterMatcher(StrMatcher delim) { if (delim == null) { this.delimMatcher = StrMatcher.noneMatcher(); } else { this.delimMatcher = delim; } return this; }
/** * Sets the field delimiter character. * * @param delim the delimiter character to use * @return this, to enable chaining */ public StrTokenizer setDelimiterChar(char delim) { return setDelimiterMatcher(StrMatcher.charMatcher(delim)); }
/** * Consume whitespace from the current parse position. * * @param pattern String to read * @param pos current position */ private void seekNonWs(String pattern, ParsePosition pos) { int len = 0; char[] buffer = pattern.toCharArray(); do { len = StrMatcher.splitMatcher().isMatch(buffer, pos.getIndex()); pos.setIndex(pos.getIndex() + len); } while (len > 0 && pos.getIndex() < pattern.length()); }
/** * Sets the field delimiter string. * * @param delim the delimiter string to use * @return this, to enable chaining */ public StrTokenizer setDelimiterString(String delim) { return setDelimiterMatcher(StrMatcher.stringMatcher(delim)); }
public QuotedStringTokenizer(char[] chars, int pos) { this.string = chars; this.pos = pos; buffer = new ArrayDeque<String>(); delimiterMatcher = StrMatcher.trimMatcher(); quoteMatcher = StrMatcher.quoteMatcher(); whitespaceMatcher = StrMatcher.trimMatcher(); quashDelimiters=true; readNext(); }
private LookupTokeniser(boolean spaceIsSeparator) { if (spaceIsSeparator) { charSetMatcher = StrMatcher.charSetMatcher(" \n\t,"); } else { charSetMatcher = StrMatcher.charSetMatcher("\n\t,"); } }
/** * Transform an input string into a list of identifiers. * @param input A comma, new line, or tab delimited set of identifiers, * with optional double quoting. * @return A list of identifiers. */ public List<String> tokenise(String input) { List<String> ret = new LinkedList<String>(); StrTokenizer tokeniser = new StrTokenizer(input, charSetMatcher); tokeniser.setQuoteChar('"'); tokeniser.setIgnoreEmptyTokens(true); tokeniser.setTrimmerMatcher(StrMatcher.trimMatcher()); while (tokeniser.hasNext()) { String token = tokeniser.nextToken().trim(); ret.add(token); } return ret; } }
/** * Consume whitespace from the current parse position. * * @param pattern String to read * @param pos current position */ private void seekNonWs(String pattern, ParsePosition pos) { int len = 0; char[] buffer = pattern.toCharArray(); do { len = StrMatcher.splitMatcher().isMatch(buffer, pos.getIndex()); pos.setIndex(pos.getIndex() + len); } while (len > 0 && pos.getIndex() < pattern.length()); }
/** * Sets the variable suffix to use. * <p> * The variable suffix is the character or characters that identify the * end of a variable. This method allows a string suffix to be easily set. * * @param suffix the suffix for variables, not null * @return this, to enable chaining * @throws IllegalArgumentException if the suffix is null */ public StrSubstitutor setVariableSuffix(String suffix) { if (suffix == null) { throw new IllegalArgumentException("Variable suffix must not be null!"); } return setVariableSuffixMatcher(StrMatcher.stringMatcher(suffix)); }
private LookupTokeniser(boolean spaceIsSeparator) { if (spaceIsSeparator) { charSetMatcher = StrMatcher.charSetMatcher(" \n\t,"); } else { charSetMatcher = StrMatcher.charSetMatcher("\n\t,"); } }
/** * Transform an input string into a list of identifiers. * @param input A comma, new line, or tab delimited set of identifiers, * with optional double quoting. * @return A list of identifiers. */ public List<String> tokenise(String input) { List<String> ret = new LinkedList<String>(); StrTokenizer tokeniser = new StrTokenizer(input, charSetMatcher); tokeniser.setQuoteChar('"'); tokeniser.setIgnoreEmptyTokens(true); tokeniser.setTrimmerMatcher(StrMatcher.trimMatcher()); while (tokeniser.hasNext()) { String token = tokeniser.nextToken().trim(); ret.add(token); } return ret; } }
/** * Searches the string builder using the matcher to find the first * match searching from the given index. * <p> * Matchers can be used to perform advanced searching behaviour. * For example you could write a matcher to find the character 'a' * followed by a number. * * @param matcher the matcher to use, null returns -1 * @param startIndex the index to start at, invalid index rounded to edge * @return the first index matched, or -1 if not found */ public int indexOf(StrMatcher matcher, int startIndex) { startIndex = (startIndex < 0 ? 0 : startIndex); if (matcher == null || startIndex >= size) { return -1; } int len = size; char[] buf = buffer; for (int i = startIndex; i < len; i++) { if (matcher.isMatch(buf, i, startIndex, len) > 0) { return i; } } return -1; }
/** * Set the character to ignore. * <p> * This character is ignored when parsing the String, unless it is * within a quoted region. * * @param ignored the ignored character to use * @return this, to enable chaining */ public StrTokenizer setIgnoredChar(char ignored) { return setIgnoredMatcher(StrMatcher.charMatcher(ignored)); }
/** * Consume whitespace from the current parse position. * * @param pattern String to read * @param pos current position */ private void seekNonWs(String pattern, ParsePosition pos) { int len = 0; char[] buffer = pattern.toCharArray(); do { len = StrMatcher.splitMatcher().isMatch(buffer, pos.getIndex()); pos.setIndex(pos.getIndex() + len); } while (len > 0 && pos.getIndex() < pattern.length()); }
/** * Sets the variable prefix to use. * <p> * The variable prefix is the characer or characters that identify the * start of a variable. This method allows a string prefix to be easily set. * * @param prefix the prefix for variables, not null * @return this, to enable chaining * @throws IllegalArgumentException if the prefix is null */ public StrSubstitutor setVariablePrefix(String prefix) { if (prefix == null) { throw new IllegalArgumentException("Variable prefix must not be null!"); } return setVariablePrefixMatcher(StrMatcher.stringMatcher(prefix)); }
/** * Sets the field delimiter matcher. * <p> * The delimitier is used to separate one token from another. * * @param delim the delimiter matcher to use * @return this, to enable chaining */ public StrTokenizer setDelimiterMatcher(StrMatcher delim) { if (delim == null) { this.delimMatcher = StrMatcher.noneMatcher(); } else { this.delimMatcher = delim; } return this; }
public List<String> split(String input) { List<String> statements = new ArrayList<String>(); StrBuilder currentSql = new StrBuilder(); StrTokenizer lineTokenizer = new StrTokenizer(input); lineTokenizer.setDelimiterMatcher(StrMatcher.charSetMatcher("\r\n")); for (String line : lineTokenizer.getTokenArray()) { String strippedLine = StringUtils.stripEnd(line, null); if (!currentSql.isEmpty()) { currentSql.append(lineEnding.get()); } currentSql.append(strippedLine); if (delimiterType.matches(strippedLine, delimiter)) { statements.add(currentSql.substring(0, currentSql.length() - delimiter.length())); currentSql.clear(); } } if (!currentSql.isEmpty()) { statements.add(currentSql.toString()); } return statements; }
/** * Searches the string builder using the matcher to find the last * match searching from the given index. * <p> * Matchers can be used to perform advanced searching behaviour. * For example you could write a matcher to find the character 'a' * followed by a number. * * @param matcher the matcher to use, null returns -1 * @param startIndex the index to start at, invalid index rounded to edge * @return the last index matched, or -1 if not found */ public int lastIndexOf(StrMatcher matcher, int startIndex) { startIndex = (startIndex >= size ? size - 1 : startIndex); if (matcher == null || startIndex < 0) { return -1; } char[] buf = buffer; int endIndex = startIndex + 1; for (int i = startIndex; i >= 0; i--) { if (matcher.isMatch(buf, i, 0, endIndex) > 0) { return i; } } return -1; }
/** * Sets the quote character to use. * <p> * The quote character is used to wrap data between the tokens. * This enables delimiters to be entered as data. * * @param quote the quote character to use * @return this, to enable chaining */ public StrTokenizer setQuoteChar(char quote) { return setQuoteMatcher(StrMatcher.charMatcher(quote)); }