001    /* $Id: FinderFromDfltResource.java 992060 2010-09-02 19:09:47Z simonetripodi $
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    package org.apache.commons.digester.plugins.strategies;
019    
020    import java.io.InputStream;
021    import java.util.Properties;
022    
023    import org.apache.commons.digester.Digester;
024    import org.apache.commons.digester.plugins.RuleFinder;
025    import org.apache.commons.digester.plugins.RuleLoader;
026    import org.apache.commons.digester.plugins.PluginException;
027    
028    /**
029     * A rule-finding algorithm which looks for a resource file in the classpath
030     * whose name is derived from the plugin class name plus a specified suffix.
031     * <p>
032     * If the resource-file is found, then it is expected to define a set of
033     * Digester rules in xmlrules format.
034     *
035     * @since 1.6
036     */
037    
038    public class FinderFromDfltResource extends RuleFinder {
039        public static String DFLT_RESOURCE_SUFFIX = "RuleInfo.xml";
040    
041        private String resourceSuffix;
042        
043        /** See {@link #findLoader}. */
044        public FinderFromDfltResource() { 
045            this(DFLT_RESOURCE_SUFFIX);
046        }
047    
048        /**
049         * Create a rule-finder which can load an xmlrules file, cache
050         * the rules away, and later add them as a plugin's custom rules
051         * when that plugin is referenced.
052         *
053         * @param resourceSuffix must be non-null.
054         */
055        public FinderFromDfltResource(String resourceSuffix) { 
056            this.resourceSuffix = resourceSuffix;
057        }
058        
059        /**
060         * If there exists a resource file whose name is equal to the plugin
061         * class name + the suffix specified in the constructor, then
062         * load that file, run it through the xmlrules module and return an object 
063         * encapsulating those rules.
064         * <p>
065         * If there is no such resource file, then just return null.
066         * <p>
067         * The returned object (when non-null) will add the selected rules to
068         * the digester whenever its addRules method is invoked.
069         */
070        @Override
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