summaryrefslogtreecommitdiffstats
blob: 69af4fbb7d05bedc05d3a96e06ee745dc82b3382 (plain)
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
/**
 * <copyright>
 *
 * Copyright (c) 2006 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 *
 * </copyright>
 *
 * $Id$
 */

package org.eclipse.jet.tests;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.manipulation.ConvertLineDelimitersOperation;
import org.eclipse.core.filebuffers.manipulation.FileBufferOperationRunner;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;

/**
 * File utilities for Unit tests getting expected results from the JUnit project.
 *
 */
public class FileUtils {
	
	/**
	 * 
	 *
	 */
	private FileUtils() {
		// prevent instantiation.
	}
	
	/**
	 * Resolve a URL to a location string (file system absolute path).
	 * @param urlString a URL string, possibly using the platform:/ format.
	 * @return an absolute file system path corresponding to <code>urlString</code>
	 * @throws IOException if an IO error occurs
	 * @throws MalformedURLException if urlString is an invalid URL
	 */
	public static String resolveURLToLocationString(String urlString) throws IOException, MalformedURLException {
		String fileName = Platform.resolve(new URL(urlString)).getFile();
		return fileName;
	}
	
	/**
	 * Return the file contents as a string. This method also converts the file's line
	 * separator to the current system's line separator if <code>convertNewLines</code> is <code>true</code>.
	 * @param urlString a URL string, possibly using the platform:/ format.
	 * @param convertNewLines Convert the file line separators to the system line separator.
	 * @return the file contents
	 * @throws IOException if an IO error occurs
	 * @throws MalformedURLException if urlString is an invalid URL
	 * @throws CoreException if an error occurs reading the file. 
	 */
	public static String getFileContents(String urlString, boolean convertNewLines) throws MalformedURLException, IOException, CoreException  {
		
		final ITextFileBufferManager textBufferMgr = FileBuffers.getTextFileBufferManager();
		String fileName = resolveURLToLocationString(urlString);

		final IPath path = new Path(fileName);
		textBufferMgr.connect(path, new NullProgressMonitor());
		final ITextFileBuffer textFileBuffer = textBufferMgr.getTextFileBuffer(path);
		
		if(convertNewLines) {
			new FileBufferOperationRunner(textBufferMgr, null)
				.execute(
					new IPath[] { path }, 
					new ConvertLineDelimitersOperation(System.getProperty("line.separator")),
					new NullProgressMonitor());
		}
		
		final String result = textFileBuffer.getDocument().get();
		textBufferMgr.disconnect(path, new NullProgressMonitor());
		return result;
	}

}