1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  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
19 package org.apache.tomcat.util.digester;
20
21
22 import org.apache.tomcat.util.IntrospectionUtils;
23 import org.xml.sax.Attributes;
24
25
26 /**
27  * <p>Rule implementation that sets properties on the object at the top of the
28  * stack, based on attributes with corresponding names.</p>
29  */

30
31 public class SetPropertiesRule extends Rule {
32
33     public interface Listener {
34         void endSetPropertiesRule();
35     }
36
37     /**
38      * Process the beginning of this element.
39      *
40      * @param namespace the namespace URI of the matching element, or an
41      *   empty string if the parser is not namespace aware or the element has
42      *   no namespace
43      * @param theName the local name if the parser is namespace aware, or just
44      *   the element name otherwise
45      * @param attributes The attribute list for this element
46      */

47     @Override
48     public void begin(String namespace, String theName, Attributes attributes)
49             throws Exception {
50
51         // Populate the corresponding properties of the top object
52         Object top = digester.peek();
53         if (digester.log.isDebugEnabled()) {
54             if (top != null) {
55                 digester.log.debug("[SetPropertiesRule]{" + digester.match +
56                                    "} Set " + top.getClass().getName() +
57                                    " properties");
58             } else {
59                 digester.log.debug("[SetPropertiesRule]{" + digester.match +
60                                    "} Set NULL properties");
61             }
62         }
63
64         for (int i = 0; i < attributes.getLength(); i++) {
65             String name = attributes.getLocalName(i);
66             if ("".equals(name)) {
67                 name = attributes.getQName(i);
68             }
69             String value = attributes.getValue(i);
70
71             if (digester.log.isDebugEnabled()) {
72                 digester.log.debug("[SetPropertiesRule]{" + digester.match +
73                         "} Setting property '" + name + "' to '" +
74                         value + "'");
75             }
76             if (!digester.isFakeAttribute(top, name)
77                     && !IntrospectionUtils.setProperty(top, name, value)
78                     && digester.getRulesValidation()) {
79                 if (!"optional".equals(name)) {
80                     digester.log.warn(sm.getString("rule.noProperty", digester.match, name, value));
81                 }
82             }
83         }
84
85         if (top instanceof Listener) {
86             ((Listener) top).endSetPropertiesRule();
87         }
88
89     }
90
91
92     /**
93      * Render a printable version of this Rule.
94      */

95     @Override
96     public String toString() {
97         StringBuilder sb = new StringBuilder("SetPropertiesRule[");
98         sb.append("]");
99         return sb.toString();
100     }
101 }
102