Source code: org/apache/hivemind/schema/rules/ReadContentRule.java
1 // Copyright 2004, 2005 The Apache Software Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // 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 {
39 private static final Log LOG = LogFactory.getLog(ReadContentRule.class);
40
41 private String _propertyName;
42
43 public void begin(SchemaProcessor processor, Element element)
44 {
45 String value = RuleUtils.processText(processor, element, element.getContent());
46
47 try
48 {
49 Translator t = processor.getContentTranslator();
50
51 Object target = processor.peek();
52
53 Class propertyType = PropertyUtils.getPropertyType(target, _propertyName);
54
55 Object finalValue =
56 t.translate(
57 processor.getContributingModule(),
58 propertyType,
59 value,
60 element.getLocation());
61
62 PropertyUtils.write(target, _propertyName, finalValue);
63 }
64 catch (Exception ex)
65 {
66 ErrorHandler eh = processor.getContributingModule().getErrorHandler();
67
68 eh.error(
69 LOG,
70 RulesMessages.readContentFailure(processor, element, ex),
71 element.getLocation(),
72 ex);
73 }
74
75 }
76
77 public String getPropertyName()
78 {
79 return _propertyName;
80 }
81
82 public void setPropertyName(String string)
83 {
84 _propertyName = string;
85 }
86
87 }