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