All Downloads are FREE. Search and download functionalities are using the official Maven repository.

toxgene.util.cdata.xmark.Birds Maven / Gradle / Ivy

The newest version!
package toxgene.util.cdata.xmark;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.jar.JarFile;

import toxgene.core.ToXgeneErrorException;
import toxgene.interfaces.ToXgeneCdataGenerator;
import toxgene.util.FileLoader;

public class Birds implements ToXgeneCdataGenerator {
	public static int len = 10681;

	//public static String[] data;

	public static boolean loaded = false;

	private static Random pick;

	private static String result;
	
	private static ArrayList data;

	/**
	 * Constructor. There are too many constants to be loaded here, so
	 * we cannot declare them explicitly as data = {...}; instead, we
	 * load the words stored in a file within the toxgene.jar package.
	 */
	public Birds() {
		init();
	}

	/**
	 * Specifies a seed for the random generator so that repeated
	 * executions always produce the same content, if the same seed is
	 * provided.
	 *
	 * @param seed random seed to initialize the random generator
	 */
	public void setRandomSeed(int seed) {
		pick = new Random(seed);
	}

	/**
	 * Generates random text whose length is determined by the parameter
	 * length. A value of -1 determines that a string of any length can
	 * be returned; a positive value means that a string must be
	 * truncated if necessary.
	 *
	 * @param length specifies the length of the CDATA value to be returned.
	 */
	public String getCdata(int length) {
		//result = data[pick.nextInt(len)];
		result = data.get(pick.nextInt(len));
		//length == -1 means that the string should not be trimmed
		if (length == -1) {
			return result;
		}

		if (result.length() > length)
			return result.substring(0, length);
		else
			return result;
	}

	/**
	 * This method does all the work of loading the words into memory. 
	 */
	public static void init() {
		if (loaded)
			return;

		//data = new String[len];
		data = new ArrayList();
		try {
			/**
			 * For convenience, the text files are shipped insed toxgene.jar
			 */
//			String tgHome = System.getProperty("ToXgene_home");
//			if (tgHome == null){
//				tgHome = ".";
//			}
//			
//			JarFile jar_file = new JarFile(tgHome + "/toxgene.jar");
//
//			InputStream i_s_r = jar_file.getInputStream(jar_file.getEntry("toxgene/util/cdata/xmark/birds_species.txt"));
			InputStream i_s_r = FileLoader.inst.loadFile("birds_species.txt");
			//BufferedReader reader =	new BufferedReader(new FileReader("/Users/claudionor/toxgene/src/toxgene/util/cdata/xmark/carparts.txt"));
			BufferedReader reader = new BufferedReader(new InputStreamReader(i_s_r));
			String line  = reader.readLine();
			while (line != null){
				data.add(line);
				line = reader.readLine();
			}
			/*String line = null, word = null;
			int index, last, count;
			count = 0;
			do {
				line = reader.readLine();
				if (line == null)
					break;

				last = 0;
				index = line.indexOf(',');
				while (index != -1) {
					data[count++] = line.substring(last, index);
					last = index + 1;
					index = line.indexOf(',', last);
				}
			} while (true);*/
			
		} catch (Exception e) {
			//throw new ToXgeneErrorException("could not initialize Xmark text "+"generator (carparts.txt)");
			System.out.println(e.getMessage());
		}

		loaded = true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy