001 package org.apache.commons.digester3.plugins.strategies; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import java.util.Properties; 023 import java.lang.reflect.Method; 024 025 import org.apache.commons.digester3.Digester; 026 import org.apache.commons.digester3.plugins.PluginException; 027 import org.apache.commons.digester3.plugins.RuleFinder; 028 import org.apache.commons.digester3.plugins.RuleLoader; 029 030 /** 031 * A rule-finding algorithm which looks for a method with a specific name on the plugin class. 032 * 033 * @since 1.6 034 */ 035 public class FinderFromDfltMethod 036 extends RuleFinder 037 { 038 private static final String DFLT_METHOD_NAME = "addRules"; 039 040 private final String methodName; 041 042 /** See {@link #findLoader}. */ 043 public FinderFromDfltMethod() 044 { 045 this( DFLT_METHOD_NAME ); 046 } 047 048 /** 049 * Create a rule-finder which invokes a specific method on the plugin class whenever dynamic rules for a plugin need 050 * to be loaded. See the findRules method for more info. 051 * 052 * @param methodName must be non-null. 053 */ 054 public FinderFromDfltMethod( String methodName ) 055 { 056 this.methodName = methodName; 057 } 058 059 /** 060 * If there exists on the plugin class a method with name matching the constructor's methodName value then locate 061 * the appropriate Method on the plugin class and return an object encapsulating that info. 062 * <p> 063 * If there is no matching method then just return null. 064 * <p> 065 * The returned object (when non-null) will invoke the target method on the plugin class whenever its addRules 066 * method is invoked. The target method is expected to have the following prototype: 067 * <code> public static void xxxxx(Digester d, String patternPrefix); </code> 068 * 069 * @param d The digester instance where locating plugin classes 070 * @param pluginClass The plugin Java class 071 * @param p The properties object that holds any xml attributes the user may have specified on the plugin 072 * declaration in order to indicate how to locate the plugin rules. 073 * @return a source of digester rules for the specified plugin class. 074 * @throws PluginException if the algorithm finds a source of rules, but there is something invalid 075 * about that source. 076 */ 077 @Override 078 public RuleLoader findLoader( Digester d, Class<?> pluginClass, Properties p ) 079 throws PluginException 080 { 081 082 Method rulesMethod = LoaderFromClass.locateMethod( pluginClass, methodName ); 083 if ( rulesMethod == null ) 084 { 085 return null; 086 } 087 088 return new LoaderFromClass( pluginClass, rulesMethod ); 089 } 090 091 }