001package org.andromda.core.translation; 002 003import org.andromda.core.common.ExceptionUtils; 004import org.apache.commons.lang.StringUtils; 005import org.apache.commons.lang.builder.ToStringBuilder; 006 007/** 008 * Stores the translated expression, 009 * 010 * @author Chad Brandon 011 */ 012public class Expression 013{ 014 /** 015 * The resulting translated expression. 016 */ 017 private StringBuffer translatedExpression; 018 019 /** 020 * Creates a new instance of this Expression object 021 * 022 * @param originalExpression the expression that will be translated. 023 */ 024 public Expression(final String originalExpression) 025 { 026 ExceptionUtils.checkEmpty("originalExpression", originalExpression); 027 this.originalExpression = StringUtils.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 { 039 this.translatedExpression.append(object); 040 } 041 042 /** 043 * Appends a space character to the current translated expression String Buffer. 044 */ 045 public void appendSpaceToTranslatedExpression() 046 { 047 this.translatedExpression.append(' '); 048 } 049 050 /** 051 * Replaces the regular expression <code>pattern</code> with the replacement within the translated expression 052 * buffer. 053 * 054 * @param pattern the regular expression pattern to replace 055 * @param replacement the replacement string. 056 */ 057 public void replaceInTranslatedExpression( 058 final String pattern, 059 final String replacement) 060 { 061 this.translatedExpression = 062 new StringBuffer(this.getTranslatedExpression().replaceAll(pattern, replacement)); 063 } 064 065 /** 066 * Performs replacement of the value of the <code>object</code>'s toString result at the start and end positions of 067 * the buffer containing the Expression. 068 * 069 * @param position the position at which to insert 070 * @param object the 071 * @see StringBuffer#insert(int,String) 072 */ 073 public void insertInTranslatedExpression( 074 final int position, 075 final Object object) 076 { 077 this.translatedExpression.insert(position, object); 078 } 079 080 /** 081 * Returns the expression after translation. 082 * 083 * @return String 084 */ 085 public String getTranslatedExpression() 086 { 087 return TranslationUtils.removeExtraWhitespace(this.translatedExpression.toString()); 088 } 089 090 /** 091 * The original expression before translation 092 */ 093 private String originalExpression; 094 095 /** 096 * Returns the expression before translation. 097 * 098 * @return String 099 */ 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}