001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.filter;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Iterator;
022import java.util.Set;
023
024/**
025 * An implementation of {@link DestinationNode} which navigates all the children of the given node
026 * ignoring the name of the current path (so for navigating using * in a wildcard).
027 *
028 * 
029 */
030public class AnyChildDestinationNode implements DestinationNode {
031    private DestinationNode node;
032
033    public AnyChildDestinationNode(DestinationNode node) {
034        this.node = node;
035    }
036
037    public void appendMatchingValues(Set answer, String[] paths, int startIndex) {
038        Iterator iter = getChildNodes().iterator();
039        while (iter.hasNext()) {
040            DestinationNode child = (DestinationNode) iter.next();
041            child.appendMatchingValues(answer, paths, startIndex);
042        }
043    }
044
045
046    public void appendMatchingWildcards(Set answer, String[] paths, int startIndex) {
047        Iterator iter = getChildNodes().iterator();
048        while (iter.hasNext()) {
049            DestinationNode child = (DestinationNode) iter.next();
050            child.appendMatchingWildcards(answer, paths, startIndex);
051        }
052    }
053
054
055    public void appendDescendantValues(Set answer) {
056        Iterator iter = getChildNodes().iterator();
057        while (iter.hasNext()) {
058            DestinationNode child = (DestinationNode) iter.next();
059            child.appendDescendantValues(answer);
060        }
061    }
062
063    public DestinationNode getChild(String path) {
064        final Collection list = new ArrayList();
065        Iterator iter = getChildNodes().iterator();
066        while (iter.hasNext()) {
067            DestinationNode child = (DestinationNode) iter.next();
068            DestinationNode answer = child.getChild(path);
069            if (answer != null) {
070                list.add(answer);
071            }
072        }
073        if (!list.isEmpty()) {
074            return new AnyChildDestinationNode(this) {
075                protected Collection getChildNodes() {
076                    return list;
077                }
078            };
079        }
080        return null;
081    }
082
083    public Collection getDesendentValues() {
084        Collection answer = new ArrayList();
085        Iterator iter = getChildNodes().iterator();
086        while (iter.hasNext()) {
087            DestinationNode child = (DestinationNode) iter.next();
088            answer.addAll(child.getDesendentValues());
089        }
090        return answer;
091    }
092
093    public Collection getValues() {
094        Collection answer = new ArrayList();
095        Iterator iter = getChildNodes().iterator();
096        while (iter.hasNext()) {
097            DestinationNode child = (DestinationNode) iter.next();
098            answer.addAll(child.getValues());
099        }
100        return answer;
101    }
102
103    public Collection getChildren() {
104        Collection answer = new ArrayList();
105        Iterator iter = getChildNodes().iterator();
106        while (iter.hasNext()) {
107            DestinationNode child = (DestinationNode) iter.next();
108            answer.addAll(child.getChildren());
109        }
110        return answer;
111    }
112
113    public Collection removeDesendentValues() {
114        Collection answer = new ArrayList();
115        Iterator iter = getChildNodes().iterator();
116        while (iter.hasNext()) {
117            DestinationNode child = (DestinationNode) iter.next();
118            answer.addAll(child.removeDesendentValues());
119        }
120        return answer;
121    }
122
123    public Collection removeValues() {
124        Collection answer = new ArrayList();
125        Iterator iter = getChildNodes().iterator();
126        while (iter.hasNext()) {
127            DestinationNode child = (DestinationNode) iter.next();
128            answer.addAll(child.removeValues());
129        }
130        return answer;
131    }
132
133    protected Collection getChildNodes() {
134        return node.getChildren();
135    }
136}
137
138