|
|||||||||||||||||||
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 | |||||||||||||||
ConversionDescriptor.java | 94.4% | 97.8% | 100% | 97.2% |
|
1 |
// Copyright 2004 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.parse;
|
|
16 |
|
|
17 |
import java.util.HashMap;
|
|
18 |
import java.util.Iterator;
|
|
19 |
import java.util.Map;
|
|
20 |
|
|
21 |
import org.apache.commons.logging.Log;
|
|
22 |
import org.apache.commons.logging.LogFactory;
|
|
23 |
import org.apache.hivemind.ErrorHandler;
|
|
24 |
import org.apache.hivemind.Location;
|
|
25 |
import org.apache.hivemind.impl.BaseLocatable;
|
|
26 |
import org.apache.hivemind.schema.AttributeModel;
|
|
27 |
import org.apache.hivemind.schema.impl.ElementModelImpl;
|
|
28 |
import org.apache.hivemind.schema.rules.CreateObjectRule;
|
|
29 |
import org.apache.hivemind.schema.rules.InvokeParentRule;
|
|
30 |
import org.apache.hivemind.schema.rules.ReadAttributeRule;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Descriptor for the <conversion> module descriptor element.
|
|
34 |
*
|
|
35 |
* @author Howard Lewis Ship
|
|
36 |
*/
|
|
37 |
public class ConversionDescriptor extends BaseLocatable |
|
38 |
{ |
|
39 |
private static final Log LOG = LogFactory.getLog(ConversionDescriptor.class); |
|
40 |
|
|
41 |
private ErrorHandler _errorHandler;
|
|
42 |
private ElementModelImpl _elementModel;
|
|
43 |
private String _defaultTranslator;
|
|
44 |
|
|
45 |
private String _className;
|
|
46 |
private String _parentMethodName = "addElement"; |
|
47 |
private Map _attributeMappings = new HashMap(); |
|
48 |
|
|
49 | 423 |
public ConversionDescriptor(
|
50 |
ErrorHandler errorHandler, |
|
51 |
ElementModelImpl elementModel, |
|
52 |
String defaultTranslator, |
|
53 |
Location location) |
|
54 |
{ |
|
55 | 423 |
_errorHandler = errorHandler; |
56 | 423 |
_elementModel = elementModel; |
57 | 423 |
_defaultTranslator = defaultTranslator; |
58 |
|
|
59 | 423 |
setLocation(location); |
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* Adds a mapping for an attribute; these come from <map>
|
|
64 |
* elements nested within the <conversion> element. A check
|
|
65 |
* for duplicate attribute mappings (that is, duplicated attribute name),
|
|
66 |
* and an error is logged (and the duplicate ignored).
|
|
67 |
*/
|
|
68 | 672 |
public void addAttributeMapping(AttributeMappingDescriptor descriptor) |
69 |
{ |
|
70 | 672 |
String attributeName = descriptor.getAttributeName(); |
71 |
|
|
72 | 672 |
AttributeMappingDescriptor existing = |
73 |
(AttributeMappingDescriptor) _attributeMappings.get(attributeName); |
|
74 |
|
|
75 | 672 |
if (existing != null) |
76 |
{ |
|
77 | 1 |
_errorHandler.error( |
78 |
LOG, |
|
79 |
ParseMessages.dupeAttributeMapping(descriptor, existing), |
|
80 |
descriptor.getLocation(), |
|
81 |
null);
|
|
82 |
|
|
83 | 1 |
return;
|
84 |
} |
|
85 |
|
|
86 | 671 |
_attributeMappings.put(attributeName, descriptor); |
87 |
} |
|
88 |
|
|
89 | 423 |
public void setClassName(String string) |
90 |
{ |
|
91 | 423 |
_className = string; |
92 |
} |
|
93 |
|
|
94 | 1 |
public void setParentMethodName(String string) |
95 |
{ |
|
96 | 1 |
_parentMethodName = string; |
97 |
} |
|
98 |
|
|
99 |
/**
|
|
100 |
* Invoked once all <map> elements have been processed; this creates
|
|
101 |
* {@link org.apache.hivemind.schema.Rule}s that are added
|
|
102 |
* to the {@link ElementModelImpl}.
|
|
103 |
*/
|
|
104 | 423 |
public void addRulesForModel() |
105 |
{ |
|
106 | 423 |
_elementModel.addRule(new CreateObjectRule(_className));
|
107 |
|
|
108 | 423 |
addAttributeRules(); |
109 |
|
|
110 | 423 |
_elementModel.addRule(new InvokeParentRule(_parentMethodName));
|
111 |
} |
|
112 |
|
|
113 | 423 |
private void addAttributeRules() |
114 |
{ |
|
115 | 423 |
Iterator i = _elementModel.getAttributeModels().iterator(); |
116 |
|
|
117 | 423 |
while (i.hasNext())
|
118 |
{ |
|
119 | 1182 |
AttributeModel am = (AttributeModel) i.next(); |
120 | 1182 |
String attributeName = am.getName(); |
121 |
|
|
122 | 1182 |
AttributeMappingDescriptor amd = |
123 |
(AttributeMappingDescriptor) _attributeMappings.get(attributeName); |
|
124 |
|
|
125 | 1182 |
if (amd == null) |
126 |
{ |
|
127 | 512 |
_elementModel.addRule( |
128 |
new ReadAttributeRule(
|
|
129 |
attributeName, |
|
130 |
constructPropertyName(attributeName), |
|
131 |
null,
|
|
132 |
getLocation())); |
|
133 |
} |
|
134 |
else
|
|
135 |
{ |
|
136 | 670 |
String propertyName = amd.getPropertyName(); |
137 | 670 |
if (propertyName == null) |
138 | 0 |
propertyName = constructPropertyName(attributeName); |
139 |
|
|
140 | 670 |
_elementModel.addRule( |
141 |
new ReadAttributeRule(attributeName, propertyName, null, amd.getLocation())); |
|
142 |
|
|
143 | 670 |
_attributeMappings.remove(attributeName); |
144 |
} |
|
145 |
} |
|
146 |
|
|
147 | 423 |
if (!_attributeMappings.isEmpty())
|
148 | 1 |
_errorHandler.error( |
149 |
LOG, |
|
150 |
ParseMessages.extraMappings(_attributeMappings.keySet(), _elementModel), |
|
151 |
_elementModel.getLocation(), |
|
152 |
null);
|
|
153 |
} |
|
154 |
|
|
155 | 512 |
private String constructPropertyName(String attributeName)
|
156 |
{ |
|
157 | 512 |
int dashx = attributeName.indexOf('-');
|
158 | 512 |
if (dashx < 0)
|
159 | 511 |
return attributeName;
|
160 |
|
|
161 | 1 |
int length = attributeName.length();
|
162 | 1 |
StringBuffer buffer = new StringBuffer(length);
|
163 |
|
|
164 | 1 |
buffer.append(attributeName.substring(0, dashx)); |
165 | 1 |
boolean toUpper = true; |
166 |
|
|
167 | 1 |
for (int i = dashx + 1; i < length; i++) |
168 |
{ |
|
169 | 14 |
char ch = attributeName.charAt(i);
|
170 |
|
|
171 | 14 |
if (ch == '-')
|
172 |
{ |
|
173 | 1 |
toUpper = true;
|
174 | 1 |
continue;
|
175 |
} |
|
176 |
|
|
177 | 13 |
if (toUpper)
|
178 | 2 |
ch = Character.toUpperCase(ch); |
179 |
|
|
180 | 13 |
buffer.append(ch); |
181 |
|
|
182 | 13 |
toUpper = false;
|
183 |
} |
|
184 |
|
|
185 | 1 |
return buffer.toString();
|
186 |
} |
|
187 |
} |
|
188 |
|
|