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); }  @Override byte[] createGrammar(final URI uri); @Override String getTypeName(); static final String TYPE_NAME; }
@Test public void testCreateGrammar() throws Exception, BadFetchError { final GrammarCreator creator = new DigitsGrammarCreator(); final URI dtmfUri = new URI("builtin:dtmf/digits"); final byte[] bytes1 = creator.createGrammar(dtmfUri); final ByteArrayInputStream in1 = new ByteArrayInputStream(bytes1); final InputSource source1 = new InputSource(in1); final SrgsXmlDocument document1 = new SrgsXmlDocument(source1); final Grammar dtmfGrammar = document1.getGrammar(); Assert.assertEquals(ModeType.DTMF, dtmfGrammar.getMode()); final Item dtmfItem = getRootItem(document1); Assert.assertEquals("1-", dtmfItem.getRepeat()); final URI voiceUri = new URI("builtin:voice/digit"); final byte[] bytes2 = creator.createGrammar(voiceUri); final ByteArrayInputStream in2 = new ByteArrayInputStream(bytes2); final InputSource source2 = new InputSource(in2); final SrgsXmlDocument document2 = new SrgsXmlDocument(source2); final Grammar voiceGrammar = document2.getGrammar(); Assert.assertEquals(ModeType.VOICE, voiceGrammar.getMode()); final Item voiceItem = getRootItem(document2); Assert.assertEquals("1-", voiceItem.getRepeat()); }
@Test public void testCreateGrammarParamters() throws Exception, BadFetchError { final GrammarCreator creator = new DigitsGrammarCreator(); final URI dtmfUri = new URI( "builtin:dtmf/digits?minlength=2;maxlength=4"); final byte[] bytes1 = creator.createGrammar(dtmfUri); final ByteArrayInputStream in1 = new ByteArrayInputStream(bytes1); final InputSource source1 = new InputSource(in1); final SrgsXmlDocument document1 = new SrgsXmlDocument(source1); final Grammar dtmfGrammar = document1.getGrammar(); Assert.assertEquals(ModeType.DTMF, dtmfGrammar.getMode()); final Item dtmfItem = getRootItem(document1); Assert.assertEquals("2-4", dtmfItem.getRepeat()); final URI voiceUri = new URI( "builtin:voice/digits?minlength=2;maxlength=4"); final byte[] bytes2 = creator.createGrammar(voiceUri); final ByteArrayInputStream in2 = new ByteArrayInputStream(bytes2); final InputSource source2 = new InputSource(in2); final SrgsXmlDocument document2 = new SrgsXmlDocument(source2); final Grammar voiceGrammar = document2.getGrammar(); Assert.assertEquals(ModeType.VOICE, voiceGrammar.getMode()); final Item voiceItem = getRootItem(document2); Assert.assertEquals("2-4", voiceItem.getRepeat()); }
@Test(expected = BadFetchError.class) public void testCreateGrammarIllegalParamters() throws Exception, BadFetchError { final GrammarCreator creator = new DigitsGrammarCreator(); final URI dtmfUri = new URI( "builtin:dtmf/digits?minlength=4;maxlength=2"); creator.createGrammar(dtmfUri); }
@Test public void testCreateGrammarLength() throws Exception, BadFetchError { final GrammarCreator creator = new DigitsGrammarCreator(); final URI dtmfUri = new URI( "builtin:dtmf/digits?length=4"); final byte[] bytes1 = creator.createGrammar(dtmfUri); final ByteArrayInputStream in1 = new ByteArrayInputStream(bytes1); final InputSource source1 = new InputSource(in1); final SrgsXmlDocument document1 = new SrgsXmlDocument(source1); final Grammar dtmfGrammar = document1.getGrammar(); Assert.assertEquals(ModeType.DTMF, dtmfGrammar.getMode()); final Item dtmfItem = getRootItem(document1); Assert.assertEquals("4", dtmfItem.getRepeat()); final URI voiceUri = new URI( "builtin:voice/digits?length=4"); final byte[] bytes2 = creator.createGrammar(voiceUri); final ByteArrayInputStream in2 = new ByteArrayInputStream(bytes2); final InputSource source2 = new InputSource(in2); final SrgsXmlDocument document2 = new SrgsXmlDocument(source2); final Grammar voiceGrammar = document2.getGrammar(); Assert.assertEquals(ModeType.VOICE, voiceGrammar.getMode()); final Item voiceItem = getRootItem(document2); Assert.assertEquals("4", voiceItem.getRepeat()); }
@Test(expected = BadFetchError.class) public void testCreateGrammarIllegalParamterCombination() throws Exception, BadFetchError { final GrammarCreator creator = new DigitsGrammarCreator(); final URI dtmfUri = new URI( "builtin:dtmf/digits?minlength=2;maxlength=4&length=3"); creator.createGrammar(dtmfUri); }