Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3cd355854bac74fefeac3e7db0891905730ab346 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*****************************************************************************
 * Copyright (c) 2014, 2018 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.tools.file;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * A simple class providing access to the file system relative to a specified project
 *
 */
public class ProjectBasedFileAccess implements IPFileSystemAccess, ICleanUntouched {

	IProject project;

	String subFolderName;

	/**
	 * Store information which files have been created in a hash map
	 * @since 3.0
	 */
	protected Map<String, Boolean> touched;

	/**
	 * force an update of resources (currently always true)
	 */
	final static boolean force = true;

	/**
	 * Create a project based file access for a specific project.
	 *
	 * @param project
	 *            the project for which file system access is provided
	 */
	public ProjectBasedFileAccess(IProject project) {
		this(project, null);
	}

	/**
	 * Create a project based file access for a specific project.
	 *
	 * @param project
	 *            the project for which file system access is provided
	 * @param subFolderName
	 *            a sub-folder within the project, e.g. "src"
	 */
	public ProjectBasedFileAccess(IProject project, String subFolderName) {
		setProject(project, subFolderName);
		touched = new HashMap<String, Boolean>();
	}

	/**
	 * Set/update project, reset subFolderName to null
	 *
	 * @param project
	 *            the project for which file system access is provided
	 */
	public void setProject(IProject project) {
		setProject(project, null);
	}

	/**
	 * Set/update project and sub-folder
	 *
	 * @param project
	 *            the project for which file system access is provided
	 * @param subFolderName
	 *            a sub-folder within the project, e.g. "src"
	 */
	public void setProject(IProject project, String subFolderName) {
		this.project = project;
		this.subFolderName = subFolderName;
	}

	/**
	 * @see org.eclipse.papyrus.infra.tools.util.IPFileSystemAccess.generator.IFileSystemAccess#generateFile(java.lang.String, java.lang.CharSequence)
	 *
	 * @param fileName
	 *            the filename relative to the project and sub-folder specified in the constructor. Use
	 *            '/' (IPFileSystemAccess.SEP_CHAR) as separation character
	 * @param contents
	 *            the content to write
	 */
	public void generateFile(String fileName, String content) {
		IFile file = getFile(fileName);
		touched.put(file.getFullPath().toString(), true);
		InputStream contentStream = new ByteArrayInputStream(content.getBytes());
		try {
			boolean needsRefresh = false;
			if (file.exists()) {
				if (!IOUtils.contentEquals( file.getContents(), contentStream)) {
					if (force) {
						contentStream.reset();
						file.setContents(contentStream, true, true, null);
						needsRefresh = true;
					}
					// else - file is not updated
				}
			} else {
				// the file does not exists
				file.create(contentStream, true, null);
				needsRefresh = true;
			}
			// Refresh the container for the newly created files. This needs to be done even
			// during error because of the possibility for partial results.
			if (needsRefresh) {
				file.refreshLocal(IResource.DEPTH_INFINITE, null);
			}
		} catch (CoreException e) {
			throw new RuntimeException(e);
		} catch (IOException ioe) {
			throw new RuntimeException(ioe);
		}
	}

	/**
	 * @see org.eclipse.papyrus.infra.tools.util.IPFileSystemAccess.generator.IFileSystemAccess#deleteFile(java.lang.String)
	 *
	 * @param fileName
	 *            the filename relative to the project and sub-folder specified in the constructor
	 */
	public void deleteFile(String fileName) {
		IFile file = getFile(fileName);
		try {
			if (file.exists()) {
				file.delete(force, null);
			}
		} catch (CoreException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Return a container (folder) for a given filename.
	 * Folders will be created, if the do not exist (comparable to "mkdir -p" in Unix).
	 *
	 * @param filename
	 *            a filename with the '/' as separation character
	 * @return file for this element
	 */
	protected IFile getFile(String filename) {
		String paths[] = filename.split(IPFileSystemAccess.SEP_CHAR);
		try {
			IContainer packageContainer = getFolder(project, subFolderName);
			for (int i = 0; i < paths.length - 1; i++) {
				String path = paths[i];
				packageContainer = getFolder(packageContainer, path);
				touched.put(packageContainer.getFullPath().toString(), true);
			}
			String last = paths[paths.length - 1];
			return getFile(packageContainer, last);
		} catch (CoreException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Return the a sub-folder from a given container
	 *
	 * @param container
	 *            a container (must be a project or a folder)
	 * @param folderName
	 *            the name of a sub-folder within the container. If null, original container is returned.
	 * @return the sub-folder, it will be created, if it does not exist
	 */
	protected IContainer getFolder(IContainer container, String folderName) throws CoreException {
		if (folderName == null) {
			return container;
		}
		IFolder folder = null;
		if (container instanceof IFolder) {
			folder = ((IFolder) container).getFolder(folderName);
		} else if (container instanceof IProject) {
			folder = ((IProject) container).getFolder(folderName);
		}
		if (folder != null && !folder.exists()) {
			folder.create(false, true, null);
		}
		return folder;
	}

	/**
	 * Return a file within a container
	 *
	 * @param container
	 *            a container (must be a project or a folder)
	 * @param fileName
	 *            the name of a file within the container.
	 * @return the file if it exists, null otherwise
	 */
	protected IFile getFile(IContainer container, String fileName) {
		if (container instanceof IFolder) {
			return ((IFolder) container).getFile(fileName);
		} else if (container instanceof IProject) {
			return ((IProject) container).getFile(fileName);
		}
		return null;
	}

	/**
	 * @see org.eclipse.papyrus.infra.tools.file.ICleanUntouched#cleanOldCode(org.eclipse.core.resources.IFolder, org.eclipse.core.runtime.IProgressMonitor)
	 *
	 * @param folder
	 * @param monitor
	 * @throws CoreException
	 */
	@Override
	public void cleanUntouched(IFolder folder, IProgressMonitor monitor) throws CoreException {
		if (folder.exists()) {
			for (IResource resource : folder.members()) {
				if (resource instanceof IFolder) {
					cleanUntouched((IFolder) resource, monitor);
				}
				if (!touched.containsKey(resource.getFullPath().toString())) {
					resource.delete(false, monitor);
				}
			}
		}
	}
}

Back to the top