001: package org.andromda.core.translation;
002:
003: import org.andromda.core.common.ExceptionUtils;
004: import org.apache.commons.lang.StringUtils;
005: import org.apache.commons.lang.builder.ToStringBuilder;
006:
007: /**
008: * Stores the translated expression,
009: *
010: * @author Chad Brandon
011: */
012: public class Expression {
013: /**
014: * The resulting translated expression.
015: */
016: private StringBuffer translatedExpression;
017:
018: /**
019: * Creates a new instance of this Expression object
020: *
021: * @param originalExpression the expression that will be translated.
022: */
023: public Expression(final String originalExpression) {
024: ExceptionUtils.checkEmpty("originalExpression",
025: originalExpression);
026: this .originalExpression = StringUtils
027: .trimToEmpty(originalExpression);
028: this .translatedExpression = new StringBuffer();
029: }
030:
031: /**
032: * Appends the value of the value of the <code>object</code>'s toString result to the current translated expression
033: * String Buffer.
034: *
035: * @param object the object to append.
036: */
037: public void appendToTranslatedExpression(final Object object) {
038: this .translatedExpression.append(object);
039: }
040:
041: /**
042: * Appends a space charater to the current translated expression String Buffer.
043: */
044: public void appendSpaceToTranslatedExpression() {
045: this .translatedExpression.append(' ');
046: }
047:
048: /**
049: * Replaces the regular expressoin <code>pattern</code> with the replacement within the translated expression
050: * buffer.
051: *
052: * @param pattern the regular expression pattern to replace
053: * @param replacement the replacement string.
054: */
055: public void replaceInTranslatedExpression(final String pattern,
056: final String replacement) {
057: this .translatedExpression = new StringBuffer(this
058: .getTranslatedExpression().replaceAll(pattern,
059: replacement));
060: }
061:
062: /**
063: * Performs replacement of the value of the <code>object</code>'s toString result at the start and end positions of
064: * the buffer containing the Expression.
065: *
066: * @param position the position at which to insert
067: * @param object the
068: * @see java.lang.StringBuffer#insert(int,java.lang.String)
069: */
070: public void insertInTranslatedExpression(final int position,
071: final Object object) {
072: this .translatedExpression.insert(position, object);
073: }
074:
075: /**
076: * Returns the expression after translation.
077: *
078: * @return String
079: */
080: public String getTranslatedExpression() {
081: return TranslationUtils
082: .removeExtraWhitespace(this .translatedExpression
083: .toString());
084: }
085:
086: /**
087: * The original expression before translation
088: */
089: private String originalExpression;
090:
091: /**
092: * Returns the expression before translation.
093: *
094: * @return String
095: */
096: public String getOriginalExpression() {
097: return TranslationUtils
098: .removeExtraWhitespace(this .originalExpression);
099: }
100:
101: /**
102: * The element to which the expression applies.
103: */
104: private String contextElement;
105:
106: /**
107: * Returns the element which is the context of this expression.
108: *
109: * @return String the context element element.
110: */
111: public String getContextElement() {
112: final String methodName = "Expression.getContextElement";
113: if (this .contextElement == null) {
114: throw new ExpressionException(methodName
115: + " - contextElement can not be null");
116: }
117: return this .contextElement;
118: }
119:
120: /**
121: * The kind of the expression that was translated.
122: */
123: private String kind;
124:
125: /**
126: * Returns the Kind of this Expression (inv, post, or pre)
127: *
128: * @return String returns the Kind of this translation
129: */
130: public String getKind() {
131: final String methodName = "Expression.getKind";
132: if (this .contextElement == null) {
133: throw new ExpressionException(methodName
134: + " - kind can not be null");
135: }
136: return this .kind;
137: }
138:
139: /**
140: * The name of the expression.
141: */
142: private String name;
143:
144: /**
145: * Gets the name of the expression.
146: *
147: * @return String
148: */
149: public String getName() {
150: return name;
151: }
152:
153: /**
154: * Sets the name.
155: *
156: * @param name the name to set.
157: */
158: public void setName(final String name) {
159: this .name = name;
160: }
161:
162: /**
163: * Sets the context element (the element to which the expression applies --> the element declared after the
164: * <code>context</code>)
165: *
166: * @param contextElement the name of the element which is the context element.
167: */
168: public void setContextElement(final String contextElement) {
169: this .contextElement = contextElement;
170: }
171:
172: /**
173: * Sets the "kind" of the expression (i.e, "pre", "post", "inv", etc.)
174: *
175: * @param kind the kind to set.
176: */
177: public void setKind(final String kind) {
178: this .kind = kind;
179: }
180:
181: /**
182: * @see java.lang.Object#toString()
183: */
184: public String toString() {
185: return ToStringBuilder.reflectionToString(this);
186: }
187: }
|