001    package railo.transformer.cfml.evaluator.impl;
002    
003    import java.util.Iterator;
004    import java.util.List;
005    
006    import railo.transformer.bytecode.Page;
007    import railo.transformer.bytecode.Statement;
008    import railo.transformer.bytecode.cast.CastBoolean;
009    import railo.transformer.bytecode.cast.CastString;
010    import railo.transformer.bytecode.expression.Expression;
011    import railo.transformer.bytecode.literal.LitBoolean;
012    import railo.transformer.bytecode.literal.LitString;
013    import railo.transformer.bytecode.statement.tag.Attribute;
014    import railo.transformer.bytecode.statement.tag.Tag;
015    import railo.transformer.bytecode.util.ASMUtil;
016    import railo.transformer.cfml.evaluator.EvaluatorException;
017    import railo.transformer.cfml.evaluator.EvaluatorSupport;
018    import railo.transformer.library.tag.TagLibTag;
019    
020    
021    
022    /**
023     * Pr�ft den Kontext des Tag break.
024     * Das Tag <code>break</code> darf nur innerhalb des Tag <code>loop, while, foreach</code> liegen.
025     */
026    public class Component extends EvaluatorSupport {
027    
028    
029            /**
030             * @see railo.transformer.cfml.evaluator.EvaluatorSupport#evaluate(org.w3c.dom.Element, railo.transformer.library.tag.TagLibTag)
031             */
032    
033            public void evaluate(Tag tag,TagLibTag tlt) throws EvaluatorException { 
034    
035                    Statement pPage = tag.getParent();
036                    String className=tag.getTagLibTag().getTagClassName();
037                    
038                    // is direct in document
039                    if(!(pPage instanceof Page)){
040                            
041                            // is script Component
042                            Tag p = ASMUtil.getParentTag(tag);
043                            if(p.getTagLibTag().getName().equals("script") && (pPage = p.getParent()) instanceof Page){
044                                    
045                                    // move imports from script to component body
046                                    List children = p.getBody().getStatements();
047                                    Iterator it = children.iterator();
048                                    Statement stat;
049                                    Tag t;
050                                    while(it.hasNext()){
051                                            stat=(Statement) it.next();
052                                            if(!(stat instanceof Tag)) continue;
053                                            t=(Tag) stat;
054                                            if(t.getTagLibTag().getName().equals("import")){
055                                                    tag.getBody().addStatement(t);
056                                            }
057                                    }
058                                    
059                                    // replace script with component        
060                                    ASMUtil.replace(p, tag, false);
061                            }
062                            else
063                                    throw new EvaluatorException("Wrong Context, tag "+tlt.getFullName()+" can't be inside other tags, tag is inside tag "+p.getFullname());
064                    }
065    
066                    Page page=(Page) pPage;
067                    
068                    // is inside a file named cfc
069                    String src=page.getSource();
070                    int pos=src.lastIndexOf(".");
071                    if(!(pos!=-1 && pos<src.length() && src.substring(pos+1).equals("cfc")))
072                            throw new EvaluatorException("Wrong Context, "+tlt.getFullName()+" tag must be inside a file with extension cfc");
073                    
074                    // check if more than one component in document and remove any other data
075                    List stats = page.getStatements();
076                    Iterator it = stats.iterator();
077                    Statement stat;
078                    int count=0;
079                    while(it.hasNext()) {
080                            stat=(Statement) it.next();
081                            if(stat instanceof Tag) {
082                                    tag=(Tag) stat;
083                                    if(tag.getTagLibTag().getTagClassName().equals(className)) count++;
084                            }
085                    }
086                    if(count>1)
087                            throw new EvaluatorException("inside one cfc file only one tag "+tlt.getFullName()+" is allowed, now we have "+count);
088    
089                    boolean isComponent="railo.runtime.tag.Component".equals(tlt.getTagClassName());
090                    boolean isInterface="railo.runtime.tag.Interface".equals(tlt.getTagClassName());
091                    if(isComponent)page.setIsComponent(true);
092                    if(isInterface)page.setIsInterface(true);
093                    
094    // Attributes
095                    
096                    // output
097                    // "output=true" wird in "railo.transformer.cfml.attributes.impl.Function" geh�ndelt
098                    Attribute attr = tag.getAttribute("output");
099                    if(attr!=null) {
100                            Expression expr = CastBoolean.toExprBoolean(attr.getValue());
101                            if(!(expr instanceof LitBoolean))
102                                    throw new EvaluatorException("Attribute output of the Tag "+tlt.getFullName()+", must contain a static boolean value (true or false, yes or no)");
103                            //boolean output = ((LitBoolean)expr).getBooleanValue();
104                            //if(!output) ASMUtil.removeLiterlChildren(tag, true);
105                    }
106                    
107                    // extends
108                    attr = tag.getAttribute("extends");
109                    if(attr!=null) {
110                            Expression expr = CastString.toExprString(attr.getValue());
111                            if(!(expr instanceof LitString)) throw new EvaluatorException("Attribute extends of the Tag "+tlt.getFullName()+", must contain a literal string value");
112                    }
113                    
114                    // implements
115                    if(isComponent){
116                            attr = tag.getAttribute("implements");
117                            if(attr!=null) {
118                                    Expression expr = CastString.toExprString(attr.getValue());
119                                    if(!(expr instanceof LitString)) throw new EvaluatorException("Attribute implements of the Tag "+tlt.getFullName()+", must contain a literal string value");
120                            }
121                    }
122            }
123    }
124    
125    
126    
127