@@ -27,30 +27,24 @@ public class GroovyIfTag extends GroovySyntaxTag {
2727 private static final String ATTRIBUTE_ENV = "env" ;
2828
2929 public void doStartTag () {
30- String env = (String ) attributes .get (ATTRIBUTE_ENV );
31- String test = (String ) attributes .get (ATTRIBUTE_TEST );
32- env = StringUtils .isBlank (env ) ? null : env ;
33- test = StringUtils .isBlank (test ) ? null : test ;
34- if ((env == null ) && (test == null ))
30+ String env = attributeValueOrNull (ATTRIBUTE_ENV );
31+ String test = attributeValueOrNull (ATTRIBUTE_TEST );
32+
33+ if ((env == null ) && (test == null )) {
3534 throw new GrailsTagException (
3635 "Tag [" +TAG_NAME +"] must have one or both of the attributes [" +ATTRIBUTE_TEST +"] or [" +ATTRIBUTE_ENV +"]" );
37- if (env != null ) {
38- env = calculateExpression (env );
39- }
40- if ((env != null ) && (test != null )) {
41- out .print ("if((GrailsUtil.environment == '" +env +"') && (" );
42- out .print (test );
43- out .println (")) {" );
44- } else if (env != null ) {
45- // double (( is deliberate... to avoid thorny logic
46- out .print ("if(GrailsUtil.environment == '" +env +"') {" );
47- } else {
48- out .print ("if(" );
49- out .print (test );
50- out .println (") {" );
5136 }
52- }
5337
38+ String envExpression = environmentExpressionOrTrue (env );
39+ String testExpression = testExpressionOrTrue (test );
40+
41+ out .print ("if(" );
42+ out .print (envExpression );
43+ out .print (" && " );
44+ out .print (testExpression );
45+ out .println (") {" );
46+ }
47+
5448 public void doEndTag () {
5549 out .println ("}" );
5650 }
@@ -66,4 +60,25 @@ public boolean isBufferWhiteSpace() {
6660 public boolean hasPrecedingContent () {
6761 return true ;
6862 }
63+
64+ private String attributeValueOrNull (String attributeName ) {
65+ String attributeValue = (String ) attributes .get (attributeName );
66+ return StringUtils .isBlank (attributeValue ) ? null : attributeValue ;
67+ }
68+
69+ private String environmentExpressionOrTrue (String envAttributeValue ) {
70+ String expression = "true" ;
71+ if (envAttributeValue != null ) {
72+ expression = "(GrailsUtil.environment == '" + calculateExpression (envAttributeValue ) + "')" ;
73+ }
74+ return expression ;
75+ }
76+
77+ private String testExpressionOrTrue (String testAttributeValue ) {
78+ String expression = "true" ;
79+ if (testAttributeValue != null ) {
80+ expression = "(" + testAttributeValue + ")" ;
81+ }
82+ return expression ;
83+ }
6984}
0 commit comments