Search in sources :

Example 1 with StringMatcher

use of org.apache.commons.text.matcher.StringMatcher in project commons-text by apache.

the class StringSubstitutorTest method testReplaceInTakingTwoAndThreeIntsReturningFalse.

@Test
public void testReplaceInTakingTwoAndThreeIntsReturningFalse() {
    final Map<String, Object> hashMap = new HashMap<>();
    final StringLookup mapStringLookup = StringLookupFactory.INSTANCE.mapStringLookup(hashMap);
    final StringMatcher strMatcher = StringMatcherFactory.INSTANCE.tabMatcher();
    final StringSubstitutor strSubstitutor = new StringSubstitutor(mapStringLookup, strMatcher, strMatcher, 'b', strMatcher);
    assertFalse(strSubstitutor.replaceIn((StringBuilder) null, 1315, (-1369)));
    assertEquals('b', strSubstitutor.getEscapeChar());
    assertFalse(strSubstitutor.isPreserveEscapes());
}
Also used : HashMap(java.util.HashMap) StringLookup(org.apache.commons.text.lookup.StringLookup) StringMatcher(org.apache.commons.text.matcher.StringMatcher) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 2 with StringMatcher

use of org.apache.commons.text.matcher.StringMatcher in project commons-text by apache.

the class StringTokenizerTest method testDelimMatcher.

@Test
public void testDelimMatcher() {
    final String input = "a/b\\c";
    final StringMatcher delimMatcher = StringMatcherFactory.INSTANCE.charSetMatcher(new char[] { '/', '\\' });
    final StringTokenizer tok = new StringTokenizer(input, delimMatcher);
    assertEquals("a", tok.next());
    assertEquals("b", tok.next());
    assertEquals("c", tok.next());
    assertFalse(tok.hasNext());
}
Also used : StringMatcher(org.apache.commons.text.matcher.StringMatcher) Test(org.junit.Test)

Example 3 with StringMatcher

use of org.apache.commons.text.matcher.StringMatcher in project commons-text by apache.

the class StringTokenizerTest method testDelimMatcherQuoteMatcher.

@Test
public void testDelimMatcherQuoteMatcher() {
    final String input = "`a`;`b`;`c`";
    final StringMatcher delimMatcher = StringMatcherFactory.INSTANCE.charSetMatcher(new char[] { ';' });
    final StringMatcher quoteMatcher = StringMatcherFactory.INSTANCE.charSetMatcher(new char[] { '`' });
    final StringTokenizer tok = new StringTokenizer(input, delimMatcher, quoteMatcher);
    assertEquals("a", tok.next());
    assertEquals("b", tok.next());
    assertEquals("c", tok.next());
    assertFalse(tok.hasNext());
}
Also used : StringMatcher(org.apache.commons.text.matcher.StringMatcher) Test(org.junit.Test)

Example 4 with StringMatcher

use of org.apache.commons.text.matcher.StringMatcher in project commons-text by apache.

the class StringSubstitutor method substitute.

/**
 * Recursive handler for multiple levels of interpolation. This is the main interpolation method, which resolves the
 * values of all variable references contained in the passed in text.
 *
 * @param buf
 *            the string builder to substitute into, not null
 * @param offset
 *            the start offset within the builder, must be valid
 * @param length
 *            the length within the builder to be processed, must be valid
 * @param priorVariables
 *            the stack keeping track of the replaced variables, may be null
 * @return the length change that occurs, unless priorVariables is null when the int represents a boolean flag as to
 *         whether any change occurred.
 */
private int substitute(final TextStringBuilder buf, final int offset, final int length, List<String> priorVariables) {
    final StringMatcher pfxMatcher = getVariablePrefixMatcher();
    final StringMatcher suffMatcher = getVariableSuffixMatcher();
    final char escape = getEscapeChar();
    final StringMatcher valueDelimMatcher = getValueDelimiterMatcher();
    final boolean substitutionInVariablesEnabled = isEnableSubstitutionInVariables();
    final boolean substitutionInValuesDisabled = isDisableSubstitutionInValues();
    final boolean top = priorVariables == null;
    boolean altered = false;
    int lengthChange = 0;
    char[] chars = buf.buffer;
    int bufEnd = offset + length;
    int pos = offset;
    while (pos < bufEnd) {
        final int startMatchLen = pfxMatcher.isMatch(chars, pos, offset, bufEnd);
        if (startMatchLen == 0) {
            pos++;
        } else {
            // found variable start marker
            if (pos > offset && chars[pos - 1] == escape) {
                // escaped
                if (preserveEscapes) {
                    pos++;
                    continue;
                }
                buf.deleteCharAt(pos - 1);
                // in case buffer was altered
                chars = buf.buffer;
                lengthChange--;
                altered = true;
                bufEnd--;
            } else {
                // find suffix
                final int startPos = pos;
                pos += startMatchLen;
                int endMatchLen = 0;
                int nestedVarCount = 0;
                while (pos < bufEnd) {
                    if (substitutionInVariablesEnabled && pfxMatcher.isMatch(chars, pos, offset, bufEnd) != 0) {
                        // found a nested variable start
                        endMatchLen = pfxMatcher.isMatch(chars, pos, offset, bufEnd);
                        nestedVarCount++;
                        pos += endMatchLen;
                        continue;
                    }
                    endMatchLen = suffMatcher.isMatch(chars, pos, offset, bufEnd);
                    if (endMatchLen == 0) {
                        pos++;
                    } else {
                        // found variable end marker
                        if (nestedVarCount == 0) {
                            String varNameExpr = new String(chars, startPos + startMatchLen, pos - startPos - startMatchLen);
                            if (substitutionInVariablesEnabled) {
                                final TextStringBuilder bufName = new TextStringBuilder(varNameExpr);
                                substitute(bufName, 0, bufName.length());
                                varNameExpr = bufName.toString();
                            }
                            pos += endMatchLen;
                            final int endPos = pos;
                            String varName = varNameExpr;
                            String varDefaultValue = null;
                            if (valueDelimMatcher != null) {
                                final char[] varNameExprChars = varNameExpr.toCharArray();
                                int valueDelimiterMatchLen = 0;
                                for (int i = 0; i < varNameExprChars.length; i++) {
                                    // then stop resolving name and default value.
                                    if (!substitutionInVariablesEnabled && pfxMatcher.isMatch(varNameExprChars, i, i, varNameExprChars.length) != 0) {
                                        break;
                                    }
                                    if (valueDelimMatcher.isMatch(varNameExprChars, i, 0, varNameExprChars.length) != 0) {
                                        valueDelimiterMatchLen = valueDelimMatcher.isMatch(varNameExprChars, i, 0, varNameExprChars.length);
                                        varName = varNameExpr.substring(0, i);
                                        varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen);
                                        break;
                                    }
                                }
                            }
                            // on the first call initialize priorVariables
                            if (priorVariables == null) {
                                priorVariables = new ArrayList<>();
                                priorVariables.add(new String(chars, offset, length));
                            }
                            // handle cyclic substitution
                            checkCyclicSubstitution(varName, priorVariables);
                            priorVariables.add(varName);
                            // resolve the variable
                            String varValue = resolveVariable(varName, buf, startPos, endPos);
                            if (varValue == null) {
                                varValue = varDefaultValue;
                            }
                            if (varValue != null) {
                                final int varLen = varValue.length();
                                buf.replace(startPos, endPos, varValue);
                                altered = true;
                                int change = 0;
                                if (!substitutionInValuesDisabled) {
                                    // recursive replace
                                    change = substitute(buf, startPos, varLen, priorVariables);
                                }
                                change = change + varLen - (endPos - startPos);
                                pos += change;
                                bufEnd += change;
                                lengthChange += change;
                                // in case buffer was
                                chars = buf.buffer;
                            // altered
                            }
                            // remove variable from the cyclic stack
                            priorVariables.remove(priorVariables.size() - 1);
                            break;
                        }
                        nestedVarCount--;
                        pos += endMatchLen;
                    }
                }
            }
        }
    }
    if (top) {
        return altered ? 1 : 0;
    }
    return lengthChange;
}
Also used : StringMatcher(org.apache.commons.text.matcher.StringMatcher)

Aggregations

StringMatcher (org.apache.commons.text.matcher.StringMatcher)4 Test (org.junit.Test)3 HashMap (java.util.HashMap)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 StringLookup (org.apache.commons.text.lookup.StringLookup)1