diff --git a/contrib/src/main/java/org/archive/modules/deciderules/ExpressionDecideRule.java b/contrib/src/main/java/org/archive/modules/deciderules/ExpressionDecideRule.java new file mode 100644 index 00000000..bce342b7 --- /dev/null +++ b/contrib/src/main/java/org/archive/modules/deciderules/ExpressionDecideRule.java @@ -0,0 +1,77 @@ +/* + * This file is part of the Heritrix web crawler (crawler.archive.org). + * + * Licensed to the Internet Archive (IA) by one or more individual + * contributors. + * + * The IA licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.archive.modules.deciderules; + +import groovy.text.SimpleTemplateEngine; +import groovy.text.Template; + +import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.archive.modules.CrawlURI; + +/** + * Example usage: + *
 <bean class="org.archive.modules.deciderules.ExpressionDecideRule">
+ *     <property name="groovyExpression" value='${curi.via == null && curi ==~ "^https?://(?:www\\.)?(facebook|vimeo|flickr)\\.com/.*"}'/>
+ * </bean>
+ * + * @contributor nlevitt + */ +public class ExpressionDecideRule extends PredicatedDecideRule { + private static final long serialVersionUID = 1L; + + private static final Logger logger = + Logger.getLogger(ExpressionDecideRule.class.getName()); + + { + setGroovyExpression(""); + } + public void setGroovyExpression(String groovyExpression) { + kp.put("groovyExpression", groovyExpression); + } + public String getGroovyExpression() { + return (String) kp.get("groovyExpression"); + } + + protected ConcurrentHashMap groovyTemplates = new ConcurrentHashMap(); + protected Template groovyTemplate() { + Template groovyTemplate = groovyTemplates.get(getGroovyExpression()); + + if (groovyTemplate == null) { + try { + groovyTemplate = new SimpleTemplateEngine().createTemplate(getGroovyExpression()); + groovyTemplates.put(getGroovyExpression(), groovyTemplate); + } catch (Exception e) { + logger.log(Level.SEVERE, "problem with groovy expression " + getGroovyExpression(), e); + } + } + + return groovyTemplate; + } + + @Override + protected boolean evaluate(CrawlURI curi) { + HashMap binding = new HashMap(); + binding.put("curi", curi); + return String.valueOf(true).equals(groovyTemplate().make(binding).toString()); + } +} \ No newline at end of file