Exemplo n.º 1
0
        public static ITimeSpaceComponent OmiDeserializeAndInitialize(LinkableComponentComplexType omi, DirectoryInfo relativePath)
        {
            if (_assemblyLoader == null)
            {
                _assemblyLoader = new AssemblyLoader();
            }

            object obj = _assemblyLoader.Load(omi.Assembly, omi.Type, relativePath);

            if (!(obj is ITimeSpaceComponent))
            {
                throw new InvalidCastException("ILinkableComponent");
            }

            ITimeSpaceComponent iLC = (ITimeSpaceComponent)obj;

            Dictionary <string, IArgument> args
                = new Dictionary <string, IArgument>();

            if (iLC.Arguments != null)
            {
                foreach (IArgument iArg in iLC.Arguments)
                {
                    args.Add(iArg.Id, iArg);
                }
            }

            if (omi.Arguments != null)
            {
                foreach (LinkableComponentComplexTypeArgument omiArg in omi.Arguments)
                {
                    if (args.ContainsKey(omiArg.Key))
                    {
                        if (args[omiArg.Key].ValueType == typeof(FileInfo) &&
                            !Path.IsPathRooted(omiArg.Value))
                        {
                            omiArg.Value = Path.Combine(relativePath.FullName, omiArg.Value);
                        }
                        else if (args[omiArg.Key].ValueType == typeof(DirectoryInfo) &&
                                 !Path.IsPathRooted(omiArg.Value))
                        {
                            omiArg.Value = Path.Combine(relativePath.FullName, omiArg.Value);
                        }

                        args[omiArg.Key].ValueAsString = omiArg.Value;
                    }
                    else
                    {// add anyway as string
                        args.Add(omiArg.Key, new ArgumentString(omiArg.Key, omiArg.Value));
                    }
                }
            }

            // Suspect this could come in handy, so added
            // args.Add("OMI_PATH", new ArgumentDirectoryInfo("OMI_PATH", relativePath));

            iLC.Initialize(new List <IArgument>(args.Values));

            return(iLC);
        }
Exemplo n.º 2
0
        public static ITimeSpaceComponent OmiDeserializeAndInitialize(string xml, DirectoryInfo omiPath)
        {
            XmlSerializer serializer =
                new XmlSerializer(typeof(LinkableComponentComplexType));

            // TODO: Validate against xsd first?

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreComments               = true;
            settings.IgnoreWhitespace             = true;
            settings.IgnoreProcessingInstructions = true;
            settings.ConformanceLevel             = ConformanceLevel.Fragment;

            // LinkableComponentComplexType comp = new LinkableComponentComplexType();


            // serializer.Serialize(new StreamWriter("C:\\Users\\Caleb\\Desktop\\Projects\\OpenMI\\1.4.0\\Oatc\\src\\csharp\\Examples\\ModelComponents\\SpatialModels\\UnitTest\\test.xml"), comp);
            //try
            //{
            using (XmlReader reader = XmlReader.Create(new StringReader(xml), settings))
            {
                LinkableComponentComplexType omi =
                    (LinkableComponentComplexType)serializer.Deserialize(reader);
                return(OmiDeserializeAndInitialize(omi, omiPath));
            }
            //}
            //catch (Exception e)
            //{
            //    // TODO: Do we want to allow unspecified xmlns?
            //    if (e.InnerException != null
            //        && e.InnerException.Message.Contains("<LinkableComponent xmlns="))
            //        throw new XmlException("xmlns=\"http://www.openmi.org\" missing in <LinkableComponent />", e);

            //    throw;
            //}
        }