001    /* $Id: FinderFromDfltResource.java 729125 2008-12-23 21:21:07Z rahul $
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */ 
018     
019    package org.apache.commons.digester.plugins.strategies;
020    
021    import java.io.InputStream;
022    import java.util.Properties;
023    
024    import org.apache.commons.digester.Digester;
025    import org.apache.commons.digester.plugins.RuleFinder;
026    import org.apache.commons.digester.plugins.RuleLoader;
027    import org.apache.commons.digester.plugins.PluginException;
028    
029    /**
030     * A rule-finding algorithm which looks for a resource file in the classpath
031     * whose name is derived from the plugin class name plus a specified suffix.
032     * <p>
033     * If the resource-file is found, then it is expected to define a set of
034     * Digester rules in xmlrules format.
035     *
036     * @since 1.6
037     */
038    
039    public class FinderFromDfltResource extends RuleFinder {
040        public static String DFLT_RESOURCE_SUFFIX = "RuleInfo.xml";
041    
042        private String resourceSuffix;
043        
044        /** See {@link #findLoader}. */
045        public FinderFromDfltResource() { 
046            this(DFLT_RESOURCE_SUFFIX);
047        }
048    
049        /**
050         * Create a rule-finder which can load an xmlrules file, cache
051         * the rules away, and later add them as a plugin's custom rules
052         * when that plugin is referenced.
053         *
054         * @param resourceSuffix must be non-null.
055         */
056        public FinderFromDfltResource(String resourceSuffix) { 
057            this.resourceSuffix = resourceSuffix;
058        }
059        
060        /**
061         * If there exists a resource file whose name is equal to the plugin
062         * class name + the suffix specified in the constructor, then
063         * load that file, run it through the xmlrules module and return an object 
064         * encapsulating those rules.
065         * <p>
066         * If there is no such resource file, then just return null.
067         * <p>
068         * The returned object (when non-null) will add the selected rules to
069         * the digester whenever its addRules method is invoked.
070         */
071        public RuleLoader findLoader(Digester d, Class<?> pluginClass, Properties p)
072                            throws PluginException {
073    
074            String resourceName = 
075                pluginClass.getName().replace('.', '/') 
076                        + resourceSuffix;
077                
078            InputStream is = 
079                pluginClass.getClassLoader().getResourceAsStream(
080                    resourceName);
081    
082            if (is == null) {
083                // ok, no such resource
084                return null;
085            }
086    
087            return FinderFromResource.loadRules(d, pluginClass, is, resourceName);
088        }
089    }
090