From b13313f7e71fd0709e9a36da6a9fe86227285fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20FOUCRET?= Date: Tue, 5 Nov 2024 09:37:28 +0100 Subject: [PATCH] KQL plugin - Code cleanup (#116180) --- x-pack/plugin/kql/build.gradle | 2 +- .../kql/licenses/antlr4-runtime-LICENSE.txt | 26 +++++++++++++++++++ .../kql/licenses/antlr4-runtime-NOTICE.txt | 0 .../plugin/kql/src/main/java/module-info.java | 19 ++++++++++++++ .../xpack/kql/parser/ParserUtils.java | 10 +++---- 5 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 x-pack/plugin/kql/licenses/antlr4-runtime-LICENSE.txt create mode 100644 x-pack/plugin/kql/licenses/antlr4-runtime-NOTICE.txt create mode 100644 x-pack/plugin/kql/src/main/java/module-info.java diff --git a/x-pack/plugin/kql/build.gradle b/x-pack/plugin/kql/build.gradle index 198099329c7c0..9d0860346b188 100644 --- a/x-pack/plugin/kql/build.gradle +++ b/x-pack/plugin/kql/build.gradle @@ -17,7 +17,7 @@ base { dependencies { compileOnly project(path: xpackModule('core')) - compileOnly "org.antlr:antlr4-runtime:${versions.antlr4}" + api "org.antlr:antlr4-runtime:${versions.antlr4}" testImplementation "org.antlr:antlr4-runtime:${versions.antlr4}" testImplementation project(':test:framework') diff --git a/x-pack/plugin/kql/licenses/antlr4-runtime-LICENSE.txt b/x-pack/plugin/kql/licenses/antlr4-runtime-LICENSE.txt new file mode 100644 index 0000000000000..95d0a2554f686 --- /dev/null +++ b/x-pack/plugin/kql/licenses/antlr4-runtime-LICENSE.txt @@ -0,0 +1,26 @@ +[The "BSD license"] +Copyright (c) 2015 Terence Parr, Sam Harwell +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/x-pack/plugin/kql/licenses/antlr4-runtime-NOTICE.txt b/x-pack/plugin/kql/licenses/antlr4-runtime-NOTICE.txt new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/x-pack/plugin/kql/src/main/java/module-info.java b/x-pack/plugin/kql/src/main/java/module-info.java new file mode 100644 index 0000000000000..c4dd539508f39 --- /dev/null +++ b/x-pack/plugin/kql/src/main/java/module-info.java @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +module org.elasticsearch.kql { + requires org.elasticsearch.server; + requires org.elasticsearch.xcontent; + requires org.antlr.antlr4.runtime; + requires org.elasticsearch.base; + requires org.apache.lucene.queryparser; + requires org.elasticsearch.logging; + requires org.apache.lucene.core; + + exports org.elasticsearch.xpack.kql; + exports org.elasticsearch.xpack.kql.parser; +} diff --git a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/ParserUtils.java b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/ParserUtils.java index f996a953ea7f7..3319d920a88ee 100644 --- a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/ParserUtils.java +++ b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/ParserUtils.java @@ -12,11 +12,11 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.TerminalNode; -import org.apache.logging.log4j.util.Strings; import org.apache.lucene.queryparser.classic.QueryParser; import java.util.ArrayList; import java.util.List; +import java.util.Locale; /** * Utility class for parsing and processing KQL expressions. @@ -211,15 +211,15 @@ private static boolean isEscapedKeywordSequence(String input, int startIndex) { if (startIndex + 1 >= input.length()) { return false; } - String remaining = Strings.toRootLowerCase(input.substring(startIndex)); + String remaining = input.substring(startIndex).toLowerCase(Locale.ROOT); return remaining.startsWith("and") || remaining.startsWith("or") || remaining.startsWith("not"); } private static String handleKeywordSequence(String input, int startIndex) { String remaining = input.substring(startIndex); - if (Strings.toRootLowerCase(remaining).startsWith("and")) return remaining.substring(0, 3); - if (Strings.toRootLowerCase(remaining).startsWith("or")) return remaining.substring(0, 2); - if (Strings.toRootLowerCase(remaining).startsWith("not")) return remaining.substring(0, 3); + if (remaining.toLowerCase(Locale.ROOT).startsWith("and")) return remaining.substring(0, 3); + if (remaining.toLowerCase(Locale.ROOT).startsWith("or")) return remaining.substring(0, 2); + if (remaining.toLowerCase(Locale.ROOT).startsWith("not")) return remaining.substring(0, 3); return ""; }