/// <summary> /// Extracts an RDF Dataset which details the Constraints violated (if any) and whether Constraints are satisified. /// </summary> /// <returns></returns> public ITripleStore Validate() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint.Uri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes, MimeTypesHelper.SupportedRdfDatasetMimeTypes); Tools.HttpDebugRequest(request); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); IStoreReader parser = MimeTypesHelper.GetStoreParser(response.ContentType); TripleStore store = new TripleStore(); parser.Load(store, new StreamReader(response.GetResponseStream())); response.Close(); return(store); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } throw new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server", webEx); } }
/// <summary> /// Extracts an RDF Dataset which details the Constraints violated (if any) and whether Constraints are satisified /// </summary> /// <param name="callback">Callback to invoke when the operation completes</param> /// <param name="state">State to pass to the callback</param> public void Validate(TripleStoreCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Endpoint.Uri); request.Method = this.Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(this.MimeTypes, MimeTypesHelper.SupportedRdfDatasetMimeTypes); #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugRequest(request); } #endif request.BeginGetResponse(result => { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { #if DEBUG if (Options.HttpDebugging) { Tools.HttpDebugResponse(response); } #endif IStoreReader parser = MimeTypesHelper.GetStoreParser(response.ContentType); TripleStore store = new TripleStore(); StreamParams parameters = new StreamParams(response.GetResponseStream()); parser.Load(store, parameters); response.Close(); callback(store, state); } }, null); }
/// <summary> /// Extracts an RDF Dataset which details the Constraints violated (if any) and whether Constraints are satisified. /// </summary> /// <param name="callback">Callback to invoke when the operation completes.</param> /// <param name="state">State to pass to the callback.</param> /// <remarks> /// If the operation succeeds the callback will be invoked normally, if there is an error the callback will be invoked with a instance of <see cref="AsyncError"/> passed as the state which provides access to the error message and the original state passed in. /// </remarks> public void Validate(TripleStoreCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint.Uri); request.Method = Endpoint.HttpMethods.First(); request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(MimeTypes, MimeTypesHelper.SupportedRdfDatasetMimeTypes); Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); IStoreReader parser = MimeTypesHelper.GetStoreParser(response.ContentType); TripleStore store = new TripleStore(); parser.Load(store, new StreamReader(response.GetResponseStream())); response.Close(); callback(store, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state)); } }
/// <summary> /// Loads the contents of the given File using a RDF Handler providing the RDF dataset format can be determined /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="filename">File to load from</param> /// <param name="parser">Parser to use to parse the given file</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then the <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file. This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports. If this suceeds then a parser is chosen and used to parse the input file. /// </para> /// </remarks> public static void Load(IRdfHandler handler, String filename, IStoreReader parser) { if (handler == null) { throw new RdfParseException("Cannot read a RDF Dataset using a null RDF Handler"); } if (filename == null) { throw new RdfParseException("Cannot read a RDF Dataset from a null File"); } if (!File.Exists(filename)) { #if SILVERLIGHT throw new FileNotFoundException("Cannot read a RDF Dataset from the File '" + filename + "' since it doesn't exist"); #else throw new FileNotFoundException("Cannot read a RDF Dataset from a File that doesn't exist", filename); #endif } if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(MimeTypesHelper.GetMimeType(Path.GetExtension(filename))); } catch (RdfParserSelectionException) { //If error then we couldn't determine MIME Type from the File Extension RaiseWarning("Unable to select a parser by determining MIME Type from the File Extension"); } } if (parser == null) { //Unable to determine format from File Extension //Read file in locally and use the StringParser to select a parser StreamReader reader = new StreamReader(filename); String data = reader.ReadToEnd(); reader.Close(); parser = StringParser.GetDatasetParser(data); RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name); parser.Warning += RaiseStoreWarning; parser.Load(handler, new TextReaderParams(new StringReader(data))); } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(filename)); } }
/// <summary> /// Internal helper method for loading the data /// </summary> /// <param name="filename">File to load from</param> private void Initialise(String filename) { try { IStoreReader reader = MimeTypesHelper.GetStoreParser(MimeTypesHelper.GetMimeType(Path.GetExtension(filename))); reader.Load(this._store, new StreamParams(filename)); this._ready = true; } catch (RdfException rdfEx) { throw new RdfStorageException("An Error occurred while trying to read the Dataset File", rdfEx); } }
/// <summary> /// Attempts to load a RDF dataset from the given URI using a RDF Handler /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="u">URI to attempt to get a RDF dataset from</param> /// <param name="parser">Parser to use to parse the RDF dataset</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </para> /// <para> /// If you know ahead of time the Content Type you can explicitly pass in the parser to use. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IStoreReader parser) { if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } if (handler == null) { throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler"); } try { #if SILVERLIGHT if (u.IsFile()) #else if (u.IsFile) #endif { //Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.ToString().Substring(7), parser); } else { FileLoader.Load(handler, u.ToString().Substring(8), parser); } return; } //Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); //Set-up the Request HttpWebRequest httpRequest; httpRequest = (HttpWebRequest)WebRequest.Create(u); //Want to ask for TriG, NQuads or TriX if (parser != null) { //If a non-null parser set up a HTTP Header that is just for the given parser httpRequest.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { httpRequest.Accept = MimeTypesHelper.HttpRdfDatasetAcceptHeader; } //Use HTTP GET httpRequest.Method = "GET"; #if !SILVERLIGHT httpRequest.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { httpRequest.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugRequest(httpRequest); } #endif using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { #if DEBUG //HTTP Debugging if (Options.HttpDebugging) { Tools.HttpDebugResponse(httpResponse); } #endif //Get a Parser and Load the RDF if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(httpResponse.ContentType); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(httpResponse.GetResponseStream())); } catch (RdfParserSelectionException selEx) { String data = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd(); parser = StringParser.GetDatasetParser(data); parser.Warning += RaiseStoreWarning; parser.Load(handler, new TextReaderParams(new StringReader(data))); } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(httpResponse.GetResponseStream())); } } } catch (UriFormatException uriEx) { //Uri Format Invalid throw new RdfException("Unable to load from the given URI '" + u.ToString() + "' since it's format was invalid, see inner exception for details", uriEx); } catch (WebException webEx) { #if DEBUG if (Options.HttpDebugging) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } } #endif //Some sort of HTTP Error occurred throw new WebException("A HTTP Error occurred resolving the URI '" + u.ToString() + "', see innner exception for details", webEx); } }
/// <summary> /// Attempts to load a RDF dataset asynchronously from the given URI using a RDF Handler. /// </summary> /// <param name="handler">RDF Handler to use.</param> /// <param name="u">URI to attempt to get a RDF dataset from.</param> /// <param name="parser">Parser to use to parse the RDF dataset.</param> /// <param name="callback">Callback to invoke when the operation completes.</param> /// <param name="state">State to pass to the callback.</param> /// <remarks> /// <para> /// If the <paramref name="parser"/> parameter is set to null then this method attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response. /// </para> /// <para> /// If you know ahead of time the Content Type you can explicitly pass in the parser to use. /// </para> /// <para> /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state. /// </para> /// </remarks> public static void Load(IRdfHandler handler, Uri u, IStoreReader parser, RdfHandlerCallback callback, Object state) { if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } if (handler == null) { throw new RdfParseException("Cannot read a RDF dataset using a null RDF handler"); } try { if (u.IsFile) { // Invoke FileLoader instead RaiseWarning("This is a file: URI so invoking the FileLoader instead"); if (Path.DirectorySeparatorChar == '/') { FileLoader.Load(handler, u.AbsoluteUri.Substring(7), parser); } else { FileLoader.Load(handler, u.AbsoluteUri.Substring(8), parser); } // FileLoader.Load() will run synchronously so once this completes we can invoke the callback callback(handler, state); return; } if (u.Scheme.Equals("data")) { // Invoke DataUriLoader instead RaiseWarning("This is a data: URI so invoking the DataUriLoader instead"); DataUriLoader.Load(handler, u); // After DataUriLoader.Load() has run (which happens synchronously) can invoke the callback callback(handler, state); return; } // Sanitise the URI to remove any Fragment ID u = Tools.StripUriFragment(u); // Setup the Request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u); // Want to ask for RDF dataset formats if (parser != null) { // If a non-null parser set up a HTTP Header that is just for the given parser request.Accept = MimeTypesHelper.CustomHttpAcceptHeader(parser); } else { request.Accept = MimeTypesHelper.HttpAcceptHeader; } // Use HTTP GET request.Method = "GET"; #if !NETCORE request.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { #if NETCORE request.Headers[HttpRequestHeader.UserAgent] = _userAgent; #else request.UserAgent = _userAgent; #endif } Tools.HttpDebugRequest(request); try { request.BeginGetResponse(result => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result)) { Tools.HttpDebugResponse(response); // Get a Parser and load the RDF if (parser == null) { try { // Only need to auto-detect the parser if a specific one wasn't specified parser = MimeTypesHelper.GetStoreParser(response.ContentType); parser.Warning += RaiseWarning; parser.Load(handler, new StreamReader(response.GetResponseStream())); } catch (RdfParserSelectionException) { RaiseStoreWarning("Unable to select a RDF Dataset parser based on Content-Type: " + response.ContentType + " - seeing if the content is an RDF Graph instead"); try { // If not a RDF Dataset format see if it is a Graph IRdfReader rdfParser = MimeTypesHelper.GetParser(response.ContentType); rdfParser.Load(handler, new StreamReader(response.GetResponseStream())); } catch (RdfParserSelectionException) { String data = new StreamReader(response.GetResponseStream()).ReadToEnd(); parser = StringParser.GetDatasetParser(data); parser.Warning += RaiseStoreWarning; parser.Load(handler, new StringReader(data)); } } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamReader(response.GetResponseStream())); } // Finally can invoke the callback callback(handler, state); } } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state)); } catch (Exception ex) { callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state)); } }, null); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(handler, new AsyncError(new RdfParseException("A HTTP Error occurred loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exeption for details", webEx), state)); } catch (Exception ex) { callback(handler, new AsyncError(new RdfParseException("Unexpected error while loading the URI '" + u.AbsoluteUri + "' asynchronously, see inner exception for details", ex), state)); } } catch (UriFormatException uriEx) { // Uri Format Invalid throw new RdfException("Unable to load from the given URI '" + u.AbsoluteUri + "' since it's format was invalid, see inner exception for details", uriEx); } }
private void btnImportFile_Click(object sender, EventArgs e) { if (this.txtSourceFile.Text.Equals(string.Empty)) { MessageBox.Show("Please enter a File you wish to import RDF from...", "No File Specified"); } else { try { //Try and get a Graph Parser and load IRdfReader parser = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeType(Path.GetExtension(this.txtSourceFile.Text))); Graph g = new Graph(); FileLoader.Load(g, this.txtSourceFile.Text); this.LogImportSuccess(this.txtSourceFile.Text, 1, g.Triples.Count); //Add to Store try { this._store.Add(g); } catch (Exception ex) { this.LogImportFailure(this.txtSourceFile.Text, ex); MessageBox.Show("An error occurred trying to add the RDF Graph to the Dataset:\n" + ex.Message, "File Import Error"); return; } } catch (RdfParserSelectionException) { try { //Try and get a Store Parser and load IStoreReader storeparser = MimeTypesHelper.GetStoreParser(MimeTypesHelper.GetMimeType(Path.GetExtension(this.txtSourceFile.Text))); int graphsBefore = this._store.Graphs.Count; int triplesBefore = this._store.Graphs.Sum(g => g.Triples.Count); storeparser.Load(this._store, new StreamParams(this.txtSourceFile.Text)); this.LogImportSuccess(this.txtSourceFile.Text, this._store.Graphs.Count - graphsBefore, this._store.Graphs.Sum(g => g.Triples.Count) - triplesBefore); } catch (RdfParserSelectionException selEx) { this.LogImportFailure(this.txtSourceFile.Text, selEx); MessageBox.Show("The given file does not appear to be an RDF Graph/Dataset File Format the tool understands", "File Import Error"); return; } catch (Exception ex) { this.LogImportFailure(this.txtSourceFile.Text, ex); MessageBox.Show("An error occurred trying to read an RDF Dataset from the file:\n" + ex.Message, "File Import Error"); return; } } catch (Exception ex) { this.LogImportFailure(this.txtSourceFile.Text, ex); MessageBox.Show("An error occurred trying to read an RDF Graph from the file:\n" + ex.Message, "File Import Error"); return; } this.stsGraphs.Text = this._store.Graphs.Count + " Graphs"; this.stsTriples.Text = this._store.Graphs.Sum(g => g.Triples.Count) + " Triples"; MessageBox.Show("RDF added to the Dataset OK", "File Import Done"); } }