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