From a2344d7ef2bd569bed80f55b6e124996dcce9c9c Mon Sep 17 00:00:00 2001 From: marmoure Date: Wed, 22 Nov 2023 18:33:23 +0100 Subject: [PATCH 1/2] [bugfix] prevent auto complete search outside the given context --- .../tei/completer/GUI/newSuggestionForm.java | 37 ++++++++++++++++++- .../oxygen/tei/completer/TeiCompleter.java | 13 ++++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java b/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java index 8c004d0..92a06e0 100644 --- a/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java +++ b/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java @@ -19,10 +19,12 @@ */ package org.humanistika.oxygen.tei.completer.GUI; +import com.evolvedbinary.xpath.parser.ast.Expr; import org.humanistika.oxygen.tei.completer.SuggestedAutocomplete; import org.humanistika.oxygen.tei.completer.TeiCompleter; import org.humanistika.oxygen.tei.completer.configuration.beans.AutoComplete; import ro.sync.contentcompletion.xml.CIValue; +import ro.sync.contentcompletion.xml.WhatPossibleValuesHasAttributeContext; import javax.annotation.Nullable; import javax.swing.*; @@ -34,6 +36,9 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import static org.humanistika.oxygen.tei.completer.XPathUtil.isSubset; +import static org.humanistika.oxygen.tei.completer.XPathUtil.parseXPath; + /** * * @author younes @@ -41,6 +46,7 @@ public class newSuggestionForm extends javax.swing.JDialog { private TeiCompleter teiCompleter; private TeiCompleter.AutoCompleteContext autoCompleteContext; + private WhatPossibleValuesHasAttributeContext context; private SuggestedAutocomplete suggestedAutocomplete = null; private ArrayList results = new ArrayList<>(); @@ -48,10 +54,11 @@ public class newSuggestionForm extends javax.swing.JDialog { /** * Creates new form JDialogForm */ - public newSuggestionForm(java.awt.Frame parent, final TeiCompleter teiCompleter, final TeiCompleter.AutoCompleteContext autoCompleteContext) { + public newSuggestionForm(java.awt.Frame parent, final TeiCompleter teiCompleter, final TeiCompleter.AutoCompleteContext autoCompleteContext, final WhatPossibleValuesHasAttributeContext context) { super(parent, ModalityType.DOCUMENT_MODAL); this.teiCompleter = teiCompleter; this.autoCompleteContext = autoCompleteContext; + this.context = context; initComponents(); customLabels(); } @@ -244,7 +251,21 @@ private void fetchjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN- // get the auto complete suggestions based on the user input final List suggestions = new ArrayList<>(); + final String elemXPath = context.computeContextXPathExpression(); + final String attrXPath = elemXPath + "/@" + context.getAttributeName(); + Expr attributeExpr; + try { + attributeExpr = parseXPath(attrXPath); + } catch (Exception e) { + return; + } + + for (final AutoComplete autoComplete : teiCompleter.getConfiguration().getAutoCompletes()) { + //check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath + final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete); + if (!isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) continue; + suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); } @@ -391,7 +412,21 @@ protected Object doInBackground() throws Exception { // get the auto complete suggestions based on the user input final List suggestions = new ArrayList<>(); + final String elemXPath = context.computeContextXPathExpression(); + final String attrXPath = elemXPath + "/@" + context.getAttributeName(); + Expr attributeExpr; + try { + attributeExpr = parseXPath(attrXPath); + } catch (Exception e) { + throw new Exception(e); + } + + for (final AutoComplete autoComplete : teiCompleter.getConfiguration().getAutoCompletes()) { + //check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath + final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete); + if (!isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) continue; + suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); } diff --git a/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java b/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java index 8f07a30..32feeff 100755 --- a/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java +++ b/src/main/java/org/humanistika/oxygen/tei/completer/TeiCompleter.java @@ -103,7 +103,7 @@ public List filterAttributeValues(final List list, final WhatP if(autoCompleteSuggestions != null) { // the value needs to be prefixed with a space character to bump it to the top of the list - list.add(new CustomCIValue("\uD83D\uDC49 Custom lookup...", this, autoCompleteSuggestions.autoCompleteContext)); + list.add(new CustomCIValue("\uD83D\uDC49 Custom lookup...", this, autoCompleteSuggestions.autoCompleteContext, context)); } } @@ -235,7 +235,7 @@ private AuthenticationType asClientFactoryAuthenticationType(@Nullable final Aut } } - protected class AutoCompleteXPaths { + public class AutoCompleteXPaths { private final Expr attributeXPath; public AutoCompleteXPaths(final Expr attributeXPath) { @@ -247,7 +247,7 @@ public Expr getAttributeXPath() { } } - protected AutoCompleteXPaths getXPaths(final AutoComplete autoComplete) { + public AutoCompleteXPaths getXPaths(final AutoComplete autoComplete) { synchronized(cachedAutoCompleteXPaths) { AutoCompleteXPaths autoCompleteXPaths = cachedAutoCompleteXPaths.get(autoComplete); if(autoCompleteXPaths == null) { @@ -414,11 +414,14 @@ public List filterElementValues(final List list, final Context public class CustomCIValue extends CIValue { private TeiCompleter teiCompleter; private AutoCompleteContext autoCompleteContext; + + private WhatPossibleValuesHasAttributeContext context; private String suggestion; - public CustomCIValue(String s, final TeiCompleter teiCompleter, final AutoCompleteContext autoCompleteContext) { + public CustomCIValue(String s, final TeiCompleter teiCompleter, final AutoCompleteContext autoCompleteContext, final WhatPossibleValuesHasAttributeContext context) { super(s); this.teiCompleter = teiCompleter; this.autoCompleteContext = autoCompleteContext; + this.context = context; } @Override @@ -436,7 +439,7 @@ private SuggestedAutocomplete promptUserForNewSuggestion() { final KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); final Component comp = keyboardFocusManager.getFocusOwner(); final Frame parentFrame = getParentFrame(comp); - final newSuggestionForm newSuggestionForm = new newSuggestionForm(parentFrame, teiCompleter, autoCompleteContext); + final newSuggestionForm newSuggestionForm = new newSuggestionForm(parentFrame, teiCompleter, autoCompleteContext, context); From eacb784866bd27361e65862657145619c3387de6 Mon Sep 17 00:00:00 2001 From: marmoure Date: Thu, 23 Nov 2023 10:03:57 +0100 Subject: [PATCH 2/2] [bugfix] addressing PR review --- .../oxygen/tei/completer/GUI/newSuggestionForm.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java b/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java index 92a06e0..ac5b61e 100644 --- a/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java +++ b/src/main/java/org/humanistika/oxygen/tei/completer/GUI/newSuggestionForm.java @@ -263,10 +263,11 @@ private void fetchjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN- for (final AutoComplete autoComplete : teiCompleter.getConfiguration().getAutoCompletes()) { //check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath + // only request auto completer suggestions if it matches final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete); - if (!isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) continue; - - suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); + if (isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) { + suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); + } } //get the model to populate the table @@ -424,10 +425,12 @@ protected Object doInBackground() throws Exception { for (final AutoComplete autoComplete : teiCompleter.getConfiguration().getAutoCompletes()) { //check if attributeExpr addresses a subset of autoCompleteXPaths.attributeXPath + // only request auto complete suggestions if it matches final TeiCompleter.AutoCompleteXPaths autoCompleteXPaths = teiCompleter.getXPaths(autoComplete); - if (!isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) continue; + if (isSubset(attributeExpr, autoCompleteXPaths.getAttributeXPath())) { + suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); - suggestions.addAll(teiCompleter.requestAutoComplete(autoComplete, selection, dependent)); + } } this.suggestions.addAll(suggestions);