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);
}
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;
}
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);
}
Aggregations