KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: FinderFromResource.java 155412 2005-02-26 12:58:36Z dirkv $
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.util.Properties JavaDoc;
21 import java.io.InputStream 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 expects the user to specify a resource
30  * name (ie a file in the classpath). The file is expected to contain Digester
31  * rules in xmlrules format.
32  *
33  * @since 1.6
34  */

35
36 public class FinderFromResource extends RuleFinder {
37     /**
38      * Name of xml attribute on the plugin declaration which is used
39      * to configure rule-loading for that declaration.
40      */

41     public static String JavaDoc DFLT_RESOURCE_ATTR = "resource";
42     
43     /** See {@link #findLoader}. */
44     private String JavaDoc resourceAttr;
45     
46     /** Constructor. */
47     public FinderFromResource() {
48         this(DFLT_RESOURCE_ATTR);
49     }
50
51     /** See {@link #findLoader}. */
52     public FinderFromResource(String JavaDoc resourceAttr) {
53         this.resourceAttr = resourceAttr;
54     }
55     
56     /**
57      * If there exists a property with the name matching constructor param
58      * resourceAttr, then load that file, run it through the xmlrules
59      * module and return an object encapsulating those rules.
60      * <p>
61      * If there is no matching property provided, then just return null.
62      * <p>
63      * The returned object (when non-null) will add the selected rules to
64      * the digester whenever its addRules method is invoked.
65      */

66     public RuleLoader findLoader(Digester d, Class JavaDoc pluginClass, Properties JavaDoc p)
67                         throws PluginException {
68
69         String JavaDoc resourceName = p.getProperty(resourceAttr);
70         if (resourceName == null) {
71             // nope, user hasn't requested dynamic rules to be loaded
72
// from a specific file.
73
return null;
74         }
75         
76         InputStream JavaDoc is =
77             pluginClass.getClassLoader().getResourceAsStream(
78                 resourceName);
79
80         if (is == null) {
81             throw new PluginException(
82                 "Resource " + resourceName + " not found.");
83         }
84         
85          return loadRules(d, pluginClass, is, resourceName);
86     }
87     
88     /**
89      * Open the specified resource file (ie a file in the classpath,
90      * including being within a jar in the classpath), run it through
91      * the xmlrules module and return an object encapsulating those rules.
92      *
93      * @param d is the digester into which rules will eventually be loaded.
94      * @param pluginClass is the class whose xml params the rules are parsing.
95      * @param is is where the xmlrules will be read from, and must be non-null.
96      * @param resourceName is a string describing the source of the xmlrules,
97      * for use in generating error messages.
98      */

99     public static RuleLoader loadRules(Digester d, Class JavaDoc pluginClass,
100                         InputStream JavaDoc is, String JavaDoc resourceName)
101                         throws PluginException {
102
103         try {
104             RuleLoader loader = new LoaderFromStream(is);
105             return loader;
106         } catch(Exception JavaDoc e) {
107             throw new PluginException(
108                 "Unable to load xmlrules from resource [" +
109                 resourceName + "]", e);
110         } finally {
111             try {
112                 is.close();
113             } catch(java.io.IOException JavaDoc ioe) {
114                 throw new PluginException(
115                     "Unable to close stream for resource [" +
116                     resourceName + "]", ioe);
117             }
118         }
119     }
120 }
121
122
Popular Tags