001    package railo.transformer.cfml.evaluator.impl;
002    
003    import java.util.Iterator;
004    import java.util.List;
005    
006    import org.w3c.dom.Element;
007    
008    import railo.transformer.bytecode.Body;
009    import railo.transformer.bytecode.Statement;
010    import railo.transformer.bytecode.statement.tag.Tag;
011    import railo.transformer.bytecode.util.ASMUtil;
012    import railo.transformer.cfml.evaluator.EvaluatorException;
013    import railo.transformer.cfml.evaluator.EvaluatorSupport;
014    
015    /**
016     * Prueft den Kontext des Tag <code>try</code>.
017     * Innerhalb des Tag try muss sich am Schluss 1 bis n Tags vom Typ catch befinden.
018     */
019    public final class Try extends EvaluatorSupport {
020            
021            /**
022             * @see railo.transformer.cfml.evaluator.EvaluatorSupport#evaluate(Element)
023             */
024            public void evaluate(Tag tag) throws EvaluatorException {
025                    Body body=tag.getBody();
026            int catchCount=0;
027            int noCatchCount=0;
028            int finallyCount=0;
029            
030            // count catch tag and other in body
031            if(body!=null) {
032                    List stats = body.getStatements();
033                    Iterator it = stats.iterator();
034                    Statement stat;
035                    Tag t;
036                String name;
037                while(it.hasNext()) {
038                    stat=(Statement) it.next();
039                    if(stat instanceof Tag) {
040                            t=(Tag) stat;
041                            name=t.getTagLibTag().getName();
042                            if(name.equals("finally")) {
043                                    finallyCount++;
044                                    noCatchCount++;
045                            }
046                            else if(name.equals("catch"))catchCount++;
047                        else noCatchCount++;
048                    }
049                    else noCatchCount++;
050                }
051            }
052            // check if has Content
053            if(catchCount==0 && finallyCount==0)
054                throw new EvaluatorException("Wrong Context, tag cftry must have at least one tag cfcatch inside or a cffinally tag.");
055            if(finallyCount>1)
056                throw new EvaluatorException("Wrong Context, tag cftry can have only one tag cffinally inside.");
057            // check if no has Content
058            if(noCatchCount==0) {
059                    ASMUtil.remove(tag);
060            }
061                    
062            }
063    }