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