rsg.RSG.java Source code

Java tutorial

Introduction

Here is the source code for rsg.RSG.java

Source

/* 
 * Copyright 2013 sh1n0b1
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
*/
/*
 * 
 * Xeger is developed by: 
 * https://code.google.com/p/xeger/
 * 
*/
package rsg;

import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import nl.flotsam.xeger.Xeger;
import org.apache.commons.cli.*;

/**
 *
 * @author Sh1n0b1/Ruihai Fang
 * @version: 1.0 beta
 */
public class RSG {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // Define all commands
        CommandLineParser parser = new PosixParser();
        Options options = new Options();
        options.addOption(OptionBuilder.withDescription("regex to generate the strings").hasArg()
                .withArgName("regex").create("r"));
        options.addOption(
                OptionBuilder.withDescription("num of Strings to print").hasArg().withArgName("size").create("s"));
        options.addOption(
                OptionBuilder.withDescription("print this message").hasArg(false).withLongOpt("help").create("h"));

        try {
            // Parse all command from arguments    
            CommandLine line = parser.parse(options, args);

            //check for neccessary parameter value
            if (args.length == 0) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("RSG", options);
                System.exit(0);
            }
            if (!line.hasOption("r")) {
                System.out.println("Please specifiy a regular expression with -r, see help(-h) for more details");
                System.exit(0);
            }
            if (line.hasOption("h")) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("RSG", options);
                System.exit(0);
            }
            if (!line.hasOption("s")) {
                System.out.println(
                        "Please enter the num of strings to print/write with the -s or --size parameter, see help(-h) for more details");
                System.exit(0);
            }

            String regex = line.getOptionValue("r");
            String fileName = line.getOptionValue("w");
            int size = Integer.parseInt(line.getOptionValue("s"));

            //check if the regex is valid
            boolean flag = false;
            try {
                Pattern.compile(regex);
                flag = true;
            } catch (PatternSyntaxException e) {
                System.out.println("The syntax of the regex you enter is incorrect:\n" + e.getMessage());
                System.exit(0);
            }

            //Generate the string 
            String output = "";
            for (int i = 0; i < size; i++) {
                Xeger generator = new Xeger(regex);
                String result = generator.generate();
                assert result.matches(regex);
                output += result + "\n";
            }

            //Print or Write to file
            System.out.println(output);

        }
        //catch exceptions
        catch (ParseException exp) {
            System.out.println("Unexpected exception:" + exp.getMessage());
        } catch (NumberFormatException exp) {
            System.out.println("Error: " + exp.getMessage() + "\nThe size argument is not an interger\n");
        }

    }
}