fixed bug in mql_key quoting with - and _ at the beginning and end of the string

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2027 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2011-02-25 22:51:37 +00:00
parent 3a9ea77b5c
commit c65627524f

View File

@ -46,7 +46,8 @@ import com.google.refine.grel.Function;
public class MqlKeyQuote implements Function {
final static private String keyStartChar = "A-Za-z0-9";
final static private String keyInternalChar = "A-Za-z0-9_-";
final static private String fullValidKey = "^[" + keyStartChar + "][" + keyInternalChar + "]*$";
final static private String keyEndChar = "A-Za-z0-9_";
final static private String fullValidKey = "^[" + keyStartChar + "][" + keyInternalChar + "]*[" + keyEndChar + "]$";
final static private String keyCharMustQuote = "([^" + keyInternalChar + "])";
final static private Pattern fullValidKeyPattern = Pattern.compile(fullValidKey);
@ -56,7 +57,23 @@ public class MqlKeyQuote implements Function {
if (args.length == 1) {
Object o1 = args[0];
if (o1 != null && o1 instanceof String) {
String s = (String) o1;
return mqlKeyQuote((String) o1);
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Unquotes a MQL key");
writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("string");
writer.endObject();
}
static String mqlKeyQuote(String s) {
if (fullValidKeyPattern.matcher(s).find()) {
return s;
}
@ -74,26 +91,33 @@ public class MqlKeyQuote implements Function {
last = end;
sb.append('$');
sb.append(StringUtils.leftPad(Integer.toHexString(s.charAt(start)).toUpperCase(), 4, '0'));
sb.append(quote(s.charAt(start)));
}
if (last < s.length()) {
sb.append(s.substring(last));
}
if (sb.length() > 0) {
if (sb.charAt(0) == '-' || sb.charAt(0) == '_') {
char c = sb.charAt(0);
sb.deleteCharAt(0);
sb.insert(0, '$');
sb.insert(1, quote(c));
}
int length = sb.length();
if (sb.charAt(length-1) == '-') {
sb.deleteCharAt(length-1);
sb.insert(length-1, '$');
sb.insert(length, quote('-'));
}
}
return sb.toString();
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Unquotes a MQL key");
writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("string");
writer.endObject();
static String quote(char c) {
return StringUtils.leftPad(Integer.toHexString(c).toUpperCase(), 4, '0');
}
}