KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > strategies > FinderFromDfltResource


1 /* $Id: FinderFromDfltResource.java 179714 2005-06-03 03:53:39Z skitching $
2  *
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17  
18 package org.apache.commons.digester.plugins.strategies;
19
20 import java.io.InputStream JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.apache.commons.digester.Digester;
24 import org.apache.commons.digester.plugins.RuleFinder;
25 import org.apache.commons.digester.plugins.RuleLoader;
26 import org.apache.commons.digester.plugins.PluginException;
27
28 /**
29  * A rule-finding algorithm which looks for a resource file in the classpath
30  * whose name is derived from the plugin class name plus a specified suffix.
31  * <p>
32  * If the resource-file is found, then it is expected to define a set of
33  * Digester rules in xmlrules format.
34  *
35  * @since 1.6
36  */

37
38 public class FinderFromDfltResource extends RuleFinder {
39     public static String JavaDoc DFLT_RESOURCE_SUFFIX = "RuleInfo.xml";
40
41     private String JavaDoc resourceSuffix;
42     
43     /** See {@link #findLoader}. */
44     public FinderFromDfltResource() {
45         this(DFLT_RESOURCE_SUFFIX);
46     }
47
48     /**
49      * Create a rule-finder which can load an xmlrules file, cache
50      * the rules away, and later add them as a plugin's custom rules
51      * when that plugin is referenced.
52      *
53      * @param resourceSuffix must be non-null.
54      */

55     public FinderFromDfltResource(String JavaDoc resourceSuffix) {
56         this.resourceSuffix = resourceSuffix;
57     }
58     
59     /**
60      * If there exists a resource file whose name is equal to the plugin
61      * class name + the suffix specified in the constructor, then
62      * load that file, run it through the xmlrules module and return an object
63      * encapsulating those rules.
64      * <p>
65      * If there is no such resource file, then just return null.
66      * <p>
67      * The returned object (when non-null) will add the selected rules to
68      * the digester whenever its addRules method is invoked.
69      */

70     public RuleLoader findLoader(Digester d, Class JavaDoc pluginClass, Properties JavaDoc p)
71                         throws PluginException {
72
73         String JavaDoc resourceName =
74             pluginClass.getName().replace('.', '/')
75                     + resourceSuffix;
76             
77         InputStream JavaDoc is =
78             pluginClass.getClassLoader().getResourceAsStream(
79                 resourceName);
80
81         if (is == null) {
82             // ok, no such resource
83
return null;
84         }
85
86         return FinderFromResource.loadRules(d, pluginClass, is, resourceName);
87     }
88 }
89
90
Popular Tags