public void WritingBackslashEscaping() { Graph g = new Graph(); INode subj = g.CreateBlankNode(); INode pred = g.CreateUriNode(new Uri("ex:string")); INode obj = g.CreateLiteralNode(@"C:\Program Files\some\path\\to\file.txt"); g.Assert(subj, pred, obj); obj = g.CreateLiteralNode(@"\"); g.Assert(subj, pred, obj); obj = g.CreateLiteralNode(@"C:\\new\\real\\ugly\\Under has all the possible escape character interactions"); g.Assert(subj, pred, obj); List <IRdfWriter> writers = new List <IRdfWriter>() { new NTriplesWriter(), new TurtleWriter(), new CompressingTurtleWriter(WriterCompressionLevel.High), new Notation3Writer() }; List <IRdfReader> parsers = new List <IRdfReader>() { new NTriplesParser(), new TurtleParser(), new TurtleParser(), new Notation3Parser() }; Console.WriteLine("Original Graph"); TestTools.ShowGraph(g); Console.WriteLine(); for (int i = 0; i < writers.Count; i++) { IRdfWriter writer = writers[i]; Console.WriteLine("Testing Writer " + writer.GetType().Name); System.IO.StringWriter strWriter = new System.IO.StringWriter(); writer.Save(g, strWriter); Console.WriteLine("Written as:"); Console.WriteLine(strWriter.ToString()); Console.WriteLine(); Graph h = new Graph(); StringParser.Parse(h, strWriter.ToString(), parsers[i]); Console.WriteLine("Parsed Graph"); TestTools.ShowGraph(h); Console.WriteLine(); Assert.AreEqual(g, h, "Graphs should be equal"); } }
/// <summary> /// Registers a writer as the default RDF Writer for all the given MIME types and updates relevant definitions to include the MIME types and file extensions /// </summary> /// <param name="writer">RDF Writer</param> /// <param name="mimeTypes">MIME Types</param> /// <param name="fileExtensions">File Extensions</param> public static void RegisterWriter(IRdfWriter writer, IEnumerable<String> mimeTypes, IEnumerable<String> fileExtensions) { if (!_init) Init(); if (!mimeTypes.Any()) throw new RdfException("Cannot register a writer 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.RdfWriterType = writer.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.RdfWriterType = writer.GetType(); AddDefinition(newDef); } }
/// <summary> /// Selects the appropriate File Extension for the given RDF Writer /// </summary> /// <param name="writer">RDF Writer</param> /// <returns></returns> public static String GetFileExtension(IRdfWriter writer) { if (!_init) Init(); Type requiredType = writer.GetType(); foreach (MimeTypeDefinition definition in MimeTypesHelper.Definitions) { if (requiredType.Equals(definition.RdfWriterType)) { return definition.CanonicalFileExtension; } } throw new RdfException("Unable to determine the appropriate File Extension for the RDF Writer '" + writer.GetType().ToString() + "'"); }
private void SaveWith(IRdfWriter writer) { IRdfReader parser = this._manager.GetParser(); Graph g = new Graph(); try { StringParser.Parse(g, textEditor.Text, parser); } catch { MessageBox.Show("Unable to Save With an RDF Writer as the current file is not a valid RDF document when parsed with the " + parser.GetType().Name + ". If you believe this is a valid RDF document please select the correct Syntax Highlighting from the Options Menu and retry", "Save With Failed"); return; } bool filenameRequired = (this._manager.CurrentFile == null); if (!filenameRequired) { MessageBoxResult res = MessageBox.Show("Are you sure you wish to overwrite your existing file with the output of the " + writer.GetType().Name + "? Click Yes to proceed, No to select a different Filename or Cancel to abort this operation", "Overwrite File",MessageBoxButton.YesNoCancel); if (res == MessageBoxResult.Cancel) { return; } else if (res == MessageBoxResult.No) { filenameRequired = true; } else if (res == MessageBoxResult.None) { return; } } //Get a Filename to Save to String origFilename = this._manager.CurrentFile; if (filenameRequired) { if (_sfd.ShowDialog() == true) { this.UpdateMruList(this._sfd.FileName); this._manager.CurrentFile = _sfd.FileName; } else { return; } } try { writer.Save(g, this._manager.CurrentFile); MessageBoxResult res = MessageBox.Show("Would you like to switch editing to the newly created file?", "Switch Editing", MessageBoxButton.YesNo); if (res == MessageBoxResult.Yes) { try { using (StreamReader reader = new StreamReader(this._manager.CurrentFile)) { String text = reader.ReadToEnd(); textEditor.Text = String.Empty; textEditor.Text = text; this._manager.AutoDetectSyntax(this._manager.CurrentFile); } this.Title = "rdfEditor - " + System.IO.Path.GetFileName(this._manager.CurrentFile); this._manager.HasChanged = false; } catch (Exception ex) { MessageBox.Show("An error occurred while opening the selected file: " + ex.Message, "Unable to Open File"); } } else { if (origFilename != null) { this._manager.CurrentFile = origFilename; } } } catch (Exception ex) { MessageBox.Show("An error occurred while saving: " + ex.Message, "Save With Failed"); } }
public static void TestWriter(StreamWriter output, Graph g, IRdfWriter writer, IRdfReader reader, String file) { Stopwatch timer = new Stopwatch(); Console.WriteLine(); Console.WriteLine(new String('-', file.Length)); Console.WriteLine(file); Console.WriteLine("Attempting serialization with " + writer.GetType().ToString()); //Show Compression Level if (writer is ICompressingWriter) { Console.WriteLine("Compression Level is " + ((ICompressingWriter)writer).CompressionLevel); } //Enable Pretty Printing if supported if (writer is IPrettyPrintingWriter) { ((IPrettyPrintingWriter)writer).PrettyPrintMode = true; } try { timer.Start(); writer.Save(g, "writer_tests/" + file + ".out"); Console.WriteLine("Serialization Done"); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } finally { timer.Stop(); Console.WriteLine("Writing took " + timer.ElapsedMilliseconds + "ms"); //Get the relevant Reader Graph h = new Graph(); try { //Read back in the serialized output to check it's valid RDF Console.WriteLine("Attempting to read back in from serialized Output using " + reader.GetType().ToString()); reader.Load(h, "writer_tests/" + file + ".out"); Console.WriteLine("Serialized Output was valid RDF"); //Check same number of Triples are present if (g.Triples.Count == h.Triples.Count) { Console.WriteLine("Correct number of Triples loaded"); } else { throw new RdfException("Incorrect number of Triples loaded, got " + h.Triples.Count + " but expected " + g.Triples.Count); } //Check same number of Subjects are present if (g.Triples.SubjectNodes.Distinct().Count() == h.Triples.SubjectNodes.Distinct().Count()) { Console.WriteLine("Correct number of Subjects loaded"); } else { throw new RdfException("Incorrect number of Subjects loaded, got " + h.Triples.SubjectNodes.Distinct().Count() + " but expected " + g.Triples.SubjectNodes.Distinct().Count()); } //Reserialize to NTriples NTriplesWriter ntwriter = new NTriplesWriter(); ntwriter.SortTriples = true; ntwriter.Save(h, "writer_tests/" + file + ".nt"); Console.WriteLine("Serialized Output reserialized to NTriples"); //Check Graphs are Equal if (g.Equals(h)) { Console.WriteLine("Graphs are Equal"); } else { Console.WriteLine("First Graphs triples:"); foreach (Triple t in g.Triples) { Console.WriteLine(t.ToString()); } Console.WriteLine(); Console.WriteLine("Second Graph triples:"); foreach (Triple t in h.Triples) { Console.WriteLine(t.ToString()); } Console.WriteLine(); throw new RdfException("Graphs are non-equal"); } } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } Console.WriteLine(); } }