KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > fragments > ASTMatchingFragmentFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.dom.fragments;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.Javadoc;
18
19 import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
20
21 class ASTMatchingFragmentFinder extends GenericVisitor {
22
23     public static IASTFragment[] findMatchingFragments(ASTNode scope, ASTFragment toMatch) {
24         return new ASTMatchingFragmentFinder(toMatch).findMatches(scope);
25     }
26
27     private ASTFragment fFragmentToMatch;
28     private List JavaDoc fMatches= new ArrayList JavaDoc();
29
30     private ASTMatchingFragmentFinder(ASTFragment toMatch) {
31         super(true);
32         fFragmentToMatch= toMatch;
33     }
34     private IASTFragment[] findMatches(ASTNode scope) {
35         fMatches.clear();
36         scope.accept(this);
37         return getMatches();
38     }
39     private IASTFragment[] getMatches() {
40         return (IASTFragment[]) fMatches.toArray(new IASTFragment[fMatches.size()]);
41     }
42     
43     public boolean visit(Javadoc node) {
44         return false;
45     }
46     
47     protected boolean visitNode(ASTNode node) {
48         IASTFragment[] localMatches= fFragmentToMatch.getMatchingFragmentsWithNode(node);
49         for(int i= 0; i < localMatches.length; i++) {
50             fMatches.add(localMatches[i]);
51         }
52         return true;
53     }
54
55 }
56
Popular Tags