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