Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23dfc743361d20b08a1b965a35a64b1805aa3d80 (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
/*******************************************************************************
 * Copyright (c) 2005, 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 Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ua.tests.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

/*
 * Utility methods for working with files.
 */
public class FileUtil {

	/**
	 * Gets the contents of the file with the given relative path in the given bundle,
	 * as a String (file must be encoded as UTF-8).
	 */
	public static String getContents(Bundle bundle, String relativePath) throws IOException {
		return readString(bundle.getEntry(relativePath).openStream());
	}

	/**
	 * Gets the contents of the file with the given absolute path as a String (file must
	 * be encoded as UTF-8).
	 */
	public static String getContents(String absolutePath) throws IOException {
		return readString(new FileInputStream(absolutePath));
	}
	
	/**
	 * Generates a filename with path to the result file that will be generated
	 * for the intro xml referred to by the string.
	 */
	public static String getResultFile(String in) {
		return getResultFile(in, false);
	}

	/**
	 * Same as above, but gives the option of appending os, ws, and arch. For example,
	 * myfile_serialized_macosx_carbon_ppc.txt.
	 */
	public static String getResultFile(String in, boolean env) {
		StringBuffer buf = new StringBuffer();
		buf.append(in.substring(0, in.lastIndexOf('.')) + "_serialized");
		if (env) {
			buf.append('_');
			buf.append(Platform.getOS());
			buf.append('_');
			buf.append(Platform.getWS());
			buf.append('_');
			buf.append(Platform.getOSArch());
		}
		buf.append(".txt");
		return buf.toString();
	}
	
	/**
	 * Gets the contents of the result file with the given original relative path in
	 * the given bundle, as a String (file must be encoded as UTF-8).
	 */
	public static String getResultFileContents(Bundle bundle, String absolutePath) throws IOException {
		/*
		 * Try [filename]_serialized_os_ws_arch.txt. If it's not there, try
		 * [filename]_serialized.txt.
		 * 
		 * We use different files for os/ws/arch combinations in order to test dynamic content,
		 * specifically filtering. Some of the files have filters by os, ws, and arch so the
		 * result is different on each combination.
		 */
		String contents = null;
		try {
			contents = getContents(bundle, getResultFile(absolutePath, true));
		}
		catch(Exception e) {
			// didn't find the _serialized_os_ws_arch.txt file, try just _serialized.txt
		}
		if (contents == null) {
			try {
				contents = getContents(bundle, getResultFile(absolutePath, false));
			}
			catch(IOException e) {
				throw e;
			}
		}
		return contents;
	}

	/**
	 * Gets the contents of the result file with the given original absolute path as
	 * a String (file must be encoded as UTF-8).
	 */
	public static String getResultFileContents(String absolutePath) throws IOException {
		/*
		 * Try [filename]_serialized_os_ws_arch.txt. If it's not there, try
		 * [filename]_serialized.txt.
		 * 
		 * We use different files for os/ws/arch combinations in order to test dynamic content,
		 * specifically filtering. Some of the files have filters by os, ws, and arch so the
		 * result is different on each combination.
		 */
		String contents = null;
		try {
			contents = getContents(getResultFile(absolutePath, true));
		}
		catch(Exception e) {
			// didn't find the _serialized_os_ws_arch.txt file, try just _serialized.txt
		}
		if (contents == null) {
			try {
				contents = getContents(getResultFile(absolutePath, false));
			}
			catch(IOException e) {
				throw e;
			}
		}
		return contents;
	}
	
	/**
	 * Reads the contents of the input stream as UTF-8 and constructs and returns
	 * as a String.
	 */
	private static String readString(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[4096];
		int num;
		while ((num = in.read(buffer)) > 0) {
			out.write(buffer, 0, num);
		}
		return new String(out.toByteArray(), "UTF-8");
	}
}

Back to the top