Search in sources :

Example 1 with UnescapedCharSequence

use of org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence in project lucene-solr by apache.

the class EscapeQuerySyntaxImpl method discardEscapeChar.

/**
   * Returns a String where the escape char has been removed, or kept only once
   * if there was a double escape.
   * 
   * Supports escaped unicode characters, e. g. translates <code>A</code> to
   * <code>A</code>.
   * 
   */
public static UnescapedCharSequence discardEscapeChar(CharSequence input) throws ParseException {
    // Create char array to hold unescaped char sequence
    char[] output = new char[input.length()];
    boolean[] wasEscaped = new boolean[input.length()];
    // The length of the output can be less than the input
    // due to discarded escape chars. This variable holds
    // the actual length of the output
    int length = 0;
    // We remember whether the last processed character was
    // an escape character
    boolean lastCharWasEscapeChar = false;
    // The multiplier the current unicode digit must be multiplied with.
    // E. g. the first digit must be multiplied with 16^3, the second with
    // 16^2...
    int codePointMultiplier = 0;
    // Used to calculate the codepoint of the escaped unicode character
    int codePoint = 0;
    for (int i = 0; i < input.length(); i++) {
        char curChar = input.charAt(i);
        if (codePointMultiplier > 0) {
            codePoint += hexToInt(curChar) * codePointMultiplier;
            codePointMultiplier >>>= 4;
            if (codePointMultiplier == 0) {
                output[length++] = (char) codePoint;
                codePoint = 0;
            }
        } else if (lastCharWasEscapeChar) {
            if (curChar == 'u') {
                // found an escaped unicode character
                codePointMultiplier = 16 * 16 * 16;
            } else {
                // this character was escaped
                output[length] = curChar;
                wasEscaped[length] = true;
                length++;
            }
            lastCharWasEscapeChar = false;
        } else {
            if (curChar == '\\') {
                lastCharWasEscapeChar = true;
            } else {
                output[length] = curChar;
                length++;
            }
        }
    }
    if (codePointMultiplier > 0) {
        throw new ParseException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION));
    }
    if (lastCharWasEscapeChar) {
        throw new ParseException(new MessageImpl(QueryParserMessages.INVALID_SYNTAX_ESCAPE_CHARACTER));
    }
    return new UnescapedCharSequence(output, wasEscaped, 0, length);
}
Also used : UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl)

Example 2 with UnescapedCharSequence

use of org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence in project lucene-solr by apache.

the class OpenRangeQueryNodeProcessor method postProcessNode.

@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
    if (node instanceof TermRangeQueryNode) {
        TermRangeQueryNode rangeNode = (TermRangeQueryNode) node;
        FieldQueryNode lowerNode = rangeNode.getLowerBound();
        FieldQueryNode upperNode = rangeNode.getUpperBound();
        CharSequence lowerText = lowerNode.getText();
        CharSequence upperText = upperNode.getText();
        if (OPEN_RANGE_TOKEN.equals(upperNode.getTextAsString()) && (!(upperText instanceof UnescapedCharSequence) || !((UnescapedCharSequence) upperText).wasEscaped(0))) {
            upperText = "";
        }
        if (OPEN_RANGE_TOKEN.equals(lowerNode.getTextAsString()) && (!(lowerText instanceof UnescapedCharSequence) || !((UnescapedCharSequence) lowerText).wasEscaped(0))) {
            lowerText = "";
        }
        lowerNode.setText(lowerText);
        upperNode.setText(upperText);
    }
    return node;
}
Also used : FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)

Example 3 with UnescapedCharSequence

use of org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence in project lucene-solr by apache.

the class TestQueryTreeBuilder method testSetFieldBuilder.

@Test
public void testSetFieldBuilder() throws QueryNodeException {
    QueryTreeBuilder qtb = new QueryTreeBuilder();
    qtb.setBuilder("field", new DummyBuilder());
    Object result = qtb.build(new FieldQueryNode(new UnescapedCharSequence("field"), "foo", 0, 0));
    Assert.assertEquals("OK", result);
    // LUCENE-4890
    qtb = new QueryTreeBuilder();
    qtb.setBuilder(DummyQueryNodeInterface.class, new DummyBuilder());
    result = qtb.build(new DummyQueryNode());
    Assert.assertEquals("OK", result);
}
Also used : FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) UnescapedCharSequence(org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence) Test(org.junit.Test)

Aggregations

UnescapedCharSequence (org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence)3 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)2 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)1 TermRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode)1 Test (org.junit.Test)1