public static void Load(this IStoreReader reader, ITripleStore store, string filename) { using (var input = new StreamReader(filename)) { reader.Load(store, input); } }
public static void Load(this IStoreReader reader, IRdfHandler handler, string filename) { using (var input = new StreamReader(filename)) { reader.Load(handler, input); } }
/// <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> /// Parses a raw RDF Dataset String using the given Parser /// </summary> /// <param name="store">Store to load into</param> /// <param name="data">Raw RDF Dataset String</param> /// <param name="reader">Parser to use</param> public static void ParseDataset(ITripleStore store, String data, IStoreReader reader) { if (store == null) { throw new RdfParseException("Cannot read a RDF dataset into a null Graph"); } if (data == null) { return; } if (reader == null) { //If no parser supplied then should auto-detect syntax ParseDataset(store, data); } else { try { reader.Load(store, new StringReader(data)); } catch { throw; } } }
/// <summary>Creates a new instance of the file triple store.</summary> /// <param name="fileStream">Stream to read/write.</param> /// <param name="storeReader">Store reader used to read the file.</param> /// <param name="storeWriter">Store writer to write the file.</param> public FileTripleStore(Stream fileStream, IStoreReader storeReader, IStoreWriter storeWriter) { _fileStream = fileStream; _storeReader = storeReader; _storeWriter = storeWriter; Read(); }
public IAsyncResult BeginLoad(String sourceFileName, TextReader sourceReader, AsyncCallback callback, object state) { _filename = sourceFileName; IStoreReader reader = MimeTypesHelper.GetStoreParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(sourceFileName)); return(_loadDelegate.BeginInvoke(sourceReader, reader, callback, state)); }
public JsonCapacityRepository(IStoreWriter <DateTime> dateWriter, IStoreReader <DateTime> dateReader, IEnumerable <IQuickening> quickenings) { this.writer = dateWriter; this.reader = dateReader; this.quickenings = quickenings; this.serializer = new JsonSerializer(); }
/// <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> /// 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)) { ThrowNotFoundException(filename); } if (parser == null) { String ext = MimeTypesHelper.GetTrueFileExtension(filename); try { parser = MimeTypesHelper.GetStoreParserByFileExtension(ext); } catch (RdfParserSelectionException) { // If error then we couldn't determine MIME Type from the File Extension RaiseWarning("Unable to select a dataset parser by determining MIME Type from the File Extension"); // Try selecting a RDF parser instead try { IRdfReader rdfParser = MimeTypesHelper.GetParserByFileExtension(ext); Graph g = new Graph(); rdfParser.Load(handler, filename); return; } catch (RdfParserSelectionException) { // Ignore this, will try and use format guessing and assume is a dataset format } } } 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(File.OpenRead(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 StringReader(data)); } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, filename); } }
public UserController(IStoreReader storeReader, IUserView userView, IUsersView usersView) { _userView = userView; _usersView = usersView; _storeReader = storeReader; this.Initialize(); }
/// <summary> /// Loads a RDF Dataset from an Embedded Resource. /// </summary> /// <param name="handler">RDF Handler to use.</param> /// <param name="resource">Assembly Qualified Name of the Resource to load.</param> /// <param name="parser">Parser to use (leave null for auto-selection).</param> public static void Load(IRdfHandler handler, String resource, IStoreReader parser) { if (resource == null) { throw new RdfParseException("Cannot read a RDF Dataset from a null Resource"); } if (handler == null) { throw new RdfParseException("Cannot read a RDF Dataset using a null Handler"); } try { String resourceName = resource; if (resource.Contains(',')) { // Resource is an external assembly String assemblyName = resource.Substring(resource.IndexOf(',') + 1).TrimStart(); resourceName = resourceName.Substring(0, resource.IndexOf(',')).TrimEnd(); // Try to load this assembly #if NETCORE Assembly asm = assemblyName.Equals(_currAsmName) ? typeof(EmbeddedResourceLoader).GetTypeInfo().Assembly : Assembly.Load(new AssemblyName(assemblyName)); #else Assembly asm = (assemblyName.Equals(_currAsmName) ? Assembly.GetExecutingAssembly() : Assembly.Load(assemblyName)) as Assembly; #endif if (asm != null) { // Resource is in the loaded assembly LoadDatasetInternal(handler, asm, resourceName, parser); } else { throw new RdfParseException("The Embedded Resource '" + resourceName + "' cannot be loaded as the required assembly '" + assemblyName + "' could not be loaded. Please ensure that the assembly name is correct and that is is referenced/accessible in your application."); } } else { // Resource is in dotNetRDF #if NETCORE LoadDatasetInternal(handler, typeof(EmbeddedResourceLoader).GetTypeInfo().Assembly, resourceName, parser); #else LoadDatasetInternal(handler, Assembly.GetExecutingAssembly(), resourceName, parser); #endif } } catch (RdfParseException) { throw; } catch (Exception ex) { throw new RdfParseException("Unable to load the Embedded Resource '" + resource + "' as an unexpected error occurred", ex); } }
/// <summary> /// Loads a RDF Dataset from an Embedded Resource. /// </summary> /// <param name="store">Store to load into.</param> /// <param name="resource">Assembly Qualified Name of the Resource to load.</param> /// <param name="parser">Parser to use (leave null for auto-selection).</param> public static void Load(ITripleStore store, String resource, IStoreReader parser) { if (store == null) { throw new RdfParseException("Cannot read RDF Dataset into a null Store"); } Load(new StoreHandler(store), resource, parser); }
/// <summary> /// Creates a new GZipped input parser /// </summary> /// <param name="parser">The underlying parser to use</param> public BaseGZipDatasetParser(IStoreReader parser) { if (parser == null) { throw new ArgumentNullException("parser"); } this._parser = parser; this._parser.Warning += this.RaiseWarning; }
/// <summary>Loads data from string with optional automated graph generation.</summary> /// <param name="store">Target store to be loaded with data.</param> /// <param name="data">String with data.</param> /// <param name="parser">Store reader.</param> /// <param name="metaGraphUri">When provided, store will have automatically created graphs for all resources that are mentioned in the meta graph provided.</param> public static void LoadFromString(this ITripleStore store, string data, IStoreReader parser, Uri metaGraphUri) { ITripleStore targetStore = (metaGraphUri != null ? new TripleStore() : store); targetStore.LoadFromString(data, parser); if (metaGraphUri != null) { store.ExpandGraphs((TripleStore)targetStore, metaGraphUri); } }
public MemoryCacheStore( MemoryCache memoryCache, IConfigurationProvider provider, IStoreReader storeReader, ILogger logger) { memCache = memoryCache; this.configurationProvider = provider; this.storeReader = storeReader; this.logger = logger; }
/// <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(); } } } }
/// <summary>Creates a new instance of the file triple store.</summary> /// <param name="filePath">Path of the file to read/write.</param> /// <param name="storeReader">Store reader used to read the file.</param> /// <param name="storeWriter">Store writer to write the file.</param> public FileTripleStore(string filePath, IStoreReader storeReader, IStoreWriter storeWriter) { if (!File.Exists(_filePath = filePath)) { File.Create(filePath).Close(); } _storeReader = storeReader; _storeWriter = storeWriter; Read(); }
/// <summary> /// Attempts to load a RDF dataset from the given URI into the given Triple Store /// </summary> /// <param name="store">Triple Store to load into</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(ITripleStore store, Uri u, IStoreReader parser) { if (store == null) { throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store"); } if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } UriLoader.Load(new StoreHandler(store), u, parser); }
/// <summary> /// Attempts to load a RDF dataset asynchronously from the given URI into the given Triple Store. /// </summary> /// <param name="store">Triple Store to load into.</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(ITripleStore store, Uri u, IStoreReader parser, TripleStoreCallback callback, Object state) { if (store == null) { throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store"); } if (u == null) { throw new RdfParseException("Cannot read a RDF dataset from a null URI"); } Load(new StoreHandler(store), u, parser, (_, s) => callback(store, s), state); }
/// <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)); } }
public JsonCmdApplicationMetaRepository(IStoreReader <CmdApplicationMeta> storeReader) { if (storeReader == null) { throw new ArgumentNullException(nameof(storeReader)); } this.StoreReader = storeReader; this.serializer = new JsonSerializer(); this.serializer.Converters.Add(new NameJsonConverter()); this.serializer.TypeNameHandling = TypeNameHandling.Auto; }
private void TestEmptyDatasetParsing(IStoreReader reader) { if (!File.Exists("empty.test")) { FileStream temp = File.Create("empty.test"); temp.Close(); } TripleStore store = new TripleStore(); reader.Load(store, new VDS.RDF.Storage.Params.StreamParams("empty.test")); Assert.AreEqual(0, store.Graphs.Count, "Store should have no Graphs"); }
/// <summary>Creates a new instance of the file triple store.</summary> /// <param name="filePath">Path of the file to read/write.</param> /// <param name="storeReader">Store reader used to read the file.</param> /// <param name="storeWriter">Store writer to write the file.</param> public FileTripleStore(string filePath, IStoreReader storeReader, IStoreWriter storeWriter) { if (!File.Exists(_filePath = EnsureAbsolute(filePath))) { File.Create(_filePath).Close(); } _watcher = CreateFileHooks(_filePath); _storeReader = storeReader; _storeWriter = storeWriter; Read(); }
/// <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.GetStoreParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename)); reader.Load(this._store, filename); this._ready = true; } catch (RdfException rdfEx) { throw new RdfStorageException("An Error occurred while trying to read the Dataset File", rdfEx); } }
private void TestEmptyDatasetParsing(IStoreReader reader) { if (!File.Exists("empty.test")) { FileStream temp = File.Create("empty.test"); temp.Close(); } TripleStore store = new TripleStore(); reader.Load(store, "empty.test"); Assert.Equal(0, store.Graphs.Count); }
// Make it a requirement // Constructor public MessageStore(IStoreWriter writer, IStoreReader reader, IFileLocator fileLocator) { if (writer == null) throw new ArgumentNullException("Writer"); if (reader == null) throw new ArgumentNullException("Reader"); if (fileLocator == null) throw new ArgumentNullException("FileLocator"); this.fileLocator = fileLocator; this.writer = writer; this.reader = reader; }
public DynamicFileStore(string baseDirectory, int cacheThreshold, int splitThreshold = 2048) { _cacheThreshold = cacheThreshold; _splitThreshold = splitThreshold; _cache = new Dictionary <string, List <string> >(_cacheThreshold); _baseDirectory = new DirectoryInfo(baseDirectory); if (!_baseDirectory.Exists) { Log.LogError("Could not find dynamic file store base directory at {0}", baseDirectory); throw new FileNotFoundException($"Could not find directory at {baseDirectory}"); } _directoryMap = BuildDirectoryMap(_baseDirectory); _lineFormat = new NQuads11Formatter(); _lineReader = new VDS.RDF.Parsing.NQuadsParser(NQuadsSyntax.Rdf11); _hasher = MD5.Create(); }
public JsonCmdApplicationConfigurationRepository( IStoreWriter<CmdApplicationConfiguration> fileStoreWriter, IStoreReader<CmdApplicationConfiguration> fileStoreReader) { if (fileStoreWriter == null) throw new ArgumentNullException(nameof(fileStoreWriter)); if (fileStoreReader == null) throw new ArgumentNullException(nameof(fileStoreReader)); this.FileStoreWriter = fileStoreWriter; this.FileStoreReader = fileStoreReader; this.serializer = new JsonSerializer(); this.serializer.Converters.Add(new NameJsonConverter()); this.serializer.TypeNameHandling = TypeNameHandling.Auto; }
private void CreateIOHandlers(string extension) { switch (extension) { case ".nq": _storeReader = new NQuadsParser(); _storeWriter = new NQuadsWriter(); break; case ".ttl": _rdfReader = new TurtleParser(); _rdfWriter = new CompressingTurtleWriter(); break; case ".trig": _storeReader = new TriGParser(); _storeWriter = new TriGWriter() { CompressionLevel = -1 }; break; case ".xml": _rdfReader = new RdfXmlParser(); _rdfWriter = new RdfXmlWriter(); break; case ".n3": _rdfReader = new Notation3Parser(); _rdfWriter = new Notation3Writer(); break; case ".trix": _storeReader = new TriXParser(); _storeWriter = new TriXWriter(); break; case ".json": _rdfReader = new RdfJsonParser(); _rdfWriter = new RdfJsonWriter(); break; default: throw new ArgumentOutOfRangeException(System.String.Format("Provided file path does not allow to detect a type of the RDF serialization type.")); } }
public void ParsingGZipDatasetByGZipStreamManual() { foreach (String filename in this._manualDatasetTestFiles) { TripleStore store = new TripleStore(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdfDatasets && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); IStoreReader reader = def.GetRdfDatasetParser(); reader.Load(store, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.Equal(this._g, store.Graphs.First()); } }
public MessageStore(IFileLocator fileLocator, IStoreWriter writer, IStoreReader reader) { if (fileLocator == null) { throw new ArgumentNullException("fileLocator"); } if (writer == null) { throw new ArgumentNullException("writer"); } if (reader == null) { throw new ArgumentNullException("reader"); } this.fileLocator = fileLocator; this.writer = writer; this.reader = reader; }
public BaseDatasetParserSuite(IStoreReader testParser, IStoreReader resultsParser, String baseDir) { if (testParser == null) { throw new ArgumentNullException("testParser"); } if (resultsParser == null) { throw new ArgumentNullException("resultsParser"); } if (baseDir == null) { throw new ArgumentNullException("baseDir"); } this._parser = testParser; this._resultsParser = resultsParser; this._baseDir = baseDir; }
private void TestWriter(IStoreWriter writer, IStoreReader reader, bool useMultiThreaded, int compressionLevel) { TripleStore store = new TripleStore(); Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); g.BaseUri = null; store.Add(g); g = new Graph(); g.LoadFromFile("resources\\InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/graph"); store.Add(g); g = new Graph(); g.LoadFromFile(@"resources\cyrillic.rdf"); g.BaseUri = new Uri("http://example.org/cyrillic"); store.Add(g); if (writer is ICompressingWriter) { ((ICompressingWriter)writer).CompressionLevel = compressionLevel; } #if !NETCOREAPP2_0 if (writer is IMultiThreadedWriter) { ((IMultiThreadedWriter)writer).UseMultiThreadedWriting = useMultiThreaded; } #endif System.IO.StringWriter strWriter = new System.IO.StringWriter(); writer.Save(store, strWriter); Console.WriteLine(strWriter.ToString()); Assert.NotEqual(strWriter.ToString(), String.Empty); TripleStore store2 = new TripleStore(); reader.Load(store2, new System.IO.StringReader(strWriter.ToString())); foreach (IGraph graph in store.Graphs) { Assert.True(store2.HasGraph(graph.BaseUri), "Parsed Stored should have contained serialized graph"); Assert.Equal(graph, store2[graph.BaseUri]); } }
/// <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> /// Registers a parser as the default RDF Dataset Parser for all the given MIME types and updates relevant definitions to include the MIME types and file extensions /// </summary> /// <param name="parser">RDF Dataset Parser</param> /// <param name="mimeTypes">MIME Types</param> /// <param name="fileExtensions">File Extensions</param> public static void RegisterParser(IStoreReader parser, IEnumerable<String> mimeTypes, IEnumerable<String> fileExtensions) { if (!_init) Init(); if (!mimeTypes.Any()) throw new RdfException("Cannot register a parser without specifying at least 1 MIME Type"); //Get any existing defintions that are to be altered IEnumerable<MimeTypeDefinition> existing = GetDefinitions(mimeTypes); foreach (MimeTypeDefinition def in existing) { foreach (String type in mimeTypes) { def.AddMimeType(type); } foreach (String ext in fileExtensions) { def.AddFileExtension(ext); } def.RdfDatasetParserType = parser.GetType(); } //Create any new defintions IEnumerable<String> newTypes = mimeTypes.Where(t => !GetDefinitions(t).Any()); if (newTypes.Any()) { MimeTypeDefinition newDef = new MimeTypeDefinition(String.Empty, newTypes, fileExtensions); newDef.RdfDatasetParserType = parser.GetType(); AddDefinition(newDef); } }
/// <summary> /// Parses a raw RDF Dataset String using the given Parser /// </summary> /// <param name="store">Store to load into</param> /// <param name="data">Raw RDF Dataset String</param> /// <param name="reader">Parser to use</param> public static void ParseDataset(ITripleStore store, String data, IStoreReader reader) { if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Graph"); if (data == null) return; if (reader == null) { //If no parser supplied then should auto-detect syntax ParseDataset(store, data); } else { try { reader.Load(store, new TextReaderParams(new StringReader(data))); } catch { throw; } } }
/// <summary> /// Attempts to load a RDF dataset from the given URI into the given Triple Store /// </summary> /// <param name="store">Triple Store to load into</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(ITripleStore store, Uri u, IStoreReader parser) { if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store"); if (u == null) throw new RdfParseException("Cannot read a RDF dataset from a null URI"); UriLoader.Load(new StoreHandler(store), u, parser); }
/// <summary> /// Internal Helper method which does the actual loading of the Triple Store from the Resource /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="asm">Assembly to get the resource stream from</param> /// <param name="resource">Full name of the Resource (without the Assembly Name)</param> /// <param name="parser">Parser to use (if null will be auto-selected)</param> private static void LoadDatasetInternal(IRdfHandler handler, Assembly asm, String resource, IStoreReader parser) { //Resource is in the given assembly using (Stream s = asm.GetManifestResourceStream(resource)) { if (s == null) { //Resource did not exist in this assembly throw new RdfParseException("The Embedded Resource '" + resource + "' does not exist inside of " + asm.GetName().Name); } else { //Resource exists //Do we have a predefined Parser? if (parser != null) { parser.Load(handler, new StreamParams(s)); } else { //Need to select a Parser or use StringParser String ext = resource.Substring(resource.LastIndexOf(".")); MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(ext)).FirstOrDefault(d => d.CanParseRdfDatasets); if (def != null) { //Resource has an appropriate file extension and we've found a candidate parser for it parser = def.GetRdfDatasetParser(); parser.Load(handler, new StreamParams(s)); } else { //See if the format was actually an RDF graph instead def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(ext)).FirstOrDefault(d => d.CanParseRdf); if (def != null) { IRdfReader rdfParser = def.GetRdfParser(); rdfParser.Load(handler, new StreamReader(s)); } else { //Resource did not have a file extension or we didn't have a parser associated with the extension //Try using StringParser instead String data; using (StreamReader reader = new StreamReader(s)) { data = reader.ReadToEnd(); reader.Close(); } parser = StringParser.GetDatasetParser(data); parser.Load(handler, new TextReaderParams(new StringReader(data))); } } } } } }
/// <summary> /// Creates a new GZipped input parser /// </summary> /// <param name="parser">The underlying parser to use</param> public BaseGZipDatasetParser(IStoreReader parser) { if (parser == null) throw new ArgumentNullException("parser"); this._parser = parser; this._parser.Warning += this.RaiseWarning; }
/// <summary> /// Loads a RDF Dataset from an Embedded Resource /// </summary> /// <param name="store">Store to load into</param> /// <param name="resource">Assembly Qualified Name of the Resource to load</param> /// <param name="parser">Parser to use (leave null for auto-selection)</param> public static void Load(ITripleStore store, String resource, IStoreReader parser) { if (store == null) throw new RdfParseException("Cannot read RDF Dataset into a null Store"); EmbeddedResourceLoader.Load(new StoreHandler(store), resource, parser); }
/// <summary> /// Loads a RDF Dataset from an Embedded Resource /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="resource">Assembly Qualified Name of the Resource to load</param> /// <param name="parser">Parser to use (leave null for auto-selection)</param> public static void Load(IRdfHandler handler, String resource, IStoreReader parser) { if (resource == null) throw new RdfParseException("Cannot read a RDF Dataset from a null Resource"); if (handler == null) throw new RdfParseException("Cannot read a RDF Dataset using a null Handler"); try { String resourceName = resource; if (resource.Contains(',')) { //Resource is an external assembly String assemblyName = resource.Substring(resource.IndexOf(',') + 1).TrimStart(); resourceName = resourceName.Substring(0, resource.IndexOf(',')).TrimEnd(); //Try to load this assembly Assembly asm = (assemblyName.Equals(_currAsmName) ? Assembly.GetExecutingAssembly() : Assembly.Load(assemblyName)) as Assembly; if (asm != null) { //Resource is in the loaded assembly EmbeddedResourceLoader.LoadDatasetInternal(handler, asm, resourceName, parser); } else { throw new RdfParseException("The Embedded Resource '" + resourceName + "' cannot be loaded as the required assembly '" + assemblyName + "' could not be loaded. Please ensure that the assembly name is correct and that is is referenced/accessible in your application."); } } else { //Resource is in dotNetRDF EmbeddedResourceLoader.LoadDatasetInternal(handler, Assembly.GetExecutingAssembly(), resourceName, parser); } } catch (RdfParseException) { throw; } catch (Exception ex) { throw new RdfParseException("Unable to load the Embedded Resource '" + resource + "' as an unexpected error occurred", ex); } }
/// <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> /// </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 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); } //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 !SILVERLIGHT request.Timeout = Options.UriLoaderTimeout; #endif if (_userAgent != null && !_userAgent.Equals(String.Empty)) { request.UserAgent = _userAgent; } #if DEBUG //HTTP Debugging 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 //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 StreamParams(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 TextReaderParams(new StringReader(data))); } } } else { parser.Warning += RaiseStoreWarning; parser.Load(handler, new StreamParams(response.GetResponseStream())); } //Finally can invoke the callback callback(handler, state); } }, null); } 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 into the given Triple Store /// </summary> /// <param name="store">Triple Store to load into</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> /// </remarks> public static void Load(ITripleStore store, Uri u, IStoreReader parser, TripleStoreCallback callback, Object state) { if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Triple Store"); if (u == null) throw new RdfParseException("Cannot read a RDF dataset from a null URI"); UriLoader.Load(new StoreHandler(store), u, parser, (_, s) => callback(store, s), state); }
/// <summary> /// Creates a Custom HTTP Accept Header containing only the Accept Types supported by a specific parser /// </summary> /// <param name="parser">RDF Parser</param> /// <returns></returns> public static String CustomHttpAcceptHeader(IStoreReader parser) { if (!_init) Init(); Type requiredType = parser.GetType(); foreach (MimeTypeDefinition definition in MimeTypesHelper.Definitions) { if (requiredType.Equals(definition.RdfDatasetParserType)) { return String.Join(",", definition.MimeTypes.ToArray()); } } return MimeTypesHelper.HttpRdfDatasetAcceptHeader; }
public StoreLogger(IStoreWriter writer, IStoreReader reader) { this.writer = writer; this.reader = reader; }
public StoreCache(IStoreWriter writer, IStoreReader reader) { this.cache = new ConcurrentDictionary<int, Maybe<string>>(); this.writer = writer; this.reader = reader; }
/// <summary> /// Loads the contents of the given File into a Triple Store providing the RDF dataset format can be determined /// </summary> /// <param name="store">Triple Store to load into</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(ITripleStore store, String filename, IStoreReader parser) { if (store == null) throw new RdfParseException("Cannot read a RDF Dataset into a null Store"); FileLoader.Load(new StoreHandler(store), filename, parser); }
/// <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 the File '" + filename + "' since it doesn't exist", filename); #endif } if (parser == null) { try { parser = MimeTypesHelper.GetStoreParser(MimeTypesHelper.GetMimeType(MimeTypesHelper.GetTrueFileExtension(filename))); } catch (RdfParserSelectionException) { //If error then we couldn't determine MIME Type from the File Extension RaiseWarning("Unable to select a dataset parser by determining MIME Type from the File Extension"); //Try selecting a RDF parser instead try { IRdfReader rdfParser = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeType(MimeTypesHelper.GetTrueFileExtension(filename))); Graph g = new Graph(); rdfParser.Load(handler, filename); return; } catch (RdfParserSelectionException) { //Ignore this, will try and use format guessing and assume is a dataset format } } } 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> /// Creates a new RDF Dataset Syntax Validator /// </summary> /// <param name="parser">Dataset Parser</param> public RdfDatasetSyntaxValidator(IStoreReader parser) { this._parser = parser; }
private void CreateIOHandlers(string extension) { switch (extension) { case ".nq": _storeReader = new NQuadsParser(); _storeWriter = new NQuadsWriter(); break; case ".ttl": _rdfReader = new TurtleParser(); _rdfWriter = new CompressingTurtleWriter(); break; case ".trig": _storeReader = new TriGParser(); _storeWriter = new TriGWriter(); break; case ".xml": _rdfReader = new RdfXmlParser(); _rdfWriter = new RdfXmlWriter(); break; case ".n3": _rdfReader = new Notation3Parser(); _rdfWriter = new Notation3Writer(); break; case ".trix": _storeReader = new TriXParser(); _storeWriter = new TriXWriter(); break; case ".json": _rdfReader = new RdfJsonParser(); _rdfWriter = new RdfJsonWriter(); break; default: throw new ArgumentOutOfRangeException(System.String.Format("Provided file path does not allow to detect a type of the RDF serialization type.")); } }