Skip to content

Commit

Permalink
Move the text search code to the :search into a separate package
Browse files Browse the repository at this point in the history
Issue: #297
  • Loading branch information
mlopatkin committed Nov 19, 2022
1 parent 6a19c65 commit 6277cbf
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,10 @@

import java.util.regex.PatternSyntaxException;

/**
* This exception is thrown when a string representation of a search pattern cannot be converted into something that the
* search machinery can use.
*/
public class RequestCompilationException extends Exception {
private String request;
private final @Nullable PatternSyntaxException cause;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,8 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

/**
* A predicate that can highlight occurrences of the pattern in the text.
*/
public interface HighlightStrategy extends SearchStrategy {
void highlightOccurences(String text, TextHighlighter highlighter);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

import name.mlopatkin.andlogview.utils.MyStringUtils;

class IgnoreCaseSearcher implements HighlightStrategy, SearchStrategy {
private String textToSearch;
private final String textToSearch;

public IgnoreCaseSearcher(String text) {
this.textToSearch = text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

class RegExpSearcher implements HighlightStrategy, SearchStrategy {
private Pattern pattern;
private final Pattern pattern;

public RegExpSearcher(String regex) throws PatternSyntaxException {
pattern = Pattern.compile(regex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -12,11 +12,11 @@
* 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.
*
*
*/

package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

import name.mlopatkin.andlogview.search.RequestCompilationException;

import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,8 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

import java.util.function.Predicate;

/**
* A predicate that can check if the String contains some pattern.
*/
public interface SearchStrategy extends Predicate<String> {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

import name.mlopatkin.andlogview.search.RequestCompilationException;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -12,11 +12,11 @@
* 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.
*
*
*/

package name.mlopatkin.andlogview.search;
package name.mlopatkin.andlogview.search.text;

import name.mlopatkin.andlogview.search.RequestCompilationException;

import java.util.function.Predicate;
import java.util.regex.Pattern;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2011 Mikhail Lopatkin
*
* Licensed 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.
*/
package name.mlopatkin.andlogview.search.text;

/**
* An implementation of this interface can mark portions of text as highlighted.
*/
public interface TextHighlighter {
/**
* Request highlighting of a text portion starting at char at {@code from} index (inclusive) and ending at char at
* {@code end} index (exclusive). The {@code end} index can be outside the text bounds if the highlighting spans to
* the end of text.
*
* @param from the index of the start of the highlighted portion of text
* @param to the first index after the end of the highlighted portion of text
*/
void highlightText(int from, int to);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Mikhail Lopatkin
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,13 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package name.mlopatkin.andlogview.search;

/**
*
* The infrastructure for the text search. Creating patterns from regular expressions, highlighting matches, etc.
*/
public interface TextHighlighter {
void highlightText(int from, int to);

void clearHighlight();
}
package name.mlopatkin.andlogview.search.text;
7 changes: 1 addition & 6 deletions src/name/mlopatkin/andlogview/TooltipGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package name.mlopatkin.andlogview;

import name.mlopatkin.andlogview.config.Configuration;
import name.mlopatkin.andlogview.search.TextHighlighter;
import name.mlopatkin.andlogview.search.text.TextHighlighter;

import com.google.common.collect.Range;
import com.google.common.html.HtmlEscapers;
Expand All @@ -40,11 +40,6 @@ public void highlightText(int from, int to) {
highlightRanges.add(Range.closedOpen(from, to));
}

@Override
public void clearHighlight() {
highlightRanges.clear();
}

private static final String highlightBackgroundColor = "yellow";
private static final String highlightTextColor = "red";
private static final String SPAN_BEGIN = String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package name.mlopatkin.andlogview.search;

import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.search.text.TextHighlighter;

import java.util.function.Predicate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package name.mlopatkin.andlogview.search;

import name.mlopatkin.andlogview.search.text.HighlightStrategy;
import name.mlopatkin.andlogview.search.text.SearchStrategyFactory;
import name.mlopatkin.andlogview.ui.logtable.Column;

import com.google.common.base.CharMatcher;
Expand Down
1 change: 1 addition & 0 deletions src/name/mlopatkin/andlogview/search/Searchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package name.mlopatkin.andlogview.search;

import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.search.text.TextHighlighter;

import com.google.common.collect.ImmutableList;

Expand Down
2 changes: 2 additions & 0 deletions src/name/mlopatkin/andlogview/search/ValueSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package name.mlopatkin.andlogview.search;

import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.search.text.HighlightStrategy;
import name.mlopatkin.andlogview.search.text.TextHighlighter;
import name.mlopatkin.andlogview.ui.logtable.Column;

class ValueSearcher implements RowSearchStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.logmodel.LogRecordPredicates;
import name.mlopatkin.andlogview.search.RequestCompilationException;
import name.mlopatkin.andlogview.search.SearchRequestParser;
import name.mlopatkin.andlogview.search.SearcherBuilder;
import name.mlopatkin.andlogview.search.text.SearchRequestParser;
import name.mlopatkin.andlogview.search.text.SearcherBuilder;
import name.mlopatkin.andlogview.utils.MorePredicates;

import com.google.common.base.Joiner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package name.mlopatkin.andlogview.ui.logtable;

import name.mlopatkin.andlogview.TooltipGenerator;
import name.mlopatkin.andlogview.search.TextHighlighter;
import name.mlopatkin.andlogview.search.text.TextHighlighter;
import name.mlopatkin.andlogview.thirdparty.styledlabel.StyledLabel;
import name.mlopatkin.andlogview.widgets.UiHelper;

Expand Down Expand Up @@ -122,8 +122,7 @@ public void highlightText(int from, int to) {
document.setCharacterAttributes(from, to - from, highlighted, true);
}

@Override
public void clearHighlight() {
private void clearHighlight() {
document.setCharacterAttributes(0, document.getLength(), BASE_STYLE, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import name.mlopatkin.andlogview.TooltipGenerator;
import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.search.RowSearchStrategy;
import name.mlopatkin.andlogview.search.TextHighlighter;
import name.mlopatkin.andlogview.search.text.TextHighlighter;
import name.mlopatkin.andlogview.ui.logtable.Column;
import name.mlopatkin.andlogview.ui.logtable.LogRecordTableModel;
import name.mlopatkin.andlogview.widgets.DecoratingCellRenderer;
Expand Down
2 changes: 2 additions & 0 deletions test/name/mlopatkin/andlogview/search/RowSearchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import name.mlopatkin.andlogview.logmodel.LogRecord;
import name.mlopatkin.andlogview.logmodel.LogRecordUtils;
import name.mlopatkin.andlogview.search.text.SearchStrategyFactory;
import name.mlopatkin.andlogview.search.text.TextHighlighter;
import name.mlopatkin.andlogview.ui.logtable.Column;

import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;

import name.mlopatkin.andlogview.search.text.SearchRequestParser;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.hamcrest.MatcherAssert.assertThat;

import name.mlopatkin.andlogview.search.text.SearcherBuilder;

import com.google.common.collect.ImmutableList;

import org.hamcrest.BaseMatcher;
Expand Down

0 comments on commit 6277cbf

Please sign in to comment.