KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > SetPropertyRule


1 /* $Id: SetPropertyRule.java 467222 2006-10-24 03:17:11Z markt $
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.tomcat.util.digester;
21
22
23 import org.apache.tomcat.util.IntrospectionUtils;
24 import org.xml.sax.Attributes JavaDoc;
25
26
27 /**
28  * Rule implementation that sets an individual property on the object at the
29  * top of the stack, based on attributes with specified names.
30  */

31
32 public class SetPropertyRule extends Rule {
33
34
35     // ----------------------------------------------------------- Constructors
36

37
38     /**
39      * Construct a "set property" rule with the specified name and value
40      * attributes.
41      *
42      * @param digester The digester with which this rule is associated
43      * @param name Name of the attribute that will contain the name of the
44      * property to be set
45      * @param value Name of the attribute that will contain the value to which
46      * the property should be set
47      *
48      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
49      * Use {@link #SetPropertyRule(String name, String value)} instead.
50      */

51     public SetPropertyRule(Digester digester, String JavaDoc name, String JavaDoc value) {
52
53         this(name, value);
54
55     }
56
57     /**
58      * Construct a "set property" rule with the specified name and value
59      * attributes.
60      *
61      * @param name Name of the attribute that will contain the name of the
62      * property to be set
63      * @param value Name of the attribute that will contain the value to which
64      * the property should be set
65      */

66     public SetPropertyRule(String JavaDoc name, String JavaDoc value) {
67
68         this.name = name;
69         this.value = value;
70
71     }
72
73     // ----------------------------------------------------- Instance Variables
74

75
76     /**
77      * The attribute that will contain the property name.
78      */

79     protected String JavaDoc name = null;
80
81
82     /**
83      * The attribute that will contain the property value.
84      */

85     protected String JavaDoc value = null;
86
87
88     // --------------------------------------------------------- Public Methods
89

90
91     /**
92      * Process the beginning of this element.
93      *
94      * @param attributes The attribute list of this element
95      *
96      * @exception NoSuchMethodException if the bean does not
97      * have a writeable property of the specified name
98      */

99     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
100
101         // Identify the actual property name and value to be used
102
String JavaDoc actualName = null;
103         String JavaDoc actualValue = null;
104         for (int i = 0; i < attributes.getLength(); i++) {
105             String JavaDoc name = attributes.getLocalName(i);
106             if ("".equals(name)) {
107                 name = attributes.getQName(i);
108             }
109             String JavaDoc value = attributes.getValue(i);
110             if (name.equals(this.name)) {
111                 actualName = value;
112             } else if (name.equals(this.value)) {
113                 actualValue = value;
114             }
115         }
116
117         // Get a reference to the top object
118
Object JavaDoc top = digester.peek();
119
120         // Log some debugging information
121
if (digester.log.isDebugEnabled()) {
122             digester.log.debug("[SetPropertyRule]{" + digester.match +
123                     "} Set " + top.getClass().getName() + " property " +
124                     actualName + " to " + actualValue);
125         }
126
127         // Set the property (with conversion as necessary)
128
// FIXME: Exception if property doesn't exist ?
129
IntrospectionUtils.setProperty(top, actualName, actualValue);
130
131     }
132
133
134     /**
135      * Render a printable version of this Rule.
136      */

137     public String JavaDoc toString() {
138
139         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SetPropertyRule[");
140         sb.append("name=");
141         sb.append(name);
142         sb.append(", value=");
143         sb.append(value);
144         sb.append("]");
145         return (sb.toString());
146
147     }
148
149
150 }
151
Popular Tags