001    package railo.runtime.interpreter;
002    
003    import railo.commons.lang.NumberUtil;
004    import railo.runtime.exp.ExpressionException;
005    import railo.runtime.exp.PageException;
006    import railo.runtime.interpreter.ref.Ref;
007    import railo.runtime.interpreter.ref.literal.LStringBuffer;
008    import railo.runtime.interpreter.ref.op.Concat;
009    
010    public class JSONExpressionInterpreter extends CFMLExpressionInterpreter {
011            public JSONExpressionInterpreter(){
012                    allowNullConstant=true;
013        }
014            
015            /**
016             *
017             * @see railo.runtime.interpreter.CFMLExpressionInterpreter#string()
018             */
019            protected Ref string() throws PageException {
020            
021            // Init Parameter
022            char quoter = cfml.getCurrentLower();
023            //String str="";
024            LStringBuffer str=new LStringBuffer();
025            Ref value=null;
026            
027            while(cfml.hasNext()) {
028                cfml.next();
029                // check sharp
030                if(cfml.isCurrent('\\')) {
031                    if(cfml.isNext(quoter)){
032                        cfml.next();
033                        str.append(quoter);
034                    }
035                    else if(cfml.isNext('\\')){
036                        cfml.next();
037                        str.append('\\');
038                    }
039                    else if(cfml.isNext('"')){
040                        cfml.next();
041                        str.append('"');
042                    }
043                    else if(cfml.isNext('\'')){
044                        cfml.next();
045                        str.append('\'');
046                    }
047                    else if(cfml.isNext('t')){
048                        cfml.next();
049                        str.append('\t');
050                    }
051                    else if(cfml.isNext('n')){
052                        cfml.next();
053                        str.append('\n');
054                    }
055                    else if(cfml.isNext('b')){
056                        cfml.next();
057                        str.append('\b');
058                    }
059                    else if(cfml.isNext('f')){
060                        cfml.next();
061                        str.append('\f');
062                    }
063                    else if(cfml.isNext('r')){
064                        cfml.next();
065                        str.append('\r');
066                    }
067                    else if(cfml.isNext('u')){
068                        cfml.next();
069                        StringBuffer sb=new StringBuffer();
070                        int i=0;
071                        
072                        for(;i<4 && cfml.hasNext();i++){
073                            cfml.next();
074                            sb.append(cfml.getCurrent());
075                        }
076                        if(i<4){
077                            str.append("\\u");
078                            str.append(sb.toString());
079                        }
080                        else{
081                            int asc = NumberUtil.hexToInt(sb.toString(),-1);
082                            if(asc!=-1)str.append((char)asc);
083                            else {
084                                    str.append("\\u");
085                                    str.append(sb.toString());
086                            }
087                        }   
088                        
089                    }
090                    else if(cfml.isNext('/')){
091                        cfml.next();
092                        str.append('/');
093                    }
094                    else {
095                            str.append('\\');
096                    }     
097                }
098                else if(cfml.isCurrent(quoter)) {
099                    break;          
100                }
101                // all other character
102                else {
103                    str.append(cfml.getCurrent());
104                }
105            }
106            if(!cfml.forwardIfCurrent(quoter))
107                throw new ExpressionException("Invalid String Literal Syntax Closing ["+quoter+"] not found");
108            
109            cfml.removeSpace();
110            mode=STATIC;
111            if(value!=null) {
112                if(str.isEmpty()) return value;
113                return new Concat(pc,value,str);
114            }
115            return str;
116        }
117    
118    }