Correct reference to EPL in source headers
[egit/chris.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / ContainerTreeIterator.java
blob88390d4cff5cef4e7787b5d45dfee5c048df1aca
1 /*******************************************************************************
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org.eclipse.egit.core;
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.InputStream;
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspaceRoot;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.egit.core.project.RepositoryMapping;
22 import org.spearce.jgit.errors.IncorrectObjectTypeException;
23 import org.spearce.jgit.lib.Constants;
24 import org.spearce.jgit.lib.FileMode;
25 import org.spearce.jgit.lib.ObjectId;
26 import org.spearce.jgit.lib.Repository;
27 import org.spearce.jgit.treewalk.AbstractTreeIterator;
28 import org.spearce.jgit.treewalk.WorkingTreeIterator;
29 import org.spearce.jgit.util.FS;
31 /**
32 * Adapts an Eclipse {@link IContainer} for use in a <code>TreeWalk</code>.
33 * <p>
34 * This iterator converts an Eclipse IContainer object into something that a
35 * TreeWalk instance can iterate over in parallel with any other Git tree data
36 * structure, such as another working directory tree from outside of the
37 * workspace or a stored tree from a Repository object database.
38 * <p>
39 * Modification times provided by this iterator are obtained from the cache
40 * Eclipse uses to track external resource modification. This can be faster, but
41 * requires the user refresh their workspace when external modifications take
42 * place. This is not really a concern as it is common practice to need to do a
43 * workspace refresh after externally modifying a file.
45 * @see org.spearce.jgit.treewalk.TreeWalk
47 public class ContainerTreeIterator extends WorkingTreeIterator {
48 private static String computePrefix(final IContainer base) {
49 final RepositoryMapping rm = RepositoryMapping.getMapping(base);
50 if (rm == null)
51 throw new IllegalArgumentException("Not in a Git project: " + base);
52 return rm.getRepoRelativePath(base);
55 private final IContainer node;
57 /**
58 * Construct a new iterator from a container in the workspace.
59 * <p>
60 * The iterator will support traversal over the named container, but only if
61 * it is contained within a project which has the Git repository provider
62 * connected and this resource is mapped into a Git repository. During the
63 * iteration the paths will be automatically generated to match the proper
64 * repository paths for this container's children.
66 * @param base
67 * the part of the workspace the iterator will walk over.
69 public ContainerTreeIterator(final IContainer base) {
70 super(computePrefix(base));
71 node = base;
72 init(entries());
75 /**
76 * Construct a new iterator from the workspace root.
77 * <p>
78 * The iterator will support traversal over workspace projects that have
79 * a Git repository provider connected and is mapped into a Git repository.
80 * During the iteration the paths will be automatically generated to match
81 * the proper repository paths for this container's children.
83 * @param root
84 * the workspace root to walk over.
86 public ContainerTreeIterator(final IWorkspaceRoot root) {
87 super("");
88 node = root;
89 init(entries());
92 /**
93 * Construct a new iterator from a container in the workspace, with a given
94 * parent iterator.
95 * <p>
96 * The iterator will support traversal over the named container, but only if
97 * it is contained within a project which has the Git repository provider
98 * connected and this resource is mapped into a Git repository. During the
99 * iteration the paths will be automatically generated to match the proper
100 * repository paths for this container's children.
102 * @param p
103 * the parent iterator we were created from.
104 * @param base
105 * the part of the workspace the iterator will walk over.
107 public ContainerTreeIterator(final WorkingTreeIterator p,
108 final IContainer base) {
109 super(p);
110 node = base;
111 init(entries());
114 @Override
115 public AbstractTreeIterator createSubtreeIterator(final Repository db)
116 throws IncorrectObjectTypeException, IOException {
117 if (FileMode.TREE.equals(mode))
118 return new ContainerTreeIterator(this,
119 (IContainer) ((ResourceEntry) current()).rsrc);
120 else
121 throw new IncorrectObjectTypeException(ObjectId.zeroId(),
122 Constants.TYPE_TREE);
126 * Get the ResourceEntry for the current entry.
128 * @return the current entry
130 public ResourceEntry getResourceEntry() {
131 return (ResourceEntry) current();
134 private Entry[] entries() {
135 final IResource[] all;
136 try {
137 all = node.members(IContainer.INCLUDE_HIDDEN);
138 } catch (CoreException err) {
139 return EOF;
142 final Entry[] r = new Entry[all.length];
143 for (int i = 0; i < r.length; i++)
144 r[i] = new ResourceEntry(all[i]);
145 return r;
149 * Wrapper for a resource in the Eclipse workspace
151 static public class ResourceEntry extends Entry {
152 final IResource rsrc;
154 private final FileMode mode;
156 private long length = -1;
158 ResourceEntry(final IResource f) {
159 rsrc = f;
161 switch (f.getType()) {
162 case IResource.FILE:
163 if (FS.INSTANCE.canExecute(asFile()))
164 mode = FileMode.EXECUTABLE_FILE;
165 else
166 mode = FileMode.REGULAR_FILE;
167 break;
168 case IResource.PROJECT:
169 case IResource.FOLDER: {
170 final IContainer c = (IContainer) f;
171 if (c.findMember(".git") != null)
172 mode = FileMode.GITLINK;
173 else
174 mode = FileMode.TREE;
175 break;
177 default:
178 mode = FileMode.MISSING;
179 break;
183 @Override
184 public FileMode getMode() {
185 return mode;
188 @Override
189 public String getName() {
190 if (rsrc.getType() == IResource.PROJECT)
191 return rsrc.getLocation().lastSegment();
192 else
193 return rsrc.getName();
196 @Override
197 public long getLength() {
198 if (length < 0) {
199 if (rsrc instanceof IFile)
200 length = asFile().length();
201 else
202 length = 0;
204 return length;
207 @Override
208 public long getLastModified() {
209 return rsrc.getLocalTimeStamp();
212 @Override
213 public InputStream openInputStream() throws IOException {
214 if (rsrc instanceof IFile) {
215 try {
216 return ((IFile) rsrc).getContents(true);
217 } catch (CoreException err) {
218 final IOException ioe = new IOException(err.getMessage());
219 ioe.initCause(err);
220 throw ioe;
223 throw new IOException("Not a regular file: " + rsrc);
227 * Get the underlying resource of this entry.
229 * @return the underlying resource
231 public IResource getResource() {
232 return rsrc;
235 private File asFile() {
236 return ((IFile) rsrc).getLocation().toFile();