Log in Help
Print
Homegatewikicowtestunitgateyam 〉 YamTestUtils.java
 
/*
 *  YamTestUtils.java
 *  Copyright (c) 1998-2008, The University of Sheffield.
 *
 *  This code is from the GATE project (http://gate.ac.uk/) and is free
 *  software licenced under the GNU General Public License version 3. It is
 *  distributed without any warranty. For more details see COPYING.txt in the
 *  top level directory (or at http://gatewiki.sf.net/COPYING.txt).
 *  
 *  Ian, Hamish, Aug 2009
 */

package gate.yam;

import java.io.*;
import junit.framework.*;
import org.apache.log4j.Logger;
import org.springframework.core.io.*;
import org.springframework.context.support.*;
import gate.util.*;
import gate.yam.*;
import gate.yam.parse.*;
import gate.yam.translate.*;
import gate.yam.convert.*;


/**
 * Utility class for YAM tests.
 */
public class YamTestUtils
{
  /**
   * Copied from gate.util.Files but modified to use the thread context
   * classloader.
   */
  public static String
    getResourceAsString(String resourceName, String encoding)
    throws IOException
  {
    // Strip any leading '/'
    if(resourceName.charAt(0) == '/') {
      resourceName = resourceName.substring(1);
    }
    InputStream resourceStream = Thread.currentThread().
      getContextClassLoader().getResourceAsStream(resourceName);
    if(resourceStream == null) return null;
    BufferedReader resourceReader;
    InputStreamReader inputStreamReader;
    if(encoding == null) {
      inputStreamReader = new InputStreamReader(resourceStream);
      if(inputStreamReader == null) return null;
    } else {
      inputStreamReader = new InputStreamReader(resourceStream, encoding);
      if(inputStreamReader == null) return null;
    }
    resourceReader = new BufferedReader(inputStreamReader);
    if(resourceReader == null) return null;
    StringBuffer resourceBuffer = new StringBuffer();

    int i;

    int charsRead = 0;
    final int size = 1024;
    char[] charArray = new char[size];

    while( (charsRead = resourceReader.read(charArray,0,size)) != -1 )
      resourceBuffer.append (charArray,0,charsRead);

    while( (i = resourceReader.read()) != -1 )
      resourceBuffer.append((char) i);

    resourceReader.close();
    return resourceBuffer.toString();
  } // getResourceAsString(String)
} // YamTestUtils