NIHVIVO-3298 Create a SparqlQueryUtils class so escapeForRegex() will be more widely available.

This commit is contained in:
j2blake 2011-11-11 22:07:03 +00:00
parent ec881de6a3
commit 402f06beb6
3 changed files with 20 additions and 14 deletions

View file

@ -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')")
+ " || "

View file

@ -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'))");
}

View file

@ -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,14 +13,17 @@ 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) {
@ -29,10 +32,13 @@ public abstract class AbstractPagingSelector {
continue outer;
}
}
if (c == '\'') {
clean.append('\\').append(c);
continue outer;
}
clean.append(c);
}
return clean.toString();
}
}