|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.hivemind.parse; |
|
16 |
| |
|
17 |
| import java.util.ArrayList; |
|
18 |
| import java.util.HashMap; |
|
19 |
| import java.util.Iterator; |
|
20 |
| import java.util.List; |
|
21 |
| import java.util.ListIterator; |
|
22 |
| import java.util.Map; |
|
23 |
| |
|
24 |
| import org.apache.commons.logging.Log; |
|
25 |
| import org.apache.commons.logging.LogFactory; |
|
26 |
| import org.apache.hivemind.Element; |
|
27 |
| import org.apache.hivemind.ErrorHandler; |
|
28 |
| import org.apache.hivemind.schema.AttributeModel; |
|
29 |
| import org.apache.hivemind.schema.ElementModel; |
|
30 |
| import org.apache.hivemind.schema.Rule; |
|
31 |
| import org.apache.hivemind.schema.SchemaProcessor; |
|
32 |
| import org.apache.hivemind.schema.rules.BaseRule; |
|
33 |
| import org.apache.hivemind.schema.rules.CreateObjectRule; |
|
34 |
| import org.apache.hivemind.schema.rules.InvokeParentRule; |
|
35 |
| import org.apache.hivemind.schema.rules.ReadAttributeRule; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class ConversionDescriptor extends BaseRule |
|
46 |
| { |
|
47 |
| private static final Log LOG = LogFactory.getLog(ConversionDescriptor.class); |
|
48 |
| |
|
49 |
| private ErrorHandler _errorHandler; |
|
50 |
| |
|
51 |
| private String _className; |
|
52 |
| |
|
53 |
| private String _parentMethodName = "addElement"; |
|
54 |
| |
|
55 |
| private Map _attributeNameMappingMap = new HashMap(); |
|
56 |
| |
|
57 |
| |
|
58 |
| private List _attributeMappings = new ArrayList(); |
|
59 |
| |
|
60 |
| private List _rules; |
|
61 |
| |
|
62 |
| private ElementModel _elementModel; |
|
63 |
| |
|
64 |
518
| public ConversionDescriptor(ErrorHandler errorHandler, ElementModel elementModel)
|
|
65 |
| { |
|
66 |
518
| _errorHandler = errorHandler;
|
|
67 |
518
| _elementModel = elementModel;
|
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
17
| public List getAttributeMappings()
|
|
74 |
| { |
|
75 |
17
| return _attributeMappings;
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
886
| public void addAttributeMapping(AttributeMappingDescriptor descriptor)
|
|
84 |
| { |
|
85 |
886
| String attributeName = descriptor.getAttributeName();
|
|
86 |
| |
|
87 |
886
| AttributeMappingDescriptor existing = (AttributeMappingDescriptor) _attributeNameMappingMap
|
|
88 |
| .get(attributeName); |
|
89 |
| |
|
90 |
886
| if (existing != null)
|
|
91 |
| { |
|
92 |
1
| _errorHandler.error(
|
|
93 |
| LOG, |
|
94 |
| ParseMessages.dupeAttributeMapping(descriptor, existing), |
|
95 |
| descriptor.getLocation(), |
|
96 |
| null); |
|
97 |
| |
|
98 |
1
| return;
|
|
99 |
| } |
|
100 |
| |
|
101 |
885
| _attributeNameMappingMap.put(attributeName, descriptor);
|
|
102 |
| |
|
103 |
885
| _attributeMappings.add(descriptor);
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
17
| public String getClassName()
|
|
110 |
| { |
|
111 |
17
| return _className;
|
|
112 |
| } |
|
113 |
| |
|
114 |
518
| public void setClassName(String string)
|
|
115 |
| { |
|
116 |
518
| _className = string;
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
17
| public String getParentMethodName()
|
|
123 |
| { |
|
124 |
17
| return _parentMethodName;
|
|
125 |
| } |
|
126 |
| |
|
127 |
1
| public void setParentMethodName(String string)
|
|
128 |
| { |
|
129 |
1
| _parentMethodName = string;
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
1975
| public void begin(SchemaProcessor processor, Element element)
|
|
136 |
| { |
|
137 |
1975
| for (Iterator i = _rules.iterator(); i.hasNext();)
|
|
138 |
| { |
|
139 |
9392
| Rule rule = (Rule) i.next();
|
|
140 |
| |
|
141 |
9392
| rule.begin(processor, element);
|
|
142 |
| } |
|
143 |
| } |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
1975
| public void end(SchemaProcessor processor, Element element)
|
|
149 |
| { |
|
150 |
1975
| for (ListIterator i = _rules.listIterator(_rules.size()); i.hasPrevious();)
|
|
151 |
| { |
|
152 |
9392
| Rule rule = (Rule) i.previous();
|
|
153 |
| |
|
154 |
9392
| rule.end(processor, element);
|
|
155 |
| } |
|
156 |
| } |
|
157 |
| |
|
158 |
518
| public void addRulesForModel()
|
|
159 |
| { |
|
160 |
518
| _rules = new ArrayList();
|
|
161 |
| |
|
162 |
518
| _rules.add(new CreateObjectRule(_className));
|
|
163 |
| |
|
164 |
518
| addAttributeRules();
|
|
165 |
| |
|
166 |
518
| _rules.add(new InvokeParentRule(_parentMethodName));
|
|
167 |
| } |
|
168 |
| |
|
169 |
518
| private void addAttributeRules()
|
|
170 |
| { |
|
171 |
518
| Iterator i = _elementModel.getAttributeModels().iterator();
|
|
172 |
| |
|
173 |
518
| while (i.hasNext())
|
|
174 |
| { |
|
175 |
1540
| AttributeModel am = (AttributeModel) i.next();
|
|
176 |
1540
| String attributeName = am.getName();
|
|
177 |
| |
|
178 |
1540
| AttributeMappingDescriptor amd = (AttributeMappingDescriptor) _attributeNameMappingMap
|
|
179 |
| .get(attributeName); |
|
180 |
| |
|
181 |
1540
| if (amd == null)
|
|
182 |
| { |
|
183 |
656
| _rules.add(new ReadAttributeRule(attributeName,
|
|
184 |
| constructPropertyName(attributeName), null, getLocation())); |
|
185 |
| } |
|
186 |
| else |
|
187 |
| { |
|
188 |
884
| String propertyName = amd.getPropertyName();
|
|
189 |
884
| if (propertyName == null)
|
|
190 |
0
| propertyName = constructPropertyName(attributeName);
|
|
191 |
| |
|
192 |
884
| _rules.add(new ReadAttributeRule(attributeName, propertyName, null, amd
|
|
193 |
| .getLocation())); |
|
194 |
| |
|
195 |
884
| _attributeNameMappingMap.remove(attributeName);
|
|
196 |
| } |
|
197 |
| } |
|
198 |
| |
|
199 |
518
| if (!_attributeNameMappingMap.isEmpty())
|
|
200 |
1
| _errorHandler.error(LOG, ParseMessages.extraMappings(
|
|
201 |
| _attributeNameMappingMap.keySet(), |
|
202 |
| _elementModel), _elementModel.getLocation(), null); |
|
203 |
| } |
|
204 |
| |
|
205 |
656
| private String constructPropertyName(String attributeName)
|
|
206 |
| { |
|
207 |
656
| int dashx = attributeName.indexOf('-');
|
|
208 |
656
| if (dashx < 0)
|
|
209 |
655
| return attributeName;
|
|
210 |
| |
|
211 |
1
| int length = attributeName.length();
|
|
212 |
1
| StringBuffer buffer = new StringBuffer(length);
|
|
213 |
| |
|
214 |
1
| buffer.append(attributeName.substring(0, dashx));
|
|
215 |
1
| boolean toUpper = true;
|
|
216 |
| |
|
217 |
1
| for (int i = dashx + 1; i < length; i++)
|
|
218 |
| { |
|
219 |
14
| char ch = attributeName.charAt(i);
|
|
220 |
| |
|
221 |
14
| if (ch == '-')
|
|
222 |
| { |
|
223 |
1
| toUpper = true;
|
|
224 |
1
| continue;
|
|
225 |
| } |
|
226 |
| |
|
227 |
13
| if (toUpper)
|
|
228 |
2
| ch = Character.toUpperCase(ch);
|
|
229 |
| |
|
230 |
13
| buffer.append(ch);
|
|
231 |
| |
|
232 |
13
| toUpper = false;
|
|
233 |
| } |
|
234 |
| |
|
235 |
1
| return buffer.toString();
|
|
236 |
| } |
|
237 |
| } |