/* * Copyright (c) 2001 by Pankaj Kumar. All Rights Reserved. * * This program is open source software; you may use, copy, modify, and * redistribute it under the terms of the LICENSE with which it was * originally distributed. The LICENSE or reference to the LICENSE * can be found in file LICENSE.txt in the base directory of distributed * software. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * LICENSE for more details. */ import java.util.Random; public class RandXMLGen { private int maxChars = 512; // Max. no. of characters in a text node. private int textOccurrencePercentage = 60; private int maxAttrs = 5; String[] elems = { "Country", "City", "Town", "MainMarket", "Stadium", "Park", "Library", "SuperMarket", "Airport", "HistoricalPlace", "RailwayStation", "ShoppingMall", "ParkingLot", "AmusementPark", "Studio", "Factory", "BookStore", "Museum" }; String[] attrs = { "name", "capacity", "location", "size", "area", "administrator", "description", "population", "length", "width", "rent", "attractiveness" }; char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '_', '=', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', ' ', ' ', '\t', '\n' }; boolean[] attrMarker = new boolean[attrs.length]; Random rand = new Random(); String outFileName = null; int noOfElems = 10; void usage() { System.err.print( "Usage\n" +" java org.xperf.xpb.RandXMLGen \n" +"\n" +"This program generates a random XML file.\n" +"\n" ); System.exit(1); } private void parseCLArgs(String[] args) { for (int i = 0; i < args.length; ++i) { String arg = args[i]; String value = null; if (i < args.length - 1) value = args[i + 1]; if (arg.charAt(0) == '-') { if (arg.equals("-help")) usage(); /* else if (arg.equals("-output")) outFileName = value; */ else if (arg.equals("-elemcount")) setNoOfElems(value); else usage(); } else { setNoOfElems(arg); } } } void setNoOfElems(String value) { try { noOfElems = (Integer.decode(value)).intValue(); } catch (NumberFormatException e) { System.err.println("Invalid Loop Count specified. Exception: " + e); System.err.println("Resorting to default value of: " + noOfElems); } } void init() { } void initAttrMarker() { for (int i = 0; i < attrMarker.length; i++) attrMarker[i] = false; } String randString(int size) { StringBuffer sb = new StringBuffer(size); for (int i = 0; i < size; i++) sb.append(chars[rand.nextInt(chars.length)]); return sb.toString(); } void generateElement(int elemIdx) { String name = elems[elemIdx]; System.out.print("<" + name); int noOfAttrs = rand.nextInt(maxAttrs); initAttrMarker(); for (int i = 0; i < noOfAttrs; i++) // Actual no. of attributes generate would be less. { int attrIdx = rand.nextInt(attrs.length); if (attrMarker[attrIdx]) // This attribute is already generated. continue; System.out.print(" " + attrs[attrIdx] + "=\"" + randString(10) + "\""); attrMarker[attrIdx] = true; } System.out.println(">"); if (rand.nextInt(100) < textOccurrencePercentage) System.out.println(randString(rand.nextInt(maxChars))); else generateElement(rand.nextInt(elems.length)); System.out.println(""); } void generate() { System.out.println(""); System.out.println(""); for (int i = 0; i < noOfElems; i++) { int elemIdx = rand.nextInt(elems.length); generateElement(elemIdx); } System.out.println(""); } public static void main(String [] args) { RandXMLGen generator = new RandXMLGen(); generator.parseCLArgs(args); generator.init(); generator.generate(); System.exit(0); } }