public void RunVocab(String[] args) { if (args.Length < 2) { Console.Error.WriteLine("rdfWebDeploy: Error: 2 Arguments are required in order to use the -vocab mode"); return; } if (File.Exists(args[1])) { Console.Error.WriteLine("rdfWebDeploy: Error: Cannot output the configuration vocabulary to " + args[1] + " as a file already exists at that location"); return; } TurtleParser ttlparser = new TurtleParser(); StreamReader reader = new StreamReader(Assembly.GetAssembly(typeof(IGraph)).GetManifestResourceStream("VDS.RDF.Configuration.configuration.ttl")); Graph g = new Graph(); ttlparser.Load(g, reader); IRdfWriter writer; try { writer = MimeTypesHelper.GetWriterByFileExtension(MimeTypesHelper.GetTrueFileExtension(args[1])); } catch (RdfWriterSelectionException) { writer = new CompressingTurtleWriter(WriterCompressionLevel.High); } writer.Save(g, args[1]); Console.WriteLine("rdfWebDeploy: Configuration Vocabulary output to " + args[1]); }
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)); }
/// <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); } }
private IRdfParser GetParser(string fileName) { Options.DefaultTokenQueueMode = TokenQueueMode.SynchronousBufferDuringParsing; var fileExtension = MimeTypesHelper.GetTrueFileExtension(fileName); #if PORTABLE bool isGZiped = fileExtension.ToLowerInvariant().EndsWith(MimeTypesHelper.DefaultGZipExtension.ToLowerInvariant()); #else bool isGZiped = fileExtension.EndsWith(MimeTypesHelper.DefaultGZipExtension, StringComparison.InvariantCultureIgnoreCase); #endif var parserDefinition = _importFormat == null ? MimeTypesHelper.GetDefinitionsByFileExtension(fileExtension).FirstOrDefault(def => def.CanParseRdf) : MimeTypesHelper.GetDefinitionsByFileExtension(_importFormat.DefaultExtension) .FirstOrDefault(def => def.CanParseRdf); if (parserDefinition != null) { var rdfReader = parserDefinition.GetRdfParser(); if (rdfReader != null) { if (rdfReader is VDS.RDF.Parsing.NTriplesParser && !isGZiped) { // Use the Brighstar NTriples Parser return(new NTriplesParser()); } return(new BrightstarRdfParserAdapter(rdfReader, isGZiped)); } } Logging.LogWarning(BrightstarEventId.ParserWarning, "Unable to select a parser by determining MIME type from file extension. Will default to NTriples parser."); return(new NTriplesParser()); }
public static void SaveToFile(this ITripleStore store, string fileName) { using (var output = new StreamWriter(fileName)) { IStoreWriter writer = MimeTypesHelper.GetStoreWriterByFileExtension(MimeTypesHelper.GetTrueFileExtension(fileName)); writer.Save(store, output); } }
public void ParsingGZipByFilenameAuto2() { foreach (String filename in this._autoTestFiles) { Graph g = new Graph(); IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename)); reader.Load(g, filename); Assert.AreEqual(this._g, g, "Graphs for file " + filename + " were not equal"); } }
public void ParsingGZipByFilenameManual2() { foreach (String filename in this._manualTestFiles) { Graph g = new Graph(); IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename)); reader.Load(g, filename); Assert.Equal(this._g, g); } }
private void FindTestData(String dir) { if (Directory.Exists(dir)) { foreach (String file in Directory.GetFiles(dir)) { String ext = MimeTypesHelper.GetTrueFileExtension(file); if (MimeTypesHelper.Definitions.Any(d => d.SupportsFileExtension(ext))) { this._dataFiles.Add(file); } } } }
/// <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); } }
/// <summary> /// Implements the import /// </summary> /// <param name="handler">Handler</param> protected override void ImportUsingHandler(IRdfHandler handler) { this.Information = "Importing from File " + this._file; try { //Assume a RDF Graph IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file)); FileLoader.Load(handler, this._file, reader); } catch (RdfParserSelectionException) { //Assume a RDF Dataset FileLoader.LoadDataset(handler, this._file); } }
public static void Load(IRdfHandler handler, string filename, Stream inputStream, IRdfReader parser) { if (handler == null) { throw new RdfParseException("Cannot read RDF using a null RDF Handler"); } if (inputStream == null) { throw new RdfParseException("Cannot read RDF from a null Stream"); } if (parser == null) { if (filename != null) { try { String ext = MimeTypesHelper.GetTrueFileExtension(filename); parser = MimeTypesHelper.GetParserByFileExtension(ext); } 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 RaiseWarning("Attempting to select parser based on analysis of the data file, this requires loading the file into memory"); StreamReader reader = new StreamReader(inputStream); String data = reader.ReadToEnd(); reader.Close(); parser = StringParser.GetParser(data); RaiseWarning("Used the StringParser to guess the parser to use - it guessed " + parser.GetType().Name); parser.Warning += RaiseWarning; parser.Load(handler, new StringReader(data)); } else { //Parser was selected based on File Extension or one was explicitly specified parser.Warning += RaiseWarning; parser.Load(handler, new StreamReader(inputStream)); } }
public void ParsingGZipExtensionDetectionNonStackable() { List <String> filenames = new List <String>() { "example.nt.abc", "example.ttl.def", "example.n3.xyx", "example.rdf.rdf", "example.rj.ttl" }; foreach (String filename in filenames) { Console.WriteLine(filename + " => " + MimeTypesHelper.GetTrueFileExtension(filename)); Assert.AreEqual(Path.GetExtension(filename), MimeTypesHelper.GetTrueFileExtension(filename)); } }
public void ParsingGZipByFilenameAuto1() { foreach (String filename in this._autoTestFiles) { Graph g = new Graph(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); IRdfReader reader = def.GetRdfParser(); reader.Load(g, filename); Assert.Equal(this._g, g); } }
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 void ParsingGZipResultsByStreamManual() { foreach (String filename in this._manualResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, File.OpenText(filename)); Assert.True(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
public void ParsingGZipByGZipStreamAuto() { foreach (String filename in this._autoTestFiles) { Graph g = new Graph(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); IRdfReader reader = def.GetRdfParser(); reader.Load(g, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.Equal(this._g, g); } }
public void ParsingGZipResultsByGZipStreamAuto() { foreach (String filename in this._autoResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); Assert.NotNull(def); ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.True(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
/// <summary> /// Loads the Configuration Graph /// </summary> /// <returns></returns> internal IGraph LoadConfigurationGraph() { IGraph g = null; try { if (File.Exists(this._configFile)) { g = new Graph(); FileLoader.Load(g, this._configFile); } else { //Try to get from embedded resource instead Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(this._configFile); if (stream == null) { Console.Error.WriteLine("rdfServer: Error: Configuration File '" + this.ConfigurationFile + "' was not found"); } else { IRdfReader reader = MimeTypesHelper.GetParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._configFile)); g = new Graph(); reader.Load(g, new StreamReader(stream)); } } } catch (RdfParserSelectionException) { g = null; Console.Error.WriteLine("rdfServer: Error: Configuration File '" + this.ConfigurationFile + "' could not be loaded as a suitable parser was not found"); } catch (RdfParseException) { g = null; Console.Error.WriteLine("rdfServer: Error: Configuration File '" + this.ConfigurationFile + "' was not valid RDF"); } //If got a Graph OK then prep the dotNetRDF Configuration API if (g != null) { ConfigurationLoader.AutoConfigure(g); } return(g); }
public void ParsingGZipByStreamAuto() { foreach (String filename in this._autoTestFiles) { Graph g = new Graph(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseRdf && d.SupportsFileExtension(ext)).FirstOrDefault(); if (def == null) { Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext); } IRdfReader reader = def.GetRdfParser(); reader.Load(g, new StreamReader(filename)); Assert.AreEqual(this._g, g, "Graphs for file " + filename + " were not equal"); } }
public void ParsingGZipDatasetByGZipStreamAuto() { foreach (String filename in this._autoDatasetTestFiles) { 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(); if (def == null) { Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext); } IStoreReader reader = def.GetRdfDatasetParser(); reader.Load(store, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.AreEqual(this._g, store.Graphs.First(), "Graphs for file " + filename + " were not equal"); } }
public void ParsingGZipResultsByStreamAuto() { foreach (String filename in this._autoResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); if (def == null) { Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext); } ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, new StreamReader(filename)); Assert.IsTrue(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
public void ParsingGZipResultsByGZipStreamManual() { foreach (String filename in this._manualResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); String ext = MimeTypesHelper.GetTrueFileExtension(filename); ext = ext.Substring(1); MimeTypeDefinition def = MimeTypesHelper.Definitions.Where(d => d.CanParseSparqlResults && d.SupportsFileExtension(ext)).FirstOrDefault(); if (def == null) { Assert.Fail("Failed to find MIME Type Definition for File Extension ." + ext); } ISparqlResultsReader reader = def.GetSparqlResultsParser(); reader.Load(results, new StreamReader(new GZipStream(new FileStream(filename, FileMode.Open, FileAccess.Read), CompressionMode.Decompress))); Assert.AreEqual(this._results, results, "Result Sets for file " + filename + " were not equal"); } }
public void ParsingGZipExtensionDetectionTrue() { List <String> filenames = new List <String>() { "example.nt.gz", "example.ttl.gz", "example.n3.gz", "example.rdf.gz", "example.rj.gz", "example.gz", "example" }; foreach (String filename in filenames) { String expectedExt = (filename.Contains('.') ? filename.Substring(filename.IndexOf('.')) : String.Empty); String realExt = MimeTypesHelper.GetTrueFileExtension(filename); Console.WriteLine(filename + " => " + realExt); Assert.AreEqual(expectedExt, realExt); } }
public static void LoadFromFile(this ITripleStore store, string fileName) { IStoreReader parser = null; using (var input = new StreamReader(fileName)) { string ext = MimeTypesHelper.GetTrueFileExtension(fileName); try { parser = MimeTypesHelper.GetStoreParserByFileExtension(ext); } catch (RdfParserSelectionException) { try { IRdfReader rdfParser = MimeTypesHelper.GetParserByFileExtension(ext); var storeHandler = new StoreHandler(store); rdfParser.Load(storeHandler, input); return; } catch (RdfParserSelectionException) { // Ignore this. Will try and use format guessing and assume it is a dataset format } } if (parser == null) { string data = input.ReadToEnd(); input.Close(); parser = StringParser.GetDatasetParser(data); var handler = new StoreHandler(store); parser.Load(handler, new StringReader(data)); } else { parser.Load(new StoreHandler(store), input); } } }
/// <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(); } } } }
private bool SetOptions(String[] args) { if (args.Length == 0 || (args.Length == 1 && args[0].Equals("-help"))) { this.ShowUsage(); return(false); } //Look through the arguments to see what we've been asked to do foreach (String arg in args) { if (arg.StartsWith("-hs")) { if (arg.Contains(':')) { bool hs; if (Boolean.TryParse(arg.Substring(arg.IndexOf(':') + 1), out hs)) { this._options.Add(new HighSpeedOption(hs)); } else { this._options.Add(new HighSpeedOption(true)); } } else { this._options.Add(new HighSpeedOption(true)); } } else if (arg.StartsWith("-pp")) { if (arg.Contains(':')) { bool pp; if (Boolean.TryParse(arg.Substring(arg.IndexOf(':') + 1), out pp)) { this._options.Add(new PrettyPrintingOption(pp)); } else { this._options.Add(new PrettyPrintingOption(true)); } } else { this._options.Add(new PrettyPrintingOption(true)); } } else if (arg.StartsWith("-c")) { if (arg.Contains(':')) { int c; if (Int32.TryParse(arg.Substring(arg.IndexOf(':') + 1), out c)) { this._options.Add(new CompressionLevelOption(c)); } else { this._options.Add(new CompressionLevelOption(WriterCompressionLevel.Default)); } } else { this._options.Add(new CompressionLevelOption(WriterCompressionLevel.Default)); } } else if (arg.StartsWith("-stylesheet:")) { String stylesheet = arg.Substring(arg.IndexOf(':') + 1); this._options.Add(new StylesheetOption(stylesheet)); } else if (arg.Equals("-overwrite")) { this._overwrite = true; } else if (arg.StartsWith("-out:") || arg.StartsWith("-output:")) { this._outputFilename = arg.Substring(arg.IndexOf(':') + 1); } else if (arg.StartsWith("-format:") || arg.StartsWith("-outformat:")) { String format = arg.Substring(arg.IndexOf(':') + 1); if (!format.Contains("/")) { try { foreach (String mimeType in MimeTypesHelper.GetMimeTypes(format)) { this._outFormats.Add(mimeType); } } catch { Console.Error.WriteLine("rdfConvert: Error: The File Extension '" + format + "' is not permissible since dotNetRDF cannot infer a MIME type from the extension"); return(false); } } else { //Validate the MIME Type if (!IsValidMimeType(format)) { Console.Error.WriteLine("rdfConvert: Error: The MIME Type '" + format + "' is not permissible since dotNetRDF does not support outputting in that format"); return(false); } this._outFormats.Add(format); } } else if (arg.StartsWith("-outext:") || arg.StartsWith("-ext:")) { this._outExt = arg.Substring(arg.IndexOf(':') + 1); if (!this._outExt.StartsWith(".")) { this._outExt = "." + this._outExt; } } else if (arg.Equals("-debug")) { this._debug = true; } else if (arg.Equals("-help")) { //Ignore help argument if other arguments are present } else if (arg.Equals("-nocache")) { Options.UriLoaderCaching = false; } else if (arg.Equals("-bom")) { Options.UseBomForUtf8 = true; } else if (arg.Equals("-nobom")) { Options.UseBomForUtf8 = false; } else if (arg.Equals("-best")) { this._best = true; } else if (arg.Equals("-verbose")) { this._verbose = true; } else if (arg.Equals("-warnings")) { this._warnings = true; UriLoader.Warning += this.ShowWarning; UriLoader.StoreWarning += this.ShowWarning; FileLoader.Warning += this.ShowWarning; FileLoader.StoreWarning += this.ShowWarning; } else { //Anything else is treated as an input if (arg.Contains("://")) { try { this._inputs.Add(new UriInput(new Uri(arg))); } catch (UriFormatException uriEx) { Console.Error.WriteLine("rdfConvert: Error: The Input '" + arg + "' which rdfConvert assumed to be a URI is not a valid URI - " + uriEx.Message); return(false); } } else { this._inputs.Add(new FileInput(arg)); } } } //If there are no this._inputs then we'll abort if (this._inputs.Count == 0) { Console.Error.WriteLine("rdfConvert: Abort: No Inputs were provided - please provide one/more files or URIs you wish to convert"); return(false); } //If there are no writers specified then we'll abort if (this._outFormats.Count == 0) { if (this._inputs.Count == 1 && !this._outputFilename.Equals(String.Empty)) { //If only 1 input and an output filename can detect the output format from that filename this._outFormats.AddRange(MimeTypesHelper.GetMimeTypes(MimeTypesHelper.GetTrueFileExtension(this._outputFilename))); } else { Console.Error.WriteLine("rdfConvert: Abort: Aborting since no output options have been specified, use either the -format:[mime/type|ext] argument to specify output format for multiple inputs of use -out:filename.ext to specify the output for a single input"); return(false); } } //Ensure Output Extension (if specified) is OK if (!this._outExt.Equals(String.Empty)) { if (!this._outExt.StartsWith(".")) { this._outExt = "." + this._outExt; } } //If more than one input and an Output Filename be sure to strip any Extension from the Filename if (this._inputs.Count > 1 && !this._outputFilename.Equals(String.Empty)) { this._outputFilename = Path.GetFileNameWithoutExtension(this._outputFilename); } return(true); }
public void Run() { if (this._args.Length == 0) { this.ShowUsage(); } else { if (!this.ParseOptions()) { Console.Error.WriteLine("rdfOptStats: Error: One/More options were invalid"); return; } if (this._inputs.Count == 0) { Console.Error.WriteLine("rdfOptStats: Error: No Inputs Specified"); return; } List <BaseStatsHandler> handlers = new List <BaseStatsHandler>(); if (this._subjects && this._predicates && this._objects) { handlers.Add(new SPOStatsHandler(this._literals)); } else if (this._subjects && this._predicates) { handlers.Add(new SPStatsHandler(this._literals)); } else { if (this._subjects) { handlers.Add(new SubjectStatsHandler(this._literals)); } if (this._predicates) { handlers.Add(new PredicateStatsHandler(this._literals)); } if (this._objects) { handlers.Add(new ObjectStatsHandler(this._literals)); } } if (this._nodes) { handlers.Add(new NodeStatsHandler()); } bool ok = true; IRdfHandler handler; if (handlers.Count == 1) { handler = handlers[0]; } else { handler = new MultiHandler(handlers.OfType <IRdfHandler>()); } Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 0; i < this._inputs.Count; i++) { Console.WriteLine("rdfOptStats: Processing Input " + (i + 1) + " of " + this._inputs.Count + " - '" + this._inputs[i] + "'"); try { FileLoader.Load(handler, this._inputs[i]); } catch (RdfParserSelectionException selEx) { ok = false; Console.Error.WriteLine("rdfOptStats: Error: Unable to select a Parser to read input"); break; } catch (RdfParseException parseEx) { ok = false; Console.Error.WriteLine("rdfOptStats: Error: Parsing Error while reading input"); break; } catch (RdfException parseEx) { ok = false; Console.Error.WriteLine("rdfOptStats: Error: RDF Error while reading input"); break; } catch (Exception ex) { ok = false; Console.Error.WriteLine("rdfOptStats: Error: Unexpected Error while reading input"); break; } } Console.WriteLine("rdfOptStats: Finished Processing Inputs"); timer.Stop(); Console.WriteLine("rdfOptStats: Took " + timer.Elapsed + " to process inputs"); timer.Reset(); if (ok) { //Output the Stats timer.Start(); Graph g = new Graph(); g.NamespaceMap.Import(handlers.First().Namespaces); try { foreach (BaseStatsHandler h in handlers) { h.GetStats(g); } IRdfWriter writer = MimeTypesHelper.GetWriterByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file)); if (writer is ICompressingWriter) { ((ICompressingWriter)writer).CompressionLevel = WriterCompressionLevel.High; } if (writer is IHighSpeedWriter) { ((IHighSpeedWriter)writer).HighSpeedModePermitted = false; } writer.Save(g, this._file); Console.WriteLine("rdfOptStats: Statistics output to " + this._file); timer.Stop(); Console.WriteLine("rdfOptStats: Took " + timer.Elapsed + " to output statistics"); } catch (Exception ex) { Console.Error.WriteLine("rdfOptStats: Error: Unexpected error outputting statistics to " + this._file); Console.Error.WriteLine(ex.Message); Console.Error.WriteLine(ex.StackTrace); } } else { Console.Error.WriteLine("rdfOptStats: Error: Unable to output statistics due to errors during input processing"); } } }
public void ParsingGZipResultsByFilenameAuto2() { foreach (String filename in this._autoResultsTestFiles) { SparqlResultSet results = new SparqlResultSet(); ISparqlResultsReader reader = MimeTypesHelper.GetSparqlParserByFileExtension(MimeTypesHelper.GetTrueFileExtension(filename)); reader.Load(results, filename); Assert.IsTrue(this._results.Equals(results), "Result Sets for file " + filename + " were not equal"); } }
private void LocalImportContinuation(MessageBoxResult dialogResult = MessageBoxResult.Yes) { try { if (dialogResult == MessageBoxResult.Yes) { var ext = MimeTypesHelper.GetTrueFileExtension(_importFileName); bool isGZipped = ext.EndsWith(MimeTypesHelper.DefaultGZipExtension); string lines; var fileTypeDefinition = MimeTypesHelper.GetDefinitionsByFileExtension(ext).FirstOrDefault(d => d.CanParseRdf); var rdfReader = fileTypeDefinition == null ? null : fileTypeDefinition.GetRdfParser(); if (rdfReader == null || rdfReader is NTriplesParser || rdfReader is NQuadsParser) { // We can't determine the file type, or it is NQuads or NTriples if (isGZipped) { using (var fileStream = new FileStream(_importFileName, FileMode.Open)) { var gZipStream = new GZipStream(fileStream, CompressionMode.Decompress); var reader = new StreamReader(gZipStream); lines = reader.ReadToEnd(); } } else { lines = File.ReadAllText(_importFileName); } } else { using (var textWriter = new StringWriter()) { try { var nQuadsFormatter = new NQuadsFormatter(); var writeThroughHandler = new WriteThroughHandler(nQuadsFormatter, textWriter); rdfReader.Load(writeThroughHandler, _importFileName); lines = textWriter.ToString(); } catch (Exception ex) { Messenger.Default.Send(new ShowDialogMessage( Strings.ParseErrorTitle, String.Format(Strings.ParseErrorDescription, _importFileName, ex.Message), MessageBoxImage.Error, MessageBoxButton.OK), "MainWindow"); return; } } } var client = BrightstarService.GetClient(Store.Source.ConnectionString); String graphUri = !String.IsNullOrWhiteSpace(ImportGraphName) ? ImportGraphName : Constants.DefaultGraphUri; _transactionJob = client.ExecuteTransaction(Store.Location, String.Empty, String.Empty, lines, waitForCompletion: false, defaultGraphUri: graphUri); _dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new TransactionViewModel.JobMonitorDelegate(CheckJobStatus)); } } catch (OutOfMemoryException) { Messenger.Default.Send(new ShowDialogMessage(Strings.ParseErrorTitle, Strings.ImportFileTooLarge, MessageBoxImage.Error, MessageBoxButton.OK), "MainWindow"); } }
/// <summary> /// Runs the task /// </summary> /// <returns></returns> protected override TaskResult RunTaskInternal() { MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file)).FirstOrDefault(d => d.CanWriteRdfDatasets); if (def == null) { throw new RdfOutputException("Cannot Export the Store to the selected File since dotNetRDF was unable to select a writer to use based on the File Extension"); } IStoreWriter writer = def.GetRdfDatasetWriter(); if (writer is IMultiThreadedWriter) { ((IMultiThreadedWriter)writer).UseMultiThreadedWriting = false; } TripleStore store = new TripleStore(); if (writer is TriXWriter) { //For TriX must load all into memory and then write out all at once foreach (Uri u in this.ListGraphs()) { Graph g = new Graph(); this._manager.LoadGraph(g, u); g.BaseUri = u; store.Add(g); this.Information = "Loading into memory prior to export, loaded " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) so far..."; if (this.HasBeenCancelled) { this.Information = "Export Cancelled"; return(new TaskResult(true)); } } this.Information = "Exporting Data all at once, have " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) to export..."; writer.Save(store, new StreamWriter(this._file)); this.Information = "Exported " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s)"; } else { if (File.Exists(this._file)) { File.Delete(this._file); } //For non-TriX formats assume it is safe to append one Graph at a time to the file int graphCount = 0, tripleCount = 0; foreach (Uri u in this.ListGraphs()) { using (FileStream stream = new FileStream(this._file, FileMode.Append)) { if (writer is IFormatterBasedWriter) { //Stream via a WriteThroughHandler this.Information = "Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default"); WriteThroughHandler handler = new WriteThroughHandler(((IFormatterBasedWriter)writer).TripleFormatterType, new StreamWriter(stream), true); ExportProgressHandler progHandler = new ExportProgressHandler(handler, this, tripleCount); this._manager.LoadGraph(progHandler, u); graphCount++; tripleCount = progHandler.TripleCount; this.Information = "Finished Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default") + ", exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far..."; } else { //Load Graph into memory Graph g = new Graph(); g.BaseUri = u; this.Information = "Loading Graph " + (u != null ? u.AbsoluteUri : "Default"); this._manager.LoadGraph(g, u); g.BaseUri = u; if (this.HasBeenCancelled) { stream.Close(); this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)"; return(new TaskResult(true)); } graphCount++; tripleCount += g.Triples.Count; //Save it store.Add(g); writer.Save(store, new StreamWriter(stream, def.Encoding)); store.Remove(u); this.Information = "Exporting Data graph by graph, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far..."; } //Check for cancellation if (this.HasBeenCancelled) { stream.Close(); this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)"; return(new TaskResult(true)); } } } this.Information = "Exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)"; } return(new TaskResult(true)); }