Updates for handling single and double quotes in html and sparql data getters

This commit is contained in:
hjkhjk54 2012-08-06 19:30:47 +00:00
parent 8225bcf91f
commit 059094be4c
6 changed files with 73 additions and 4 deletions

View file

@ -282,6 +282,8 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
private void addJSONArrayToFormSpecificData(JSONArray jsonArray, EditConfigurationVTwo editConfig) { private void addJSONArrayToFormSpecificData(JSONArray jsonArray, EditConfigurationVTwo editConfig) {
HashMap<String, Object> data = editConfig.getFormSpecificData(); HashMap<String, Object> data = editConfig.getFormSpecificData();
data.put("existingPageContentUnits", jsonArray.toString()); data.put("existingPageContentUnits", jsonArray.toString());
//Experimenting with putting actual array in
data.put("existingPageContentUnitsJSONArray", jsonArray);
} }

View file

@ -199,10 +199,14 @@ public class ManagePagePreprocessor extends
if(jsonValue instanceof String) { if(jsonValue instanceof String) {
//TODO: Deal with multiple submission values //TODO: Deal with multiple submission values
//This retrieves the value for this particular json object //This retrieves the value for this particular json object
literalValues.add(jsonObject.getString(literalLabel)); String jsonString = jsonObject.getString(literalLabel);
jsonString = pn.replaceEncodedQuotesWithEscapedQuotes(jsonString);
literalValues.add(jsonString);
} else if(jsonValue instanceof JSONArray) { } else if(jsonValue instanceof JSONArray) {
JSONArray values = jsonObject.getJSONArray(literalLabel); JSONArray values = jsonObject.getJSONArray(literalLabel);
literalValues = (List<String>) JSONSerializer.toJava(values); literalValues = (List<String>) JSONSerializer.toJava(values);
//Replacing encoded quotes here as well
this.replaceEncodedQuotesInList(pn, literalValues);
} else if(jsonValue instanceof Boolean) { } else if(jsonValue instanceof Boolean) {
Boolean booleanValue = jsonObject.getBoolean(literalLabel); Boolean booleanValue = jsonObject.getBoolean(literalLabel);
//Adds string version //Adds string version
@ -232,6 +236,7 @@ public class ManagePagePreprocessor extends
//multiple values //multiple values
JSONArray values = jsonObject.getJSONArray(uriLabel); JSONArray values = jsonObject.getJSONArray(uriLabel);
uriValues = (List<String>) JSONSerializer.toJava(values); uriValues = (List<String>) JSONSerializer.toJava(values);
} else { } else {
//This may include JSON Objects but no way to deal with these right now //This may include JSON Objects but no way to deal with these right now
} }
@ -254,7 +259,17 @@ public class ManagePagePreprocessor extends
} }
private void replaceEncodedQuotesInList(ProcessDataGetterN3 pn, List<String> values) {
int i;
int len = values.size();
for(i = 0; i < len; i++) {
String value = values.get(i);
if(value.contains("&quot;") || value.contains("&#39;")) {
value = pn.replaceEncodedQuotesWithEscapedQuotes(value);
values.set(i,value);
}
}
}
private void addFields(ProcessDataGetterN3 pn, int counter) { private void addFields(ProcessDataGetterN3 pn, int counter) {

View file

@ -54,6 +54,13 @@ public abstract class ProcessDataGetterAbstract implements ProcessDataGetterN3 {
return "?" + getVarName(base, counter); return "?" + getVarName(base, counter);
} }
//For handling encoded single and double quotes
//For fixed html and sparql data getters, replaces encoded quotes with escaped quotes
//Can be overridden in other processors if need be
public String replaceEncodedQuotesWithEscapedQuotes(String inputStr) {
return inputStr.replaceAll("&#39;", "\'").replaceAll("&quot;", "\"");
}
//Return name of new resources //Return name of new resources
public List<String> getNewResources(int counter) { public List<String> getNewResources(int counter) {
//Each data getter requires a new resource //Each data getter requires a new resource

View file

@ -36,5 +36,6 @@ public interface ProcessDataGetterN3 {
public Map<String, List<String>> retrieveExistingUriValues(); public Map<String, List<String>> retrieveExistingUriValues();
public void populateExistingValues(String dataGetterURI, int counter, OntModel queryModel); public void populateExistingValues(String dataGetterURI, int counter, OntModel queryModel);
public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context); public JSONObject getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context);
public String replaceEncodedQuotesWithEscapedQuotes(String inputStr);
} }

View file

@ -14,7 +14,7 @@ var processFixedHTMLDataGetterContent = {
//JSON parsing on the server side does not handle single quotes, as it appears it thinks the string has //JSON parsing on the server side does not handle single quotes, as it appears it thinks the string has
//ended. Different data getter types may handle apostrophes/single quotes differently //ended. Different data getter types may handle apostrophes/single quotes differently
//In this case, this is HTML so it simply html Encodes any apostrophes //In this case, this is HTML so it simply html Encodes any apostrophes
htmlValue = processFixedHTMLDataGetterContent.replaceSingleQuote(htmlValue); htmlValue = processFixedHTMLDataGetterContent.encodeQuotes(htmlValue);
var returnObject = {saveToVar:saveToVarValue, htmlValue:htmlValue, dataGetterClass:this.dataGetterClass}; var returnObject = {saveToVar:saveToVarValue, htmlValue:htmlValue, dataGetterClass:this.dataGetterClass};
return returnObject; return returnObject;
}, },
@ -22,6 +22,8 @@ var processFixedHTMLDataGetterContent = {
populatePageContentSection:function(existingContentObject, pageContentSection) { populatePageContentSection:function(existingContentObject, pageContentSection) {
var saveToVarValue = existingContentObject["saveToVar"]; var saveToVarValue = existingContentObject["saveToVar"];
var htmlValue = existingContentObject["htmlValue"]; var htmlValue = existingContentObject["htmlValue"];
//In displaying the html value for the edit field, replace the encoded quotes with regular quotes
htmlValue = processFixedHTMLDataGetterContent.replaceEncodedWithEscapedQuotes(htmlValue);
//Now find and set value //Now find and set value
pageContentSection.find("input[name='saveToVar']").val(saveToVarValue); pageContentSection.find("input[name='saveToVar']").val(saveToVarValue);
pageContentSection.find("textarea[name='htmlValue']").val(htmlValue); pageContentSection.find("textarea[name='htmlValue']").val(htmlValue);
@ -55,7 +57,7 @@ var processFixedHTMLDataGetterContent = {
} }
return validationError; return validationError;
}, },
replaceSingleQuote:function(inputStr) { encodeQuotes:function(inputStr) {
return inputStr.replace(/'/g, '&#39;').replace(/"/g, '&quot;'); return inputStr.replace(/'/g, '&#39;').replace(/"/g, '&quot;');
}, },
//For the variable name, no single quote should be allowed //For the variable name, no single quote should be allowed
@ -65,6 +67,9 @@ var processFixedHTMLDataGetterContent = {
}, },
stringHasDoubleQuote:function(inputStr) { stringHasDoubleQuote:function(inputStr) {
return(inputStr.indexOf("\"") != -1); return(inputStr.indexOf("\"") != -1);
},
replaceEncodedWithEscapedQuotes: function(inputStr) {
return inputStr.replace(/&#39;/g, "\'").replace(/&quot;/g, "\"");
} }

View file

@ -11,6 +11,7 @@ var processSparqlDataGetterContent = {
var variableValue = pageContentSection.find("input[name='saveToVar']").val(); var variableValue = pageContentSection.find("input[name='saveToVar']").val();
var queryValue = pageContentSection.find("textarea[name='query']").val(); var queryValue = pageContentSection.find("textarea[name='query']").val();
queryValue = processSparqlDataGetterContent.encodeQuotes(queryValue);
var queryModel = pageContentSection.find("input[name='queryModel']").val(); var queryModel = pageContentSection.find("input[name='queryModel']").val();
//query model should also be an input //query model should also be an input
@ -22,7 +23,11 @@ var processSparqlDataGetterContent = {
populatePageContentSection:function(existingContentObject, pageContentSection) { populatePageContentSection:function(existingContentObject, pageContentSection) {
var saveToVarValue = existingContentObject["saveToVar"]; var saveToVarValue = existingContentObject["saveToVar"];
var queryValue = existingContentObject["query"]; var queryValue = existingContentObject["query"];
//replace any encoded quotes with escaped quotes that will show up as quotes in the textarea
queryValue = processSparqlDataGetterContent.replaceEncodedWithEscapedQuotes(queryValue);
var queryModelValue = existingContentObject["queryModel"]; var queryModelValue = existingContentObject["queryModel"];
//Now find and set value //Now find and set value
pageContentSection.find("input[name='saveToVar']").val(saveToVarValue); pageContentSection.find("input[name='saveToVar']").val(saveToVarValue);
pageContentSection.find("textarea[name='query']").val(queryValue); pageContentSection.find("textarea[name='query']").val(queryValue);
@ -45,11 +50,45 @@ var processSparqlDataGetterContent = {
if(variableValue == "") { if(variableValue == "") {
validationError += pageContentSectionLabel + ": You must supply a variable to save query results. <br />" validationError += pageContentSectionLabel + ": You must supply a variable to save query results. <br />"
} }
if(processSparqlDataGetterContent.stringHasSingleQuote(variableValue)) {
validationError += pageContentSectionLabel + ": The variable name should not have an apostrophe . <br />";
}
if(processSparqlDataGetterContent.stringHasDoubleQuote(variableValue)) {
validationError += pageContentSectionLabel + ": The variable name should not have a double quote . <br />";
}
//Check that query model does not have single or double quotes within it
//Uncomment this/adapt this when we actually allow display the query model input
/*
var queryModelValue = pageContentSection.find("input[name='queryModel']").val();
if(processSparqlDataGetterContent.stringHasSingleQuote(queryModelValue)) {
validationError += pageContentSectionLabel + ": The query model should not have an apostrophe . <br />";
}
if(processSparqlDataGetterContent.stringHasDoubleQuote(queryModelValue)) {
validationError += pageContentSectionLabel + ": The query model should not have a double quote . <br />";
}*/
var queryValue = pageContentSection.find("textarea[name='query']").val(); var queryValue = pageContentSection.find("textarea[name='query']").val();
if(queryValue == "") { if(queryValue == "") {
validationError += pageContentSectionLabel + ": You must supply a Sparql query. <br />"; validationError += pageContentSectionLabel + ": You must supply a Sparql query. <br />";
} }
return validationError; return validationError;
},
encodeQuotes:function(inputStr) {
return inputStr.replace(/'/g, '&#39;').replace(/"/g, '&quot;');
},
//For the variable name, no single quote should be allowed
//This can be extended for other special characters
stringHasSingleQuote:function(inputStr) {
return(inputStr.indexOf("'") != -1);
},
stringHasDoubleQuote:function(inputStr) {
return(inputStr.indexOf("\"") != -1);
},
replaceEncodedWithEscapedQuotes: function(inputStr) {
return inputStr.replace(/&#39;/g, "\'").replace(/&quot;/g, "\"");
} }