[f-spot/taglib-metadata: 13/13] Add XMP sidecar parsing.



commit 815ba60dd261630ffcf67113838c75ecba8e7281
Author: Ruben Vermeersch <ruben savanne be>
Date:   Fri Jun 18 19:39:27 2010 +0200

    Add XMP sidecar parsing.

 lib/TagLib/TagLib                                |    2 +-
 src/Utils/Makefile.am                            |    3 +
 src/Utils/SidecarXmpExtensions.cs                |   31 ++++++++
 src/Utils/Tests/GIOTagLibFileAbstractionTests.cs |   34 ++-------
 src/Utils/Tests/ImageTestHelper.cs               |   30 ++++++++
 src/Utils/Tests/SidecarXmpExtensionsTests.cs     |   88 ++++++++++++++++++++++
 tests/Makefile.am                                |    3 +-
 tests/data/taglib-sample.xmp                     |   13 +++
 8 files changed, 174 insertions(+), 30 deletions(-)
---
diff --git a/lib/TagLib/TagLib b/lib/TagLib/TagLib
index 15ebc4d..f631276 160000
--- a/lib/TagLib/TagLib
+++ b/lib/TagLib/TagLib
@@ -1 +1 @@
-Subproject commit 15ebc4d34996f62e9c5337694d344c8e5df11a78
+Subproject commit f631276698e5a67863458d335ce3fc6e63384348
diff --git a/src/Utils/Makefile.am b/src/Utils/Makefile.am
index e8af1a6..8f4d0fb 100644
--- a/src/Utils/Makefile.am
+++ b/src/Utils/Makefile.am
@@ -15,12 +15,15 @@ SOURCES = \
 	RecursiveFileEnumerator.cs \
 	SafeUri.cs \
 	SafeUriExtensions.cs \
+	SidecarXmpExtensions.cs \
 	Unix.cs \
 	UriExtensions.cs \
 	UriUtils.cs \
 	XdgThumbnailSpec.cs \
 	Tests/GIOTagLibFileAbstractionTests.cs \
+	Tests/ImageTestHelper.cs \
 	Tests/SafeUriTests.cs \
+	Tests/SidecarXmpExtensionsTests.cs \
 	Tests/XdgThumbnailSpecTests.cs
 
 RESOURCES =
diff --git a/src/Utils/SidecarXmpExtensions.cs b/src/Utils/SidecarXmpExtensions.cs
new file mode 100644
index 0000000..e682190
--- /dev/null
+++ b/src/Utils/SidecarXmpExtensions.cs
@@ -0,0 +1,31 @@
+using System;
+using System.IO;
+using GLib;
+using Hyena;
+using TagLib.Image;
+using TagLib.Xmp;
+
+namespace FSpot.Utils
+{
+    public static class SidecarXmpExtensions
+    {
+        /// <summary>
+        ///    Parses the XMP file identified by resource and replaces the XMP
+        ///    tag of file by the parsed data.
+        /// </summary>
+        public static void ParseXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
+        {
+            string xmp;
+            using (var stream = resource.ReadStream) {
+                using (var reader = new StreamReader (stream)) {
+                    xmp = reader.ReadToEnd ();
+                }
+            }
+
+            var tag = new XmpTag (xmp);
+            var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, true) as XmpTag;
+            xmp_tag.ReplaceFrom (tag);
+        }
+
+    }
+}
diff --git a/src/Utils/Tests/GIOTagLibFileAbstractionTests.cs b/src/Utils/Tests/GIOTagLibFileAbstractionTests.cs
index ed2d965..c4be825 100644
--- a/src/Utils/Tests/GIOTagLibFileAbstractionTests.cs
+++ b/src/Utils/Tests/GIOTagLibFileAbstractionTests.cs
@@ -14,16 +14,15 @@ namespace FSpot.Utils.Tests
     [TestFixture]
     public class GIOTagLibFileAbstractionTests
     {
-        static bool initialized = false;
-        static void Initialize () {
+		[SetUp]
+        public void Initialize () {
             GLib.GType.Init ();
-            initialized = true;
         }
 
         [Test]
         public void StraightIOTest ()
         {
-            var uri = CreateTempFile ();
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
 
             var file = File.Create (uri.AbsolutePath) as TagLib.Image.File;
             Assert.IsTrue (file != null);
@@ -37,13 +36,13 @@ namespace FSpot.Utils.Tests
             Validate (file);
             ValidateChangedMetadata (file);
 
-            DeleteTempFile (uri);
+            ImageTestHelper.DeleteTempFile (uri);
         }
 
         [Test]
         public void GIOTest ()
         {
-            var uri = CreateTempFile ();
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
 
             var res = new GIOTagLibFileAbstraction () { Uri = uri };
 
@@ -59,28 +58,7 @@ namespace FSpot.Utils.Tests
             Validate (file);
             ValidateChangedMetadata (file);
 
-            DeleteTempFile (uri);
-        }
-
-        private SafeUri CreateTempFile ()
-        {
-            if (!initialized)
-                Initialize ();
-
-            var uri = new SafeUri (Environment.CurrentDirectory + "/../tests/data/taglib-sample.jpg");
-            var file = GLib.FileFactory.NewForUri (uri);
-
-            var tmp = System.IO.Path.GetTempFileName ()+".jpg"; // hack!
-            var uri2 = new SafeUri (tmp);
-            var file2 = GLib.FileFactory.NewForUri (uri2);
-            file.Copy (file2, GLib.FileCopyFlags.Overwrite, null, null);
-            return uri2;
-        }
-
-        private void DeleteTempFile (SafeUri uri)
-        {
-            var file = GLib.FileFactory.NewForUri (uri);
-            file.Delete ();
+            ImageTestHelper.DeleteTempFile (uri);
         }
 
         private void Validate (TagLib.Image.File file)
diff --git a/src/Utils/Tests/ImageTestHelper.cs b/src/Utils/Tests/ImageTestHelper.cs
new file mode 100644
index 0000000..7e3f29e
--- /dev/null
+++ b/src/Utils/Tests/ImageTestHelper.cs
@@ -0,0 +1,30 @@
+#if ENABLE_TESTS
+using NUnit.Framework;
+using System;
+using Hyena;
+using TagLib;
+
+namespace FSpot.Utils.Tests
+{
+	public static class ImageTestHelper
+	{
+        public static SafeUri CreateTempFile (string name)
+        {
+            var uri = new SafeUri (Environment.CurrentDirectory + "/../tests/data/" + name);
+            var file = GLib.FileFactory.NewForUri (uri);
+
+            var tmp = System.IO.Path.GetTempFileName ()+".jpg"; // hack!
+            var uri2 = new SafeUri (tmp);
+            var file2 = GLib.FileFactory.NewForUri (uri2);
+            file.Copy (file2, GLib.FileCopyFlags.Overwrite, null, null);
+            return uri2;
+        }
+
+        public static void DeleteTempFile (SafeUri uri)
+        {
+            var file = GLib.FileFactory.NewForUri (uri);
+            file.Delete ();
+        }
+	}
+}
+#endif
diff --git a/src/Utils/Tests/SidecarXmpExtensionsTests.cs b/src/Utils/Tests/SidecarXmpExtensionsTests.cs
new file mode 100644
index 0000000..3aa5232
--- /dev/null
+++ b/src/Utils/Tests/SidecarXmpExtensionsTests.cs
@@ -0,0 +1,88 @@
+#if ENABLE_TESTS
+using NUnit.Framework;
+using System;
+using Hyena;
+using TagLib;
+using TagLib.IFD;
+using TagLib.IFD.Entries;
+using TagLib.IFD.Tags;
+using TagLib.Xmp;
+
+namespace FSpot.Utils.Tests
+{
+    [TestFixture]
+    public class SidecarXmpExtensionsTests
+    {
+        [SetUp]
+        public void Initialize () {
+            GLib.GType.Init ();
+        }
+
+        [Test]
+        public void ValidateWithoutSidecar ()
+        {
+            // Tests the file in its original state
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
+            var res = new GIOTagLibFileAbstraction () { Uri = uri };
+
+            var file = File.Create (res) as TagLib.Image.File;
+            Assert.IsTrue (file != null);
+
+            XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
+            // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
+            {
+                var node = xmp.NodeTree;
+                node = node.GetChild (XmpTag.MS_PHOTO_NS, "DateAcquired");
+                Assert.IsNotNull (node);
+                Assert.AreEqual ("2009-08-04T20:42:36Z", node.Value);
+                Assert.AreEqual (XmpNodeType.Simple, node.Type);
+                Assert.AreEqual (0, node.Children.Count);
+            }
+
+            Assert.AreEqual (new string [] { "Kirche Sulzbach" }, file.ImageTag.Keywords);
+
+            ImageTestHelper.DeleteTempFile (uri);
+        }
+
+        [Test]
+        public void ValidateWithSidecar ()
+        {
+            // Tests the file with a sidecar
+            var uri = ImageTestHelper.CreateTempFile ("taglib-sample.jpg");
+            var res = new GIOTagLibFileAbstraction () { Uri = uri };
+            var sidecar_uri = CopySidecarToTest (uri);
+            var sidecar_res = new GIOTagLibFileAbstraction () { Uri = sidecar_uri };
+
+            var file = File.Create (res) as TagLib.Image.File;
+            Assert.IsTrue (file != null);
+
+            // Override by sidecar file
+            file.ParseXmpSidecar (sidecar_res);
+
+            XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;
+            // Xmp.MicrosoftPhoto_1_.DateAcquired (XmpText/20) "2009-08-04T20:42:36Z"
+            {
+                var node = xmp.NodeTree;
+                node = node.GetChild (XmpTag.MS_PHOTO_NS, "DateAcquired");
+                Assert.IsNull (node);
+            }
+
+            Assert.AreEqual (new string [] { "F-Spot", "metadata", "test" }, file.ImageTag.Keywords);
+
+            ImageTestHelper.DeleteTempFile (uri);
+            ImageTestHelper.DeleteTempFile (sidecar_uri);
+        }
+
+        SafeUri CopySidecarToTest (SafeUri uri)
+        {
+            var target = uri.GetBaseUri ().Append (uri.GetFilenameWithoutExtension () + ".xmp");
+
+            var orig_uri = new SafeUri (Environment.CurrentDirectory + "/../tests/data/taglib-sample.xmp");
+            var file = GLib.FileFactory.NewForUri (orig_uri);
+            var file2 = GLib.FileFactory.NewForUri (target);
+            file.Copy (file2, GLib.FileCopyFlags.Overwrite, null, null);
+            return target;
+        }
+    }
+}
+#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 26b39c5..3ddf9d9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -5,7 +5,8 @@ EXTRA_DIST = \
 	data/f-spot-0.6.2.db \
 	data/f-spot-0.7.0-17.2.db \
 	data/f-spot-0.7.0-18.0.db \
-	data/taglib-sample.jpg
+	data/taglib-sample.jpg \
+	data/taglib-sample.xmp
 
 if ENABLE_TESTS
 
diff --git a/tests/data/taglib-sample.xmp b/tests/data/taglib-sample.xmp
new file mode 100644
index 0000000..dba2a5a
--- /dev/null
+++ b/tests/data/taglib-sample.xmp
@@ -0,0 +1,13 @@
+<x:xmpmeta xmlns:x="adobe:ns:meta/">
+	<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";>
+		<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/";>
+			<dc:subject>
+				<rdf:Bag>
+					<rdf:li>F-Spot</rdf:li>
+					<rdf:li>metadata</rdf:li>
+					<rdf:li>test</rdf:li>
+				</rdf:Bag>
+			</dc:subject>
+		</rdf:Description>
+	</rdf:RDF>
+</x:xmpmeta>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]