KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > PluginDeclarationRule


1 /* $Id: PluginDeclarationRule.java 179714 2005-06-03 03:53:39Z skitching $
2  *
3  * Copyright 2003-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;
19
20 import java.util.Properties JavaDoc;
21
22 import org.apache.commons.digester.Rule;
23 import org.apache.commons.digester.Digester;
24
25 import org.apache.commons.logging.Log;
26
27 /**
28  * A Digester rule which allows the user to pre-declare a class which is to
29  * be referenced later at a plugin point by a PluginCreateRule.
30  * <p>
31  * Normally, a PluginDeclarationRule is added to a Digester instance with
32  * the pattern "{root}/plugin" or "* /plugin" where {root} is the name of
33  * the root tag in the input document.
34  *
35  * @since 1.6
36  */

37
38 public class PluginDeclarationRule extends Rule {
39
40     //------------------- constructors ---------------------------------------
41

42     /** constructor */
43     public PluginDeclarationRule() {
44         super();
45     }
46
47     //------------------- methods --------------------------------------------
48

49     /**
50      * Invoked upon reading a tag defining a plugin declaration. The tag
51      * must have the following mandatory attributes:
52      * <ul>
53      * <li> id </li>
54      * <li> class </li>
55      * </ul>
56      *
57      *@param namespace The xml namespace in which the xml element which
58      * triggered this rule resides.
59      *@param name The name of the xml element which triggered this rule.
60      *@param attributes The set of attributes on the xml element which
61      * triggered this rule.
62      *@exception java.lang.Exception
63      */

64
65     public void begin(String JavaDoc namespace, String JavaDoc name,
66                       org.xml.sax.Attributes JavaDoc attributes)
67                       throws java.lang.Exception JavaDoc {
68                  
69         int nAttrs = attributes.getLength();
70         Properties JavaDoc props = new Properties JavaDoc();
71         for(int i=0; i<nAttrs; ++i) {
72             String JavaDoc key = attributes.getLocalName(i);
73             if ((key == null) || (key.length() == 0)) {
74                 key = attributes.getQName(i);
75             }
76             String JavaDoc value = attributes.getValue(i);
77             props.setProperty(key, value);
78         }
79         
80         try {
81             declarePlugin(digester, props);
82         } catch(PluginInvalidInputException ex) {
83             throw new PluginInvalidInputException(
84                 "Error on element [" + digester.getMatch() +
85                 "]: " + ex.getMessage());
86         }
87     }
88     
89     public static void declarePlugin(Digester digester, Properties JavaDoc props)
90     throws PluginException {
91         
92         Log log = digester.getLogger();
93         boolean debug = log.isDebugEnabled();
94         
95         String JavaDoc id = props.getProperty("id");
96         String JavaDoc pluginClassName = props.getProperty("class");
97         
98         if (id == null) {
99             throw new PluginInvalidInputException(
100                 "mandatory attribute id not present on plugin declaration");
101         }
102
103         if (pluginClassName == null) {
104             throw new PluginInvalidInputException(
105                 "mandatory attribute class not present on plugin declaration");
106         }
107
108         Declaration newDecl = new Declaration(pluginClassName);
109         newDecl.setId(id);
110         newDecl.setProperties(props);
111
112         PluginRules rc = (PluginRules) digester.getRules();
113         PluginManager pm = rc.getPluginManager();
114
115         newDecl.init(digester, pm);
116         pm.addDeclaration(newDecl);
117         
118         // Note that it is perfectly safe to redeclare a plugin, because
119
// the declaration doesn't add any rules to digester; all it does
120
// is create a RuleLoader instance whch is *capable* of adding the
121
// rules to the digester.
122
}
123 }
124
125
Popular Tags