001    package railo.transformer.bytecode.expression.var;
002    
003    import java.util.ArrayList;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    import org.objectweb.asm.Type;
008    
009    import railo.runtime.type.scope.Scope;
010    import railo.runtime.type.scope.ScopeFactory;
011    import railo.transformer.bytecode.BytecodeContext;
012    import railo.transformer.bytecode.BytecodeException;
013    import railo.transformer.bytecode.Literal;
014    import railo.transformer.bytecode.expression.ExprString;
015    import railo.transformer.bytecode.expression.Expression;
016    import railo.transformer.bytecode.expression.ExpressionBase;
017    import railo.transformer.bytecode.literal.Identifier;
018    import railo.transformer.bytecode.literal.LitString;
019    
020    public final class VariableString extends ExpressionBase implements ExprString {
021    
022            private Expression expr;
023    
024            public VariableString(Expression expr) {
025                    super(expr.getStart(),expr.getEnd());
026                    this.expr=expr;
027            }
028     
029            public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
030                    return translateVariableToExprString(expr,false).writeOut(bc, mode);
031            }
032    
033            public static ExprString toExprString(Expression expr) {
034                    if(expr instanceof ExprString) return (ExprString) expr;
035                    return new VariableString(expr);
036            }
037            
038            public static ExprString translateVariableToExprString(Expression expr, boolean rawIfPossible) throws BytecodeException {
039                    if(expr instanceof ExprString) return (ExprString) expr;
040                    return LitString.toExprString(translateVariableToString(expr,rawIfPossible), expr.getStart(),expr.getEnd());
041            }
042            
043            private static String translateVariableToString(Expression expr, boolean rawIfPossible) throws BytecodeException {
044                    if(!(expr instanceof Variable)) throw new BytecodeException("can't translate value to a string",expr.getStart());
045                    return variableToString((Variable) expr,rawIfPossible);
046            }
047                    
048    
049            public static String variableToString(Variable var, boolean rawIfPossible) throws BytecodeException {
050                    return railo.runtime.type.util.ListUtil.arrayToList(variableToStringArray(var,rawIfPossible),".");
051            }
052            public static String[] variableToStringArray(Variable var, boolean rawIfPossible) throws BytecodeException {
053                    List members = var.getMembers();
054                            
055                    List<String> arr=new ArrayList<String>();
056                    if(var.getScope()!=Scope.SCOPE_UNDEFINED)arr.add(ScopeFactory.toStringScope(var.getScope(),"undefined"));
057                    Iterator it = members.iterator();
058                    DataMember dm;
059                    Expression n;
060                    while(it.hasNext()) {
061                            Object o = it.next();
062                            if(!(o instanceof DataMember)) throw new BytecodeException("can't translate Variable to a String",var.getStart());
063                            dm=(DataMember) o;
064                            n=dm.getName();
065                            if(n instanceof Literal) {
066                                    if(rawIfPossible && n instanceof Identifier) {
067                                            arr.add(((Identifier) n).getRaw());
068                                    }
069                                    else {
070                                            arr.add(((Literal) n).getString());
071                                    }
072                            }
073                            else throw new BytecodeException("argument name must be a constant value",var.getStart());
074                    }
075                    return arr.toArray(new String[arr.size()]);
076            }
077            
078            public String castToString() throws BytecodeException{
079                    return translateVariableToString(expr,false);
080            }
081    }