1 package org.apache.commons.digester3.plugins.strategies; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.Properties; 23 24 import org.apache.commons.digester3.Digester; 25 import org.apache.commons.digester3.plugins.PluginException; 26 import org.apache.commons.digester3.plugins.RuleFinder; 27 import org.apache.commons.digester3.plugins.RuleLoader; 28 29 /** 30 * A rule-finding algorithm which looks for a method with a specific name on a class whose name is derived from the 31 * plugin class name. 32 * 33 * @since 1.6 34 */ 35 public class FinderFromDfltClass 36 extends RuleFinder 37 { 38 private static final String DFLT_RULECLASS_SUFFIX = "RuleInfo"; 39 40 private static final String DFLT_METHOD_NAME = "addRules"; 41 42 private final String rulesClassSuffix; 43 44 private final String methodName; 45 46 /** See {@link #findLoader}. */ 47 public FinderFromDfltClass() 48 { 49 this( DFLT_RULECLASS_SUFFIX, DFLT_METHOD_NAME ); 50 } 51 52 /** 53 * Create a rule-finder which invokes a method on a class whenever dynamic rules for a plugin need to be loaded. See 54 * the findRules method for more info. 55 * 56 * @param rulesClassSuffix must be non-null. 57 * @param methodName may be null. 58 */ 59 public FinderFromDfltClass( String rulesClassSuffix, String methodName ) 60 { 61 this.rulesClassSuffix = rulesClassSuffix; 62 this.methodName = methodName; 63 } 64 65 /** 66 * If there exists a class whose name is the plugin class name + the suffix specified to the constructor, then load 67 * that class, locate the appropriate rules-adding method on that class, and return an object encapsulating that 68 * info. 69 * <p> 70 * If there is no such class, then just return null. 71 * <p> 72 * The returned object (when non-null) will invoke the target method on the selected class whenever its addRules 73 * method is invoked. The target method is expected to have the following prototype: 74 * <code> public static void xxxxx(Digester d, String patternPrefix); </code> 75 * 76 * @param digester The digester instance where locating plugin classes 77 * @param pluginClass The plugin Java class 78 * @param p The properties object that holds any xml attributes the user may have specified on the plugin 79 * declaration in order to indicate how to locate the plugin rules. 80 * @return a source of digester rules for the specified plugin class. 81 * @throws PluginException if the algorithm finds a source of rules, but there is something invalid 82 * about that source. 83 */ 84 @Override 85 public RuleLoader findLoader( Digester digester, Class<?> pluginClass, Properties p ) 86 throws PluginException 87 { 88 String rulesClassName = pluginClass.getName() + rulesClassSuffix; 89 90 Class<?> rulesClass = null; 91 try 92 { 93 rulesClass = digester.getClassLoader().loadClass( rulesClassName ); 94 } 95 catch ( ClassNotFoundException cnfe ) 96 { 97 // nope, no rule-info class in the classpath 98 return null; 99 } 100 101 if ( methodName == null ) 102 { 103 return new LoaderFromClass( rulesClass, DFLT_METHOD_NAME ); 104 } 105 106 return new LoaderFromClass( rulesClass, methodName ); 107 } 108 109 }