1 package org.ocltf.concretesyntax.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.apache.commons.lang.ObjectUtils;
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.ocltf.concretesyntax.OperationCS;
13 import org.ocltf.concretesyntax.VariableDeclarationCS;
14 import org.ocltf.parser.node.AActualParameterList;
15 import org.ocltf.parser.node.ACommaExpression;
16 import org.ocltf.parser.node.AFeatureCall;
17 import org.ocltf.parser.node.AFeatureCallParameters;
18 import org.ocltf.parser.node.AOperation;
19 import org.ocltf.parser.node.APropertyCallExpression;
20 import org.ocltf.parser.node.ARelationalExpression;
21 import org.ocltf.parser.node.ARelationalExpressionTail;
22 import org.ocltf.parser.node.AStandardDeclarator;
23 import org.ocltf.parser.node.ATypeDeclaration;
24 import org.ocltf.parser.node.AVariableDeclaration;
25 import org.ocltf.parser.node.AVariableDeclarationList;
26 import org.ocltf.parser.node.AVariableDeclarationListTail;
27 import org.ocltf.parser.node.PActualParameterList;
28 import org.ocltf.parser.node.PEqualExpression;
29 import org.ocltf.parser.node.POperation;
30 import org.ocltf.parser.node.PRelationalExpression;
31 import org.ocltf.parser.node.PVariableDeclaration;
32 import org.ocltf.parser.node.PVariableDeclarationList;
33 import org.ocltf.parser.node.TName;
34 import org.ocltf.translation.TranslationUtils;
35 import org.ocltf.utils.ExceptionUtils;
36
37 /***
38 * Contains some utilities for concrete syntax value retrieval.
39 *
40 * @author Chad Brandon
41 */
42 public class ConcreteSyntaxUtils {
43
44 private static final Log logger = LogFactory.getLog(ConcreteSyntaxUtils.class);
45
46 /***
47 * Iterates through the passed in list and concates all
48 * the values of objects toString value to a StringBuffer
49 * and returns the StringBuffer.
50 *
51 * @param list the List of objects to concatinate.
52 * @return StringBuffer the concatinated contents of the list.
53 */
54 public static StringBuffer concatContents(List list) {
55 StringBuffer name = new StringBuffer();
56 if (list != null) {
57 Iterator iterator = list.iterator();
58 while (iterator.hasNext()) {
59 String value = ObjectUtils.toString(iterator.next());
60 name.append(value);
61 }
62 }
63 return name;
64 }
65
66 /***
67 * Retrieves all the variable declarations for the passed in operation.
68 *
69 * @param operation
70 * @return VariableDeclarations
71 */
72 public static OperationCS getOperationDeclaration(POperation operation) {
73 String methodName = "getOperationDeclaration";
74 ExceptionUtils.checkNull(methodName, "operation", operation);
75
76 OperationCS operationDeclaration = null;
77
78 AOperation op = (AOperation)operation;
79
80 ATypeDeclaration typeDeclaration = (ATypeDeclaration)op.getReturnTypeDeclaration();
81 String returnType = null;
82 if (typeDeclaration != null) {
83 returnType = ObjectUtils.toString(typeDeclaration .getType());
84 }
85
86 operationDeclaration =
87 new OperationDeclarationCSImpl(
88 ObjectUtils.toString(op.getName()),
89 returnType,
90 ConcreteSyntaxUtils.getVariableDeclarations(operation));
91
92 if (logger.isDebugEnabled()) {
93 logger.debug("completed " + methodName +
94 " with operationDeclaration --> " + operationDeclaration);
95 }
96
97 return operationDeclaration;
98 }
99
100 /***
101 * Retrieves all the variable declarations for the passed in
102 * <code>operation</code>.
103 *
104 * @param operation the operation for which to retrieve the variable declarations.
105 * @return VariableDeclaration[]
106 */
107 public static VariableDeclarationCS[] getVariableDeclarations(POperation operation) {
108 String methodName = "getVariableDeclarations";
109 ExceptionUtils.checkNull(methodName, "operation", operation);
110 return ConcreteSyntaxUtils.getVariableDeclarations(
111 ((AOperation)operation).getParameters());
112 }
113
114 /***
115 * Retrieves all the variable declarations for the passed in
116 * <code>standardDeclarator</code>.
117 *
118 * @param standardDeclarator the standard declartor for which to retrieve the
119 * VariableDeclarationCS instances.
120 * @return VariableDeclarationCS[]
121 */
122 public static VariableDeclarationCS[] getVariableDeclarations(AStandardDeclarator standardDeclarator) {
123 String methodName = "getVariableDeclarations";
124 ExceptionUtils.checkNull(methodName, "standardDeclarator", standardDeclarator);
125 return ConcreteSyntaxUtils.getVariableDeclarations(
126 standardDeclarator.getVariableDeclarationList());
127 }
128
129 /***
130 * Creates a new VariableDeclarationCS from the passed in PVariableDeclaration.
131 * @param variableDeclaration the PVariableDeclaration that the new VariableDeclarationCS
132 * will be created from.
133 * @param initialValue the initial value of the variable declaration.
134 * @return VariableDeclarationCS the new VariableDeclarationCS
135 */
136 protected static VariableDeclarationCS newVariableDeclaration(
137 PVariableDeclaration variableDeclaration,
138 PEqualExpression initialValue) {
139 String methodName = "newVariableDeclaration";
140 ExceptionUtils.checkNull(methodName, "variableDeclaration", variableDeclaration);
141
142 AVariableDeclaration declaration = (AVariableDeclaration)variableDeclaration;
143 ATypeDeclaration typeDeclaration = (ATypeDeclaration )declaration.getTypeDeclaration();
144 String type = null;
145 String name = ObjectUtils.toString(declaration.getName()).trim();
146 if (typeDeclaration != null) {
147 type = ObjectUtils.toString(typeDeclaration.getType());
148 }
149 return new VariableDeclarationCSImpl(name, type, ObjectUtils.toString(initialValue).trim());
150 }
151
152 /***
153 * Creates an array of VariableDeclarationCS[] from the passed in PVariableDeclarationList.
154 * @param variableDeclarationList the PVariableDeclarationList that the new VariableDeclarationCS
155 * will be created from.
156 * @return VariableDeclarationCS[] the new VariableDeclarationCS array
157 */
158 public static VariableDeclarationCS[] getVariableDeclarations(
159 PVariableDeclarationList variableDeclarationList) {
160
161 Collection declarations = new ArrayList();
162
163 if (variableDeclarationList != null) {
164 AVariableDeclarationList variables =
165 (AVariableDeclarationList)variableDeclarationList;
166
167 if (variables != null) {
168
169 declarations.add(
170 ConcreteSyntaxUtils.newVariableDeclaration(
171 variables.getVariableDeclaration(), variables.getVariableDeclarationValue()));
172
173
174 List variableTails = variables.getVariableDeclarationListTail();
175 if (variableTails != null) {
176 Iterator variableTailIt = variableTails.iterator();
177 while (variableTailIt.hasNext()) {
178 AVariableDeclarationListTail tail =
179 (AVariableDeclarationListTail)variableTailIt.next();
180 declarations.add(
181 ConcreteSyntaxUtils.newVariableDeclaration(
182 tail.getVariableDeclaration(), tail.getVariableDeclarationValue()));
183 }
184 }
185 }
186 }
187 return (VariableDeclarationCS[])declarations.toArray(new VariableDeclarationCS[0]);
188 }
189
190 /***
191 * Gets all the parameters from a AFeatureCall instance.
192 *
193 * @param featureCall the featureCall for which to retrieve the parameters
194 * @return List the list containing any parameters retrieved, or
195 * an empty array if none could be retrieved
196 */
197 public static List getParameters(AFeatureCall featureCall) {
198 List parameters = new ArrayList();
199 if (featureCall != null) {
200 AFeatureCallParameters callParameters =
201 (AFeatureCallParameters)
202 featureCall.getFeatureCallParameters();
203 if (callParameters != null) {
204 PActualParameterList parameterList =
205 callParameters.getActualParameterList();
206 if (parameterList != null) {
207 AActualParameterList params = (AActualParameterList)parameterList;
208
209
210 String firstParam = TranslationUtils.trimToEmpty(params.getExpression());
211 if (StringUtils.isNotEmpty(firstParam)) {
212 parameters.add(firstParam);
213 }
214
215
216 List restOfParams = params.getCommaExpression();
217 if (restOfParams != null && !restOfParams.isEmpty()) {
218 Iterator paramIt = restOfParams.iterator();
219 while (paramIt.hasNext()) {
220 ACommaExpression parameterListTail =
221 (ACommaExpression)paramIt.next();
222 parameters.add(
223 TranslationUtils.trimToEmpty(
224 parameterListTail.getExpression()));
225 }
226 }
227 }
228 }
229 }
230 return parameters;
231 }
232
233 /***
234 * Gets all the parameters from a AFeatureCall instance as a comma
235 * seperated String.
236 *
237 * @param featureCall the featureCall for which to retrieve the parameters
238 * @return String the comma seperated String
239 */
240 public static String getParametersAsString(AFeatureCall featureCall) {
241 return StringUtils.join(
242 ConcreteSyntaxUtils.getParameters(
243 featureCall).iterator(), ",");
244 }
245
246 /***
247 * Gets the left and right expressions of a PRelationalExpression
248 * and puts then into a List. The left expression will be the first
249 * expression in the list.
250 * @param relationalExpression
251 * @return the left and right paranthesis in [0] and [1] of the String[]/
252 */
253 public static String[] getLeftAndRightExpressions(PRelationalExpression relationalExpression) {
254 String[] expressions = new String[2];
255 ARelationalExpression expression = (ARelationalExpression)relationalExpression;
256
257
258 expressions[0] = TranslationUtils.trimToEmpty(expression.getAdditiveExpression());
259
260 ARelationalExpressionTail expressionTail =
261 (ARelationalExpressionTail)expression.getRelationalExpressionTail();
262
263
264 expressions[1] = TranslationUtils.trimToEmpty(expressionTail.getAdditiveExpression());
265
266 return expressions;
267 }
268
269 /***
270 * Concatinates the type from the passed in name and pathNameTail.
271 * @param name the starting name of the type
272 * @param pathNameTail the tail pieces of the name
273 * @return String the concatinated name.
274 */
275 public static String getType(TName name, List pathNameTail) {
276 StringBuffer type =
277 ConcreteSyntaxUtils.concatContents(pathNameTail);
278 type.insert(0, TranslationUtils.trimToEmpty(name));
279 return StringUtils.deleteWhitespace(type.toString());
280 }
281
282 /***
283 * Gets the "real" primary expression, as opposed to the primary expression
284 * retrieved from the parser syntax (since it leaves off any navigational
285 * relationships).
286 *
287 * @param expression the APosfixExpression instance for which to retrieve
288 * the primary expression
289 *
290 * @return String the "real" primary expression or the passed in expression.
291 */
292 public static String getPrimaryExpression(APropertyCallExpression expression) {
293 String methodName = "getPrimaryExpression";
294 if (logger.isDebugEnabled()) {
295 logger.debug("performing " + methodName +
296 " with expression --> '" + expression + "'");
297 }
298 StringBuffer primaryExpression = new StringBuffer();
299 if (expression != null) {
300
301 primaryExpression.append(
302 TranslationUtils.trimToEmpty(expression.getPrimaryExpression()));
303
304 List expressionTail = new ArrayList(expression.getPropertyCallExpressionTail());
305 if (expressionTail.size() > 1) {
306
307
308
309
310 expressionTail.remove(expressionTail.size() - 1);
311 primaryExpression.append(ConcreteSyntaxUtils.concatContents(expressionTail));
312 }
313
314 if (logger.isDebugEnabled()) {
315 logger.debug("completed " + methodName + " with primaryExpression --> '"
316 + primaryExpression + "'");
317 }
318 }
319 return StringUtils.deleteWhitespace(primaryExpression.toString());
320 }
321
322 /***
323 * Gets all feature calls from the passed in APropertyCallExpression instance.
324 *
325 * @param expression the APosfixExpression instance for which to retrieve
326 * the primary expression
327 *
328 * @return String the "real" primary expression or the passed in expression.
329 */
330 public static List getFeatureCalls(APropertyCallExpression expression) {
331 String methodName = "getFeatureCalls";
332 if (logger.isDebugEnabled()) {
333 logger.debug("performing " + methodName +
334 " with expression --> '" + expression + "'");
335 }
336 List featureCalls = new ArrayList();
337 if (expression != null) {
338 List tails = expression.getPropertyCallExpressionTail();
339 if (tails != null && !tails.isEmpty()) {
340 for (int ctr = 0; ctr < tails.size(); ctr++) {
341 featureCalls.add(
342 TranslationUtils.getProperty(tails.get(ctr),
343 "featureCall"));
344 }
345 }
346 }
347 return featureCalls;
348 }
349
350
351 /***
352 * Loads a List of variable declaration names from the
353 * <code>variableDeclarations</code>
354 *
355 * @param variableDeclarations an array of VariableDeclarationCS objects
356 * @return List the list of argument names as Strings/
357 */
358 public static List getArgumentNames(VariableDeclarationCS[] variableDeclarations) {
359 List names = new ArrayList();
360 for (int ctr = 0; ctr < variableDeclarations.length; ctr++) {
361 names.add(variableDeclarations[ctr].getName());
362 }
363 return names;
364 }
365
366
367 }