1 package org.ocltf.translation; 2 3 import org.apache.commons.lang.StringUtils; 4 import org.apache.commons.lang.builder.ToStringBuilder; 5 import org.ocltf.utils.ExceptionUtils; 6 7 /*** 8 * Contains 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 * The OCL expression before translation 21 */ 22 private String originalExpression; 23 24 /*** 25 * The type which is constained by the the OCL expression. 26 */ 27 private String contextType; 28 29 /*** 30 * The kind of the OCL that was translated. 31 */ 32 private String kind; 33 34 /*** 35 * The name of the constraint. 36 */ 37 private String name; 38 39 /*** 40 * Creates a new instance of this Expression object 41 * @param originalExpression - the OCL expression that will be translated. 42 */ 43 public Expression(String originalExpression) { 44 String methodName = "Expression"; 45 ExceptionUtils.checkEmpty(methodName, "originalExpression", originalExpression); 46 this.originalExpression = StringUtils.trimToEmpty(originalExpression); 47 this.translatedExpression = new StringBuffer(); 48 } 49 50 /*** 51 * Appends the value of the value of the <code>object</code>'s toString 52 * result to the current translated expression String Buffer. 53 * @param object the object to append. 54 */ 55 public void appendToTranslatedExpression(Object object) { 56 this.translatedExpression.append(object); 57 } 58 59 /*** 60 * Appends a space charater to the current translated 61 * expression String Buffer. 62 */ 63 public void appendSpaceToTranslatedExpression() { 64 this.translatedExpression.append(' '); 65 } 66 67 /*** 68 * Replaces the pattern with the replacement within the translated expression 69 * buffer. 70 * @param pattern the pattern to search for. 71 * @param replacement the replacement. 72 */ 73 public void replaceInTranslatedExpression(String pattern, String replacement) { 74 this.translatedExpression = 75 new StringBuffer(TranslationUtils.replacePattern( 76 this.getTranslatedExpression(), pattern, replacement)); 77 } 78 79 /*** 80 * Performs replacement of the value of the <code>object</code>'s 81 * toString result at the start and end positions of the buffer 82 * containing the Expression. 83 * 84 * @param position the position at which to insert 85 * @param object the 86 * 87 * @see java.lang.StringBuffer#insert(int,java.lang.String) 88 */ 89 public void insertInTranslatedExpression(int position, Object object) { 90 this.translatedExpression.insert(position, object); 91 } 92 93 /*** 94 * Returns the expression after translation. 95 * 96 * @return String 97 */ 98 public String getTranslatedExpression() { 99 return TranslationUtils.removeExtraWhitespace(this.translatedExpression.toString()); 100 } 101 102 /*** 103 * Returns the expression before translation. 104 * 105 * @return String 106 */ 107 public String getOriginalExpression() { 108 return TranslationUtils.removeExtraWhitespace(this.originalExpression); 109 } 110 111 /*** 112 * Returns the type which is the context of this 113 * expression. 114 * @return String the context type element. 115 */ 116 public String getContextType() { 117 String methodName = "getContextType"; 118 if (this.contextType == null) { 119 throw new ExpressionException( 120 methodName + " - contextType can not be null"); 121 } 122 return this.contextType; 123 } 124 125 /*** 126 * Returns the Kind of this Expression (inv, post, or pre) 127 * @return String - returns the Kind of this translation 128 */ 129 public String getKind() { 130 String methodName = "getKind"; 131 if (this.contextType == null) { 132 throw new ExpressionException( 133 methodName + " - kind can not be null"); 134 } 135 return this.kind; 136 } 137 138 /*** 139 * @return String 140 */ 141 public String getName() { 142 return name; 143 } 144 145 /*** 146 * Sets the name. 147 * @param name the name to set. 148 */ 149 public void setName(String name) { 150 this.name = name; 151 } 152 153 /*** 154 * Sets the context type (the type to which the expression applies --> 155 * the type declared after the 'context') 156 * @param contextType the name of the element which is the context type. 157 */ 158 public void setContextType(String contextType) { 159 this.contextType = contextType; 160 } 161 162 /*** 163 * Sets the "kind" of the OCL contraint (i.e, "pre", "post", "inv") 164 * @param string 165 */ 166 public void setKind(String string) { 167 kind = string; 168 } 169 170 /*** 171 * @see java.lang.Object#toString() 172 */ 173 public String toString() { 174 return ToStringBuilder.reflectionToString(this); 175 } 176 177 }