SrgsXmlGrammarIdentifier implements GrammarIdentifier { @Override public GrammarType identify(final GrammarDocument grammar) { if (grammar == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("grammar is null or empty"); } return null; } final byte[] buffer = grammar.getBuffer(); if (buffer == null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("grammar buffer is null or empty"); } return null; } try { final ByteArrayInputStream stream = new ByteArrayInputStream(buffer); final InputSource input = new InputSource(stream); final SrgsXmlDocument srgs = new SrgsXmlDocument(input); final Grammar gr = srgs.getGrammar(); if (gr == null) { return null; } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Found a SRGS XML grammar header."); } if (!isVersionCompliant(gr)) { return null; } if (!isModeCompliant(gr)) { return null; } } catch (ParserConfigurationException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); } return null; } catch (SAXException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); } return null; } catch (IOException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(e.getMessage(), e); } return null; } return GrammarType.SRGS_XML; }  @Override GrammarType identify(final GrammarDocument grammar); @Override GrammarType getSupportedType();  }
@Test public void test1() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE21 + "1/1_grammar.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void test2a() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE21 + "2/2_grammar_a.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void test2b() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE21 + "2/2_grammar_b.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void test3() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE21 + "3/3_grammar_a.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void test5() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE21 + "5/first.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void test7() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE21 + "7/7.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void test338() throws Exception { GrammarDocument doc = GrammarUtil.getGrammarFromResource(BASE20 + "338/338Grammar.grxml"); final GrammarType type = identifier.identify(doc); Assert.assertNull(type); }
@Test public void testValidHeader() { final GrammarDocument doc = GrammarUtil.getGrammarFromString( "<?xml version=\"1.0\" ?>" + "<grammar version=\"1.0\" " + "xmlns=\"http: + "></grammar>"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void testValidVoicemodeHeader() { final GrammarDocument doc = GrammarUtil.getGrammarFromString( "<?xml version=\"1.0\" ?>" + "<grammar version=\"1.0\" " + "xmlns=\"http: + "mode=\"voice\" xml:lang=\"en-US\"></grammar>"); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void testValidDoctypeHeader() { final StringBuilder builder = new StringBuilder(); builder.append("<?xml version=\"1.0\"?>"); builder.append("<!DOCTYPE grammar PUBLIC "); builder.append("\"- builder.append("\"http: builder.append("<grammar version=\"1.0\" "); builder.append("xmlns=\"http: builder.append("mode=\"voice\" xml:lang=\"de-DE\"/>"); final GrammarDocument doc = GrammarUtil.getGrammarFromString(builder.toString()); final GrammarType type = identifier.identify(doc); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void testInvalidHeader() { final StringBuilder builder = new StringBuilder(); builder.append("<?xml version=\"1.0\" ?>"); builder.append("<grammar/>"); final GrammarDocument doc = GrammarUtil.getGrammarFromString(builder.toString()); final GrammarType type = identifier.identify(doc); Assert.assertNull(type); }
@Test public void testInvalidVoiceModeHeader() { final StringBuilder builder = new StringBuilder(); builder.append("<?xml version=\"1.0\" ?>"); builder.append("<grammar version=\"1.0\" "); builder.append(" xmlns=\"http: builder.append(" mode=\"voice\"></grammar>"); final GrammarDocument doc = GrammarUtil.getGrammarFromString(builder.toString()); final GrammarType type = identifier.identify(doc); Assert.assertNull(type); }