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 the top object on the stack to the value
27: * of the element's content. Created from the <code><read-content></code>
28: * element.
29: *
30: * <p>
31: * Note: an {@link org.apache.hivemind.Element}'s content is trimmed
32: * of leading and trailing whitespace as it is parsed and, additionally,
33: * will never be null (though it may be the empty string).
34: *
35: * @author Howard Lewis Ship
36: */
37: public class ReadContentRule extends BaseRule {
38: private static final Log LOG = LogFactory
39: .getLog(ReadContentRule.class);
40:
41: private String _propertyName;
42:
43: public void begin(SchemaProcessor processor, Element element) {
44: String value = RuleUtils.processText(processor, element,
45: element.getContent());
46:
47: try {
48: Translator t = processor.getContentTranslator();
49:
50: Object target = processor.peek();
51:
52: Class propertyType = PropertyUtils.getPropertyType(target,
53: _propertyName);
54:
55: Object finalValue = t.translate(processor
56: .getContributingModule(), propertyType, value,
57: element.getLocation());
58:
59: PropertyUtils.write(target, _propertyName, finalValue);
60: } catch (Exception ex) {
61: ErrorHandler eh = processor.getContributingModule()
62: .getErrorHandler();
63:
64: eh.error(LOG, RulesMessages.readContentFailure(processor,
65: element, ex), element.getLocation(), ex);
66: }
67:
68: }
69:
70: public String getPropertyName() {
71: return _propertyName;
72: }
73:
74: public void setPropertyName(String string) {
75: _propertyName = string;
76: }
77:
78: }
|