Merge the recent changes from HEAD onto the branch
[beagle.git] / Util / SemWeb / RdfParser.cs
blob26727d8c8dc70e19cebbccfe4cdfcb278cd3427e
1 using System;
2 using System.Collections;
3 using System.IO;
5 namespace SemWeb {
6 public class ParserException : ApplicationException {
7 public ParserException (string message) : base (message) {}
8 public ParserException (string message, Exception cause) : base (message, cause) {}
11 public abstract class RdfReader : StatementSource, IDisposable {
12 Entity meta = Statement.DefaultMeta;
13 string baseuri = null;
14 ArrayList warnings = new ArrayList();
15 ArrayList variables = new ArrayList();
16 bool reuseentities = false;
17 bool dupcheck = false;
19 public Entity Meta {
20 get {
21 return meta;
23 set {
24 meta = value;
28 public string BaseUri {
29 get {
30 return baseuri;
32 set {
33 baseuri = value;
37 public bool ReuseEntities {
38 get {
39 return reuseentities;
41 set {
42 reuseentities = value;
46 public bool DuplicateCheck {
47 get {
48 return dupcheck;
50 set {
51 dupcheck = value;
55 public ICollection Variables { get { return ArrayList.ReadOnly(variables); } }
57 protected void AddVariable(Entity variable) {
58 variables.Add(variable);
61 public abstract void Select(StatementSink sink);
63 public virtual void Dispose() {
66 public static RdfReader Create(string type, string source) {
67 switch (type) {
68 case "xml":
69 case "text/xml":
70 return new RdfXmlReader(source);
71 case "n3":
72 case "text/n3":
73 return new N3Reader(source);
74 default:
75 throw new ArgumentException("Unknown parser type: " + type);
79 internal static TextReader GetReader(string file) {
80 if (file == "-") return Console.In;
81 return new StreamReader(file);
84 protected void OnWarning(string message) {
85 warnings.Add(message);
88 internal string GetAbsoluteUri(string baseuri, string uri) {
89 if (baseuri == null) return uri;
90 if (uri.IndexOf(':') != -1) return uri;
91 try {
92 UriBuilder b = new UriBuilder(baseuri);
93 b.Fragment = null; // per W3 RDF/XML test suite
94 return new Uri(b.Uri, uri, true).ToString();
95 } catch (UriFormatException e) {
96 return baseuri + uri;
100 internal StatementSink GetDupCheckSink(StatementSink sink) {
101 if (!dupcheck) return sink;
102 if (!(sink is Store)) return sink;
103 return new DupCheckSink((Store)sink);
106 private class DupCheckSink : StatementSink {
107 Store store;
108 public DupCheckSink(Store store) { this.store = store; }
109 public bool Add(Statement s) {
110 if (store.Contains(s)) return true;
111 store.Add(s);
112 return true;
117 internal class MultiRdfReader : RdfReader {
118 private ArrayList parsers = new ArrayList();
120 public ArrayList Parsers { get { return parsers; } }
122 public override void Select(StatementSink storage) {
123 foreach (RdfReader p in Parsers)
124 p.Select(storage);