1 package org.apache.commons.digester3.plugins.strategies;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Properties;
26
27 import org.apache.commons.digester3.Digester;
28 import org.apache.commons.digester3.plugins.PluginException;
29 import org.apache.commons.digester3.plugins.RuleFinder;
30 import org.apache.commons.digester3.plugins.RuleLoader;
31
32 /**
33 * A rule-finding algorithm which expects the user to specify an absolute or relative path in the plugin declaration.
34 * <p>
35 * The file is expected to contain Digester rules in xmlrules format.
36 *
37 * @since 1.6
38 */
39 public class FinderFromFile
40 extends RuleFinder
41 {
42
43 /**
44 * The default XML attribute that needs to be present on a plugin declaration in order to specify the file to
45 * load rules from.
46 */
47 private static final String DFLT_FILENAME_ATTR = "file";
48
49 /** See {@link #findLoader}. */
50 private final String filenameAttr;
51
52 /** See {@link #findLoader}. */
53 public FinderFromFile()
54 {
55 this( DFLT_FILENAME_ATTR );
56 }
57
58 /**
59 * See {@link #findLoader}.
60 *
61 * @param filenameAttr the XML attribute that needs to be present on a plugin declaration in order to specify the
62 * file to load rules from.
63 */
64 public FinderFromFile( String filenameAttr )
65 {
66 this.filenameAttr = filenameAttr;
67 }
68
69 /**
70 * If there exists a property with the name specified in the constructor, then load that file, run it through the
71 * xmlrules module and return an object encapsulating those rules.
72 * <p>
73 * If there is no matching property provided, then just return null.
74 * <p>
75 * The returned object (when non-null) will add the selected rules to the digester whenever its addRules method is
76 * invoked.
77 *
78 * @param d The digester instance where locating plugin classes
79 * @param pluginClass The plugin Java class
80 * @param p The properties object that holds any xml attributes the user may have specified on the plugin
81 * declaration in order to indicate how to locate the plugin rules.
82 * @return a source of digester rules for the specified plugin class.
83 * @throws PluginException if the algorithm finds a source of rules, but there is something invalid
84 * about that source.
85 */
86 @Override
87 public RuleLoader findLoader( Digester d, Class<?> pluginClass, Properties p )
88 throws PluginException
89 {
90
91 String rulesFileName = p.getProperty( filenameAttr );
92 if ( rulesFileName == null )
93 {
94 // nope, user hasn't requested dynamic rules to be loaded
95 // from a specific file.
96 return null;
97 }
98
99 InputStream is = null;
100 try
101 {
102 is = new FileInputStream( rulesFileName );
103 }
104 catch ( IOException ioe )
105 {
106 throw new PluginException( "Unable to process file [" + rulesFileName + "]", ioe );
107 }
108
109 try
110 {
111 RuleLoader loader = new LoaderFromStream( is );
112 return loader;
113 }
114 catch ( Exception e )
115 {
116 throw new PluginException( "Unable to load xmlrules from file [" + rulesFileName + "]", e );
117 }
118 finally
119 {
120 try
121 {
122 is.close();
123 }
124 catch ( IOException ioe )
125 {
126 throw new PluginException( "Unable to close stream for file [" + rulesFileName + "]", ioe );
127 }
128 }
129 }
130
131 }