|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ReadAttributeRule.java | 75% | 100% | 100% | 97.4% |
|
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><read-attribute></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 _attributeName;
|
|
38 |
|
|
39 |
private String _propertyName;
|
|
40 |
|
|
41 |
private boolean _skipIfNull = true; |
|
42 |
|
|
43 |
private String _translator;
|
|
44 |
|
|
45 | 2821 |
public ReadAttributeRule()
|
46 |
{ |
|
47 |
} |
|
48 |
|
|
49 | 1454 |
public ReadAttributeRule(String attributeName, String propertyName, String translator,
|
50 |
Location location) |
|
51 |
{ |
|
52 | 1454 |
setLocation(location); |
53 |
|
|
54 | 1454 |
_attributeName = attributeName; |
55 | 1454 |
_propertyName = propertyName; |
56 | 1454 |
_translator = translator; |
57 |
} |
|
58 |
|
|
59 | 9709 |
public void begin(SchemaProcessor processor, Element element) |
60 |
{ |
|
61 | 9709 |
String rawValue = element.getAttributeValue(_attributeName); |
62 |
|
|
63 | 9709 |
if (rawValue == null && _skipIfNull) |
64 | 4431 |
return;
|
65 |
|
|
66 | 5278 |
String value = RuleUtils.processText(processor, element, rawValue); |
67 |
|
|
68 | 5278 |
Object target = processor.peek(); |
69 |
|
|
70 | 5278 |
try
|
71 |
{ |
|
72 | 5278 |
Translator t = _translator == null ? processor.getAttributeTranslator(_attributeName)
|
73 |
: processor.getTranslator(_translator); |
|
74 |
|
|
75 | 5278 |
Class propertyType = PropertyUtils.getPropertyType(target, _propertyName); |
76 |
|
|
77 | 5278 |
Object finalValue = t.translate( |
78 |
processor.getContributingModule(), |
|
79 |
propertyType, |
|
80 |
value, |
|
81 |
element.getLocation()); |
|
82 |
|
|
83 | 5274 |
PropertyUtils.write(target, _propertyName, finalValue); |
84 |
|
|
85 |
} |
|
86 |
catch (Exception ex)
|
|
87 |
{ |
|
88 | 4 |
ErrorHandler eh = processor.getContributingModule().getErrorHandler(); |
89 |
|
|
90 | 4 |
eh.error(LOG, RulesMessages |
91 |
.readAttributeFailure(_attributeName, element, processor, ex), element |
|
92 |
.getLocation(), ex); |
|
93 |
} |
|
94 |
|
|
95 |
} |
|
96 |
|
|
97 | 67 |
public String getAttributeName()
|
98 |
{ |
|
99 | 67 |
return _attributeName;
|
100 |
} |
|
101 |
|
|
102 | 67 |
public String getPropertyName()
|
103 |
{ |
|
104 | 67 |
return _propertyName;
|
105 |
} |
|
106 |
|
|
107 | 67 |
public boolean getSkipIfNull() |
108 |
{ |
|
109 | 67 |
return _skipIfNull;
|
110 |
} |
|
111 |
|
|
112 |
/**
|
|
113 |
* @since 1.1
|
|
114 |
*/
|
|
115 | 66 |
public String getTranslator()
|
116 |
{ |
|
117 | 66 |
return _translator;
|
118 |
} |
|
119 |
|
|
120 | 2821 |
public void setAttributeName(String string) |
121 |
{ |
|
122 | 2821 |
_attributeName = string; |
123 |
} |
|
124 |
|
|
125 | 2821 |
public void setPropertyName(String string) |
126 |
{ |
|
127 | 2821 |
_propertyName = string; |
128 |
} |
|
129 |
|
|
130 | 2819 |
public void setSkipIfNull(boolean b) |
131 |
{ |
|
132 | 2819 |
_skipIfNull = b; |
133 |
} |
|
134 |
|
|
135 | 2819 |
public void setTranslator(String string) |
136 |
{ |
|
137 | 2819 |
_translator = string; |
138 |
} |
|
139 |
|
|
140 |
} |
|