001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.aspects.dbc.condition.parser;
023:
024: /**
025: *
026: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
027: * @version $Revision: 57186 $
028: */
029: public class BeanshellGenerator implements ExpressionVisitor {
030: Expression expr;
031: StringBuffer code;
032: int indent;
033:
034: //int level;
035:
036: public BeanshellGenerator(Expression expr) {
037: this .expr = expr;
038: }
039:
040: public String createBeanshellCode() {
041: code = new StringBuffer();
042: //code.append("boolean " + RET_NAME + " = false;\n");
043: expr.accept(this );
044: return code.toString();
045: }
046:
047: public void visit(Expression expr) {
048: expr.accept(this );
049: }
050:
051: public void visit(BooleanExpression expr) {
052: if (expr.isTopLevel()) {
053: code.append("return (" + expr.getExpression() + ");\n");
054: } else {
055: code.append(expr.getExpression());
056: }
057: }
058:
059: public void visit(ForAllExpression expr) {
060: createLoopCode(expr, false);
061: }
062:
063: public void visit(ExistsExpression expr) {
064: createLoopCode(expr, true);
065: }
066:
067: public void visit(ImpliesExpression expr) {
068: indent();
069: code.append("if (");
070: expr.getLhs().accept(this );
071: code.append("){\n");
072: indent++;
073: indent();
074: code.append("if(!(");
075: expr.getRhs().accept(this );
076: code.append(")){\n");
077: indent++;
078: indent();
079: code.append("return false;\n");
080: indent--;
081: indent();
082: code.append("}\n");
083:
084: indent--;
085: indent();
086: code.append("}\n");
087:
088: if (expr.isTopLevel()) {
089: code.append("return true;\n");
090: }
091: }
092:
093: public void visit(JavaExpression expr) {
094: code.append(expr.getJava());
095: code.append("\nreturn true;\n");
096: }
097:
098: private void indent() {
099: for (int i = 0; i < indent * 2; i++) {
100: code.append(" ");
101: }
102: }
103:
104: private void createLoopCode(LoopExpression expr, boolean breakType) {
105: String declaration = expr.getDeclaration();
106: String collection = expr.getCollection();
107: Expression body = expr.getBody();
108:
109: indent();
110: code
111: .append("for (" + declaration + " : " + collection
112: + "){\n");
113: indent++;
114:
115: if (body instanceof BooleanExpression) {
116: indent();
117: code.append("if (");
118:
119: if (!breakType) {
120: code.append("!");
121: }
122:
123: code.append("(");
124: body.accept(this );
125: code.append(")){\n");
126:
127: indent++;
128: indent();
129: code.append("return " + breakType + ";\n");
130:
131: indent--;
132: indent();
133: code.append("}\n");
134: } else if (body instanceof ImpliesExpression) {
135: body.accept(this );
136: } else {
137: body.accept(this );
138: }
139: indent--;
140: indent();
141: code.append("}\n");
142: if (expr.isTopLevel()) {
143: indent();
144: code.append("return " + !breakType + ";\n");
145: }
146: }
147: }
|