KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > schema > rules > ReadAttributeRule


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.Location;
22 import org.apache.hivemind.schema.SchemaProcessor;
23 import org.apache.hivemind.schema.Translator;
24 import org.apache.hivemind.util.PropertyUtils;
25
26 /**
27  * Reads an attribute of an element and uses it to set a property of the top object on the stack.
28  * Created from the <code>&lt;read-attribute&gt;</code> element.
29  *
30  * @author Howard Lewis Ship
31  */

32 public class ReadAttributeRule extends BaseRule
33 {
34
35     private static final Log LOG = LogFactory.getLog(ReadAttributeRule.class);
36
37     private String JavaDoc _attributeName;
38
39     private String JavaDoc _propertyName;
40
41     private boolean _skipIfNull = true;
42
43     private String JavaDoc _translator;
44
45     public ReadAttributeRule()
46     {
47     }
48
49     public ReadAttributeRule(String JavaDoc attributeName, String JavaDoc propertyName, String JavaDoc translator,
50             Location location)
51     {
52         setLocation(location);
53
54         _attributeName = attributeName;
55         _propertyName = propertyName;
56         _translator = translator;
57     }
58
59     public void begin(SchemaProcessor processor, Element element)
60     {
61         String JavaDoc value = getAttributeValue(processor, element);
62
63         if (value == null && _skipIfNull)
64             return;
65
66         value = RuleUtils.processText(processor, element, value);
67
68         Object JavaDoc target = processor.peek();
69
70         try
71         {
72             Translator t = _translator == null ? processor.getAttributeTranslator(_attributeName)
73                     : processor.getTranslator(_translator);
74
75             Class JavaDoc propertyType = PropertyUtils.getPropertyType(target, _propertyName);
76
77             Object JavaDoc finalValue = t.translate(
78                     processor.getContributingModule(),
79                     propertyType,
80                     value,
81                     element.getLocation());
82
83             PropertyUtils.write(target, _propertyName, finalValue);
84
85         }
86         catch (Exception JavaDoc ex)
87         {
88             ErrorHandler eh = processor.getContributingModule().getErrorHandler();
89
90             eh.error(LOG, RulesMessages
91                     .readAttributeFailure(_attributeName, element, processor, ex), element
92                     .getLocation(), ex);
93         }
94
95     }
96
97     private String JavaDoc getAttributeValue(SchemaProcessor processor, Element element)
98     {
99         final String JavaDoc rawValue = element.getAttributeValue(_attributeName);
100
101         return rawValue == null ? processor.getAttributeDefault(_attributeName) : rawValue;
102     }
103
104     public String JavaDoc getAttributeName()
105     {
106         return _attributeName;
107     }
108
109     public String JavaDoc getPropertyName()
110     {
111         return _propertyName;
112     }
113
114     public boolean getSkipIfNull()
115     {
116         return _skipIfNull;
117     }
118
119     /**
120      * @since 1.1
121      */

122     public String JavaDoc getTranslator()
123     {
124         return _translator;
125     }
126
127     public void setAttributeName(String JavaDoc string)
128     {
129         _attributeName = string;
130     }
131
132     public void setPropertyName(String JavaDoc string)
133     {
134         _propertyName = string;
135     }
136
137     public void setSkipIfNull(boolean b)
138     {
139         _skipIfNull = b;
140     }
141
142     public void setTranslator(String JavaDoc string)
143     {
144         _translator = string;
145     }
146
147 }
Popular Tags