SrgsXmlOptionConverter implements OptionConverter { @Override public Grammar createVoiceGrammar(final Collection<Option> options, final Locale language) { if ((options == null) || options.isEmpty()) { return null; } try { final SrgsXmlDocument document = new SrgsXmlDocument(); final Grammar grammar = document.getGrammar(); grammar.setXmlLang(language); grammar.setType(GrammarType.SRGS_XML); final Rule rule = grammar.appendChild(Rule.class); final String name = getName(ModeType.VOICE); rule.setId(name); grammar.setRoot(rule); final OneOf oneof = rule.appendChild(OneOf.class); for (Option option : options) { final Item item = oneof.appendChild(Item.class); final String text = option.getTextContent(); item.addText(text); } return grammar; } catch (ParserConfigurationException e) { LOGGER.warn(e.getMessage(), e); return null; } }  @Override Grammar createVoiceGrammar(final Collection<Option> options,
            final Locale language); @Override Grammar createDtmfGrammar(final Collection<Option> options);  }
@Test public void testCreateVoiceGrammar() throws Exception { final VoiceXmlDocument document = new VoiceXmlDocument(); final Vxml vxml = document.getVxml(); final Form form = vxml.appendChild(Form.class); final Field field = form.appendChild(Field.class); final Option option1 = field.appendChild(Option.class); option1.addText("one"); final Option option2 = field.appendChild(Option.class); option2.addText("two"); final Option option3 = field.appendChild(Option.class); option3.addText("three"); final Collection<Option> options = field.getChildNodes(Option.class); final OptionConverter converter = new SrgsXmlOptionConverter(); final Grammar grammar = converter.createVoiceGrammar(options, Locale.getDefault()); final Rule rule = grammar.getRootRule(); Assert.assertNotNull(rule); final OneOf oneof = rule.getChildNodes(OneOf.class).iterator().next(); Assert.assertNotNull(oneof); final Collection<Item> items = oneof.getChildNodes(Item.class); Assert.assertEquals(3, items.size()); final Iterator<Item> iterator = items.iterator(); final Item item1 = iterator.next(); Assert.assertEquals(option1.getTextContent(), item1.getTextContent()); final Item item2 = iterator.next(); Assert.assertEquals(option2.getTextContent(), item2.getTextContent()); final Item item3 = iterator.next(); Assert.assertEquals(option3.getTextContent(), item3.getTextContent()); }
@Test public void testCreateVoiceGrammarNoOptions() throws Exception { final Collection<Option> options = new java.util.ArrayList<Option>(); final OptionConverter converter = new SrgsXmlOptionConverter(); final Grammar grammar = converter.createVoiceGrammar(options, Locale.getDefault()); Assert.assertNull(grammar); }