Initial revision
[beagle.git] / indexer / Content.cs
blob199a23171abf39b1618cf9b52f968ac63f60f47a
2 using System;
3 using System.Collections;
4 using System.IO;
5 using System.Reflection;
7 namespace Dewey {
9 abstract public class Content {
11 static Hashtable registry = new Hashtable();
13 static public void Register(Type handler) {
14 if (! handler.IsSubclassOf(typeof(Content))) {
15 // throw exception: type not subclass of Content
18 Content dummy = (Content)Activator.CreateInstance(handler);
19 foreach (String mime_type in dummy.HandledMimeTypes()) {
20 if (registry.Contains(mime_type)) {
21 // throw exception: duplicated mime type
23 registry[mime_type] = handler;
27 static public Content Extract(String mime_type, Stream stream) {
28 Content content = null;
29 if (registry.Contains(mime_type)) {
30 Type handler = registry[mime_type] as Type;
31 content = Activator.CreateInstance(handler) as Content;
33 if (! content.Read(stream))
34 return null;
36 return content;
39 //////////////////////////////
41 Hashtable metadata = new Hashtable();
42 String body;
44 public Content() { }
46 abstract public String[] HandledMimeTypes();
47 abstract public bool Read(Stream content_stream);
49 protected void SetMetadata(String key, String value) {
50 metadata[key.ToLower()] = value;
53 protected void SetBody(String _body) {
54 if (body != null) {
55 // FIXME: complain that you can't set the body twice
57 body = _body;
60 public ICollection MetadataKeys {
61 get { return metadata.Keys; }
64 public String this[String key] {
65 get { return metadata[key.ToLower()] as String; }
68 public String Body {
69 get { return body; }