class SrgsXmlGrammarIdentifier implements GrammarIdentifier {

    @Override
    public GrammarType identify(final GrammarDocument grammar) {
        /* make sure grammar is neither null nor empty */
        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);

            /* no exception, this must be an XML element */
            /* Lets test, if it is srgs+xml */
            final Grammar gr = srgs.getGrammar();
            if (gr == null) {
                return null;
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Found a SRGS XML grammar header.");
            }

            /* Is there a standard compliant version attribute? */
            if (!isVersionCompliant(gr)) {
                return null;
            }

            /* Is there a standard compliant mode attribute? */
            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;
    }

}