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 void TestLangSpecParsing(String file) { this.EnsureTestData(file); var def = MimeTypesHelper.GetDefinitionsByFileExtension(Path.GetExtension(file)).FirstOrDefault(); //MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(Path.GetExtension(file))).FirstOrDefault(); Assert.NotNull(def); if (def != null) { if (def.CanParseRdf) { Graph g = new Graph(); g.LoadFromFile(file); Assert.Equal(this._original, g); } else if (def.CanParseRdfDatasets) { TripleStore store = new TripleStore(); store.LoadFromFile(file); Assert.Equal(this._original, store.Graphs.First()); } } }
private void EnsureTestData(String file) { if (this._original == null) { Graph g = new Graph(); g.Assert(g.CreateBlankNode(), g.CreateUriNode(UriFactory.Create("http://example.org/predicate")), g.CreateLiteralNode("literal", "en-123")); g.Assert(g.CreateBlankNode(), g.CreateUriNode(UriFactory.Create("http://example.org/predicate")), g.CreateLiteralNode("literal", "en-gb-us")); g.Assert(g.CreateBlankNode(), g.CreateUriNode(UriFactory.Create("http://example.org/predicate")), g.CreateLiteralNode("literal", "en-123-abc")); this._original = g; this._store = new TripleStore(); this._store.Add(this._original); } if (!File.Exists(file)) { var def = MimeTypesHelper.GetDefinitionsByFileExtension(Path.GetExtension(file)).FirstOrDefault(); //MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(Path.GetExtension(file))).FirstOrDefault(); Assert.NotNull(def); if (def != null) { Assert.True(def.CanWriteRdf || def.CanWriteRdfDatasets, "Unable to ensure test data"); if (def.CanWriteRdf) { this._original.SaveToFile(file); } else if (def.CanWriteRdfDatasets) { this._store.SaveToFile(file); } } } Assert.True(File.Exists(file), "Unable to ensure test data:" + Path.GetFullPath(file)); }
/// <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 " + GetAssemblyName(asm)); } else { //Resource exists //Do we have a predefined Parser? if (parser != null) { parser.Load(handler, new StreamReader(s)); } else { //Need to select a Parser or use StringParser String ext = MimeTypesHelper.GetTrueResourceExtension(resource); MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(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 StreamReader(s)); } else { //See if the format was actually an RDF graph instead def = MimeTypesHelper.GetDefinitionsByFileExtension(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(); #if !PORTABLE reader.Close(); #endif } parser = StringParser.GetDatasetParser(data); parser.Load(handler, new StringReader(data)); } } } } } }
/// <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> /// 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)); }
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> /// Handles the start of requests by doing conneg wherever applicable /// </summary> /// <param name="sender">Sender of the Event</param> /// <param name="e">Event Arguments</param> void context_BeginRequest(object sender, EventArgs e) { HttpApplication app = sender as HttpApplication; if (app == null) { return; } HttpContext context = app.Context; if (context == null) { return; } if (context.Request.Url.AbsolutePath.Contains(".")) { String actualPath = context.Request.MapPath(context.Request.Path); if (!File.Exists(actualPath)) { // Get the File Extension and see if it is for an RDF format String ext = context.Request.Url.AbsolutePath.Substring(context.Request.Url.AbsolutePath.LastIndexOf(".")); switch (ext) { case ".aspx": case ".asmx": case ".ashx": case ".axd": // The above file extensions are special to ASP.Net and so may not actually exist as files // so we need to ignore them for the purposes of negotiating by file extension return; } try { List <MimeTypeDefinition> defs = MimeTypesHelper.GetDefinitionsByFileExtension(ext).ToList(); if (defs.Count == 0) { return; } context.Request.Headers["Accept"] = String.Join(",", defs.Select(d => d.CanonicalMimeType).ToArray()); String filePath = Path.GetFileNameWithoutExtension(actualPath); if (filePath == null || filePath.Equals(String.Empty)) { if (context.Request.Url.AbsolutePath.EndsWith(ext)) { filePath = context.Request.Url.AbsolutePath.Substring(0, context.Request.Url.AbsolutePath.Length - ext.Length); } } String query = context.Request.Url.Query; if (query.StartsWith("?")) { query = query.Substring(1); } context.RewritePath(filePath, String.Empty, query, true); } catch (RdfParserSelectionException) { // If we get a RdfParserSelectionException we shouldn't do anything, this fixes bug CORE-94 } } } }
private void btnContinue_Click(object sender, RoutedEventArgs e) { if (this.chkAlwaysCheckFileAssociations.IsChecked != null) { Properties.Settings.Default.AlwaysCheckFileAssociations = (bool)this.chkAlwaysCheckFileAssociations.IsChecked; Properties.Settings.Default.Save(); } //Set Associations appropriately foreach (FileAssociationInfo info in this._associations) { //Ensure File Association exists in the registry if (!info.Exists) { info.Create(); try { info.ContentType = MimeTypesHelper.GetDefinitionsByFileExtension(info.Extension).Select(m => m.CanonicalMimeType).FirstOrDefault(); } catch { //Ignore error, just means we don't know the MIME type for the Extension } info.PerceivedType = PerceivedTypes.Text; } //Ensure there is always a Perceived Type and a Content Type registered if (info.PerceivedType == PerceivedTypes.None) { info.PerceivedType = PerceivedTypes.Text; } if (info.ContentType.Equals(String.Empty)) { try { var mimeType = MimeTypesHelper.GetDefinitionsByFileExtension(info.Extension).Select(x => x.CanonicalMimeType).FirstOrDefault(); info.ContentType = mimeType; } catch { //Ignore error, just means we don't know the MIME type for the Extension } } //Add/Remove the association bool addAssociation = this.IsAssociationChecked(info.Extension); if (addAssociation) { info.ProgID = RegistryProgramID; } else if (this._currentAssociations.Contains(info.Extension)) { //We want to remove the association to ourselves info.ProgID = info.Extension.Substring(1) + "_auto_file"; } //TODO: Ensure we are in the OpenWith List } this.Close(); }
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 (var mimeType in MimeTypesHelper.GetDefinitionsByFileExtension(format).SelectMany(d => d.MimeTypes).Distinct()) { 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.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._outputFilename)).SelectMany(d => d.MimeTypes)); } 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); }