diff --git a/.classpath b/.classpath index 18d858602..02068d41a 100644 --- a/.classpath +++ b/.classpath @@ -1,58 +1,59 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LICENSE.txt b/LICENSE.txt index 52fb5c047..ea325338d 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -122,6 +122,9 @@ licenses/json.LICENSE.txt licenses/mockito.LICENSE.txt mockito + +licenses/jsoup.LICENSE.txt + jsoup http://www.opensource.org/licenses/mit-license.php jquery.cookie diff --git a/licenses/jsoup.LICENSE.txt b/licenses/jsoup.LICENSE.txt new file mode 100644 index 000000000..e836992f7 --- /dev/null +++ b/licenses/jsoup.LICENSE.txt @@ -0,0 +1,11 @@ +jsoup License +The jsoup code-base (include source and compiled packages) are distributed under the open source MIT license as described below. + +The MIT License +Copyright (c) 2009, 2010 Jonathan Hedley (jonathan@hedley.net) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/main/src/com/google/refine/expr/functions/html/HtmlAttr.java b/main/src/com/google/refine/expr/functions/html/HtmlAttr.java new file mode 100644 index 000000000..505fe367b --- /dev/null +++ b/main/src/com/google/refine/expr/functions/html/HtmlAttr.java @@ -0,0 +1,75 @@ +/* + +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.expr.functions.html; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.jsoup.nodes.Element; + +import com.google.refine.expr.EvalError; +import com.google.refine.grel.ControlFunctionRegistry; +import com.google.refine.grel.Function; + +public class HtmlAttr implements Function { + + public Object call(Properties bindings, Object[] args) { + if (args.length >= 2) { + Object o1 = args[0]; + Object o2 = args[1]; + if (o1 != null && o1 instanceof Element) { + Element e1 = (Element)o1; + if(o2 != null && o2 instanceof String){ + return e1.attr(o2.toString()); + } + }else{ + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function"); + } + } + return null; + } + + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Selects a value from an attribute on an Html Element"); + writer.key("params"); writer.value("Element e, String s"); + writer.key("returns"); writer.value("String attribute Value"); + writer.endObject(); + } +} + diff --git a/main/src/com/google/refine/expr/functions/html/HtmlText.java b/main/src/com/google/refine/expr/functions/html/HtmlText.java new file mode 100644 index 000000000..66a2dcb85 --- /dev/null +++ b/main/src/com/google/refine/expr/functions/html/HtmlText.java @@ -0,0 +1,73 @@ +/* + +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.expr.functions.html; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.jsoup.nodes.Element; + +import com.google.refine.expr.EvalError; +import com.google.refine.grel.ControlFunctionRegistry; +import com.google.refine.grel.Function; + +public class HtmlText implements Function { + + public Object call(Properties bindings, Object[] args) { + if (args.length >= 1) { + Object o1 = args[0]; + if (o1 != null && o1 instanceof Element) { + Element e1 = (Element)o1; + return e1.text(); + + }else{ + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function"); + } + } + return null; + } + + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Selects the text from within an element"); + writer.key("params"); writer.value("Element e"); + writer.key("returns"); writer.value("String text"); + writer.endObject(); + } +} + diff --git a/main/src/com/google/refine/expr/functions/html/InnerHtml.java b/main/src/com/google/refine/expr/functions/html/InnerHtml.java new file mode 100644 index 000000000..db700340a --- /dev/null +++ b/main/src/com/google/refine/expr/functions/html/InnerHtml.java @@ -0,0 +1,73 @@ +/* + +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.expr.functions.html; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.jsoup.nodes.Element; + +import com.google.refine.expr.EvalError; +import com.google.refine.grel.ControlFunctionRegistry; +import com.google.refine.grel.Function; + +public class InnerHtml implements Function { + + public Object call(Properties bindings, Object[] args) { + if (args.length >= 1) { + Object o1 = args[0]; + if (o1 != null && o1 instanceof Element) { + Element e1 = (Element)o1; + return e1.html(); + + }else{ + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function"); + } + } + return null; + } + + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("The innerHtml of an HTML element"); + writer.key("params"); writer.value("Element e"); + writer.key("returns"); writer.value("String innerHtml"); + writer.endObject(); + } +} + diff --git a/main/src/com/google/refine/expr/functions/html/ParseHtml.java b/main/src/com/google/refine/expr/functions/html/ParseHtml.java new file mode 100644 index 000000000..0c548d9f0 --- /dev/null +++ b/main/src/com/google/refine/expr/functions/html/ParseHtml.java @@ -0,0 +1,67 @@ +/* + +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.expr.functions.html; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.jsoup.Jsoup; + +import com.google.refine.grel.Function; + +public class ParseHtml implements Function { + + public Object call(Properties bindings, Object[] args) { + if (args.length >= 1) { + Object o1 = args[0]; + if (o1 != null && o1 instanceof String) { + return Jsoup.parse(o1.toString()); + } + } + return null; + } + + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Parses a string as HTML"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("HTML object"); + writer.endObject(); + } +} + diff --git a/main/src/com/google/refine/expr/functions/html/SelectHtml.java b/main/src/com/google/refine/expr/functions/html/SelectHtml.java new file mode 100644 index 000000000..8df670b73 --- /dev/null +++ b/main/src/com/google/refine/expr/functions/html/SelectHtml.java @@ -0,0 +1,75 @@ +/* + +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.expr.functions.html; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.jsoup.nodes.Element; + +import com.google.refine.expr.EvalError; +import com.google.refine.grel.ControlFunctionRegistry; +import com.google.refine.grel.Function; + +public class SelectHtml implements Function { + + public Object call(Properties bindings, Object[] args) { + if (args.length >= 2) { + Object o1 = args[0]; + Object o2 = args[1]; + if (o1 != null && o1 instanceof Element) { + Element e1 = (Element)o1; + if(o2 != null && o2 instanceof String){ + return e1.select(o2.toString()); + } + }else{ + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element. Please first use parseHtml(string)"); + } + } + return null; + } + + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Selects an element from an HTML elementn using selector syntax"); + writer.key("params"); writer.value("Element e, String s"); + writer.key("returns"); writer.value("HTML Elements"); + writer.endObject(); + } +} + diff --git a/main/src/com/google/refine/grel/ControlFunctionRegistry.java b/main/src/com/google/refine/grel/ControlFunctionRegistry.java index 8bd3fff7e..bccd8a436 100644 --- a/main/src/com/google/refine/grel/ControlFunctionRegistry.java +++ b/main/src/com/google/refine/grel/ControlFunctionRegistry.java @@ -98,12 +98,16 @@ import com.google.refine.expr.functions.strings.Diff; import com.google.refine.expr.functions.strings.EndsWith; import com.google.refine.expr.functions.strings.Escape; import com.google.refine.expr.functions.strings.Fingerprint; +import com.google.refine.expr.functions.html.HtmlAttr; +import com.google.refine.expr.functions.html.HtmlText; +import com.google.refine.expr.functions.html.InnerHtml; import com.google.refine.expr.functions.strings.IndexOf; import com.google.refine.expr.functions.strings.LastIndexOf; import com.google.refine.expr.functions.strings.MD5; import com.google.refine.expr.functions.strings.Match; import com.google.refine.expr.functions.strings.NGram; import com.google.refine.expr.functions.strings.NGramFingerprint; +import com.google.refine.expr.functions.html.ParseHtml; import com.google.refine.expr.functions.strings.ParseJson; import com.google.refine.expr.functions.strings.Partition; import com.google.refine.expr.functions.strings.Phonetic; @@ -112,6 +116,7 @@ import com.google.refine.expr.functions.strings.Reinterpret; import com.google.refine.expr.functions.strings.Replace; import com.google.refine.expr.functions.strings.ReplaceChars; import com.google.refine.expr.functions.strings.SHA1; +import com.google.refine.expr.functions.html.SelectHtml; import com.google.refine.expr.functions.strings.SmartSplit; import com.google.refine.expr.functions.strings.Split; import com.google.refine.expr.functions.strings.SplitByCharType; @@ -220,6 +225,12 @@ public class ControlFunctionRegistry { registerFunction("ngram", new NGram()); registerFunction("match", new Match()); + registerFunction("parseHtml", new ParseHtml()); + registerFunction("select", new SelectHtml()); + registerFunction("htmlAttr", new HtmlAttr()); + registerFunction("htmlText", new HtmlText()); + registerFunction("innerHtml", new InnerHtml()); + registerFunction("indexOf", new IndexOf()); registerFunction("lastIndexOf", new LastIndexOf()); registerFunction("startsWith", new StartsWith()); diff --git a/main/webapp/WEB-INF/lib-src/jsoup-1.4.1-sources.jar b/main/webapp/WEB-INF/lib-src/jsoup-1.4.1-sources.jar new file mode 100644 index 000000000..018d5e46a Binary files /dev/null and b/main/webapp/WEB-INF/lib-src/jsoup-1.4.1-sources.jar differ diff --git a/main/webapp/WEB-INF/lib/jsoup-1.4.1.jar b/main/webapp/WEB-INF/lib/jsoup-1.4.1.jar new file mode 100644 index 000000000..e5effe5e5 Binary files /dev/null and b/main/webapp/WEB-INF/lib/jsoup-1.4.1.jar differ