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:
parent
3a9ea77b5c
commit
c65627524f
@ -46,7 +46,8 @@ import com.google.refine.grel.Function;
|
|||||||
public class MqlKeyQuote implements Function {
|
public class MqlKeyQuote implements Function {
|
||||||
final static private String keyStartChar = "A-Za-z0-9";
|
final static private String keyStartChar = "A-Za-z0-9";
|
||||||
final static private String keyInternalChar = "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 String keyCharMustQuote = "([^" + keyInternalChar + "])";
|
||||||
|
|
||||||
final static private Pattern fullValidKeyPattern = Pattern.compile(fullValidKey);
|
final static private Pattern fullValidKeyPattern = Pattern.compile(fullValidKey);
|
||||||
@ -56,37 +57,12 @@ public class MqlKeyQuote implements Function {
|
|||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
Object o1 = args[0];
|
Object o1 = args[0];
|
||||||
if (o1 != null && o1 instanceof String) {
|
if (o1 != null && o1 instanceof String) {
|
||||||
String s = (String) o1;
|
return mqlKeyQuote((String) o1);
|
||||||
if (fullValidKeyPattern.matcher(s).find()) {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
|
|
||||||
int last = 0;
|
|
||||||
Matcher m = keyCharMustQuotePattern.matcher(s);
|
|
||||||
while (m.find()) {
|
|
||||||
int start = m.start();
|
|
||||||
int end = m.end();
|
|
||||||
if (start > last) {
|
|
||||||
sb.append(s.substring(last, start));
|
|
||||||
}
|
|
||||||
last = end;
|
|
||||||
|
|
||||||
sb.append('$');
|
|
||||||
sb.append(StringUtils.leftPad(Integer.toHexString(s.charAt(start)).toUpperCase(), 4, '0'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (last < s.length()) {
|
|
||||||
sb.append(s.substring(last));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(JSONWriter writer, Properties options)
|
public void write(JSONWriter writer, Properties options)
|
||||||
throws JSONException {
|
throws JSONException {
|
||||||
|
|
||||||
@ -96,4 +72,52 @@ public class MqlKeyQuote implements Function {
|
|||||||
writer.key("returns"); writer.value("string");
|
writer.key("returns"); writer.value("string");
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String mqlKeyQuote(String s) {
|
||||||
|
if (fullValidKeyPattern.matcher(s).find()) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
|
||||||
|
int last = 0;
|
||||||
|
Matcher m = keyCharMustQuotePattern.matcher(s);
|
||||||
|
while (m.find()) {
|
||||||
|
int start = m.start();
|
||||||
|
int end = m.end();
|
||||||
|
if (start > last) {
|
||||||
|
sb.append(s.substring(last, start));
|
||||||
|
}
|
||||||
|
last = end;
|
||||||
|
|
||||||
|
sb.append('$');
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
static String quote(char c) {
|
||||||
|
return StringUtils.leftPad(Integer.toHexString(c).toUpperCase(), 4, '0');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user