Fixed #374055:Only the first "tag" is detected in digikam.
[beagle.git] / Util / MetadataStore.cs
blob9205cb4292f5def1cdb055955fe36bd5c566b4d7
1 using SemWeb;
2 using SemWeb.Util;
3 using Mono.Posix;
5 namespace Beagle.Util {
6 public class MetadataStore : MemoryStore
8 public static NamespaceManager Namespaces;
9 private static MetadataStore descriptions;
11 static MetadataStore ()
13 Namespaces = new NamespaceManager ();
15 Namespaces.AddNamespace ("http://ns.adobe.com/photoshop/1.0/", "photoshop");
16 Namespaces.AddNamespace ("http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/", "Iptc4xmpCore");
17 Namespaces.AddNamespace ("http://purl.org/dc/elements/1.1/", "dc");
18 Namespaces.AddNamespace ("http://ns.adobe.com/xap/1.0/", "xmp");
19 Namespaces.AddNamespace ("http://ns.adobe.com/xmp/Identifier/qual/1.0", "xmpidq");
20 Namespaces.AddNamespace ("http://ns.adobe.com/xap/1.0/rights/", "xmpRights");
21 Namespaces.AddNamespace ("http://ns.adobe.com/xap/1.0/bj/", "xmpBJ");
22 Namespaces.AddNamespace ("http://ns.adobe.com/xap/1.0/mm/", "xmpMM");
23 Namespaces.AddNamespace ("http://ns.adobe.com/exif/1.0/", "exif");
24 Namespaces.AddNamespace ("http://ns.adobe.com/tiff/1.0/", "tiff");
25 Namespaces.AddNamespace ("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf");
26 Namespaces.AddNamespace ("http://www.w3.org/2000/01/rdf-schema#", "rdfs");
29 public static MetadataStore Descriptions {
30 get {
31 if (descriptions == null) {
32 descriptions = new MetadataStore ();
33 System.IO.Stream stream = System.Reflection.Assembly.GetCallingAssembly ().GetManifestResourceStream ("dces.rdf");
34 if (stream != null) {
35 descriptions.Import (new RdfXmlReader (stream));
36 } else {
37 System.Console.WriteLine ("Can't find resource");
41 return descriptions;
45 public void Dump ()
47 foreach (SemWeb.Statement stmt in this) {
48 System.Console.WriteLine(stmt);
52 XPathSemWebNavigator navi = new XPathSemWebNavigator (this, Namespaces);
53 navi.MoveToRoot ();
54 navi.MoveToFirstChild ();
55 navi.MoveToFirstChild ();
56 DumpNode (navi, 0);
59 /* Use the statement writer to filter the messages */
61 foreach (string nspace in Namespaces.GetNamespaces ()) {
62 this.Select (new StatementWriter (nspace));
67 public static void AddLiteral (StatementSink sink, string predicate, string type, Literal value)
69 Entity empty = new Entity (null);
70 Statement top = new Statement ("", (Entity)MetadataStore.Namespaces.Resolve (predicate), empty);
71 Statement desc = new Statement (empty,
72 (Entity)MetadataStore.Namespaces.Resolve ("rdf:type"),
73 (Entity)MetadataStore.Namespaces.Resolve (type));
74 sink.Add (desc);
75 Statement literal = new Statement (empty,
76 (Entity)MetadataStore.Namespaces.Resolve ("rdf:li"),
77 value);
78 sink.Add (literal);
79 sink.Add (top);
82 public static void AddLiteral (StatementSink sink, string predicate, string value)
84 Statement stmt = new Statement ((Entity)"",
85 (Entity)MetadataStore.Namespaces.Resolve (predicate),
86 new Literal (value));
87 sink.Add (stmt);
90 public static void Add (StatementSink sink, string predicate, string type, string [] values)
92 Add (sink, new Entity (""), predicate, type, values);
95 public static void Add (StatementSink sink, Entity subject, string predicate, string type, string [] values)
97 if (values == null) {
98 System.Console.WriteLine ("{0} has no values; skipping", predicate);
99 return;
102 Entity empty = new Entity (null);
103 Statement top = new Statement (subject, (Entity)MetadataStore.Namespaces.Resolve (predicate), empty);
104 Statement desc = new Statement (empty,
105 (Entity)MetadataStore.Namespaces.Resolve ("rdf:type"),
106 (Entity)MetadataStore.Namespaces.Resolve (type));
107 sink.Add (desc);
108 foreach (string value in values) {
109 Statement literal = new Statement (empty,
110 (Entity)MetadataStore.Namespaces.Resolve ("rdf:li"),
111 new Literal (value, null, null));
112 sink.Add (literal);
114 sink.Add (top);
117 private class StatementWriter : StatementSink
119 string name;
120 public StatementWriter (string name)
122 this.name = name;
125 public bool Add (Statement stmt)
127 string predicate = stmt.Predicate.ToString ();
129 if (predicate.StartsWith (name))
130 System.Console.WriteLine ("----------- {0}", stmt);
132 return true;
136 private class SelectFirst : StatementSink
138 public Statement Statement;
140 public bool Add (Statement stmt)
142 this.Statement = stmt;
143 return false;
147 public void DumpNode (XPathSemWebNavigator navi, int depth)
149 do {
150 System.Console.WriteLine ("node [{0}] {1} {2}", depth, navi.Name, navi.Value);
151 } while (navi.MoveToNext ());