01: /* $Id: FinderFromDfltResource.java 471661 2006-11-06 08:09:25Z skitching $
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.apache.commons.digester.plugins.strategies;
20:
21: import java.io.InputStream;
22: import java.util.Properties;
23:
24: import org.apache.commons.digester.Digester;
25: import org.apache.commons.digester.plugins.RuleFinder;
26: import org.apache.commons.digester.plugins.RuleLoader;
27: import org.apache.commons.digester.plugins.PluginException;
28:
29: /**
30: * A rule-finding algorithm which looks for a resource file in the classpath
31: * whose name is derived from the plugin class name plus a specified suffix.
32: * <p>
33: * If the resource-file is found, then it is expected to define a set of
34: * Digester rules in xmlrules format.
35: *
36: * @since 1.6
37: */
38:
39: public class FinderFromDfltResource extends RuleFinder {
40: public static String DFLT_RESOURCE_SUFFIX = "RuleInfo.xml";
41:
42: private String resourceSuffix;
43:
44: /** See {@link #findLoader}. */
45: public FinderFromDfltResource() {
46: this (DFLT_RESOURCE_SUFFIX);
47: }
48:
49: /**
50: * Create a rule-finder which can load an xmlrules file, cache
51: * the rules away, and later add them as a plugin's custom rules
52: * when that plugin is referenced.
53: *
54: * @param resourceSuffix must be non-null.
55: */
56: public FinderFromDfltResource(String resourceSuffix) {
57: this .resourceSuffix = resourceSuffix;
58: }
59:
60: /**
61: * If there exists a resource file whose name is equal to the plugin
62: * class name + the suffix specified in the constructor, then
63: * load that file, run it through the xmlrules module and return an object
64: * encapsulating those rules.
65: * <p>
66: * If there is no such resource file, then just return null.
67: * <p>
68: * The returned object (when non-null) will add the selected rules to
69: * the digester whenever its addRules method is invoked.
70: */
71: public RuleLoader findLoader(Digester d, Class pluginClass,
72: Properties p) throws PluginException {
73:
74: String resourceName = pluginClass.getName().replace('.', '/')
75: + resourceSuffix;
76:
77: InputStream is = pluginClass.getClassLoader()
78: .getResourceAsStream(resourceName);
79:
80: if (is == null) {
81: // ok, no such resource
82: return null;
83: }
84:
85: return FinderFromResource.loadRules(d, pluginClass, is,
86: resourceName);
87: }
88: }
|