Skip to content

Commit

Permalink
[bugfix] prevent auto complete search outside the given context
Browse files Browse the repository at this point in the history
  • Loading branch information
marmoure committed Nov 22, 2023
1 parent 217bb3a commit a2344d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -34,24 +36,29 @@
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
*/
public class newSuggestionForm extends javax.swing.JDialog {
private TeiCompleter teiCompleter;
private TeiCompleter.AutoCompleteContext autoCompleteContext;
private WhatPossibleValuesHasAttributeContext context;
private SuggestedAutocomplete suggestedAutocomplete = null;

private ArrayList<SuggestedAutocomplete> results = new ArrayList<>();

/**
* 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();
}
Expand Down Expand Up @@ -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<CIValue> 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));
}

Expand Down Expand Up @@ -391,7 +412,21 @@ protected Object doInBackground() throws Exception {
// get the auto complete suggestions based on the user input
final List<CIValue> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public List<CIValue> filterAttributeValues(final List<CIValue> 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));
}

}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -414,11 +414,14 @@ public List<CIValue> filterElementValues(final List<CIValue> 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
Expand All @@ -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);



Expand Down

0 comments on commit a2344d7

Please sign in to comment.