001: /* $Id: SetPropertyRule.java 467222 2006-10-24 03:17:11Z markt $
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.tomcat.util.digester;
020:
021: import org.apache.tomcat.util.IntrospectionUtils;
022: import org.xml.sax.Attributes;
023:
024: /**
025: * Rule implementation that sets an individual property on the object at the
026: * top of the stack, based on attributes with specified names.
027: */
028:
029: public class SetPropertyRule extends Rule {
030:
031: // ----------------------------------------------------------- Constructors
032:
033: /**
034: * Construct a "set property" rule with the specified name and value
035: * attributes.
036: *
037: * @param digester The digester with which this rule is associated
038: * @param name Name of the attribute that will contain the name of the
039: * property to be set
040: * @param value Name of the attribute that will contain the value to which
041: * the property should be set
042: *
043: * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
044: * Use {@link #SetPropertyRule(String name, String value)} instead.
045: */
046: public SetPropertyRule(Digester digester, String name, String value) {
047:
048: this (name, value);
049:
050: }
051:
052: /**
053: * Construct a "set property" rule with the specified name and value
054: * attributes.
055: *
056: * @param name Name of the attribute that will contain the name of the
057: * property to be set
058: * @param value Name of the attribute that will contain the value to which
059: * the property should be set
060: */
061: public SetPropertyRule(String name, String value) {
062:
063: this .name = name;
064: this .value = value;
065:
066: }
067:
068: // ----------------------------------------------------- Instance Variables
069:
070: /**
071: * The attribute that will contain the property name.
072: */
073: protected String name = null;
074:
075: /**
076: * The attribute that will contain the property value.
077: */
078: protected String value = null;
079:
080: // --------------------------------------------------------- Public Methods
081:
082: /**
083: * Process the beginning of this element.
084: *
085: * @param attributes The attribute list of this element
086: *
087: * @exception NoSuchMethodException if the bean does not
088: * have a writeable property of the specified name
089: */
090: public void begin(Attributes attributes) throws Exception {
091:
092: // Identify the actual property name and value to be used
093: String actualName = null;
094: String actualValue = null;
095: for (int i = 0; i < attributes.getLength(); i++) {
096: String name = attributes.getLocalName(i);
097: if ("".equals(name)) {
098: name = attributes.getQName(i);
099: }
100: String value = attributes.getValue(i);
101: if (name.equals(this .name)) {
102: actualName = value;
103: } else if (name.equals(this .value)) {
104: actualValue = value;
105: }
106: }
107:
108: // Get a reference to the top object
109: Object top = digester.peek();
110:
111: // Log some debugging information
112: if (digester.log.isDebugEnabled()) {
113: digester.log.debug("[SetPropertyRule]{" + digester.match
114: + "} Set " + top.getClass().getName()
115: + " property " + actualName + " to " + actualValue);
116: }
117:
118: // Set the property (with conversion as necessary)
119: // FIXME: Exception if property doesn't exist ?
120: IntrospectionUtils.setProperty(top, actualName, actualValue);
121:
122: }
123:
124: /**
125: * Render a printable version of this Rule.
126: */
127: public String toString() {
128:
129: StringBuffer sb = new StringBuffer("SetPropertyRule[");
130: sb.append("name=");
131: sb.append(name);
132: sb.append(", value=");
133: sb.append(value);
134: sb.append("]");
135: return (sb.toString());
136:
137: }
138:
139: }
|