01: /**
02: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
03: *
04: * This program is free software; you can redistribute it and/or modify
05: * it under the terms of the latest version of the GNU Lesser General
06: * Public License as published by the Free Software Foundation;
07: *
08: * This program is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: * GNU Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public License
14: * along with this program (LICENSE.txt); if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16: */package org.jamwiki;
17:
18: import java.io.File;
19: import java.io.FileReader;
20: import org.apache.commons.lang.StringUtils;
21: import org.jamwiki.utils.Utilities;
22: import org.jamwiki.utils.WikiLogger;
23:
24: /**
25: *
26: */
27: public class TestFileUtil {
28:
29: private static final WikiLogger logger = WikiLogger
30: .getLogger(TestFileUtil.class.getName());
31: public static final String TEST_TOPICS_DIR = "data/topics/";
32: public static final String TEST_RESULTS_DIR = "data/results/";
33:
34: /**
35: *
36: */
37: private static File retrieveFile(String directory, String fileName) {
38: // files containing colons aren't allowed, so replace with "_-_"
39: String fullName = directory
40: + StringUtils.replace(fileName, ":", "_-_");
41: File file = null;
42: try {
43: return Utilities.getClassLoaderFile(fullName);
44: } catch (Exception e) {
45: }
46: try {
47: return new File(Utilities.getClassLoaderRoot(), fullName);
48: } catch (Exception e) {
49: }
50: return null;
51: }
52:
53: /**
54: *
55: */
56: public static String retrieveFileContent(String directory,
57: String fileName) throws Exception {
58: FileReader reader = null;
59: try {
60: File file = TestFileUtil.retrieveFile(directory, fileName);
61: if (file == null) {
62: return null;
63: }
64: reader = new FileReader(file);
65: StringBuffer output = new StringBuffer();
66: char[] buf = new char[4096];
67: int c;
68: while ((c = reader.read(buf, 0, buf.length)) != -1) {
69: output.append(buf, 0, c);
70: }
71: return output.toString();
72: } finally {
73: if (reader != null) {
74: try {
75: reader.close();
76: } catch (Exception e) {
77: }
78: }
79: }
80: }
81: }
|