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.io.InputStream;
23  import java.util.Properties;
24  
25  import org.apache.commons.digester3.Digester;
26  import org.apache.commons.digester3.plugins.PluginException;
27  import org.apache.commons.digester3.plugins.RuleFinder;
28  import org.apache.commons.digester3.plugins.RuleLoader;
29  
30  /**
31   * A rule-finding algorithm which looks for a resource file in the classpath whose name is derived from the plugin class
32   * name plus a specified suffix.
33   * <p>
34   * If the resource-file is found, then it is expected to define a set of Digester rules in xmlrules format.
35   * 
36   * @since 1.6
37   */
38  public class FinderFromDfltResource
39      extends RuleFinder
40  {
41  
42      private static final String DFLT_RESOURCE_SUFFIX = "RuleInfo.xml";
43  
44      private final String resourceSuffix;
45  
46      /** See {@link #findLoader}. */
47      public FinderFromDfltResource()
48      {
49          this( DFLT_RESOURCE_SUFFIX );
50      }
51  
52      /**
53       * Create a rule-finder which can load an xmlrules file, cache the rules away, and later add them as a plugin's
54       * custom rules when that plugin is referenced.
55       * 
56       * @param resourceSuffix must be non-null.
57       */
58      public FinderFromDfltResource( String resourceSuffix )
59      {
60          this.resourceSuffix = resourceSuffix;
61      }
62  
63      /**
64       * If there exists a resource file whose name is equal to the plugin class name + the suffix specified in the
65       * constructor, then load that file, run it through the xmlrules module and return an object encapsulating those
66       * rules.
67       * <p>
68       * If there is no such resource file, then just return null.
69       * <p>
70       * The returned object (when non-null) will add the selected rules to the digester whenever its addRules method is
71       * invoked.
72       *
73       * @param d The digester instance where locating plugin classes
74       * @param pluginClass The plugin Java class
75       * @param p The properties object that holds any xml attributes the user may have specified on the plugin
76       *          declaration in order to indicate how to locate the plugin rules.
77       * @return a source of digester rules for the specified plugin class.
78       * @throws PluginException if the algorithm finds a source of rules, but there is something invalid
79       *         about that source.
80       */
81      @Override
82      public RuleLoader findLoader( Digester d, Class<?> pluginClass, Properties p )
83          throws PluginException
84      {
85  
86          String resourceName = pluginClass.getName().replace( '.', '/' ) + resourceSuffix;
87  
88          InputStream is = pluginClass.getClassLoader().getResourceAsStream( resourceName );
89  
90          if ( is == null )
91          {
92              // ok, no such resource
93              return null;
94          }
95  
96          return FinderFromResource.loadRules( d, pluginClass, is, resourceName );
97      }
98  
99  }