From 26dbe4729e470ba00c4cb98706bdaef1ba8deb7a Mon Sep 17 00:00:00 2001 From: Thomas Krause Date: Mon, 11 Mar 2024 11:46:06 +0100 Subject: [PATCH] Avoid null pointer exceptions when merging corpus configs for search options. --- CHANGELOG.md | 5 +++++ .../org/corpus_tools/annis/gui/Helper.java | 4 +++- .../gui/controlpanel/SearchOptionsPanel.java | 20 ++++++++++++++++--- src/main/webapp/VAADIN/help/css/general.css | 1 + src/main/webapp/VAADIN/help/css/print.css | 8 -------- src/main/webapp/VAADIN/help/css/variables.css | 10 ++++++++++ 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5c04e29b7..5eea44fd50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Avoid null pointer exceptions when merging corpus configs for search + options. + ## [4.11.0] - 2024-02-09 ### Added diff --git a/src/main/java/org/corpus_tools/annis/gui/Helper.java b/src/main/java/org/corpus_tools/annis/gui/Helper.java index 568ebd39f5..f27b191680 100644 --- a/src/main/java/org/corpus_tools/annis/gui/Helper.java +++ b/src/main/java/org/corpus_tools/annis/gui/Helper.java @@ -119,6 +119,8 @@ */ public class Helper { + private static final int DEFAULT_CONTEXT = 5; + private static final String UTF_8 = "UTF-8"; private static final Pattern validQNamePattern = @@ -663,7 +665,7 @@ public static CorpusConfiguration getDefaultCorpusConfig() { defaultCorpusConfig.setVisualizers(new LinkedList<>()); defaultCorpusConfig.getView().setPageSize(10); - defaultCorpusConfig.getContext().setDefault(5); + defaultCorpusConfig.getContext().setDefault(DEFAULT_CONTEXT); defaultCorpusConfig.getContext().setSizes(Arrays.asList(1, 2, 5, 10)); defaultCorpusConfig.getContext().setMax(Integer.MAX_VALUE); diff --git a/src/main/java/org/corpus_tools/annis/gui/controlpanel/SearchOptionsPanel.java b/src/main/java/org/corpus_tools/annis/gui/controlpanel/SearchOptionsPanel.java index ad57eca6ce..8f129be80e 100644 --- a/src/main/java/org/corpus_tools/annis/gui/controlpanel/SearchOptionsPanel.java +++ b/src/main/java/org/corpus_tools/annis/gui/controlpanel/SearchOptionsPanel.java @@ -334,12 +334,14 @@ private CorpusConfiguration mergeConfigs(Collection corpora, // Calculate merged context Optional mergedDefaultCtx = selectedConfigs.stream() + .filter(config -> config.getContext() != null && config.getContext().getDefault() != null) .map(config -> config.getContext().getDefault()).min(Comparator.naturalOrder()); if (mergedDefaultCtx.isPresent()) { corpusConfig.getContext().setDefault(mergedDefaultCtx.get()); } Optional mergedMaxCtx = selectedConfigs.stream() + .filter(config -> config.getContext() != null && config.getContext().getMax() != null) .map(config -> config.getContext().getMax()).filter(maxCtx -> maxCtx != null) .min(Comparator.naturalOrder()); if (mergedMaxCtx.isPresent()) { @@ -349,7 +351,9 @@ private CorpusConfiguration mergeConfigs(Collection corpora, // Add all context steps and sort them TreeSet contextSizes = new TreeSet<>(); for (CorpusConfiguration config : selectedConfigs) { - contextSizes.addAll(config.getContext().getSizes()); + if (config.getContext() != null && config.getContext().getSizes() != null) { + contextSizes.addAll(config.getContext().getSizes()); + } } if (!contextSizes.isEmpty()) { corpusConfig.getContext().setSizes(new ArrayList<>(contextSizes)); @@ -357,6 +361,7 @@ private CorpusConfiguration mergeConfigs(Collection corpora, // merge the results per page Optional mergedPageSize = selectedConfigs.stream() + .filter(config -> config.getView() != null && config.getView().getPageSize() != null) .map(config -> config.getView().getPageSize()).min(Comparator.naturalOrder()); if (mergedPageSize.isPresent()) { corpusConfig.getView().setPageSize(mergedPageSize.get()); @@ -366,14 +371,23 @@ private CorpusConfiguration mergeConfigs(Collection corpora, // It is not guaranteed that the different corpora have the same segmentation layers, but all // corpora have token Set allContextSegmentations = selectedConfigs.stream() + .filter( + config -> config.getContext() != null && config.getContext().getSegmentation() != null) .map(config -> config.getContext().getSegmentation()).collect(Collectors.toSet()); - if (allContextSegmentations.size() == 1) { + boolean anyTokenAsSegmentation = selectedConfigs.stream().anyMatch( + config -> config.getContext() != null && config.getContext().getSegmentation() == null); + if (!anyTokenAsSegmentation && allContextSegmentations.size() == 1) { corpusConfig.getContext().setSegmentation(allContextSegmentations.iterator().next()); } Set allBaseTextSegmentations = selectedConfigs.stream() + .filter(config -> config.getView() != null + && config.getView().getBaseTextSegmentation() != null) .map(config -> config.getView().getBaseTextSegmentation()).collect(Collectors.toSet()); - if (allBaseTextSegmentations.size() == 1) { + boolean anyTokenAsBaseText = selectedConfigs.stream().anyMatch( + config -> config.getContext() != null && config.getContext().getSegmentation() == null); + + if (!anyTokenAsBaseText && allBaseTextSegmentations.size() == 1) { corpusConfig.getView().setBaseTextSegmentation(allBaseTextSegmentations.iterator().next()); } return corpusConfig; diff --git a/src/main/webapp/VAADIN/help/css/general.css b/src/main/webapp/VAADIN/help/css/general.css index 344b53eb7f..a6db6805a9 100644 --- a/src/main/webapp/VAADIN/help/css/general.css +++ b/src/main/webapp/VAADIN/help/css/general.css @@ -5,6 +5,7 @@ :root { /* Browser default font-size is 16px, this way 1 rem = 10px */ font-size: 62.5%; + color-scheme: var(--color-scheme); } html { diff --git a/src/main/webapp/VAADIN/help/css/print.css b/src/main/webapp/VAADIN/help/css/print.css index 5e690f7559..27d05e9285 100644 --- a/src/main/webapp/VAADIN/help/css/print.css +++ b/src/main/webapp/VAADIN/help/css/print.css @@ -22,14 +22,6 @@ overflow-y: initial; } -code { - background-color: #666666; - border-radius: 5px; - - /* Force background to be printed in Chrome */ - -webkit-print-color-adjust: exact; -} - pre > .buttons { z-index: 2; } diff --git a/src/main/webapp/VAADIN/help/css/variables.css b/src/main/webapp/VAADIN/help/css/variables.css index 21bf8e55e0..59fcda88f4 100644 --- a/src/main/webapp/VAADIN/help/css/variables.css +++ b/src/main/webapp/VAADIN/help/css/variables.css @@ -50,6 +50,8 @@ --searchresults-border-color: #888; --searchresults-li-bg: #252932; --search-mark-bg: #e3b171; + + --color-scheme: dark; } .coal { @@ -90,6 +92,8 @@ --searchresults-border-color: #98a3ad; --searchresults-li-bg: #2b2b2f; --search-mark-bg: #355c7d; + + --color-scheme: dark; } .light { @@ -130,6 +134,8 @@ --searchresults-border-color: #888; --searchresults-li-bg: #e4f2fe; --search-mark-bg: #a2cff5; + + --color-scheme: light; } .navy { @@ -170,6 +176,8 @@ --searchresults-border-color: #5c5c68; --searchresults-li-bg: #242430; --search-mark-bg: #a2cff5; + + --color-scheme: dark; } .rust { @@ -210,6 +218,8 @@ --searchresults-border-color: #888; --searchresults-li-bg: #dec2a2; --search-mark-bg: #e69f67; + + --color-scheme: light; } @media (prefers-color-scheme: dark) {