001    package org.mesopotamia.lang.java;
002    
003    import java.util.List;
004    
005    import org.mesopotamia.MesopotamiaException;
006    import org.mesopotamia.NodeData;
007    import org.mesopotamia.RepositoryLanguage;
008    import org.mesopotamia.Scan;
009    import org.mesopotamia.lang.java.ref.MethodInfo;
010    import org.mesopotamia.lang.java.ref.Scope;
011    import org.mesopotamia.lang.java.ref.TypeInfo;
012    import org.mesopotamia.lang.java.ref.VariableInfo;
013    import org.w3c.dom.Element;
014    
015    import biz.hammurapi.util.CollectionVisitable;
016    import biz.hammurapi.util.Visitable;
017    import biz.hammurapi.util.Visitor;
018    
019    public class For extends JavaLanguageElement implements Statement, Scope {
020    
021            public For(NodeData xData, Class<?> context, Scan scan,
022                            RepositoryLanguage language, Object environment) throws MesopotamiaException {
023                    super(xData, context, scan, language, environment);
024    
025                    // Select attributes
026                    Initializers = select(JavaLanguageElement.class, "FOR_INIT/*");
027                    Condition = (Expression) selectSingleElement(Expression.class, "FOR_CONDITION/*");
028                    Iterator = select(Expression.class, "FOR_ITERATOR/ELIST/*");
029                    if (language.supportsTokenName("FOR_EACH_CLAUSE")) {
030                            ForEachClause = selectSingleElement(ForEachClause.class, "FOR_EACH_CLAUSE");
031                    }
032                    Statement = (Statement) selectSingleElement(Statement.class, "#org.mesopotamia.lang.java.Statement");
033    
034            }
035    
036            public void toDom(Element holder) {
037                    super.toDom(holder);
038    
039                    // Serialize attributes
040                    setElement(holder, "Initializers", Initializers);
041                    setElement(holder, "Condition", Condition);
042                    setElement(holder, "Iterator", Iterator);
043                    setElement(holder, "ForEachClause", ForEachClause);
044                    setElement(holder, "Statement", Statement);
045            }
046    
047            // Attributes
048            private List<JavaLanguageElement> Initializers;
049    
050            private Expression Condition;
051    
052            private List<Expression> Iterator;
053    
054            private ForEachClause ForEachClause;
055    
056            private Statement Statement;
057    
058            // Accessors
059            public List<JavaLanguageElement> getInitializers() {
060                    return Initializers;
061            }
062    
063            public Expression getCondition() {
064                    return Condition;
065            }
066    
067            public List<Expression> getIterator() {
068                    return Iterator;
069            }
070    
071            public ForEachClause getForEachClause() {
072                    return ForEachClause;
073            }
074    
075            public Statement getStatement() {
076                    return Statement;
077            }
078    
079            protected void acceptChildren(Visitor visitor) {
080                    super.acceptChildren(visitor);
081                    // Visiting non-text attributes
082                    new CollectionVisitable(Initializers, false).accept(visitor);
083                    if (Condition instanceof Visitable) {
084                            ((Visitable) Condition).accept(visitor);
085                    }
086                    new CollectionVisitable(Iterator, false).accept(visitor);
087                    if (ForEachClause instanceof Visitable) {
088                            ((Visitable) ForEachClause).accept(visitor);
089                    }
090                    if (Statement instanceof Visitable) {
091                            ((Visitable) Statement).accept(visitor);
092                    }
093            }
094    
095            public MethodInfo findMethod(String name, String[] parameterTypes) {
096                    Scope ec = findParent(Scope.class);
097                    return ec==null ? null : ec.findMethod(name, parameterTypes);
098            }
099    
100            public TypeInfo findType(String name) {
101                    Scope ec = findParent(Scope.class);
102                    return ec==null ? null : ec.findType(name);
103            }
104    
105            public VariableInfo findVariable(String name) {
106                    Scope ec = findParent(Scope.class);
107                    for (JavaLanguageElement initializer: getInitializers()) {
108                            if (initializer instanceof VariableDefinition) {                                
109                                    VariableDefinition ret = (VariableDefinition) initializer;
110                                    if (ret.getName().equals(name)) {
111                                            return ret;
112                                    }
113                                    
114                            }
115                    }
116                    
117                    if (getForEachClause()!=null) {
118                            ParameterDefinition pd = getForEachClause().getParameterDefinition();
119                            if (pd.getName().equals(name)) {
120                                    return pd;
121                            }
122                    }
123                    return ec==null ? null : ec.findVariable(name);
124            }
125    
126    }