Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 481d979b66b1d3c892d60677497a08e5fe02c429 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package org.eclipse.team.internal.ccvs.core.resources;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.Policy;
import org.eclipse.team.internal.ccvs.core.resources.api.CVSFileNotFoundException;
import org.eclipse.team.internal.ccvs.core.resources.api.ICVSFolder;
import org.eclipse.team.internal.ccvs.core.resources.api.ICVSResource;
import org.eclipse.team.internal.ccvs.core.util.Assert;
import org.eclipse.team.internal.ccvs.core.util.Util;

/**
 * CVSResource, CVSFile, CVSFolder implement the 
 * ICVSResource, ICVSFile, ICVSFolder interfaces that are needed
 * to use the cvs-client.
 * 
 * Just call CVSFolder.createFolderFromPath(String path) to create an 
 * ICVSFolder and pass it to the client.
 * 
 * @see CVSFolder#createFolderFromPath(String)
 * @see CVSFolder
 * @see CVSFile
 * @see CVSResource
 * @see ICVSFolder
 * @see ICVSFile
 * @see ICVSResource
 */
abstract class CVSResource implements ICVSResource, Comparable {

	public static final String PLATFORM_NEWLINE = System.getProperty("line.separator");

	File ioResource;
	
	CVSResource(String path) {
		this(new File(path));
	}


	CVSResource(File ioResource) {
		this.ioResource = ioResource;
	}	


	/**
	 * @see ICVSResource#getName()
	 */
	public String getName() {
		
//		String path;
//		int lastFileSeperatorPos;
//		String name;
//		
//		path = ioResource.getAbsolutePath();
//		lastFileSeperatorPos = path.lastIndexOf(File.separator);
//		// check that
//		name = path.substring(lastFileSeperatorPos + 1);
		
		return ioResource.getName();
	}
	
	/**
	 * @see ICVSResource#getPath()
	 */
	public String getPath() {
		return ioResource.getAbsolutePath();
	}


	/**
	 * @see ICVSResource#delete()
	 */
	public void delete() {
		ioResource.delete();
	}


	/**
	 * @see ICVSResource#getParent()
	 */
	public ICVSFolder getParent() {
		
		try {
			return CVSFolder.createInternalFolderFrom(ioResource.getParentFile());
		} catch (CVSException e) {
			// This should not happen, because the canonical Path of
			// a parent should be O.K.
			Util.logError(Policy.bind("CVSFolder.invalidPath"),e);
			Assert.isTrue(false);
			return null;
		}
	}

	/**
	 * Equals is equals on the abstract pathnames
	 */
	public boolean equals(Object obj) {
		
		if (!(obj instanceof CVSResource)) {
			return false;
		} else {
			return ((CVSResource) obj).getPath().equals(getPath());
		}
	}
	
	/**
	 * Generate a Standard CVSException for an
	 * IOException
	 */
	protected static CVSException wrapException(IOException e) {
		return new CVSException(IStatus.ERROR,
								CVSException.IO_FAILED,
								"An IOException occured while using your file-system.",
								e);
	}
	
	/** Clean up fileName. "/" and "\" are both replaced for
	 * File.seperator
	 */
	protected static String convertSeparator(String path) {
		if (File.separatorChar == '/') {
			return path.replace('\\','/');
		} else {
			return path.replace('/','\\');
		}			
	}

	/**
	 * Give the pathname back
	 */
	public String toString() {
		return getPath();
	}

	/**
	 * @see ICVSResource#isFolder()
	 */
	public boolean isFolder() {
		return false;
	}


	/**
	 * @see ICVSResource#exists()
	 */
	public boolean exists() {
		return ioResource.exists();
	}
	
	/**
	 * throw an exeption, if the underlying resource
	 * does not exist.
	 */
	void exceptionIfNotExist() throws CVSFileNotFoundException {
		
		if (!exists()) {
			throw new CVSFileNotFoundException(ioResource + " not found");
		}
	}
	
	/**
	 * Comparing for the work with the sets,
	 * 
	 * The coparison is done by the paths.
	 */
	public int compareTo(Object obj) {
		
		if (!(obj instanceof CVSResource)) {
			return -1;
		} else {
			return getPath().compareTo(((CVSResource)obj).getPath());
		}
	}

	/**
	 * Write String[] to file as lines
	 * 
	 * @param file has to be non-null
	 * @param content has to be non-null
	 */
	protected static void writeToFile(File file, String[] content)
		throws CVSException {
		writeToFile(file, content, PLATFORM_NEWLINE);
		/*
		BufferedWriter fileWriter;
		
		try {
			fileWriter = new BufferedWriter(new FileWriter(file));
			for (int i = 0; i<content.length; i++) {
				fileWriter.write(content[i]);
				fileWriter.newLine();
			}
			fileWriter.close();
		} catch (IOException e) {
			throw rapIOtoCVS(e);
		}
		*/
	}
	
	/**
	 * Write String[] to file as lines
	 * 
	 * @param file has to be non-null
	 * @param content has to be non-null
	 */
	protected static void writeToFile(File file, String[] content, String delim)
		throws CVSException {
		
		BufferedWriter fileWriter;

		deleteIfProtected(file);
		
		try {
			fileWriter = new BufferedWriter(new FileWriter(file));
			for (int i = 0; i<content.length; i++) {
				fileWriter.write(content[i]);
				fileWriter.write(delim);
			}
			fileWriter.close();
		} catch (IOException e) {
			throw wrapException(e);
		}
	}
	
	/**
	 * Delete the file if it is WriteProtected in order to be able to 
	 * write new content in this place
	 */
	protected static void deleteIfProtected(File ioFile) throws CVSException {
		
		boolean sucess;
		
		// If the file is read-only we need to delete it before
		// we can write it new
		if (!ioFile.canWrite()) {
			sucess = ioFile.delete();
			if (!sucess && ioFile.exists()) {
				throw new CVSException("Not able to delete file");
			}
		}			
	}
	
	/**
	 * load file in lines to String[]
	 * 
	 * @param file has to be non-null and file.exists() == true
	 */
	protected static String[] readFromFile(File file)
		throws CVSException {

		BufferedReader fileReader;
		List fileContentStore = new ArrayList();
		String line;
		
		try {
			fileReader = new BufferedReader(new FileReader(file));
			while ((line = fileReader.readLine()) != null) {
				fileContentStore.add(line);
			}
			fileReader.close();
		} catch (IOException e) {
			throw wrapException(e);
		}
			
		return (String[]) fileContentStore.toArray(new String[fileContentStore.size()]);
	}	
	
	/**
	 * This is to be used by the ResourceFactory only
	 */
	public File getIOResource() {
		return ioResource;
	}

	/**
	 * Implement the hashcode on the underlying strings, like it
	 * is done in the equals.
	 */
	public int hashCode() {
		return getPath().hashCode();
	}	
}


Back to the top