/[xmltv_parser]/trunk/libxmltv/Core/ExtraMetaData.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/ExtraMetaData.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/ExtraMetaData.cs	2013/03/10 13:01:09	109
+++ trunk/libxmltv/Core/ExtraMetaData.cs	2013/03/10 14:28:57	110
@@ -4,34 +4,85 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.Xml.Linq;
+using System.IO;
+using System.Xml.Serialization;
+using System.Xml;
 
 namespace libxmltv.Core
 {
-    internal class ExtraMetaData : ExtraMetaData<XElement>, IExtraMetaData
+    [Serializable]
+    internal class ExtraMetaData : IExtraMetaData
     {
-        public ExtraMetaData() : base() { }
-        public ExtraMetaData(XElement value) : base(value) { }
-    }
-    internal class ExtraMetaData<TValue> : IExtraMetaData<TValue>
-        where TValue: XElement
-    {
-        public ExtraMetaData() : this(default(TValue)) { }
-        public ExtraMetaData(TValue value) 
+        public ExtraMetaData() : this(string.Empty, string.Empty) { }
+        public ExtraMetaData(XElement node) : this()
         {
-            if (value == null) { throw new ArgumentNullException("value", "cannot be null"); }
-            if (value.Name == null) { throw new ArgumentNullException("value.Name", "cannot be null"); }
-            this.Name = value.Name.ToString(); 
-            this.Value = value; 
+            //throw new NotImplementedException("ExtraMetaData from XElement as not been implemented");
+            if (node == null) { throw new ArgumentNullException("value", "cannot be null"); }
+            if (node.Name == null) { throw new ArgumentNullException("value.Name", "cannot be null"); }
+            this.Name = node.Name.ToString();
+            this.Value = XElementSerializer<string>.FromXElement(node); 
+        }
+        public ExtraMetaData(string name, string value)
+        {
+            this.Name = name;
+            this.Value = value;
         }
-        #region IExtraMetaData<TKey,TValue> memebers
-        #endregion
-        private string _name;
-        public string Name { get { return _name; } protected set { _name = value; } }
-        private TValue _value;
-        public TValue Value { get { return _value; } protected set { _value = value; } }
         public override string ToString()
         {
             return string.Format("{0}", this.Name);
         }
+        private string _Name;
+        public string Name { get { return _Name; } set { _Name = value; } }
+        private string _Value;
+        public string Value { get { return _Value; } set { _Value = value; } }
+
+        public XElement AsXElement()
+        {
+            //throw new NotImplementedException("ExtraMetaData.AsXElement() has not been implemented");
+            return XElementSerializer<string>.ToXElement(Value); 
+        }
+
+        #region sub-classes
+        private static class XElementSerializer<TValue>
+        {
+            public static XElement ToXElement(TValue value)
+            {
+                if (value == null)
+                {
+                    throw new ArgumentNullException("value", string.Format("cannot convert a null '{0}' value to '{1}'.", typeof(TValue).Name, typeof(XElement).Name));
+                }
+                XElement element = null;
+                try
+                {
+                    element = XElement.Parse(value.ToString());
+                }
+                catch (Exception ex)
+                {
+                    throw ex;
+                }
+
+
+
+                return element;
+            }
+            public static TValue FromXElement(XElement node)
+            {
+                if (node == null)
+                {
+                    throw new ArgumentNullException("node", string.Format("cannot convert a null '{0}' value to {1}.", typeof(XElement).Name, typeof(TValue).Name));
+                }
+                TValue value = default(TValue);                
+                object raw_object = new object();
+                using (var ms = new MemoryStream())
+                {
+                    using (var tw = XmlWriter.Create(ms)) { node.WriteTo(tw); tw.Flush(); tw.Close(); }
+                    ms.Seek(0, SeekOrigin.Begin);
+                    using (var sr = new StreamReader(ms)) { raw_object = sr.ReadToEnd(); }
+                }
+                value = (TValue)Convert.ChangeType(raw_object, typeof(TValue));
+                return value;
+            }
+        }
+        #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22