KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > resources > mapping > ResourceTraversal


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.core.resources.mapping;
12
13 import java.util.ArrayList JavaDoc;
14 import org.eclipse.core.internal.resources.MarkerManager;
15 import org.eclipse.core.internal.resources.Workspace;
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.CoreException;
18
19 /**
20  * A resource traversal is simply a set of resources and the depth to which
21  * each is to be traversed. A set of traversals is used to describe the
22  * resources that constitute a model element.
23  * <p>
24  * The flags of the traversal indicate which special resources should be
25  * included or excluded from the traversal. The flags used are the same as
26  * those passed to the {@link IResource#accept(IResourceVisitor, int, int)} method.
27  *
28  * <p>
29  * This class may be instantiated or subclassed by clients.
30  * </p>
31
32  * @see org.eclipse.core.resources.IResource
33  * @since 3.2
34  */

35 public class ResourceTraversal {
36
37     private final int depth;
38     private final int flags;
39     private final IResource[] resources;
40
41     /**
42      * Creates a new resource traversal.
43      * @param resources The resources in the traversal
44      * @param depth The traversal depth
45      * @param flags the flags for this traversal. The traversal flags match those
46      * that are passed to the <code>IResource#accept</code> method.
47      */

48     public ResourceTraversal(IResource[] resources, int depth, int flags) {
49         if (resources == null)
50             throw new NullPointerException JavaDoc();
51         this.resources = resources;
52         this.depth = depth;
53         this.flags = flags;
54     }
55
56     /**
57      * Visits all existing resources defined by this traversal.
58      *
59      * @param visitor a resource visitor
60      * @exception CoreException if this method fails. Reasons include:
61      * <ul>
62      * <li> The visitor failed with this exception.</li>
63      * </ul>
64      */

65     public void accept(IResourceVisitor visitor) throws CoreException {
66         for (int i = 0, imax = resources.length; i < imax; i++)
67             try {
68                 if (resources[i].exists())
69                     resources[i].accept(visitor, depth, flags);
70             } catch (CoreException e) {
71                 //ignore failure in the case of concurrent deletion
72
if (e.getStatus().getCode() != IResourceStatus.RESOURCE_NOT_FOUND)
73                     throw e;
74             }
75     }
76
77     /**
78      * Return whether the given resource is contained in or
79      * covered by this traversal, regardless of whether the resource
80      * currently exists.
81      *
82      * @param resource the resource to be tested
83      * @return <code>true</code> if the resource is in this traversal, and
84      * <code>false</code> otherwise.
85      */

86     public boolean contains(IResource resource) {
87         for (int i = 0; i < resources.length; i++) {
88             IResource member = resources[i];
89             if (contains(member, resource)) {
90                 return true;
91             }
92         }
93         return false;
94     }
95
96     private boolean contains(IResource resource, IResource child) {
97         if (resource.equals(child))
98             return true;
99         if (depth == IResource.DEPTH_ZERO)
100             return false;
101         if (child.getParent().equals(resource))
102             return true;
103         if (depth == IResource.DEPTH_INFINITE)
104             return resource.getFullPath().isPrefixOf(child.getFullPath());
105         return false;
106     }
107
108     /**
109      * Efficient implementation of {@link #findMarkers(String, boolean)}, not
110      * available to clients because underlying non-API methods are used that
111      * may change.
112      */

113     void doFindMarkers(ArrayList JavaDoc result, String JavaDoc type, boolean includeSubtypes) {
114         MarkerManager markerMan = ((Workspace) ResourcesPlugin.getWorkspace()).getMarkerManager();
115         for (int i = 0; i < resources.length; i++)
116             markerMan.doFindMarkers(resources[i], result, type, includeSubtypes, depth);
117     }
118
119     /**
120      * Returns all markers of the specified type on existing resources in this traversal.
121      * If <code>includeSubtypes</code> is <code>false</code>, only markers
122      * whose type exactly matches the given type are returned. Returns an empty
123      * array if there are no matching markers.
124      *
125      * @param type the type of marker to consider, or <code>null</code> to indicate all types
126      * @param includeSubtypes whether or not to consider sub-types of the given type
127      * @return an array of markers
128      * @exception CoreException if this method fails.
129      * @see IResource#findMarkers(String, boolean, int)
130      */

131     public IMarker[] findMarkers(String JavaDoc type, boolean includeSubtypes) throws CoreException {
132         if (resources.length == 0)
133             return new IMarker[0];
134         ArrayList JavaDoc result = new ArrayList JavaDoc();
135         doFindMarkers(result, type, includeSubtypes);
136         return (IMarker[]) result.toArray(new IMarker[result.size()]);
137     }
138
139     /**
140      * Returns the depth to which the resources should be traversed.
141      *
142      * @return the depth to which the physical resources are to be traversed
143      * (one of IResource.DEPTH_ZERO, IResource.DEPTH_ONE or
144      * IResource.DEPTH_INFINITE)
145      */

146     public int getDepth() {
147         return depth;
148     }
149
150     /**
151      * Return the flags for this traversal.
152      * The flags of the traversal indicate which special resources should be
153      * included or excluded from the traversal. The flags used are the same as
154      * those passed to the <code>IResource#accept(IResourceVisitor, int, int)</code> method.
155      * Clients who traverse the resources manually (i.e. without calling <code>accept</code>)
156      * should respect the flags when determining which resources are included
157      * in the traversal.
158      *
159      * @return the flags for this traversal
160      */

161     public int getFlags() {
162         return flags;
163     }
164
165     /**
166      * Returns the file system resource(s) for this traversal. The returned
167      * resources must be contained within the same project and need not exist in
168      * the local file system. The traversal of the returned resources should be
169      * done considering the flag returned by getDepth. If a resource returned by
170      * a traversal is a file, it should always be visited. If a resource of a
171      * traversal is a folder then files contained in the folder can only be
172      * visited if the folder is IResource.DEPTH_ONE or IResource.DEPTH_INFINITE.
173      * Child folders should only be visited if the depth is
174      * IResource.DEPTH_INFINITE.
175      *
176      * @return The resources in this traversal
177      */

178     public IResource[] getResources() {
179         return resources;
180     }
181 }
182
Popular Tags