From 402f06beb60c5828d85a0c189858d0964e4bdd4f Mon Sep 17 00:00:00 2001 From: j2blake Date: Fri, 11 Nov 2011 22:07:03 +0000 Subject: [PATCH] NIHVIVO-3298 Create a SparqlQueryUtils class so escapeForRegex() will be more widely available. --- .../accounts/UserAccountsSelector.java | 8 ++++---- .../ProxyRelationshipSelector.java | 6 +++--- .../SparqlQueryUtils.java} | 20 ++++++++++++------- 3 files changed, 20 insertions(+), 14 deletions(-) rename webapp/src/edu/cornell/mannlib/vitro/webapp/{controller/AbstractPagingSelector.java => utils/SparqlQueryUtils.java} (62%) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java index 3db6dbdbf..987d1564c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/UserAccountsSelector.java @@ -19,15 +19,15 @@ import com.hp.hpl.jena.rdf.model.Resource; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount.Status; -import edu.cornell.mannlib.vitro.webapp.controller.AbstractPagingSelector; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering.Field; import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner; import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner.QueryParser; +import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryUtils; /** * Pull some UserAccounts from the model, based on a set of criteria. */ -public class UserAccountsSelector extends AbstractPagingSelector { +public class UserAccountsSelector { private static final Log log = LogFactory .getLog(UserAccountsSelector.class); @@ -171,7 +171,7 @@ public class UserAccountsSelector extends AbstractPagingSelector { String searchTerm = criteria.getSearchTerm(); if (!roleFilterUri.isEmpty()) { - String clean = escapeForRegex(roleFilterUri); + String clean = SparqlQueryUtils.escapeForRegex(roleFilterUri); filters += "OPTIONAL { ?uri auth:hasPermissionSet ?role } \n" + " FILTER (REGEX(str(?role), '^" + clean + "$'))"; } @@ -181,7 +181,7 @@ public class UserAccountsSelector extends AbstractPagingSelector { } if (!searchTerm.isEmpty()) { - String clean = escapeForRegex(searchTerm); + String clean = SparqlQueryUtils.escapeForRegex(searchTerm); filters += "FILTER (" + ("REGEX(?email, '" + clean + "', 'i')") + " || " diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java index 233d2d238..40781e278 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ProxyRelationshipSelector.java @@ -13,18 +13,18 @@ import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; -import edu.cornell.mannlib.vitro.webapp.controller.AbstractPagingSelector; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionBuilder.ItemInfo; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionBuilder.Relationship; import edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies.ProxyRelationshipSelectionCriteria.ProxyRelationshipView; import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner; import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryRunner.QueryParser; +import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryUtils; /** * A class which will accept a ProxyRelationshipSelectionCriteria and produce a * ProxyRelationshipSelection. */ -public class ProxyRelationshipSelector extends AbstractPagingSelector { +public class ProxyRelationshipSelector { private static final Log log = LogFactory .getLog(ProxyRelationshipSelector.class); @@ -106,7 +106,7 @@ public class ProxyRelationshipSelector extends AbstractPagingSelector { if (searchTerm.isEmpty()) { return q.replace("%filterClause%", ""); } else { - String clean = escapeForRegex(searchTerm); + String clean = SparqlQueryUtils.escapeForRegex(searchTerm); return q.replace("%filterClause%", "FILTER (REGEX(str(?label), '^" + clean + "', 'i'))"); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/AbstractPagingSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryUtils.java similarity index 62% rename from webapp/src/edu/cornell/mannlib/vitro/webapp/controller/AbstractPagingSelector.java rename to webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryUtils.java index f6d2c5cfa..21036340c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/AbstractPagingSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/SparqlQueryUtils.java @@ -1,11 +1,11 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.webapp.controller; +package edu.cornell.mannlib.vitro.webapp.utils; /** - * A base class with some methods for building a selector. + * Some utility methods that help when dealing with SPARQL queries. */ -public abstract class AbstractPagingSelector { +public class SparqlQueryUtils { /** * If the user enters any of these characters in a search term, escape it * with a backslash. @@ -13,26 +13,32 @@ public abstract class AbstractPagingSelector { private static final char[] REGEX_SPECIAL_CHARACTERS = "[\\^$.|?*+()]" .toCharArray(); - /** * Escape any regex special characters in the string. * * Note that the SPARQL parser requires two backslashes, in order to pass a * single backslash to the REGEX function. + * + * Also escape a single quote ('), but only with a single backslash, since + * this one is for the SPARQL parser itself (single quote is not a special + * character to REGEX). */ - protected String escapeForRegex(String raw) { + public static String escapeForRegex(String raw) { StringBuilder clean = new StringBuilder(); outer: for (char c : raw.toCharArray()) { for (char special : REGEX_SPECIAL_CHARACTERS) { if (c == special) { clean.append('\\').append('\\').append(c); continue outer; - } + } + } + if (c == '\'') { + clean.append('\\').append(c); + continue outer; } clean.append(c); } return clean.toString(); } - }