class DigitsGrammarCreator extends AbstractGrammarCreator implements GrammarCreator {

    @Override
    public byte[] createGrammar(final URI uri)
        throws BadFetchError, IOException {
        final ModeType mode = getMode(uri);
        final Map<String, String> parameters = getParameters(uri);
        final SrgsXmlDocument document;
        try {
            document = new SrgsXmlDocument();
        } catch (ParserConfigurationException e) {
            throw new BadFetchError(e.getMessage(), e);
        }
        final Grammar grammar = document.getGrammar();
        grammar.setMode(mode);
        if (mode == ModeType.VOICE) {
            grammar.setAttribute("xml:lang", "en");
        }
        grammar.setType(GrammarType.SRGS_XML);
        final Rule digit = grammar.appendChild(Rule.class);
        digit.setId("digit");
        final OneOf oneof = digit.appendChild(OneOf.class);
        for (int i = 0; i < MAX_DIGIT; i++) {
            final Item item = oneof.appendChild(Item.class);
            item.addText(Integer.toString(i));
        }
        final Rule digits = grammar.appendChild(Rule.class);
        digits.makePublic();
        digits.setId("digits");
        final Item digitsItem = digits.appendChild(Item.class);
        final int length = getIntParameter(parameters, "length", -1);
        if (length < 0) {
            final int min = getIntParameter(parameters, "minlength", 1);
            final int max = getIntParameter(parameters, "maxlength", -1);
            try {
                digitsItem.setRepeat(min, max);
            } catch (IllegalArgumentException e) {
                throw new BadFetchError(e.getMessage(), e);
            }
        } else {
            try {
                digitsItem.setRepeat(length);
            } catch (IllegalArgumentException e) {
                throw new BadFetchError(e.getMessage(), e);
            }
        }
        final Ruleref ref = digitsItem.appendChild(Ruleref.class);
        ref.setUri(digit);
        grammar.setRoot(digits);
        return getBytes(document);
    }

}