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)) { 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)); }
public void TestLangSpecParsing(String file) { this.EnsureTestData(file); MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(Path.GetExtension(file))).FirstOrDefault(); if (def != null) { if (def.CanParseRdf) { Graph g = new Graph(); g.LoadFromFile(file); Assert.AreEqual(this._original, g); } else if (def.CanParseRdfDatasets) { TripleStore store = new TripleStore(); store.LoadFromFile(file); Assert.AreEqual(this._original, store.Graphs.First()); } } else { Assert.Fail("Unsupported file type"); } }
/// <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))); } } } } } }
public void AutoDetectSyntax() { if (this._filename != null && !this._filename.Equals(String.Empty)) { try { //Try filename based syntax detection MimeTypeDefinition def = MimeTypesHelper.GetDefinitions(MimeTypesHelper.GetMimeTypes(Path.GetExtension(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(); } } } }
protected override void ImportUsingHandler(IRdfHandler handler) { this.Information = "Importing from File " + this._file; try { //Assume a RDF Graph IRdfReader reader = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeTypes(Path.GetExtension(this._file))); FileLoader.Load(handler, this._file, reader); } catch (RdfParserSelectionException) { //Assume a RDF Dataset FileLoader.LoadDataset(handler, this._file); } }
/// <summary> /// Loads the contents of the given File using a RDF Handler using the given RDF Parser /// </summary> /// <param name="handler">RDF Handler to use</param> /// <param name="filename">File to load from</param> /// <param name="parser">Parser to use</param> public static void Load(IRdfHandler handler, String filename, IRdfReader parser) { if (handler == null) { throw new RdfParseException("Cannot read RDF using a null RDF Handler"); } if (filename == null) { throw new RdfParseException("Cannot read RDF from a null File"); } //Try to get a Parser from the File Extension if one isn't explicitly specified if (parser == null) { try { parser = MimeTypesHelper.GetParser(MimeTypesHelper.GetMimeTypes(Path.GetExtension(filename))); RaiseWarning("Selected Parser " + parser.ToString() + " based on file extension, if this is incorrect consider specifying the parser explicitly"); } 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(filename); 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, filename); } }
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); }
private void EnsureTestData(String file) { ISparqlResultsWriter writer = MimeTypesHelper.GetSparqlWriter(MimeTypesHelper.GetMimeTypes(Path.GetExtension(file))); this.EnsureTestData(file, writer); }
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.GetWriter(MimeTypesHelper.GetMimeTypes(Path.GetExtension(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"); } } }
/// <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.GetDefinitions(MimeTypesHelper.GetMimeTypes(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 } } } }