USER'S GUIDE FOR SUN XML GENERATOR
Version @@VERSION@@
Copyright (c) Sun Microsystems, 2001-@@YEAR@@

Document written by Kohsuke KAWAGUCHI
$Id: HowToUse.html 1793 2013-02-18 12:52:53Z snajper $

Sun XML Generator is a tool to generate XML instances from various kinds of schemas.

# Contents of This Document

1. Quick Start
2. Specifying the Root Element
3. Providing Examples
4. Controlling the Size of Documents
5. Generating Invalid Documents

# Quick Start

To generate a valid instance from a RELAX grammar, enter the following command:

    $ java -jar xmlgen.jar myGrammar.rxg

To generate 100 valid instances from a TREX pattern, enter the command:

    $ java -jar xmlgen.jar -n 100 myPattern.trex out$.xml

This will create out00.xml, out01.xml, ... out99.xml.

XML Generator is also capable of generating invalid instances. The following example generates one instance(`invalid.xml`) from a W3C XML Schema(`mine.xsd`). The generated file is usually almost valid.

    $ java -jar xmlgen.jar -error 1/100 mine.xsd invalid.xml

The next example also generates an invalid instance. But this time the generated instance is almost completely wrong.

    $ java -jar xmlgen.jar -error 1/2 mine.xsd terrible.xml

# Specifying the Root Element

With schema languages like XML Schema, the generator cannot tell what element should be used as the root element. Therefore, by default it picks one randomly. The "-root" option allows you to change this behavior by telling the generator to use a specific element.

To generate XML files that start with a `foo` element of the namespace `http://my.name.space/`, specify:

    $ java -jar xmlgen.jar mine.xsd -root {http://my.name.space/}foo

If you are not using XML namespaces, you say

    $ java -jar xmlgen.jar mine.xsd -root {}foo

# Providing Examples

Due to the expressiveness of W3C XML Schema Part 2, sometimes it is difficult to generate a string that satisfies a given datatype, especially when the type is derived with many facets.

If this is the case, XML Generator reports the following error and stops.

    unable to generate value for this datatype: XXXX

You can provide example instances to avoid this problem. If you have a valid instance `abc.xml` that conforms to `joe.trex`, then enter the following command:

    $ java -jar xmlgen.jar joe.trex -example abc.xml

XML Generator will read that file and extract literals found in it. Those literals are then used to generate instances. The "-example" option can be used more than once to provide additional examples.

    $ java -jar xmlgen.jar joe.trex -example ex1.xml -example ex2.xml

# Controlling the Size of Documents

The size of generated documents can be controlled with two parameters: cut-back depth and width.

Cut-back depth decides how deep a document can be. Once the depth exceeds this value, the generator is switched to "cut back" mode. In this mode, the generator skips any optional expressions (like `'?'` or `'*'` in a DTD) and tries to stop further generation. Therefore if the cut-back depth is set to a bigger value, generated documents tend to be big.

Width decides how many times repeatable items are repeated. If you set this value to 5, then `'*'` will be repeated from 0 to 5 times (uniformly distributed), and `'+'` will be repeated from 1 to 6 times (uniformly distributed). Therefore if width is set to a bigger value, generated documents tend to be also big.

Depending on the schema used, the correlation between the size of generated documents and these parameters varies. So you may need to tune these parameters through trial-and-error.

# Generating Invalid Documents

Generator is capable of generating invalid instances. In this mode, generator randomly inserts errors into the document. Errors are made by manipulating the document according to several predefined patterns.

You can control the probability of each pattern, or you can set all probabilities to the same value with the "-error" option.

Probabilities are set by a fractional number "m/n". "2/100" indicates probability of 2%.

The following sub-sections enumerates each pattern and command line option to control the probability.

### Greedy Choice

A "greedy choice" error is one that selects more than one choice at a time.

    Schema:
    <choice>
      <element name="A"> .... </element>
      <element name="B"> .... </element>
    </choice>

    Instance:
    <A> ... </A>
    <B> ... </B>

This probability can be controlled through the "-error-greedyChoice" option.

### Missing Attribute

A "missing attribute" error is made by intentionally skipping a required attribute. This probability can be controlled through the "-error-missingAttribute" option.

    Schema:
    <element name="foo">
      <attribute name="bar">
      ...
    </element>

    Instance:
    <foo>
       ...
    </foo>

### Missing Element

A "missing element" error is made by intentionally skipping a required element. This probability can be controlled through the "-error-missingElement" option.

### Mutated Attribute

A "mutated attribute" error is made by intentionally replacing an attribute by another completely irrelevant attribute. This probability can be controlled through the "-error-mutatedAttribute" option.

    Schema:
    <element name="foo">
      <attribute name="bar">
      ...
    </element>
    ...
    <attribute name="zoo" />

    Instance:
    <foo zoo="...">
       ...
    </foo>

### Mutated Element

A "mutated element" error is made by intentionally replacing an element by another completely irrelevant element. This probability can be controlled through the "-error-mutatedElement" option.

### Sequence Error

A "sequence error" is made by swapping the order of items of a sequence. This probability can be controlled through the "-error-sequenceError" option.

    Schema:
    <sequence>
      <element name="foo"/>
      <element name="bar"/>
    </sequence>

    Instance:
    <bar/>
    <foo/>

### Slip-in Attribute

A "slip-in attribute" error is made by adding an irrelevant attribute to an element. This probability can be controlled through the "-error-slipinAttribute" option.

    Schema:
    <element name="foo"/>
      <attribute name="a"/>
      ...
    </element>

    Instance:
    <foo a="..." xyz="...">
      ...
    </foo>

### Slip-in Element

A "slip-in element" error is the element counterpart of the "slip-in attribute" error. This probability can be controlled through the "-error-slipinElement" option.

### Missing '+'

A "missing plus" error is made by not generating a non-optional repeatable item. This probability can be controlled through the "-error-missingPlus" option.

    Schema:
    <!ELEMENT foo  (a,b+,c)>

    Instance:
    <foo>
      <a/><c/>
    </foo>

### Attribute Name Typo

An "attribute name typo" error is made by modifying several characters of an attribute name. This probability can be controlled through the "-error-attributeNameTypo" option.

    Schema:
    <!ATTLIST foo  bar CDATA #IMPLIED>

    Instance:
    <foo bbr="..." />

### Element Name Typo

An "element name typo" error is the element version of the "attribute name typo" error. It is made by modifying several characters of an element name. This probability can be controlled through the "-error-elementNameTypo" option.