01: // Copyright 2004, 2005 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.hivemind.schema.rules;
16:
17: import org.apache.commons.logging.Log;
18: import org.apache.commons.logging.LogFactory;
19: import org.apache.hivemind.Element;
20: import org.apache.hivemind.ErrorHandler;
21: import org.apache.hivemind.schema.SchemaProcessor;
22: import org.apache.hivemind.schema.Translator;
23: import org.apache.hivemind.util.PropertyUtils;
24:
25: /**
26: * Used to set a property of an object to a literal value.
27: *
28: * @author Howard Lewis Ship
29: */
30: public class SetPropertyRule extends BaseRule {
31: private static final Log LOG = LogFactory
32: .getLog(SetPropertyRule.class);
33:
34: private String _propertyName;
35: private String _value;
36: private Translator _smartTranslator;
37:
38: public void begin(SchemaProcessor processor, Element element) {
39: String value = RuleUtils
40: .processText(processor, element, _value);
41:
42: Object target = processor.peek();
43:
44: try {
45: if (_smartTranslator == null)
46: _smartTranslator = RuleUtils.getTranslator(processor,
47: "smart");
48:
49: Class propertyType = PropertyUtils.getPropertyType(target,
50: _propertyName);
51:
52: Object finalValue = _smartTranslator.translate(processor
53: .getContributingModule(), propertyType, value,
54: element.getLocation());
55:
56: PropertyUtils.write(target, _propertyName, finalValue);
57: } catch (Exception ex) {
58: ErrorHandler eh = processor.getContributingModule()
59: .getErrorHandler();
60:
61: String message = RulesMessages.unableToSetProperty(
62: _propertyName, target, ex);
63:
64: eh.error(LOG, message, element.getLocation(), ex);
65: }
66:
67: }
68:
69: public void setPropertyName(String string) {
70: _propertyName = string;
71: }
72:
73: public void setValue(String string) {
74: _value = string;
75: }
76:
77: public String getPropertyName() {
78: return _propertyName;
79: }
80:
81: public String getValue() {
82: return _value;
83: }
84:
85: }
|