
toxgene.util.cdata.xmark.Plants Maven / Gradle / Ivy
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 Plants implements ToXgeneCdataGenerator {
public static int len = 34780;
//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 Plants() {
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/plants_species.txt"));
InputStream i_s_r = FileLoader.inst.loadFile("plants_species.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(i_s_r));
String line = reader.readLine();
while (line != null){
data.add(line);
line = reader.readLine();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
loaded = true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy