QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / Util / SemWeb / RdfWriter.cs
blob01a64f68a45d31705da1e31b4746273d5ae851c9
1 using System;
2 using System.Collections;
3 using System.IO;
5 namespace SemWeb {
6 public abstract class RdfWriter : IDisposable, StatementSink {
7 string baseuri;
8 bool closed;
10 public abstract NamespaceManager Namespaces { get; }
12 public string BaseUri {
13 get {
14 return baseuri;
16 set {
17 baseuri = value;
21 protected object GetResourceKey(Resource resource) {
22 return resource.GetResourceKey(this);
25 protected void SetResourceKey(Resource resource, object value) {
26 resource.SetResourceKey(this, value);
29 internal static TextWriter GetWriter(string dest) {
30 if (dest == "-")
31 return Console.Out;
32 return new StreamWriter(dest);
35 bool StatementSink.Add(Statement statement) {
36 Add(statement);
37 return true;
40 public void Add(Statement statement) {
41 if (statement.AnyNull)
42 throw new ArgumentNullException();
44 string s = getUri(statement.Subject);
45 string p = getUri(statement.Predicate);
47 if (statement.Object is Literal) {
48 Literal lit = (Literal)statement.Object;
49 WriteStatement(s, p, lit);
50 } else {
51 string o = getUri((Entity)statement.Object);
52 WriteStatement(s, p, o);
56 private string getUri(Entity e) {
57 if (e.Uri != null) return e.Uri;
58 string uri = (string)GetResourceKey(e);
59 if (uri != null) return uri;
60 uri = CreateAnonymousEntity();
61 SetResourceKey(e, uri);
62 return uri;
65 public abstract void WriteStatement(string subj, string pred, string obj);
67 public abstract void WriteStatement(string subj, string pred, Literal literal);
69 public abstract string CreateAnonymousEntity();
71 public virtual void Close() {
72 if (closed) return;
73 closed = true;
76 void IDisposable.Dispose() {
77 Close();