/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Model.Importer.Native { using System; using System.IO; using System.Xml.Serialization; using OpenTraffic.Model.Route; public class NativeFileImporter : IImporterModel, IExporter { public string GetName() { return "OpenTraffic"; } public string GetExtensions() { return "OpenTraffic (*.otx);(*.otx.gz)|*.OTX;*.OTX.GZ"; } public void LoadFile(FileInfo sr, OpenTraffic.Model.TrafficModel nm) { if (sr != null && nm != null) { Lookup.Get().Clear(); XmlSerializer reader = new XmlSerializer(nm.GetType()); Stream file = null; try { if (sr.Extension == ".gz") { file = new System.IO.Compression.GZipStream(sr.OpenRead(), System.IO.Compression.CompressionMode.Decompress); } else { file = sr.OpenRead(); } Lookup.Get().Add(0, sr); TrafficModel m = (TrafficModel)reader.Deserialize(file); nm.LinkCategories = m.LinkCategories; nm.VehicleClasses = m.VehicleClasses; nm.TrafficModelConnectors = m.TrafficModelConnectors; nm.TrafficGenerator = m.TrafficGenerator; nm.IntersectionNodes = m.IntersectionNodes; nm.TrafficModelLinks = m.TrafficModelLinks; nm.Routes = m.Routes; nm.SpeedFlows = m.SpeedFlows; nm.StopCriterias = m.StopCriterias; nm.ResultFactory = m.ResultFactory; nm.TrafficZones = m.TrafficZones; nm.BackgroundImages = m.BackgroundImages; nm.ResultStore = m.ResultStore; nm.StartTime = m.StartTime; nm.StopTime = m.StopTime; nm.SignalPlans = m.SignalPlans; nm.Flares = m.Flares; nm.TimeSlices = m.TimeSlices; nm.PostLoad(); foreach (RouteContainer r in nm.Routes) { RouteContainer.NextId = Math.Max(RouteContainer.NextId, r.Id + 1); } } finally { if (file != null) { file.Close(); } } } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] private static TrafficModel Deserialize(byte[] data) { Lookup.Get().Clear(); try { XmlSerializer reader = new XmlSerializer(typeof(TrafficModel)); TrafficModel m = null; MemoryStream ms = null; try { ms = new MemoryStream(data); Stream file = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress); m = (TrafficModel)reader.Deserialize(file); m.PostLoad(); } finally { if (ms != null) { ms.Close(); } } return m; } catch (Exception exp) { throw new Exception("Error loading network file", exp); } } public static TrafficModel DeserializeRaw(byte[] data) { Lookup.Get().Clear(); try { XmlSerializer reader = new XmlSerializer(typeof(TrafficModel)); TrafficModel m = null; using (MemoryStream ms = new MemoryStream(data)) { m = (TrafficModel)reader.Deserialize(ms); m.PostLoad(); } return m; } catch (Exception exp) { throw new Exception("Error loading network file", exp); } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] private static byte[] Serialize(OpenTraffic.Model.TrafficModel m) { byte[] ba = null; if (m != null) { using (MemoryStream ms = new MemoryStream()) { Stream file = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress); XmlSerializer ser = new XmlSerializer(m.GetType()); // ser.Formatting = Formatting.None; ser.Serialize(file, m); ba = ms.ToArray(); } } return ba; } public static byte[] SerializeRaw(OpenTraffic.Model.TrafficModel m) { byte[] ba = null; if (m != null) { using (MemoryStream ms = new MemoryStream()) { XmlSerializer ser = new XmlSerializer(m.GetType()); ser.Serialize(ms, m); ba = ms.ToArray(); } } return ba; } public void SaveFile(FileInfo sr, OpenTraffic.Model.TrafficModel m) { if (sr != null && m != null) { Stream file = null; try { if (sr.Extension == ".gz") { // XmlTextWriter file = file = new System.IO.Compression.GZipStream(sr.Create(), System.IO.Compression.CompressionMode.Compress); // file.Formatting = Formatting.Indented; } else { file = sr.Create(); } XmlSerializer ser = new XmlSerializer(m.GetType()); ser.Serialize(file, m); } finally { if (file != null) { file.Close(); } } } } } }