Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process escape characters when searching for entities, fix #1217 #1218

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,15 @@ public OWLEntityFinderImpl(OWLModelManagerImpl mngr, OWLEntityRenderingCache ren
this.renderingCache = renderingCache;
}


private static final String ESCAPE_CHAR = "'";


private String stripAndEscapeRendering(String rendering) {
String strippedRendering;
if(rendering.startsWith("'") && rendering.endsWith("'") && rendering.length() > 1) {
strippedRendering = rendering.substring(1, rendering.length() - 1);
if (rendering.startsWith("'") && rendering.endsWith("'") && rendering.length() > 1) {
rendering = rendering.substring(1, rendering.length() - 1);
}
else {
strippedRendering = rendering;
}
return RenderingEscapeUtils.getEscapedRendering(strippedRendering);
return RenderingEscapeUtils.getEscapedRendering(rendering);
}

public OWLClass getOWLClass(String rendering) {
OWLClass cls = renderingCache.getOWLClass(stripAndEscapeRendering(rendering));
if (cls == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)) {
cls = renderingCache.getOWLClass(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return cls;
return renderingCache.getOWLClass(stripAndEscapeRendering(rendering));
}

@Override
Expand All @@ -70,56 +58,32 @@ public Set<OWLEntity> getOWLEntities(String rendering) {
}

public OWLObjectProperty getOWLObjectProperty(String rendering) {
OWLObjectProperty prop = renderingCache.getOWLObjectProperty(rendering);
if (prop == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)){
prop = renderingCache.getOWLObjectProperty(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return prop;
return renderingCache.getOWLObjectProperty(stripAndEscapeRendering(rendering));
}


public OWLDataProperty getOWLDataProperty(String rendering) {
OWLDataProperty prop = renderingCache.getOWLDataProperty(rendering);
if (prop == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)){
prop = renderingCache.getOWLDataProperty(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return prop;
return renderingCache.getOWLDataProperty(stripAndEscapeRendering(rendering));
}


public OWLAnnotationProperty getOWLAnnotationProperty(String rendering) {
OWLAnnotationProperty prop = renderingCache.getOWLAnnotationProperty(rendering);
if (prop == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)){
prop = renderingCache.getOWLAnnotationProperty(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return prop;
return renderingCache.getOWLAnnotationProperty(stripAndEscapeRendering(rendering));
}


public OWLNamedIndividual getOWLIndividual(String rendering) {
OWLNamedIndividual individual = renderingCache.getOWLIndividual(rendering);
if (individual == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)){
individual = renderingCache.getOWLIndividual(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return individual;
return renderingCache.getOWLIndividual(stripAndEscapeRendering(rendering));
}


public OWLDatatype getOWLDatatype(String rendering) {
OWLDatatype dataType = renderingCache.getOWLDatatype(rendering);
if (dataType == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)){
dataType = renderingCache.getOWLDatatype(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return dataType;
return renderingCache.getOWLDatatype(stripAndEscapeRendering(rendering));
}


public OWLEntity getOWLEntity(String rendering) {
OWLEntity entity = renderingCache.getOWLEntity(rendering);
if (entity == null && !rendering.startsWith(ESCAPE_CHAR) && !rendering.endsWith(ESCAPE_CHAR)){
entity = renderingCache.getOWLEntity(RenderingEscapeUtils.getEscapedRendering(rendering));
}
return entity;
return renderingCache.getOWLEntity(stripAndEscapeRendering(rendering));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
public interface OWLObjectHierarchyProviderListener<N extends OWLObject> {

/**
* Notifies the listener that the parents and or children
* of the specified node might have changed. This is usually
* Notifies the listener that the equivalent objects, parents and or
* children of the specified node might have changed. This is usually
* called in response to an add/remove axiom change.
*/
public void nodeChanged(N node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ public void save() {
tmp.crossedOutEntities.addAll(crossedOutEntities);
tmp.unsatisfiableNames.clear();
tmp.unsatisfiableNames.addAll(unsatisfiableNames);
tmp.equivalentObjects.clear();
tmp.equivalentObjects.addAll(equivalentObjects);
tmp.boxedNames.clear();
tmp.boxedNames.addAll(boxedNames);
unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public enum RenderingEscapeSetting {
* @return The escaped rendering.
*/
public static String getEscapedRendering(String originalRendering) {
originalRendering = originalRendering.replace("'", "\\'");
String rendering = originalRendering;
rendering = rendering.replace("\\", "\\\\");
rendering = rendering.replace("'", "\\'");
rendering = rendering.replace("\"", "\\\"");
if (originalRendering.indexOf(' ') != -1
|| originalRendering.indexOf('\\') != -1
|| originalRendering.indexOf(',') != -1
|| originalRendering.indexOf('<') != -1
|| originalRendering.indexOf('>') != -1
Expand All @@ -47,22 +51,20 @@ public static String getEscapedRendering(String originalRendering) {
|| originalRendering.indexOf(']') != -1
|| originalRendering.indexOf('(') != -1
|| originalRendering.indexOf(')') != -1) {
return "'" + originalRendering + "'";
}
else {
return originalRendering;
rendering = "'" + rendering + "'";
}
return rendering;
}

@Nonnull
public static String unescape(@Nonnull String rendering) {
rendering = rendering.replace("\\'", "'");
if(rendering.startsWith("'") && rendering.endsWith("'")) {
return rendering.substring(1, rendering.length() - 1);
}
else {
return rendering;
rendering = rendering.substring(1, rendering.length() - 1);
}
rendering = rendering.replace("\\\"", "\"");
rendering = rendering.replace("\\'", "'");
rendering = rendering.replace("\\\\", "\\");
return rendering;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ private void handleRenderingChanged(OWLEntity entity) {
try {
for (OWLObjectTreeNode<N> node : getNodes(entity)) {
DefaultTreeModel model = (DefaultTreeModel) getModel();
model.nodeStructureChanged(node);
model.nodeChanged(node);
for (N equivalentEntity: node.getEquivalentObjects()) {
for (OWLObjectTreeNode<N> equivalentNode : getNodes(equivalentEntity)) {
model.nodeChanged(equivalentNode);
}
}
}
}
catch (ClassCastException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,21 @@ public String getToolTipText(MouseEvent event) {

@SuppressWarnings("unchecked")
private void updateNode(N node) {
// This method is called when the parents or children of
// a node might have changed. We handle the following possibilities
// This method is called when the node, its parents or children
// might have changed. We handle the following possibilities
// 1) The node had a child added
// 2) The node had a child removed
// If we are displaying the node which had the child added or removed
// then we update the node

final Set<OWLObjectTreeNode<N>> treeNodes = nodeMap.get(node);

// The parents/children might have changed
// The node/parents/children might have changed
if (treeNodes != null && !treeNodes.isEmpty()) {
// The node equivalent objects might have changed
// @@TODO: use provider to check if this were the case
treeNodes.forEach(((DefaultTreeModel) getModel())::nodeChanged);

// Remove children that aren't there any more
Set<N> children = provider.getChildren(node);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,112 +1,113 @@
package org.protege.editor.owl.ui.renderer;

import org.hamcrest.Matchers;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 21 Feb 2018
*/
public class RenderingEscapeUtils_TestCase {

private static void assertEscaped(String original, String escaped) {
String actualEscaped = RenderingEscapeUtils.getEscapedRendering(original);
assertThat(actualEscaped, Matchers.is(escaped));
String actualUnescaped = RenderingEscapeUtils.unescape(escaped);
assertThat(actualUnescaped, Matchers.is(original));
}

@Test
public void shouldNotEscapeRendering() {
String rendering = RenderingEscapeUtils.getEscapedRendering("AB");
assertThat(rendering, is("AB"));
assertEscaped("AB", "AB");
}

@Test
public void shouldEscapeRenderingContainingSpace() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A B");
assertThat(rendering, is("'A B'"));
}
public void shouldEscapeRenderingContainingSpace() {
assertEscaped("A B", "'A B'");
}

@Test
public void shouldEscapeRenderingContainingComma() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A,B");
assertThat(rendering, is("'A,B'"));
assertEscaped("A,B", "'A,B'");
}

@Test
public void shouldEscapeRenderingContainingLeftBracket() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A(B");
assertThat(rendering, is("'A(B'"));
assertEscaped("A(B", "'A(B'");
}

@Test
public void shouldEscapeRenderingContainingRightBracket() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A)B");
assertThat(rendering, is("'A)B'"));
assertEscaped("A)B", "'A)B'");
}

@Test
public void shouldEscapeRenderingContainingLeftSquareBracket() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A[B");
assertThat(rendering, is("'A[B'"));
assertEscaped("A[B", "'A[B'");
}

@Test
public void shouldEscapeRenderingContainingRightSquareBracket() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A]B");
assertThat(rendering, is("'A]B'"));
assertEscaped("A]B", "'A]B'");
}

@Test
public void shouldEscapeRenderingContainingLeftBrace() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A{B");
assertThat(rendering, is("'A{B'"));
assertEscaped("A{B", "'A{B'");
}

@Test
public void shouldEscapeRenderingContainingRightBrace() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A}B");
assertThat(rendering, is("'A}B'"));
assertEscaped("A}B", "'A}B'");
}

@Test
public void shouldEscapeRenderingContainingHat() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A^B");
assertThat(rendering, is("'A^B'"));
assertEscaped("A^B", "'A^B'");
}

@Test
public void shouldEscapeRenderingContainingAt() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A@B");
assertThat(rendering, is("'A@B'"));
assertEscaped("A@B", "'A@B'");
}

@Test
public void shouldEscapeRenderingContainingLessThan() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A<B");
assertThat(rendering, is("'A<B'"));
assertEscaped("A<B", "'A<B'");
}

@Test
public void shouldEscapeRenderingContainingGreaterThan() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A>B");
assertThat(rendering, is("'A>B'"));
assertEscaped("A>B", "'A>B'");
}

@Test
public void shouldEscapeRenderingContainingEquals() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A=B");
assertThat(rendering, is("'A=B'"));
assertEscaped("A=B", "'A=B'");
}

@Test
public void shouldEscapeSingleQuote() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A's");
assertThat(rendering, is("A\\'s"));
assertEscaped("A'B", "A\\'B");
}

@Test
public void shouldEscapeDoubleQuote() {
assertEscaped("A\"B", "A\\\"B");
}

@Test
public void shouldEscapeBackslash() {
assertEscaped("A\\B", "'A\\\\B'");
}

@Test
public void shouldEscapeSingleQuoteWithSpaces() {
String rendering = RenderingEscapeUtils.getEscapedRendering("A's and B's");
assertThat(rendering, is("'A\\'s and B\\'s'"));
assertEscaped("A's and B's", "'A\\'s and B\\'s'");
}

@Test
Expand Down