GrammarIdentifierCentral { public GrammarType identifyGrammar(final GrammarDocument grammar, final GrammarType expectedType) throws UnsupportedFormatError { if (grammar == null) { throw new UnsupportedFormatError("Cannot identify a null grammar!"); } if (identifier.isEmpty()) { LOGGER.warn("no registered identifier!"); return null; } final GrammarIdentifier expectedIdentifier = getIdentifierByType(expectedType); if (expectedIdentifier != null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("trying to identify grammar with '" + expectedIdentifier.getClass() + "'"); } final GrammarType type = expectedIdentifier.identify(grammar); if (type != null) { return type; } } for (GrammarIdentifier current : identifier) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("trying to identify grammar with '" + current.getClass() + "'"); } if (current.getSupportedType() != expectedType) { final GrammarType currentType = current.identify(grammar); if (currentType != null) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("identified grammar with type '" + currentType + "'"); } return currentType; } } } LOGGER.warn("unable to identify grammar"); return null; } GrammarIdentifierCentral(); GrammarType identifyGrammar(final GrammarDocument grammar,
            final GrammarType expectedType); boolean typeSupported(final GrammarType type); void setIdentifier(final List<GrammarIdentifier> grammarIdenifier); void addIdentifier(final GrammarIdentifier id);  }
@Test public void testIdentifyInlineGrammar() throws UnsupportedFormatError { final String grammar = "<grammar type=\"application/srgs+xml\" " + "root=\"r2\" version=\"1.0\"></grammar>"; final GrammarDocument doc = getGrammarFromString(grammar); final GrammarType type = central.identifyGrammar(doc, GrammarType.SRGS_XML); Assert.assertEquals(GrammarType.SRGS_XML, type); }
@Test public void testIdentifyExternalGrammar() throws UnsupportedFormatError { final String grammar = "<grammar type=\"application/srgs+xml\" " + "root=\"r2\" src=\"grammar.grxml\" version=\"1.0\"></grammar>"; final GrammarDocument doc = getGrammarFromString(grammar); final GrammarType type = central.identifyGrammar(doc, GrammarType.SRGS_XML); Assert.assertEquals(GrammarType.SRGS_XML, type); }