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

com.sun.msv.generator.Driver Maven / Gradle / Ivy

/*
 * Copyright (c) 2001-2013 Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sun.msv.generator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.Random;
import java.util.Set;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.sun.msv.driver.textui.DebugController;
import com.sun.msv.grammar.ElementExp;
import com.sun.msv.grammar.Expression;
import com.sun.msv.grammar.ExpressionPool;
import com.sun.msv.grammar.Grammar;
import com.sun.msv.grammar.relax.RELAXModule;
import com.sun.msv.grammar.util.RefExpRemover;
import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar;
import com.sun.msv.reader.dtd.DTDReader;
import com.sun.msv.reader.util.GrammarLoader;
import com.sun.msv.relaxns.grammar.RELAXGrammar;
import com.sun.msv.util.StringPair;
import com.sun.msv.verifier.Verifier;
import com.sun.msv.verifier.regexp.ElementsOfConcernCollector;
import com.sun.msv.verifier.regexp.REDocumentDeclaration;
import com.sun.msv.verifier.util.ErrorHandlerImpl;
import com.sun.msv.verifier.util.IgnoreErrorHandler;

/**
 * command line driver.
 * 
 * @author Kohsuke KAWAGUCHI
 */
public class Driver {
	
	private static void usage() {
		System.err.println(
			"Sun XMLGenerator\n"+
			"----------------\n"+
			"Usage: XMLGenerator   []\n"+
			"Options:\n"+
			"  -dtd      : use a DTD as a schema\n"+
			"  -ascii    : use US-ASCII characters only\n"+
			"  -seed  : set random seed\n"+
			"  -depth : set cut back depth\n"+
			"  -width : maximum number of times '*'/'+' are repeated\n" +
			"  -n     : # of files to be generated\n" +
			"  -warning  : show warnings.\n"+
			"  -quiet    : be quiet.\n"+
            "  -root {}\n"+
            "      fix the root element to the given element\n"+
			"  -encoding \n"+
			"      output encoding (Java name)\n"+
			"  -example \n"+
			"      use the given file as an example. tokens found in the example\n"+
			"      is used to generate documents\n"+
			"  -error /\n"+
			"      error ratio. generate n errors per m elemnts (average).\n"+
			"      to control error generation, see manual for details.\n"+
			"  -nocomment: suppress insertion of comments that indicate generated errors.\n"+
			"\n"+
			"   must include one '$'. '$' will be replaced by number.\n"+
			"  e.g., test.$.xml -> test.1.xml test.2.xml test.3.xml ...\n"+
			"  if omitted, generated file will be sent to stdout.\n"
			);
	}

	public static void main( String[] args ) throws Exception {
		try {
			Driver driver = new Driver();
			try {
				driver.parseArguments(args);
			} catch( CommandLineException e ) {
                System.err.println(e.getMessage());
				usage();
				System.exit(-1);
			}
			
			System.exit( driver.run(System.err) );
		} catch( DataTypeGenerator.GenerationException e ) {
			System.err.println(e.getMessage());
		}
	}
	
	protected double getRatio( String s ) {
		int idx = s.indexOf('/');
		double n = Double.parseDouble(s.substring(0,idx));
		double m = Double.parseDouble(s.substring(idx+1));
						
		double ratio = n/m;
						
		if( ratio<=0 || ratio>1 ) {
			System.err.println("error ratio out of range");
			usage();
			System.exit(-1);
		}
		return ratio;
	}
	
	public Grammar grammar;
	public String outputName=null;
	private String encoding="UTF-8";
	private boolean createError = false;
	private boolean validate = true;
	private boolean debug = false;
	private boolean quiet = false;
	private boolean warning = false;
	private GeneratorOption opt = new GeneratorOption();
	{
		opt.random = new Random();
	}
	private int number = 1;
	
    /** designated root element name. */
    private StringPair rootName = null;
	
	private SAXParserFactory factory = SAXParserFactory.newInstance();
	{
		factory.setNamespaceAware(true);
		factory.setValidating(false);
		try {
			factory.setFeature("http://xml.org/sax/features/validation",false);
			factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
		} catch(Exception e) { ; }
	}

	private DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
	{
		domFactory.setNamespaceAware(true);
		domFactory.setValidating(false);
	}
	
	// this set will receive tokens found in the given examples.
	public final Set exampleTokens = new java.util.HashSet();
	private DataTypeGeneratorImpl dtgi = new DataTypeGeneratorImpl();
	{
		opt.dtGenerator = dtgi;
		dtgi.tokens = exampleTokens;
	}
	
	/** Command line argument error. */
    private static class CommandLineException extends Exception {
        public CommandLineException( String msg ) { super(msg); }
    }
    
	/**
	 * Parses the arguments and fill the fields accordingly.
	 */
    public void parseArguments( String[] args ) throws CommandLineException, ParserConfigurationException {
		String grammarName=null;
		boolean dtdAsSchema = false;

		for( int i=0; i");
			}
			
			org.w3c.dom.Document dom;
			int retry=0;
			
			while(true) {
				dom = domFactory.newDocumentBuilder().newDocument();
				Generator.generate(topLevel,dom,opt);
				
				if(!validate)		break;
				
				// check the validity of generated document.
				DOM2toSAX2 d2s = new DOM2toSAX2();
				Verifier v = new Verifier(
					new REDocumentDeclaration(grammar),
					debug?
						(ErrorHandler)new ErrorHandlerImpl():
						(ErrorHandler)new IgnoreErrorHandler() );
				d2s.setContentHandler(v);
				d2s.traverse(dom);
				
				if( createError && !v.isValid() )	break;
				if( !createError && v.isValid() )	break;
				
				// do it again
				if( retry++ == 100 ) {
					out.println("unable to generate a proper instance.");
					return -1;
				}
			}
		
			// serialize it
			OutputStream os;
			if( outputName==null )		os = System.out;	// in case no output file name is specified
			else {
				int idx = outputName.indexOf('$');
				if( idx!=-1 ) {
					String s = Integer.toString(i);
					for( int j=s.length(); j




© 2015 - 2024 Weber Informatics LLC | Privacy Policy