001 /* $Id: FinderFromMethod.java 471661 2006-11-06 08:09:25Z skitching $ 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.commons.digester.plugins.strategies; 020 021 import java.util.Properties; 022 import org.apache.commons.digester.Digester; 023 import org.apache.commons.digester.plugins.RuleFinder; 024 import org.apache.commons.digester.plugins.RuleLoader; 025 import org.apache.commons.digester.plugins.PluginException; 026 027 /** 028 * A rule-finding algorithm which expects the caller to specify a methodname 029 * as a plugin property, where the method exists on the plugin class. 030 * 031 * @since 1.6 032 */ 033 034 public class FinderFromMethod extends RuleFinder { 035 /** 036 * Xml attribute that needs to be present on a plugin declaration 037 * in order to specify the method to load rules from. 038 */ 039 public static String DFLT_METHOD_ATTR = "method"; 040 041 /** See {@link #findLoader}. */ 042 private String methodAttr; 043 044 /** Constructor. */ 045 public FinderFromMethod() { 046 this(DFLT_METHOD_ATTR); 047 } 048 049 /** See {@link #findLoader}. */ 050 public FinderFromMethod(String methodAttr) { 051 this.methodAttr = methodAttr; 052 } 053 054 /** 055 * If there exists a property with the name matching constructor param 056 * methodAttr, then locate the appropriate Method on the plugin class 057 * and return an object encapsulating that info. 058 * <p> 059 * If there is no matching property provided, then just return null. 060 * <p> 061 * The returned object (when non-null) will invoke the target method 062 * on the plugin class whenever its addRules method is invoked. The 063 * target method is expected to have the following prototype: 064 * <code> public static void xxxxx(Digester d, String patternPrefix); </code> 065 */ 066 public RuleLoader findLoader(Digester d, Class pluginClass, Properties p) 067 throws PluginException { 068 069 String methodName = p.getProperty(methodAttr); 070 if (methodName == null) { 071 // nope, user hasn't requested dynamic rules to be loaded 072 // from a specific class. 073 return null; 074 } 075 076 return new LoaderFromClass(pluginClass, methodName); 077 } 078 } 079