001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.pageload;
016    
017    import java.util.Collection;
018    import java.util.Iterator;
019    
020    import org.apache.tapestry.IComponent;
021    import org.apache.tapestry.Tapestry;
022    
023    /**
024     *  Walks through the tree of components and invokes the visitors on each of 
025     *  of the components in the tree.
026     * 
027     *  @author mindbridge
028     *  @since 3.0
029     */
030    public class ComponentTreeWalker
031    {
032        private IComponentVisitor[] _visitors;
033        
034        public ComponentTreeWalker(IComponentVisitor[] visitors)
035        {
036            _visitors = visitors;
037        }
038    
039        public void walkComponentTree(IComponent component)
040        {
041            // Invoke visitors
042            for (int i = 0; i < _visitors.length; i++)
043            {
044                IComponentVisitor visitor = _visitors[i];
045                visitor.visitComponent(component);
046            }
047    
048            // Recurse into the embedded components
049            Collection components = component.getComponents().values();
050    
051            if (Tapestry.size(components) == 0)
052                return;
053    
054            for (Iterator it = components.iterator(); it.hasNext();)
055            {
056                IComponent embedded = (IComponent) it.next();
057                walkComponentTree(embedded);
058            }
059        }
060    }