Apache Solr open-source search software
SOLR-8283: factor out StrParser from QueryParsing.StrParser (Christine Poerschke)
Christine Poerschkee776333c021c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex e1f1a26d7d6..06577f81024 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -532,6 +532,8 @@ Other Changes* SOLR-8300: Use constants for the /overseer_elect znode (Varun Thacker)+* SOLR-8283: factor out StrParser from QueryParsing.StrParser (Christine Poerschke)+================== 5.3.1 ==================Bug Fixesdiff --git a/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java b/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.javaindex a16a1391849..80c51b2865b 100644--- a/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java+++ b/solr/core/src/java/org/apache/solr/request/macro/MacroExpander.java@@ -18,7 +18,7 @@ package org.apache.solr.request.macro;*/import org.apache.solr.common.SolrException;-import org.apache.solr.search.QueryParsing;+import org.apache.solr.search.StrParser;import org.apache.solr.search.SyntaxError;import java.util.ArrayList;@@ -148,7 +148,7 @@ public class MacroExpander {start = rbrace + 1;// String inbetween = val.substring(idx, rbrace);- QueryParsing.StrParser parser = new QueryParsing.StrParser(val, idx, rbrace);+ StrParser parser = new StrParser(val, idx, rbrace);try {String paramName = parser.getId();String defVal = null;diff --git a/solr/core/src/java/org/apache/solr/search/FunctionQParser.java b/solr/core/src/java/org/apache/solr/search/FunctionQParser.javaindex ec6ecd7da24..beddc53ba64 100644--- a/solr/core/src/java/org/apache/solr/search/FunctionQParser.java+++ b/solr/core/src/java/org/apache/solr/search/FunctionQParser.java@@ -36,7 +36,7 @@ public class FunctionQParser extends QParser {public static final int FLAG_DEFAULT = FLAG_CONSUME_DELIMITER;/** @lucene.internal */- public QueryParsing.StrParser sp;+ public StrParser sp;boolean parseMultipleSources = true;boolean parseToEnd = true;@@ -49,7 +49,7 @@ public class FunctionQParser extends QParser {public void setString(String s) {super.setString(s);if (s != null) {- sp = new QueryParsing.StrParser( s );+ sp = new StrParser( s );}}@@ -331,7 +331,7 @@ public class FunctionQParser extends QParser {ch = val.length()==0 ? '\0' : val.charAt(0);if (ch>='0' && ch<='9' || ch=='.' || ch=='+' || ch=='-') {- QueryParsing.StrParser sp = new QueryParsing.StrParser(val);+ StrParser sp = new StrParser(val);Number num = sp.getNumber();if (num instanceof Long) {valueSource = new LongConstValueSource(num.longValue());@@ -342,7 +342,7 @@ public class FunctionQParser extends QParser {valueSource = new ConstValueSource(num.floatValue());}} else if (ch == '"' || ch == '\'') {- QueryParsing.StrParser sp = new QueryParsing.StrParser(val);+ StrParser sp = new StrParser(val);val = sp.getQuotedString();valueSource = new LiteralValueSource(val);} else {diff --git a/solr/core/src/java/org/apache/solr/search/QueryParsing.java b/solr/core/src/java/org/apache/solr/search/QueryParsing.javaindex cccffb20864..d9e2d4123d2 100644--- a/solr/core/src/java/org/apache/solr/search/QueryParsing.java+++ b/solr/core/src/java/org/apache/solr/search/QueryParsing.java@@ -49,7 +49,6 @@ import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;-import java.util.Locale;import java.util.Map;/**@@ -573,323 +572,6 @@ public class QueryParsing {}}- /**- * Simple class to help with parsing a string.- * <b>Note: This API is experimental and may change in non backward-compatible ways in the future</b>- */- public static class StrParser {- public String val;- public int pos;- public int end;-- public StrParser(String val) {- this(val, 0, val.length());- }-- public StrParser(String val, int start, int end) {- this.val = val;- this.pos = start;- this.end = end;- }-- public void eatws() {- while (pos < end && Character.isWhitespace(val.charAt(pos))) pos++;- }-- public char ch() {- return pos < end ? val.charAt(pos) : 0;- }-- public void skip(int nChars) {- pos = Math.max(pos + nChars, end);- }-- public boolean opt(String s) {- eatws();- int slen = s.length();- if (val.regionMatches(pos, s, 0, slen)) {- pos += slen;- return true;- }- return false;- }-- public boolean opt(char ch) {- eatws();- if (pos < end && val.charAt(pos) == ch) {- pos++;- return true;- }- return false;- }--- public void expect(String s) throws SyntaxError {- eatws();- int slen = s.length();- if (val.regionMatches(pos, s, 0, slen)) {- pos += slen;- } else {- throw new SyntaxError("Expected '" + s + "' at position " + pos + " in '" + val + "'");- }- }-- public float getFloat() {- eatws();- char[] arr = new char[end - pos];- int i;- for (i = 0; i < arr.length; i++) {- char ch = val.charAt(pos);- if ((ch >= '0' && ch <= '9')- || ch == '+' || ch == '-'- || ch == '.' || ch == 'e' || ch == 'E'- ) {- pos++;- arr[i] = ch;- } else {- break;- }- }-- return Float.parseFloat(new String(arr, 0, i));- }-- public Number getNumber() {- eatws();- int start = pos;- boolean flt = false;-- while (pos < end) {- char ch = val.charAt(pos);- if ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-') {- pos++;- } else if (ch == '.' || ch =='e' || ch=='E') {- flt = true;- pos++;- } else {- break;- }- }-- String v = val.substring(start,pos);- if (flt) {- return Double.parseDouble(v);- } else {- return Long.parseLong(v);- }- }-- public double getDouble() {- eatws();- char[] arr = new char[end - pos];- int i;- for (i = 0; i < arr.length; i++) {- char ch = val.charAt(pos);- if ((ch >= '0' && ch <= '9')- || ch == '+' || ch == '-'- || ch == '.' || ch == 'e' || ch == 'E'- ) {- pos++;- arr[i] = ch;- } else {- break;- }- }-- return Double.parseDouble(new String(arr, 0, i));- }-- public int getInt() {- eatws();- char[] arr = new char[end - pos];- int i;- for (i = 0; i < arr.length; i++) {- char ch = val.charAt(pos);- if ((ch >= '0' && ch <= '9')- || ch == '+' || ch == '-'- ) {- pos++;- arr[i] = ch;- } else {- break;- }- }-- return Integer.parseInt(new String(arr, 0, i));- }--- public String getId() throws SyntaxError {- return getId("Expected identifier");- }-- public String getId(String errMessage) throws SyntaxError {- eatws();- int id_start = pos;- char ch;- if (pos < end && (ch = val.charAt(pos)) != '$' && Character.isJavaIdentifierStart(ch)) {- pos++;- while (pos < end) {- ch = val.charAt(pos);-// if (!Character.isJavaIdentifierPart(ch) && ch != '.' && ch != ':') {- if (!Character.isJavaIdentifierPart(ch) && ch != '.') {- break;- }- pos++;- }- return val.substring(id_start, pos);- }-- if (errMessage != null) {- throw new SyntaxError(errMessage + " at pos " + pos + " str='" + val + "'");- }- return null;- }-- public String getGlobbedId(String errMessage) throws SyntaxError {- eatws();- int id_start = pos;- char ch;- if (pos < end && (ch = val.charAt(pos)) != '$' && (Character.isJavaIdentifierStart(ch) || ch=='?' || ch=='*')) {- pos++;- while (pos < end) {- ch = val.charAt(pos);- if (!(Character.isJavaIdentifierPart(ch) || ch=='?' || ch=='*') && ch != '.') {- break;- }- pos++;- }- return val.substring(id_start, pos);- }-- if (errMessage != null) {- throw new SyntaxError(errMessage + " at pos " + pos + " str='" + val + "'");- }- return null;- }-- /**- * Skips leading whitespace and returns whatever sequence of non- * whitespace it can find (or hte empty string)- */- public String getSimpleString() {- eatws();- int startPos = pos;- char ch;- while (pos < end) {- ch = val.charAt(pos);- if (Character.isWhitespace(ch)) break;- pos++;- }- return val.substring(startPos, pos);- }-- /**- * Sort direction or null if current position does not indicate a- * sort direction. (True is desc, False is asc).- * Position is advanced to after the comma (or end) when result is non null- */- public Boolean getSortDirection() throws SyntaxError {- final int startPos = pos;- final String order = getId(null);-- Boolean top = null;-- if (null != order) {- final String orderLowerCase = order.toLowerCase(Locale.ROOT);- if ("desc".equals(orderLowerCase) || "top".equals(orderLowerCase)) {- top = true;- } else if ("asc".equals(orderLowerCase) || "bottom".equals(orderLowerCase)) {- top = false;- }-- // it's not a legal direction if more stuff comes after it- eatws();- final char c = ch();- if (0 == c) {- // :NOOP- } else if (',' == c) {- pos++;- } else {- top = null;- }- }-- if (null == top) pos = startPos; // no direction, reset- return top;- }-- // return null if not a string- public String getQuotedString() throws SyntaxError {- eatws();- char delim = peekChar();- if (!(delim == '\"' || delim == '\'')) {- return null;- }- int val_start = ++pos;- StringBuilder sb = new StringBuilder(); // needed for escaping- for (; ;) {- if (pos >= end) {- throw new SyntaxError("Missing end quote for string at pos " + (val_start - 1) + " str='" + val + "'");- }- char ch = val.charAt(pos);- if (ch == '\\') {- pos++;- if (pos >= end) break;- ch = val.charAt(pos);- switch (ch) {- case 'n':- ch = '\n';- break;- case 't':- ch = '\t';- break;- case 'r':- ch = '\r';- break;- case 'b':- ch = '\b';- break;- case 'f':- ch = '\f';- break;- case 'u':- if (pos + 4 >= end) {- throw new SyntaxError("bad unicode escape \\uxxxx at pos" + (val_start - 1) + " str='" + val + "'");- }- ch = (char) Integer.parseInt(val.substring(pos + 1, pos + 5), 16);- pos += 4;- break;- }- } else if (ch == delim) {- pos++; // skip over the quote- break;- }- sb.append(ch);- pos++;- }-- return sb.toString();- }-- // next non-whitespace char- public char peek() {- eatws();- return pos < end ? val.charAt(pos) : 0;- }-- // next char- public char peekChar() {- return pos < end ? val.charAt(pos) : 0;- }-- @Override- public String toString() {- return "'" + val + "'" + ", pos=" + pos;- }-- }-/*** Builds a list of String which are stringified versions of a list of Queries*/diff --git a/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java b/solr/core/src/java/org/apache/solr/search/SolrReturnFields.javaindex 0307910b891..88ee9971e1e 100644--- a/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java+++ b/solr/core/src/java/org/apache/solr/search/SolrReturnFields.java@@ -152,7 +152,7 @@ public class SolrReturnFields extends ReturnFields {}// like getId, but also accepts dashes for legacy fields- public static String getFieldName(QueryParsing.StrParser sp) {+ public static String getFieldName(StrParser sp) {sp.eatws();int id_start = sp.pos;char ch;@@ -176,7 +176,7 @@ public class SolrReturnFields extends ReturnFields {return;}try {- QueryParsing.StrParser sp = new QueryParsing.StrParser(fl);+ StrParser sp = new StrParser(fl);for(;;) {sp.opt(',');diff --git a/solr/core/src/java/org/apache/solr/search/StrParser.java b/solr/core/src/java/org/apache/solr/search/StrParser.javanew file mode 100644index 00000000000..b20ff245483--- /dev/null+++ b/solr/core/src/java/org/apache/solr/search/StrParser.java@@ -0,0 +1,337 @@+package org.apache.solr.search;++import java.util.Locale;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++/**+ * Simple class to help with parsing a string.+ * <b>Note: This API is experimental and may change in non backward-compatible ways in the future</b>+ */+ public class StrParser {+ public String val;+ public int pos;+ public int end;++ public StrParser(String val) {+ this(val, 0, val.length());+ }++ public StrParser(String val, int start, int end) {+ this.val = val;+ this.pos = start;+ this.end = end;+ }++ public void eatws() {+ while (pos < end && Character.isWhitespace(val.charAt(pos))) pos++;+ }++ public char ch() {+ return pos < end ? val.charAt(pos) : 0;+ }++ public void skip(int nChars) {+ pos = Math.max(pos + nChars, end);+ }++ public boolean opt(String s) {+ eatws();+ int slen = s.length();+ if (val.regionMatches(pos, s, 0, slen)) {+ pos += slen;+ return true;+ }+ return false;+ }++ public boolean opt(char ch) {+ eatws();+ if (pos < end && val.charAt(pos) == ch) {+ pos++;+ return true;+ }+ return false;+ }+++ public void expect(String s) throws SyntaxError {+ eatws();+ int slen = s.length();+ if (val.regionMatches(pos, s, 0, slen)) {+ pos += slen;+ } else {+ throw new SyntaxError("Expected '" + s + "' at position " + pos + " in '" + val + "'");+ }+ }++ public float getFloat() {+ eatws();+ char[] arr = new char[end - pos];+ int i;+ for (i = 0; i < arr.length; i++) {+ char ch = val.charAt(pos);+ if ((ch >= '0' && ch <= '9')+ || ch == '+' || ch == '-'+ || ch == '.' || ch == 'e' || ch == 'E'+ ) {+ pos++;+ arr[i] = ch;+ } else {+ break;+ }+ }++ return Float.parseFloat(new String(arr, 0, i));+ }++ public Number getNumber() {+ eatws();+ int start = pos;+ boolean flt = false;++ while (pos < end) {+ char ch = val.charAt(pos);+ if ((ch >= '0' && ch <= '9') || ch == '+' || ch == '-') {+ pos++;+ } else if (ch == '.' || ch =='e' || ch=='E') {+ flt = true;+ pos++;+ } else {+ break;+ }+ }++ String v = val.substring(start,pos);+ if (flt) {+ return Double.parseDouble(v);+ } else {+ return Long.parseLong(v);+ }+ }++ public double getDouble() {+ eatws();+ char[] arr = new char[end - pos];+ int i;+ for (i = 0; i < arr.length; i++) {+ char ch = val.charAt(pos);+ if ((ch >= '0' && ch <= '9')+ || ch == '+' || ch == '-'+ || ch == '.' || ch == 'e' || ch == 'E'+ ) {+ pos++;+ arr[i] = ch;+ } else {+ break;+ }+ }++ return Double.parseDouble(new String(arr, 0, i));+ }++ public int getInt() {+ eatws();+ char[] arr = new char[end - pos];+ int i;+ for (i = 0; i < arr.length; i++) {+ char ch = val.charAt(pos);+ if ((ch >= '0' && ch <= '9')+ || ch == '+' || ch == '-'+ ) {+ pos++;+ arr[i] = ch;+ } else {+ break;+ }+ }++ return Integer.parseInt(new String(arr, 0, i));+ }+++ public String getId() throws SyntaxError {+ return getId("Expected identifier");+ }++ public String getId(String errMessage) throws SyntaxError {+ eatws();+ int id_start = pos;+ char ch;+ if (pos < end && (ch = val.charAt(pos)) != '$' && Character.isJavaIdentifierStart(ch)) {+ pos++;+ while (pos < end) {+ ch = val.charAt(pos);+// if (!Character.isJavaIdentifierPart(ch) && ch != '.' && ch != ':') {+ if (!Character.isJavaIdentifierPart(ch) && ch != '.') {+ break;+ }+ pos++;+ }+ return val.substring(id_start, pos);+ }++ if (errMessage != null) {+ throw new SyntaxError(errMessage + " at pos " + pos + " str='" + val + "'");+ }+ return null;+ }++ public String getGlobbedId(String errMessage) throws SyntaxError {+ eatws();+ int id_start = pos;+ char ch;+ if (pos < end && (ch = val.charAt(pos)) != '$' && (Character.isJavaIdentifierStart(ch) || ch=='?' || ch=='*')) {+ pos++;+ while (pos < end) {+ ch = val.charAt(pos);+ if (!(Character.isJavaIdentifierPart(ch) || ch=='?' || ch=='*') && ch != '.') {+ break;+ }+ pos++;+ }+ return val.substring(id_start, pos);+ }++ if (errMessage != null) {+ throw new SyntaxError(errMessage + " at pos " + pos + " str='" + val + "'");+ }+ return null;+ }++ /**+ * Skips leading whitespace and returns whatever sequence of non+ * whitespace it can find (or hte empty string)+ */+ public String getSimpleString() {+ eatws();+ int startPos = pos;+ char ch;+ while (pos < end) {+ ch = val.charAt(pos);+ if (Character.isWhitespace(ch)) break;+ pos++;+ }+ return val.substring(startPos, pos);+ }++ /**+ * Sort direction or null if current position does not indicate a+ * sort direction. (True is desc, False is asc).+ * Position is advanced to after the comma (or end) when result is non null+ */+ public Boolean getSortDirection() throws SyntaxError {+ final int startPos = pos;+ final String order = getId(null);++ Boolean top = null;++ if (null != order) {+ final String orderLowerCase = order.toLowerCase(Locale.ROOT);+ if ("desc".equals(orderLowerCase) || "top".equals(orderLowerCase)) {+ top = true;+ } else if ("asc".equals(orderLowerCase) || "bottom".equals(orderLowerCase)) {+ top = false;+ }++ // it's not a legal direction if more stuff comes after it+ eatws();+ final char c = ch();+ if (0 == c) {+ // :NOOP+ } else if (',' == c) {+ pos++;+ } else {+ top = null;+ }+ }++ if (null == top) pos = startPos; // no direction, reset+ return top;+ }++ // return null if not a string+ public String getQuotedString() throws SyntaxError {+ eatws();+ char delim = peekChar();+ if (!(delim == '\"' || delim == '\'')) {+ return null;+ }+ int val_start = ++pos;+ StringBuilder sb = new StringBuilder(); // needed for escaping+ for (; ;) {+ if (pos >= end) {+ throw new SyntaxError("Missing end quote for string at pos " + (val_start - 1) + " str='" + val + "'");+ }+ char ch = val.charAt(pos);+ if (ch == '\\') {+ pos++;+ if (pos >= end) break;+ ch = val.charAt(pos);+ switch (ch) {+ case 'n':+ ch = '\n';+ break;+ case 't':+ ch = '\t';+ break;+ case 'r':+ ch = '\r';+ break;+ case 'b':+ ch = '\b';+ break;+ case 'f':+ ch = '\f';+ break;+ case 'u':+ if (pos + 4 >= end) {+ throw new SyntaxError("bad unicode escape \\uxxxx at pos" + (val_start - 1) + " str='" + val + "'");+ }+ ch = (char) Integer.parseInt(val.substring(pos + 1, pos + 5), 16);+ pos += 4;+ break;+ }+ } else if (ch == delim) {+ pos++; // skip over the quote+ break;+ }+ sb.append(ch);+ pos++;+ }++ return sb.toString();+ }++ // next non-whitespace char+ public char peek() {+ eatws();+ return pos < end ? val.charAt(pos) : 0;+ }++ // next char+ public char peekChar() {+ return pos < end ? val.charAt(pos) : 0;+ }++ @Override+ public String toString() {+ return "'" + val + "'" + ", pos=" + pos;+ }++ }diff --git a/solr/core/src/java/org/apache/solr/search/facet/LegacyFacet.java b/solr/core/src/java/org/apache/solr/search/facet/LegacyFacet.javaindex 3f07f57b5b3..df4cd1db153 100644--- a/solr/core/src/java/org/apache/solr/search/facet/LegacyFacet.java+++ b/solr/core/src/java/org/apache/solr/search/facet/LegacyFacet.java@@ -35,6 +35,7 @@ import org.apache.solr.common.util.StrUtils;import org.apache.solr.core.SolrCore;import org.apache.solr.search.QueryParsing;import org.apache.solr.search.SolrReturnFields;+import org.apache.solr.search.StrParser;import org.apache.solr.search.SyntaxError;public class LegacyFacet {@@ -233,7 +234,7 @@ public class LegacyFacet {private void addStat(String val) {- QueryParsing.StrParser sp = new QueryParsing.StrParser(val);+ StrParser sp = new StrParser(val);int start = 0;sp.eatws();if (sp.pos >= sp.end) addStat(val, val);diff --git a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.javaindex 80fa60329b1..a95abb8b660 100644--- a/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java+++ b/solr/core/src/test/org/apache/solr/search/QueryEqualityTest.java@@ -1020,7 +1020,7 @@ public class QueryEqualityTest extends SolrTestCaseJ4 {protected void assertFuncEquals(final SolrQueryRequest req,final String... inputs) throws Exception {// pull out the function name- final String funcName = (new QueryParsing.StrParser(inputs[0])).getId();+ final String funcName = (new StrParser(inputs[0])).getId();valParsersTested.add(funcName);assertQueryEquals(FunctionQParserPlugin.NAME, req, inputs);