KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: FinderFromFile.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 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.commons.digester.Digester;
26 import org.apache.commons.digester.plugins.RuleFinder;
27 import org.apache.commons.digester.plugins.RuleLoader;
28 import org.apache.commons.digester.plugins.PluginException;
29
30 /**
31  * A rule-finding algorithm which expects the user to specify an absolute
32  * or relative path in the plugin declaration.
33  * <p>
34  * The file is expected to contain Digester rules in xmlrules format.
35  *
36  * @since 1.6
37  */

38
39 public class FinderFromFile extends RuleFinder {
40     /**
41      * Xml attribute that needs to be present on a plugin declaration
42      * in order to specify the file to load rules from.
43      */

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

69     public RuleLoader findLoader(Digester d, Class JavaDoc pluginClass, Properties JavaDoc p)
70                         throws PluginException {
71
72         String JavaDoc rulesFileName = p.getProperty(filenameAttr);
73         if (rulesFileName == null) {
74             // nope, user hasn't requested dynamic rules to be loaded
75
// from a specific file.
76
return null;
77         }
78         
79         InputStream JavaDoc is = null;
80         try {
81             is = new FileInputStream JavaDoc(rulesFileName);
82         } catch(IOException JavaDoc ioe) {
83             throw new PluginException(
84                 "Unable to process file [" + rulesFileName + "]", ioe);
85         }
86         
87         try {
88             RuleLoader loader = new LoaderFromStream(is);
89             return loader;
90         } catch(Exception JavaDoc e) {
91             throw new PluginException(
92                 "Unable to load xmlrules from file [" +
93                 rulesFileName + "]", e);
94         } finally {
95             try {
96                 is.close();
97             } catch(java.io.IOException JavaDoc ioe) {
98                 throw new PluginException(
99                     "Unable to close stream for file [" +
100                     rulesFileName + "]", ioe);
101             }
102         }
103     }
104 }
105
106
Popular Tags