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

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

package toxgene.util.cdata.xmark;

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

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

/**
 * This class implements a simple CDATA generator that produces random
 * first names according to the rules specified by the
 * XMark benchmark.
 * 

* This code is provided as part of ToXgene - (c) 2001 University of * Toronto and IBM Corporation. * * @author Denilson Barbosa * @version 1.0 */ public class FirstNames implements ToXgeneCdataGenerator { public static int len = 10000; public static String[] data; public static boolean loaded = false; private static Random pick; private static String result; /** * 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 FirstNames() { 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)]; //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]; 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/fnames.txt")); InputStream i_s_r = FileLoader.inst.loadFile("fnames.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(i_s_r)); 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 (fnames.txt)"); } loaded = true; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy