001    package railo.transformer.bytecode.literal;
002    
003    import org.objectweb.asm.Type;
004    
005    import railo.runtime.op.Caster;
006    import railo.transformer.bytecode.BytecodeContext;
007    import railo.transformer.bytecode.BytecodeException;
008    import railo.transformer.bytecode.Literal;
009    import railo.transformer.bytecode.Position;
010    import railo.transformer.bytecode.expression.ExprString;
011    import railo.transformer.bytecode.expression.ExpressionBase;
012    import railo.transformer.bytecode.op.OpString;
013    import railo.transformer.bytecode.util.Types;
014    
015    /**
016     * A Literal String
017     */
018    public class LitString extends ExpressionBase implements Literal,ExprString {
019        
020            public static final int MAX_SIZE = 65535;
021            public static final int TYPE_ORIGINAL = 0;
022            public static final int TYPE_UPPER = 1;
023            public static final int TYPE_LOWER = 2;
024            public static final LitString EMPTY = new LitString("",null,null);
025            private String str;
026            private boolean fromBracket;
027    
028    
029            public static ExprString toExprString(String str, Position start,Position end) {
030                    return new LitString(str,start,end);
031            }
032    
033            public static ExprString toExprString(String str) {
034                    return new LitString(str,null,null);
035            }
036    
037            public static LitString toLitString(String str) {
038                    return new LitString(str,null,null);
039            }
040    
041        /**
042         * constructor of the class
043         * @param str
044         * @param line 
045         */
046            public LitString(String str, Position start,Position end) {
047            super(start,end);
048            this.str=str;
049        }
050        
051        /**
052         * @see railo.transformer.bytecode.Literal#getString()
053         */
054        public String getString() {
055            return str;
056        }
057    
058        /**
059         * @see railo.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
060         */
061        private static  Type _writeOut(BytecodeContext bc, int mode,String str) throws BytecodeException {
062            if(str.length()>MAX_SIZE) {
063                    ExprString expr=_toExpr(str);
064                    expr.writeOut(bc, mode);
065            }
066            else {
067                    bc.getAdapter().push(str);
068            }
069            return Types.STRING;
070        }
071        public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException {
072            return _writeOut(bc, mode, str);
073        }
074        
075        public Type writeOut(BytecodeContext bc, int mode, int caseType) throws BytecodeException {
076            if(TYPE_UPPER==caseType)        return _writeOut(bc, mode, str.toUpperCase());
077            if(TYPE_LOWER==caseType)        return _writeOut(bc, mode, str.toLowerCase());
078            return _writeOut(bc, mode, str);
079        }
080    
081        private static ExprString _toExpr(String str) {
082            int size=MAX_SIZE-1;
083            ExprString left = LitString.toExprString(str.substring(0,size));
084            str = str.substring(size);
085            
086            ExprString right = (str.length()>size)?_toExpr(str):toExprString(str);
087    
088            return OpString.toExprString(left, right, false);
089            }
090    
091    
092        /**
093         * @see railo.transformer.bytecode.Literal#getDouble(java.lang.Double)
094         */
095        public Double getDouble(Double defaultValue) {
096            return Caster.toDouble(getString(),defaultValue);
097        }
098    
099        /**
100         * @see railo.transformer.bytecode.Literal#getBoolean(java.lang.Boolean)
101         */
102        public Boolean getBoolean(Boolean defaultValue) {
103            return Caster.toBoolean(getString(),defaultValue);
104        }
105        
106    
107        /**
108             *
109             * @see java.lang.Object#equals(java.lang.Object)
110             */
111            public boolean equals(Object obj) {
112                    if(this==obj) return true;
113                    if(!(obj instanceof LitString)) return false;
114                    return str.equals(((LitString)obj).str);
115            }
116    
117            /**
118             *
119             * @see java.lang.Object#toString()
120             */
121            public String toString() {
122                    return str;
123            }
124    
125            public void upperCase() {
126                    str=str.toUpperCase(); 
127            }
128            public void lowerCase() {
129                    str=str.toLowerCase();
130            }
131    
132            public LitString duplicate() {
133                    return new LitString(str,getStart(),getEnd());
134            }
135    
136            public void fromBracket(boolean fromBracket) {
137                    this.fromBracket=fromBracket;
138            }
139            public boolean fromBracket() {
140                    return fromBracket;
141            }
142    
143    
144        /* *
145         * @see railo.transformer.bytecode.expression.Expression#getType()
146         * /
147        public int getType() {
148            return Types._STRING;
149        }*/
150    }