View Javadoc

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 expects the caller to specify a methodname as a plugin property, where the method
31   * exists on the plugin class.
32   * 
33   * @since 1.6
34   */
35  public class FinderFromMethod
36      extends RuleFinder
37  {
38  
39      /**
40       * Default XML attribute that needs to be present on a plugin declaration in order to specify the method
41       * to load rules from.
42       */
43      private static final String DFLT_METHOD_ATTR = "method";
44  
45      /** See {@link #findLoader}. */
46      private final String methodAttr;
47  
48      /** Constructor. */
49      public FinderFromMethod()
50      {
51          this( DFLT_METHOD_ATTR );
52      }
53  
54      /**
55       * See {@link #findLoader}.
56       *
57       * @param methodAttr The XML attribute that needs to be present on a plugin declaration in order to specify the
58       *        method to load rules from.
59       */
60      public FinderFromMethod( String methodAttr )
61      {
62          this.methodAttr = methodAttr;
63      }
64  
65      /**
66       * If there exists a property with the name matching constructor param methodAttr, then locate the appropriate
67       * Method on the plugin class and return an object encapsulating that info.
68       * <p>
69       * If there is no matching property provided, then just return null.
70       * <p>
71       * The returned object (when non-null) will invoke the target method on the plugin class whenever its addRules
72       * method is invoked. The target method is expected to have the following prototype:
73       * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
74       *
75       * @param d The digester instance where locating plugin classes
76       * @param pluginClass The plugin Java class
77       * @param p The properties object that holds any xml attributes the user may have specified on the plugin
78       *          declaration in order to indicate how to locate the plugin rules.
79       * @return a source of digester rules for the specified plugin class.
80       * @throws PluginException if the algorithm finds a source of rules, but there is something invalid
81       *         about that source.
82       */
83      @Override
84      public RuleLoader findLoader( Digester d, Class<?> pluginClass, Properties p )
85          throws PluginException
86      {
87  
88          String methodName = p.getProperty( methodAttr );
89          if ( methodName == null )
90          {
91              // nope, user hasn't requested dynamic rules to be loaded
92              // from a specific class.
93              return null;
94          }
95  
96          return new LoaderFromClass( pluginClass, methodName );
97      }
98  
99  }