01 /*
02 * YamTestUtils.java
03 * Copyright (c) 1998-2008, The University of Sheffield.
04 *
05 * This code is from the GATE project (http://gate.ac.uk/) and is free
06 * software licenced under the GNU General Public License version 3. It is
07 * distributed without any warranty. For more details see COPYING.txt in the
08 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
09 *
10 * Ian, Hamish, Aug 2009
11 */
12
13 package gate.yam;
14
15 import java.io.*;
16 import junit.framework.*;
17 import org.apache.log4j.Logger;
18 import org.springframework.core.io.*;
19 import org.springframework.context.support.*;
20 import gate.util.*;
21 import gate.yam.*;
22 import gate.yam.parse.*;
23 import gate.yam.translate.*;
24 import gate.yam.convert.*;
25
26
27 /**
28 * Utility class for YAM tests.
29 */
30 public class YamTestUtils
31 {
32 /**
33 * Copied from gate.util.Files but modified to use the thread context
34 * classloader.
35 */
36 public static String
37 getResourceAsString(String resourceName, String encoding)
38 throws IOException
39 {
40 // Strip any leading '/'
41 if(resourceName.charAt(0) == '/') {
42 resourceName = resourceName.substring(1);
43 }
44 InputStream resourceStream = Thread.currentThread().
45 getContextClassLoader().getResourceAsStream(resourceName);
46 if(resourceStream == null) return null;
47 BufferedReader resourceReader;
48 InputStreamReader inputStreamReader;
49 if(encoding == null) {
50 inputStreamReader = new InputStreamReader(resourceStream);
51 if(inputStreamReader == null) return null;
52 } else {
53 inputStreamReader = new InputStreamReader(resourceStream, encoding);
54 if(inputStreamReader == null) return null;
55 }
56 resourceReader = new BufferedReader(inputStreamReader);
57 if(resourceReader == null) return null;
58 StringBuffer resourceBuffer = new StringBuffer();
59
60 int i;
61
62 int charsRead = 0;
63 final int size = 1024;
64 char[] charArray = new char[size];
65
66 while( (charsRead = resourceReader.read(charArray,0,size)) != -1 )
67 resourceBuffer.append (charArray,0,charsRead);
68
69 while( (i = resourceReader.read()) != -1 )
70 resourceBuffer.append((char) i);
71
72 resourceReader.close();
73 return resourceBuffer.toString();
74 } // getResourceAsString(String)
75 } // YamTestUtils
|