001    package railo.transformer.cfml.expression;
002    
003    import railo.runtime.exp.TemplateException;
004    import railo.transformer.bytecode.expression.Expression;
005    import railo.transformer.bytecode.literal.LitString;
006    import railo.transformer.cfml.ExprTransformer;
007    import railo.transformer.cfml.evaluator.EvaluatorPool;
008    import railo.transformer.library.function.FunctionLib;
009    import railo.transformer.util.CFMLString;
010    
011    /**
012     * Zum lesen von Attributen bei dem cold fusion expressions nicht geparst werden sollen (cfloop condition) 
013     */
014    public final class SimpleExprTransformer implements ExprTransformer {
015            
016            //char specialChar=0;
017            //protected CFMLString cfml;
018    
019            /* *
020             * Setzt welcher Character speziell behandelt werden soll.
021             * @param c char der speziell behandelt werden soll.
022             * /
023            public void setSpecialChar(char c) {
024                    specialChar=c;
025            }*/
026            
027    
028            private char specialChar;
029    
030            public SimpleExprTransformer(char specialChar) {
031                    this.specialChar=specialChar;
032            }
033    
034            /**
035             * @see railo.transformer.cfml.ExprTransformer#transformAsString(railo.transformer.library.function.FunctionLib[], org.w3c.dom.Document, railo.transformer.util.CFMLString)
036             */
037            public Expression transformAsString(EvaluatorPool ep,FunctionLib[] fld, CFMLString cfml, boolean allowLowerThan) throws TemplateException {
038                    return transform(ep,fld, cfml);
039            }
040            
041            /**
042             * @see railo.transformer.cfml.ExprTransformer#transform(railo.transformer.library.function.FunctionLib[], org.w3c.dom.Document, railo.transformer.util.CFMLString)
043             */
044            public Expression transform(EvaluatorPool ep,FunctionLib[] fld, CFMLString cfml) throws TemplateException {
045                            Expression expr=null;
046                            // String
047                                    if((expr=string(cfml))!=null) {
048                                            return expr;
049                                    }
050                            // Simple
051                                    return simple(cfml);
052            }
053            /**
054             * Liest den String ein
055             * @return Element 
056             * @throws TemplateException
057             */
058            public Expression string(CFMLString cfml)
059                    throws TemplateException {
060                    cfml.removeSpace();
061                    char quoter=cfml.getCurrentLower();
062                    if(quoter!='"' && quoter!='\'')
063                            return null;
064                    StringBuffer str=new StringBuffer();
065                    boolean insideSpecial=false;
066            
067                    int line=cfml.getLine();
068                    while(cfml.hasNext()) {
069                            cfml.next();
070                            // check special
071                            if(cfml.isCurrent(specialChar)) {
072                                    insideSpecial=!insideSpecial;
073                                    str.append(specialChar);
074                                                            
075                            }
076                            // check quoter
077                            else if(!insideSpecial && cfml.isCurrent(quoter)) {
078                                    // Ecaped sharp
079                                    if(cfml.isNext(quoter)){
080                                            cfml.next();
081                                            str.append(quoter);
082                                    }
083                                    // finsish
084                                    else {
085                                            break;
086                                    }                               
087                            }
088                            // all other character
089                            else {
090                                    str.append(cfml.getCurrent());
091                            }
092                    }               
093    
094    
095                    if(!cfml.forwardIfCurrent(quoter))
096                            throw new TemplateException(cfml,"Invalid Syntax Closing ["+quoter+"] not found");
097            
098                    LitString rtn = new LitString(str.toString(),line);
099                    cfml.removeSpace();
100                    return rtn;
101            }
102            
103            /**
104             * Liest ein
105             * @return Element
106             * @throws TemplateException
107             */
108            public Expression simple(CFMLString cfml) throws TemplateException {
109                    StringBuffer sb=new StringBuffer();
110                    int line = cfml.getLine();
111                    while(cfml.isValidIndex()) {
112                            if(cfml.isCurrent(' ') || cfml.isCurrent('>') || cfml.isCurrent("/>")) break;
113                            else if(cfml.isCurrent('"') || cfml.isCurrent('#') || cfml.isCurrent('\'')) {
114                                    throw new TemplateException(cfml,"simple attribute value can't contain ["+cfml.getCurrent()+"]");
115                            }
116                            else sb.append(cfml.getCurrent());
117                            cfml.next();
118                    }
119                    cfml.removeSpace();
120                    
121                    return new LitString(sb.toString(),line);
122            }
123            
124    
125    }