Index: DefaultCodeBuilder.java
===================================================================
--- DefaultCodeBuilder.java	(revision 44197)
+++ DefaultCodeBuilder.java	(revision 44198)
@@ -10,14 +10,21 @@
 import static org.gvsig.expressionevaluator.Code.CONSTANT;
 import static org.gvsig.expressionevaluator.Code.IDENTIFIER;
 import static org.gvsig.expressionevaluator.Code.UNDEFINED;
+import static org.gvsig.expressionevaluator.Code.CODES;
 import org.gvsig.expressionevaluator.Code.Caller;
 import org.gvsig.expressionevaluator.Code.Constant;
+import static org.gvsig.expressionevaluator.Code.EMPTY_FORMATTER;
 import org.gvsig.expressionevaluator.Code.Identifier;
 import org.gvsig.expressionevaluator.Code.Method;
 import org.gvsig.expressionevaluator.CodeBuilder;
 import org.gvsig.expressionevaluator.Codes;
+import org.gvsig.expressionevaluator.ExpressionBuilder;
+import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
+import org.gvsig.expressionevaluator.ExpressionUtils;
+import org.gvsig.expressionevaluator.Formatter;
 import org.gvsig.expressionevaluator.Function;
 import org.gvsig.expressionevaluator.Interpreter;
+import org.gvsig.expressionevaluator.SymbolTable;
 import org.gvsig.expressionevaluator.impl.function.operator.AddOperator;
 import org.gvsig.expressionevaluator.impl.function.operator.AndOperator;
 import org.gvsig.expressionevaluator.impl.function.operator.ConcatOperator;
@@ -45,9 +52,9 @@
 import org.gvsig.tools.visitor.Visitor;
 
 @SuppressWarnings("UseSpecificCatch")
-    public class DefaultCodeBuilder implements CodeBuilder {
+public class DefaultCodeBuilder implements CodeBuilder {
 
-    public class BaseCode implements Code {
+    public abstract class BaseCode implements Code {
 
         @Override
         public int code() {
@@ -59,6 +66,31 @@
             visitor.visit(this);
         }
 
+        @Override
+        public Value toValue(ExpressionBuilder builder) {
+            return null;
+        }
+
+        @Override
+        public Value toValue() {
+            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
+            return this.toValue(builder);
+        }
+        
+        @Override
+        public void link(SymbolTable symbolTable) {
+            if( this.code() == Code.CALLER ) {
+                Caller caller = (Caller) this;
+                if( caller.function() == null ) {
+                    caller.function(symbolTable.function(caller.name()));
+                }
+                if( caller.parameters() != null ) {
+                    for( Code arg : caller.parameters() ) {
+                        arg.link(symbolTable);
+                    }
+                }
+            }
+        }
     }
 
     class BaseConstant extends BaseCode implements Constant {
@@ -80,7 +112,20 @@
         }
 
         @Override
+        public Value toValue(ExpressionBuilder builder) {
+            return builder.constant(this.value);
+        }
+
+        @Override
         public String toString() {
+            return this.toString(EMPTY_FORMATTER);
+        }
+        
+        @Override
+        public String toString(Formatter<Code> formatter) {
+            if( formatter.canApply(this) ) {
+                return formatter.format(this);
+            }
             StringBuilder builder = new StringBuilder();
             Object v = this.value();
             if( v == null ) {
@@ -89,7 +134,7 @@
                 if( StringUtils.isEmpty((CharSequence)v) ) {
                     builder.append("''");
                 } else {
-                    v = StringUtils.wrap(v.toString(), "'");
+                    v = "'" + StringUtils.replace(v.toString(), "'", "''") + "'";
                     builder.append(v);
                 }
                 
@@ -150,8 +195,8 @@
     
     public class BaseIdentifier extends BaseCode implements Identifier, RecursionControlSupport {
 
-        private String name;
-        private RecursionSupport recursionSupport;
+        private final String name;
+        private final RecursionSupport recursionSupport;
 
         public BaseIdentifier(String name) {
             this.name = name;
@@ -169,7 +214,20 @@
         }
 
         @Override
+        public Value toValue(ExpressionBuilder builder) {
+            return builder.variable(this.name);
+        }
+
+        @Override
         public String toString() {
+            return this.toString(EMPTY_FORMATTER);
+        }
+        
+        @Override
+        public String toString(Formatter<Code> formatter) {
+            if( formatter.canApply(this) ) {
+                return formatter.format(this);
+            }
             StringBuilder builder = new StringBuilder();
             builder.append("\"");
             builder.append(this.name());
@@ -203,6 +261,11 @@
         }
 
         @Override
+        public int code() {
+            return CODES;
+        }
+        
+        @Override
         public int size() {
             if( codes == null ) {
                 return 0;
@@ -243,6 +306,24 @@
 
         @Override
         public String toString() {
+            return this.toString(EMPTY_FORMATTER);
+        }
+        
+        @Override
+        public Value toValue(ExpressionBuilder builder) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public Value toValue() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public String toString(Formatter<Code> formatter) {
+            if( formatter.canApply(this) ) {
+                return formatter.format(this);
+            }
             if( codes != null ) {
                 StringBuilder builder = new StringBuilder();
                 boolean skipcoma = true;
@@ -255,13 +336,20 @@
                     } else {
                         builder.append(", ");
                     }
-                    builder.append(code.toString());
+                    builder.append(code.toString(formatter));
                 }
                 return builder.toString();
             }
             return "";
         }
 
+        @Override
+        public void link(SymbolTable symbolTable) {
+            for (Code code : this.codes) {
+                code.link(symbolTable);
+            }
+        }
+
     }
 
     public class BaseCaller extends BaseCode implements Caller, RecursionControlSupport {
@@ -270,7 +358,7 @@
         private final Codes args;
         private Function function;
         private final int type;
-        private RecursionSupport recursionSupport;
+        private final RecursionSupport recursionSupport;
 
         public BaseCaller(String name, int type, Codes args) {
             this.name = name;
@@ -307,7 +395,7 @@
         }
 
         @Override
-        public Codes args() {
+        public Codes parameters() {
             return this.args;
         }
 
@@ -325,14 +413,75 @@
         }
 
         @Override
+        public Value toValue(ExpressionBuilder builder) {
+            switch(this.type) {
+                case UNARY_OPERATOR:
+                    return builder.function(
+                            NegOperator.NAME.equalsIgnoreCase(this.name())?
+                                "-" :
+                                this.name(),
+                            this.parameters().get(0).toValue(builder)
+                    );
+                case BINARY_OPERATOR:
+                    return builder.binaryOperator(
+                            this.name(),
+                            this.parameters().get(0).toValue(builder),
+                            this.parameters().get(1).toValue(builder)
+                    );
+                case FUNCTION:
+                default:
+                    ExpressionBuilder.Function f = builder.function(this.name());
+                    if( this.parameters()!=null ) {
+                        for (Code parameter : this.parameters()) {
+                            f.parameter(parameter.toValue(builder));
+                        }
+                        
+                    }
+                    return f;
+
+            }
+        }
+
+        @Override
         public String toString() {
+            return this.toString(EMPTY_FORMATTER);
+        }
+        
+        @Override
+        public String toString(Formatter<Code> formatter) {
+            if( formatter.canApply(this) ) {
+                return formatter.format(this);
+            }
             StringBuilder builder = new StringBuilder();
-            builder.append(this.name());
-            builder.append("(");
-            if( this.args()!=null ) {
-                builder.append(this.args().toString());
+            switch(this.type) {
+                case UNARY_OPERATOR:
+                    if( NegOperator.NAME.equalsIgnoreCase(this.name()) ) {
+                        builder.append("-");
+                    } else {
+                        builder.append(this.name());
+                    }
+                    builder.append("(");
+                    builder.append(this.parameters().get(0).toString(formatter));
+                    builder.append(")");
+                    break;
+                case BINARY_OPERATOR:
+                    builder.append("(");
+                    builder.append(this.parameters().get(0).toString(formatter));
+                    builder.append(" ");
+                    builder.append(this.name());
+                    builder.append(" ");
+                    builder.append(this.parameters().get(1).toString(formatter));
+                    builder.append(")");
+                    break;
+                case FUNCTION:
+                default:
+                    builder.append(this.name());
+                    builder.append("(");
+                    if( this.parameters()!=null ) {
+                        builder.append(this.parameters().toString(formatter));
+                    }
+                    builder.append(")");
             }
-            builder.append(")");
             return builder.toString();
         }
 
@@ -380,13 +529,32 @@
 
         @Override
         public String toString() {
+            return this.toString(EMPTY_FORMATTER);
+        }
+
+        @Override
+        public Value toValue(ExpressionBuilder builder) {
+            ExpressionBuilder.Method m = builder.method(this.obj.toValue(builder), this.methodname);
+            if( this.parameters()!=null ) {
+                for (Code parameter : this.parameters()) {
+                    m.parameter(parameter.toValue(builder));
+                }
+            }
+            return m;
+        }
+
+        @Override
+        public String toString(Formatter<Code> formatter) {
+            if( formatter.canApply(this) ) {
+                return formatter.format(this);
+            }
             StringBuilder builder = new StringBuilder();
-            builder.append(this.obj.toString());
+            builder.append(this.obj.toString(formatter));
             builder.append("->");
             builder.append(this.methodname());
             builder.append("(");
-            if( this.args()!=null ) {
-                builder.append(this.args().toString());
+            if( this.parameters()!=null ) {
+                builder.append(this.parameters().toString(formatter));
             }
             builder.append(")");
             return builder.toString();
@@ -446,7 +614,7 @@
         args.add(arg2);
         return function(name, Caller.BINARY_OPERATOR, args);
     }
-
+    
     @Override
     public Code not(Code op1) {
         return operator(NotOperator.NAME, op1);
@@ -564,4 +732,5 @@
     }
 
     
+    
 }
