use of org.eclipse.core.resources.IContainer in project sling by apache.
the class ProjectAdapter method ensureDirectoryExists.
/**
* Ensures that the specified directory exists
*
* @param path the path where the directory should exist
* @return the created or existing directory
* @throws CoreException
*/
public IContainer ensureDirectoryExists(IPath path) throws CoreException {
IContainer current = project;
for (int i = 0; i < path.segmentCount(); i++) {
String currentSegment = path.segment(i);
IResource container = current.findMember(currentSegment);
if (container != null) {
if (container.getType() != IContainer.FOLDER) {
throw new IllegalArgumentException("Resource " + container + " exists and is not a folder; unable to create file at path " + path);
}
current = (IContainer) container;
} else {
IFolder newFolder = ((IContainer) current).getFolder(Path.fromPortableString(currentSegment));
newFolder.create(true, true, new NullProgressMonitor());
current = newFolder;
}
}
return current;
}
use of org.eclipse.core.resources.IContainer in project sling by apache.
the class ProjectAdapter method createOrUpdateFile.
/**
* Creates or updates an existing file
*
* @param fileLocation the path where the resource will be created or updated
* @param contents the contents to write to the file. This stream will be closed after being used
* @throws CoreException
*/
public void createOrUpdateFile(IPath fileLocation, InputStream contents) throws CoreException {
if (contents == null) {
throw new IllegalArgumentException("resourceAsStream may not be null");
}
try {
IContainer holder = ensureDirectoryExists(fileLocation.removeLastSegments(1));
IPath fileName = Path.fromPortableString(fileLocation.lastSegment());
IFile file = holder.getFile(fileName);
if (file.exists()) {
file.setContents(contents, true, true, new NullProgressMonitor());
} else {
file.create(contents, true, new NullProgressMonitor());
}
} finally {
IOUtils.closeQuietly(contents);
}
}
use of org.eclipse.core.resources.IContainer in project bndtools by bndtools.
the class OpenExtConfigsContributionItem method getContributionItems.
@Override
protected IContributionItem[] getContributionItems() {
try {
IFile buildFile = Central.getWorkspaceBuildFile();
if (buildFile == null)
return EMPTY;
IContainer cnfDir = buildFile.getParent();
IFolder extDir = cnfDir.getFolder(new Path("ext"));
if (extDir == null || !extDir.exists())
return EMPTY;
IResource[] extFiles = extDir.members();
List<IContributionItem> result = new ArrayList<IContributionItem>(extFiles.length);
for (final IResource extFile : extFiles) {
if (extFile.getType() == IResource.FILE && "bnd".equalsIgnoreCase(extFile.getFileExtension())) {
Action action = new Action() {
@Override
public void run() {
try {
FileEditorInput input = new FileEditorInput((IFile) extFile);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.openEditor(input, "bndtools.bndWorkspaceConfigEditor", true);
} catch (PartInitException e) {
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", "Unable to open editor", e.getStatus());
}
}
};
action.setText("Open " + extFile.getProjectRelativePath());
action.setImageDescriptor(extFileImg);
result.add(new ActionContributionItem(action));
}
}
return result.toArray(new IContributionItem[0]);
} catch (Exception e) {
logger.logError("Unable to find default config files", e);
return EMPTY;
}
}
use of org.eclipse.core.resources.IContainer in project applause by applause.
the class ApplauseEclipseResourceFileSystemAccess2 method getFile.
protected IFile getFile(String fileName, String outputName) {
OutputConfiguration configuration = getOutputConfig(outputName);
IContainer container = getContainer(configuration);
IFile result = container.getFile(new Path(fileName));
syncIfNecessary(result);
return result;
}
use of org.eclipse.core.resources.IContainer in project bndtools by bndtools.
the class FileUtils method mkdirs.
public static void mkdirs(IContainer container, IProgressMonitor monitor) throws CoreException {
SubMonitor progress = SubMonitor.convert(monitor, 2);
if (container.exists())
return;
IContainer parent = container.getParent();
if (parent != null)
mkdirs(parent, progress.newChild(1));
if (container.getType() == IResource.FOLDER) {
IFolder folder = (IFolder) container;
folder.create(false, true, progress.newChild(1));
} else {
throw new CoreException(new Status(IStatus.ERROR, "bndtools.utils", 0, "Can only create plain Folder parent containers.", null));
}
}
Aggregations