Adding classes to now make it possible to parse Html in GREL. Uses small subset of methods from the JSoup library, licensed under the MIT license.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1948 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Iain Sproat 2010-12-06 23:15:24 +00:00
parent 9aaa1c9919
commit f55f11cd0d
11 changed files with 447 additions and 58 deletions

View File

@ -1,58 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="main/src"/>
<classpathentry kind="src" path="broker/core/src"/>
<classpathentry kind="src" path="extensions/jython/src"/>
<classpathentry kind="src" path="server/src"/>
<classpathentry kind="src" path="extensions/freebase/src"/>
<classpathentry kind="src" path="extensions/gdata/src"/>
<classpathentry kind="src" path="extensions/sample/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/ant-tools-1.8.0.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/arithcode-1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/butterfly-trunk.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/clojure-1.1.0.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-codec-1.4.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-collections-3.2.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-fileupload-1.2.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-io-1.4.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-lang-2.5.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/httpclient-4.0.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/httpcore-4.0.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/icu4j-4.2.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jackson-core-asl-1.5.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jcl-over-slf4j-1.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jrdf-0.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/json-20100208.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/lessen-trunk-r8.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/marc4j-2.4.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/opencsv-2.2.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/poi-3.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/poi-ooxml-3.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/poi-ooxml-schemas-3.6-20091214.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/rhino-1.7R2.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/secondstring-20100303.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/signpost-commonshttp4-1.2.1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/signpost-core-1.2.1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/slf4j-api-1.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/slf4j-log4j12-1.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/velocity-1.5.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/vicino-1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/xmlbeans-2.3.0.jar"/>
<classpathentry kind="lib" path="server/lib/jdatapath-alpha2.jar"/>
<classpathentry kind="lib" path="server/lib/jetty-6.1.22.jar"/>
<classpathentry kind="lib" path="server/lib/jetty-util-6.1.22.jar"/>
<classpathentry kind="lib" path="server/lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="server/lib/servlet-api-2.5.jar"/>
<classpathentry kind="lib" path="server/lib/slf4j-api-1.5.6.jar"/>
<classpathentry kind="lib" path="server/lib/slf4j-log4j12-1.5.6.jar"/>
<classpathentry kind="lib" path="broker/appengine/WEB-INF/lib/slf4j-jdk14-1.5.6.jar"/>
<classpathentry kind="lib" path="extensions/jython/module/MOD-INF/lib/jython-2.5.1.jar"/>
<classpathentry kind="lib" path="broker/core/module/MOD-INF/lib/bdb-je-4.0.103.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/gdata-core-1.0.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/gdata-spreadsheet-3.0.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/google-collect-1.0-rc1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="main/src"/>
<classpathentry kind="src" path="broker/core/src"/>
<classpathentry kind="src" path="extensions/jython/src"/>
<classpathentry kind="src" path="server/src"/>
<classpathentry kind="src" path="extensions/freebase/src"/>
<classpathentry kind="src" path="extensions/gdata/src"/>
<classpathentry kind="src" path="extensions/sample/src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/ant-tools-1.8.0.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/arithcode-1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/butterfly-trunk.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/clojure-1.1.0.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-codec-1.4.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-collections-3.2.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-fileupload-1.2.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-io-1.4.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/commons-lang-2.5.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/httpclient-4.0.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/httpcore-4.0.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/icu4j-4.2.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jackson-core-asl-1.5.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jcl-over-slf4j-1.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jrdf-0.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/json-20100208.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/lessen-trunk-r8.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/marc4j-2.4.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/opencsv-2.2.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/poi-3.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/poi-ooxml-3.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/poi-ooxml-schemas-3.6-20091214.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/rhino-1.7R2.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/secondstring-20100303.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/signpost-commonshttp4-1.2.1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/signpost-core-1.2.1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/slf4j-api-1.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/slf4j-log4j12-1.5.6.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/velocity-1.5.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/vicino-1.1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/xmlbeans-2.3.0.jar"/>
<classpathentry kind="lib" path="server/lib/jdatapath-alpha2.jar"/>
<classpathentry kind="lib" path="server/lib/jetty-6.1.22.jar"/>
<classpathentry kind="lib" path="server/lib/jetty-util-6.1.22.jar"/>
<classpathentry kind="lib" path="server/lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="server/lib/servlet-api-2.5.jar"/>
<classpathentry kind="lib" path="server/lib/slf4j-api-1.5.6.jar"/>
<classpathentry kind="lib" path="server/lib/slf4j-log4j12-1.5.6.jar"/>
<classpathentry kind="lib" path="broker/appengine/WEB-INF/lib/slf4j-jdk14-1.5.6.jar"/>
<classpathentry kind="lib" path="extensions/jython/module/MOD-INF/lib/jython-2.5.1.jar"/>
<classpathentry kind="lib" path="broker/core/module/MOD-INF/lib/bdb-je-4.0.103.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/gdata-core-1.0.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/gdata-spreadsheet-3.0.jar"/>
<classpathentry kind="lib" path="extensions/gdata/module/MOD-INF/lib/google-collect-1.0-rc1.jar"/>
<classpathentry kind="lib" path="main/webapp/WEB-INF/lib/jsoup-1.4.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -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

View File

@ -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.

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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());

Binary file not shown.

Binary file not shown.