Пример #1
0
        /// <summary>
        /// Requests that the document auto-detect its syntax
        /// </summary>
        public void AutoDetectSyntax()
        {
            if (this._filename != null && !this._filename.Equals(String.Empty))
            {
                try
                {
                    //Try filename based syntax detection
                    MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._filename)).FirstOrDefault();
                    if (def != null)
                    {
                        this.Syntax = def.SyntaxName.GetSyntaxName();
                        return;
                    }
                }
                catch (RdfParserSelectionException)
                {
                    //Ignore and use string based detection instead
                }
            }

            //Otherwise try and use string based detection
            //First take a guess at it being a SPARQL Results format
            String text = this.Text;

            try
            {
                ISparqlResultsReader resultsReader = StringParser.GetResultSetParser(text);
                this.Syntax = resultsReader.GetSyntaxName();
            }
            catch (RdfParserSelectionException)
            {
                //Then see whether it may be a SPARQL query
                if (text.Contains("SELECT") || text.Contains("CONSTRUCT") || text.Contains("DESCRIBE") || text.Contains("ASK"))
                {
                    //Likely a SPARQL Query
                    this.Syntax = "SparqlQuery11";
                }
                else
                {
                    //Then take a guess at it being a RDF format
                    try
                    {
                        IRdfReader rdfReader = StringParser.GetParser(text);
                        this.Syntax = rdfReader.GetSyntaxName();
                    }
                    catch (RdfParserSelectionException)
                    {
                        //Finally take a guess at it being a RDF Dataset format
                        IStoreReader datasetReader = StringParser.GetDatasetParser(text);
                        this.Syntax = datasetReader.GetSyntaxName();
                    }
                }
            }
        }