public void Execute() { System.Diagnostics.Trace.WriteLine("\r\nLoading MIF Files", "information"); // If no mif repository exists in the pipeline data segment, create one if (!context.Data.ContainsKey("MIF20Repository")) { context.Data.Add("MIF20Repository", new PackageRepository()); } PackageRepository repository = (PackageRepository)context.Data["MIF20Repository"]; List <PackageArtifact> loadedPackages = new List <PackageArtifact>(); // Preserve the stack Stack <String> nonLoadedFiles = new Stack <string>(); Type[] modelTypes = new Type[] { typeof(StaticModel.Flat.GlobalStaticModel), typeof(DynamicModel.GlobalInteraction), typeof(Vocabulary.GlobalCodeSystem), typeof(Vocabulary.GlobalCodeSystemSupplement), typeof(Vocabulary.GlobalValueSet), typeof(Vocabulary.GlobalVocabularyModel), typeof(Interfaces.CommonModelElementPackage), typeof(SerializedStaticModel) }; MifTransformer transformer = new MifTransformer(); // Load the mif files into a repository structure ;) while (context.InputFiles.Count > 0) { string fileName = context.InputFiles.Pop(); Stream fs = null; System.Diagnostics.Trace.Write(".", "information"); try { // JF: Revised so that we can load multiple versions of the MIF using compiler transforms fs = transformer.GetFile(fileName); if (fs == null) { continue; } // Validate if (fs.CanSeek) { ValidateMifFile(fs, fileName); fs.Seek(0, SeekOrigin.Begin); } bool loaded = false; foreach (Type t in modelTypes) { try { fs.Seek(0, SeekOrigin.Begin); XmlSerializer xsz = new XmlSerializer(t); Object o = xsz.Deserialize(fs); if (o == null) { throw new NotSupportedException("Loader didn't recognize data as this type"); } else { PackageArtifact model = o as PackageArtifact; if (model.PackageLocation == null) { throw new InvalidDataException("This model has no package location, aborting pipeline load"); } loadedPackages.Add(o as PackageArtifact); loaded = true; break; } } catch (InvalidDataException e) { throw new InvalidDataException(String.Format("Could not load '{0}'", fileName), e); } catch (InvalidOperationException e) { if (!e.Message.Contains(" (1,") && !e.Message.Contains(" (2,")) { System.Diagnostics.Trace.Write(String.Format("{0} -> {1}", e.Message, e.InnerException != null ? e.InnerException.Message : "N/A"), "warn"); } if (e.InnerException != null && e.InnerException is OperationCanceledException) { throw e.InnerException; } } catch (OperationCanceledException e) { throw e; } catch (Exception) { //System.Diagnostics.Trace.Write(e.Message); } } if (!loaded) { throw new NotSupportedException(String.Format("File format not recognized : '{0}'", fileName)); } } catch (InvalidDataException e) { throw e; } catch (OperationCanceledException e) { throw new OperationCanceledException(String.Format("Can't load MIF file '{0}' into repository", fileName), e); } catch (XmlSchemaException e) { throw e; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(string.Format("Can't load {0}...", fileName), "warn1"); nonLoadedFiles.Push(fileName); System.Diagnostics.Trace.WriteLine(e.ToString(), "warn1"); } if (fs != null) { fs.Close(); } } // Sort loadedPackages.Sort(repository); foreach (PackageArtifact model in loadedPackages) { repository.Add(model); } // Warn if a transform has been applied if (transformer.DidTransform) { Trace.WriteLine("\r\n--!! TRANSFORM APPLIED, MIF CONTENTS IN PIPELINE MAY NOT MATCH PHYSICAL FILE !!--\r\n", "warn"); } // Hand off models we couldn't load back to the input file stack so another loader can load it while (nonLoadedFiles.Count > 0) { context.InputFiles.Push(nonLoadedFiles.Pop()); } }
public void Execute() { System.Diagnostics.Trace.WriteLine("\r\nLoading MIF Files", "information"); // If no mif repository exists in the pipeline data segment, create one if (!context.Data.ContainsKey("MIF20Repository")) context.Data.Add("MIF20Repository", new PackageRepository()); PackageRepository repository = (PackageRepository)context.Data["MIF20Repository"]; List<PackageArtifact> loadedPackages = new List<PackageArtifact>(); // Preserve the stack Stack<String> nonLoadedFiles = new Stack<string>(); Type[] modelTypes = new Type[] { typeof(StaticModel.Flat.GlobalStaticModel), typeof(DynamicModel.GlobalInteraction), typeof(Vocabulary.GlobalCodeSystem), typeof(Vocabulary.GlobalCodeSystemSupplement), typeof(Vocabulary.GlobalValueSet), typeof(Vocabulary.GlobalVocabularyModel), typeof(Interfaces.CommonModelElementPackage), typeof(SerializedStaticModel) }; MifTransformer transformer = new MifTransformer(); // Load the mif files into a repository structure ;) while (context.InputFiles.Count > 0) { string fileName = context.InputFiles.Pop(); Stream fs = null; System.Diagnostics.Trace.Write(".","information"); try { // JF: Revised so that we can load multiple versions of the MIF using compiler transforms fs = transformer.GetFile(fileName); if (fs == null) continue; // Validate if (fs.CanSeek) { ValidateMifFile(fs, fileName); fs.Seek(0, SeekOrigin.Begin); } bool loaded = false; foreach (Type t in modelTypes) { try { fs.Seek(0, SeekOrigin.Begin); XmlSerializer xsz = new XmlSerializer(t); Object o = xsz.Deserialize(fs); if (o == null) throw new NotSupportedException("Loader didn't recognize data as this type"); else { PackageArtifact model = o as PackageArtifact; if (model.PackageLocation == null) throw new InvalidDataException("This model has no package location, aborting pipeline load"); loadedPackages.Add(o as PackageArtifact); loaded = true; break; } } catch (InvalidDataException e) { throw new InvalidDataException(String.Format("Could not load '{0}'", fileName), e); } catch (InvalidOperationException e) { if (!e.Message.Contains(" (1,") && !e.Message.Contains(" (2,")) System.Diagnostics.Trace.Write(String.Format("{0} -> {1}", e.Message, e.InnerException != null ? e.InnerException.Message : "N/A"), "warn"); if (e.InnerException != null && e.InnerException is OperationCanceledException) throw e.InnerException; } catch (OperationCanceledException e) { throw e; } catch (Exception) { //System.Diagnostics.Trace.Write(e.Message); } } if (!loaded) { throw new NotSupportedException(String.Format("File format not recognized : '{0}'", fileName)); } } catch (InvalidDataException e) { throw e; } catch (OperationCanceledException e) { throw new OperationCanceledException(String.Format("Can't load MIF file '{0}' into repository", fileName), e); } catch (XmlSchemaException e) { throw e; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(string.Format("Can't load {0}...", fileName), "warn1"); nonLoadedFiles.Push(fileName); System.Diagnostics.Trace.WriteLine(e.ToString(), "warn1"); } if (fs != null) fs.Close(); } // Sort loadedPackages.Sort(repository); foreach (PackageArtifact model in loadedPackages) repository.Add(model); // Warn if a transform has been applied if (transformer.DidTransform) Trace.WriteLine("\r\n--!! TRANSFORM APPLIED, MIF CONTENTS IN PIPELINE MAY NOT MATCH PHYSICAL FILE !!--\r\n", "warn"); // Hand off models we couldn't load back to the input file stack so another loader can load it while (nonLoadedFiles.Count > 0) context.InputFiles.Push(nonLoadedFiles.Pop()); }