From c7a70333d9212d4d97bd12982fdb8a31778cd32c Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 25 Oct 2010 22:23:04 +0000 Subject: [PATCH] Added mql key quote/unquote functions git-svn-id: http://google-refine.googlecode.com/svn/trunk@1693 7d457c2a-affb-35e4-300a-418c747d4874 --- .../freebase/module/MOD-INF/controller.js | 5 + .../refine/freebase/expr/MqlKeyQuote.java | 99 +++++++++++++++++++ .../refine/freebase/expr/MqlKeyUnquote.java | 87 ++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyQuote.java create mode 100644 extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyUnquote.java diff --git a/extensions/freebase/module/MOD-INF/controller.js b/extensions/freebase/module/MOD-INF/controller.js index 72b7b560e..0ab7a0e45 100644 --- a/extensions/freebase/module/MOD-INF/controller.js +++ b/extensions/freebase/module/MOD-INF/controller.js @@ -79,6 +79,11 @@ function init() { ER.registerExporter("tripleloader", new Packages.com.google.refine.freebase.ProtographTransposeExporter.TripleLoaderExporter()); 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. registerOverlayModel("freebaseProtograph", Packages.com.google.refine.freebase.protograph.Protograph); diff --git a/extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyQuote.java b/extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyQuote.java new file mode 100644 index 000000000..5eba21317 --- /dev/null +++ b/extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyQuote.java @@ -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(); + } +} diff --git a/extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyUnquote.java b/extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyUnquote.java new file mode 100644 index 000000000..47d98fa31 --- /dev/null +++ b/extensions/freebase/src/com/google/refine/freebase/expr/MqlKeyUnquote.java @@ -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(); + } +}