Added mql key quote/unquote functions

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1693 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-10-25 22:23:04 +00:00
parent 96b41450da
commit c7a70333d9
3 changed files with 191 additions and 0 deletions

View File

@ -79,6 +79,11 @@ function init() {
ER.registerExporter("tripleloader", new Packages.com.google.refine.freebase.ProtographTransposeExporter.TripleLoaderExporter()); ER.registerExporter("tripleloader", new Packages.com.google.refine.freebase.ProtographTransposeExporter.TripleLoaderExporter());
ER.registerExporter("mqlwrite", new Packages.com.google.refine.freebase.ProtographTransposeExporter.MqlwriteLikeExporter()); ER.registerExporter("mqlwrite", new Packages.com.google.refine.freebase.ProtographTransposeExporter.MqlwriteLikeExporter());
var FCR = Packages.com.google.refine.grel.ControlFunctionRegistry;
FCR.registerFunction("mqlKeyQuote", new Packages.com.google.refine.freebase.expr.MqlKeyQuote());
FCR.registerFunction("mqlKeyUnquote", new Packages.com.google.refine.freebase.expr.MqlKeyUnquote());
Packages.com.google.refine.model.Project. Packages.com.google.refine.model.Project.
registerOverlayModel("freebaseProtograph", Packages.com.google.refine.freebase.protograph.Protograph); registerOverlayModel("freebaseProtograph", Packages.com.google.refine.freebase.protograph.Protograph);

View File

@ -0,0 +1,99 @@
/*
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.freebase.expr;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONWriter;
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 keyCharMustQuote = "([^" + keyInternalChar + "])";
final static private Pattern fullValidKeyPattern = Pattern.compile(fullValidKey);
final static private Pattern keyCharMustQuotePattern = Pattern.compile(keyCharMustQuote);
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object o1 = args[0];
if (o1 != null && o1 instanceof String) {
String s = (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;
}
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();
}
}

View File

@ -0,0 +1,87 @@
/*
Copyright 2010, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.freebase.expr;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.grel.Function;
public class MqlKeyUnquote implements Function {
final static private Pattern quotedCharPattern = Pattern.compile("\\$([0-9A-Fa-f]{4})");
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
Object o1 = args[0];
if (o1 != null && o1 instanceof String) {
String s = (String) o1;
StringBuffer sb = new StringBuffer();
int last = 0;
Matcher m = quotedCharPattern.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((char)Integer.parseInt(s.substring(start + 1, end), 16));
}
if (last < s.length()) {
sb.append(s.substring(last));
}
return sb.toString();
}
}
return null;
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Quotes a string into a MQL key");
writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("string");
writer.endObject();
}
}