// clsXML.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;
using System.Diagnostics;
//using System.Xml.

namespace LibControlLogic
{
    /// <summary>
    /// Summary description for clsXML.
    /// </summary>
    /// <remarks>None</remarks>
    public class clsXML
    {
        #region Variable declaration

        /// <summary>
        /// Reference to a new binary formatter.
        /// </summary>
        /// <remarks>None</remarks>
        private readonly BinaryFormatter bformatter = new BinaryFormatter();

        //private  bformatter = new BinaryFormatter();	

        #endregion

        #region Structures

        #endregion

        #region Functions

        /// <summary>
        /// Deserialize a binary file
        /// </summary>
        /// <param name="IOFileName">The filename to deserialize</param>
        /// <returns>The deserialized object</returns>
        /// <remarks>None</remarks>
        public object DeserializeBinaryIOFile(string IOFileName)
        {
            try
            {
                //Deserialize the text file
                Stream stream = new FileStream(IOFileName, FileMode.Open, FileAccess.Read, FileShare.None);
                object DeserializedObject = bformatter.Deserialize(stream);
                stream.Close();

                return DeserializedObject;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Serialize a binary object
        /// </summary>
        /// <param name="IOFileName">Filename to serialize to</param>
        /// <returns>Serialized object</returns>
        /// <remarks>None</remarks>
        public object SerializeBinaryObject(string IOFileName)
        {
            try
            {
                //serialize the text file
                object obj = new object();
                Stream stream = new FileStream(IOFileName, FileMode.Open, FileAccess.Read, FileShare.None);
                bformatter.Serialize(stream, obj);
                stream.Close();

                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Serialize a object to XML string
        /// </summary>
        /// <param name="obj">The object to serialize</param>
        /// <returns>returns the xml as string</returns>
        public string SerializeXMLObject(object obj)
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(obj.GetType());
                MemoryStream ms = new MemoryStream();
                xs.Serialize(ms, obj);

                // create string
                StreamReader sr = new StreamReader(ms);
                ms.Seek(0, 0);
                string retval = sr.ReadToEnd();
                sr.Close();
                ms.Close();


                return retval;
            }
            catch (Exception)
            {
                return "Nothing Error";
            }
        }

        public object DeserializeXMLObject(string XMLData, object obj)
        {
            try
            {
                //Deserialize the xml string


                XmlSerializer xs = new XmlSerializer(obj.GetType());
                StringReader stringReader = new StringReader(XMLData);

                XmlTextReader xmlReader = new XmlTextReader(stringReader);
                object retval = xs.Deserialize(xmlReader);
                //object obj = xs.Deserialize(stringReader);

                xmlReader.Close();
                stringReader.Close();
                return retval;
            }
            catch (Exception)
            {
                Debug.WriteLine("xml deserialize error");
                return null;
            }
        }

        #endregion

        #region Menu handlers

        #endregion

        #region Custom Windows Form Designer generated code

        #endregion

        #region Events

        #endregion

        #region Enumerations

        #region CharacteristicType enum

        /// <summary>
        /// The searchitemcharacteristics type
        /// Either the characteristics name will be searched or the asosiated value of an searchable item
        /// </summary>
        /// <remarks>None</remarks>
        public enum CharacteristicType
        {
            Name = 0,
            Value = 1
        }

        #endregion

        #region SerializeDirection enum

        /// <summary>
        /// The filtermethod used for the searchitemcharacteristics specified as a search criteria item
        /// </summary>
        public enum SerializeDirection
        {
            Serialze = 0,
            Deselilize = 1
        }

        #endregion

        #endregion
    }
}