1 package org.andromda.translation.ocl.query;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import org.andromda.core.translation.TranslationUtils;
6 import org.apache.commons.lang.StringUtils;
7
8
9
10
11
12
13 public class EjbQLTranslator
14 extends QueryTranslator
15 {
16
17
18
19 private static final String ARG_COUNTER = "counter";
20
21
22
23
24 private short argCounter;
25
26
27
28
29
30 private Map<String, String> usedArguments = new HashMap<String, String>();
31
32
33
34
35 public void preProcess()
36 {
37 super.preProcess();
38 this.usedArguments.clear();
39 this.resetArgCounter();
40 }
41
42
43
44
45 private void resetArgCounter()
46 {
47 this.argCounter = 1;
48 }
49
50
51
52
53
54
55
56 protected String getCounter()
57 {
58 return String.valueOf(argCounter++);
59 }
60
61
62
63
64
65
66
67
68
69
70 protected String replaceFragment(String fragment, String replacement, int index)
71 {
72 if (this.isOperationArgument(replacement))
73 {
74
75
76 String usedArgument = this.usedArguments.get(replacement);
77 if (StringUtils.isEmpty(usedArgument))
78 {
79 String argument = this.getTranslationFragment("argument");
80 argument = this.replaceCounterPattern(argument);
81 this.usedArguments.put(replacement, argument);
82 replacement = argument;
83 }
84 else
85 {
86 replacement = usedArgument;
87 }
88 }
89 return super.replaceFragment(fragment, replacement, index);
90 }
91
92
93
94
95
96
97
98 protected String replaceCounterPattern(String fragment)
99 {
100 if (TranslationUtils.containsPattern(fragment, EjbQLTranslator.ARG_COUNTER))
101 {
102 fragment = TranslationUtils.replacePattern(fragment, EjbQLTranslator.ARG_COUNTER, this.getCounter());
103 }
104 return fragment;
105 }
106 }