1 package org.andromda.core.translation;
2
3 import org.andromda.core.common.ExceptionUtils;
4 import org.apache.commons.lang.StringUtils;
5 import org.apache.commons.lang.builder.ToStringBuilder;
6
7 /**
8 * Stores the translated expression,
9 *
10 * @author Chad Brandon
11 */
12 public class Expression
13 {
14 /**
15 * The resulting translated expression.
16 */
17 private StringBuffer translatedExpression;
18
19 /**
20 * Creates a new instance of this Expression object
21 *
22 * @param originalExpression the expression that will be translated.
23 */
24 public Expression(final String originalExpression)
25 {
26 ExceptionUtils.checkEmpty("originalExpression", originalExpression);
27 this.originalExpression = StringUtils.trimToEmpty(originalExpression);
28 this.translatedExpression = new StringBuffer();
29 }
30
31 /**
32 * Appends the value of the value of the <code>object</code>'s toString result to the current translated expression
33 * String Buffer.
34 *
35 * @param object the object to append.
36 */
37 public void appendToTranslatedExpression(final Object object)
38 {
39 this.translatedExpression.append(object);
40 }
41
42 /**
43 * Appends a space character to the current translated expression String Buffer.
44 */
45 public void appendSpaceToTranslatedExpression()
46 {
47 this.translatedExpression.append(' ');
48 }
49
50 /**
51 * Replaces the regular expression <code>pattern</code> with the replacement within the translated expression
52 * buffer.
53 *
54 * @param pattern the regular expression pattern to replace
55 * @param replacement the replacement string.
56 */
57 public void replaceInTranslatedExpression(
58 final String pattern,
59 final String replacement)
60 {
61 this.translatedExpression =
62 new StringBuffer(this.getTranslatedExpression().replaceAll(pattern, replacement));
63 }
64
65 /**
66 * Performs replacement of the value of the <code>object</code>'s toString result at the start and end positions of
67 * the buffer containing the Expression.
68 *
69 * @param position the position at which to insert
70 * @param object the
71 * @see StringBuffer#insert(int,String)
72 */
73 public void insertInTranslatedExpression(
74 final int position,
75 final Object object)
76 {
77 this.translatedExpression.insert(position, object);
78 }
79
80 /**
81 * Returns the expression after translation.
82 *
83 * @return String
84 */
85 public String getTranslatedExpression()
86 {
87 return TranslationUtils.removeExtraWhitespace(this.translatedExpression.toString());
88 }
89
90 /**
91 * The original expression before translation
92 */
93 private String originalExpression;
94
95 /**
96 * Returns the expression before translation.
97 *
98 * @return String
99 */
100 public String getOriginalExpression()
101 {
102 return TranslationUtils.removeExtraWhitespace(this.originalExpression);
103 }
104
105 /**
106 * The element to which the expression applies.
107 */
108 private String contextElement;
109
110 /**
111 * Returns the element which is the context of this expression.
112 *
113 * @return String the context element element.
114 */
115 public String getContextElement()
116 {
117 final String methodName = "Expression.getContextElement";
118 if (this.contextElement == null)
119 {
120 throw new ExpressionException(methodName + " - contextElement can not be null");
121 }
122 return this.contextElement;
123 }
124
125 /**
126 * The kind of the expression that was translated.
127 */
128 private String kind;
129
130 /**
131 * Returns the Kind of this Expression (inv, post, or pre)
132 *
133 * @return String returns the Kind of this translation
134 */
135 public String getKind()
136 {
137 final String methodName = "Expression.getKind";
138 if (this.contextElement == null)
139 {
140 throw new ExpressionException(methodName + " - kind can not be null");
141 }
142 return this.kind;
143 }
144
145 /**
146 * The name of the expression.
147 */
148 private String name;
149
150 /**
151 * Gets the name of the expression.
152 *
153 * @return String
154 */
155 public String getName()
156 {
157 return name;
158 }
159
160 /**
161 * Sets the name.
162 *
163 * @param name the name to set.
164 */
165 public void setName(final String name)
166 {
167 this.name = name;
168 }
169
170 /**
171 * Sets the context element (the element to which the expression applies: the element declared after the
172 * <code>context</code>)
173 *
174 * @param contextElement the name of the element which is the context element.
175 */
176 public void setContextElement(final String contextElement)
177 {
178 this.contextElement = contextElement;
179 }
180
181 /**
182 * Sets the "kind" of the expression (i.e, "pre", "post", "inv", etc.)
183 *
184 * @param kind the kind to set.
185 */
186 public void setKind(final String kind)
187 {
188 this.kind = kind;
189 }
190
191 /**
192 * @see Object#toString()
193 */
194 public String toString()
195 {
196 return ToStringBuilder.reflectionToString(this);
197 }
198 }