/// <summary> /// Initializes the component passing the environment host variable. /// </summary> /// <param name="environment">The host reference</param> public override void Initialize(IServiceProvider environment) { base.Initialize(environment); ICurrentState state; state = Host.GetService(typeof(ICurrentState)) as ICurrentState; if (state == null) { GeneratorHost.ThrowInvalidHostResponse(typeof(CurrentState)); } Unit = state.Unit; // Retrieve the current generator configuration. object service = Host.GetService(typeof(IConfigurationRetriever)); if (service == null) { GeneratorHost.ThrowInvalidHostResponse(typeof(IConfigurationRetriever)); } Configuration = ((IConfigurationRetriever)service).GetConfig("generator") as GeneratorSection; // Retrieve the current ICustomizationManager instance. service = Host.GetService(typeof(ICustomizationManager)); if (service == null) { GeneratorHost.ThrowInvalidHostResponse(typeof(ICustomizationManager)); } Retriever = (ICustomizationManager)service; }
/// <summary> /// Initializes the traversing with the XmlSchema. /// </summary> /// <param name="sourceFile">The schema file to traverse.</param> /// <returns>An <c>IVisitableComponent</c> containing all the visitable nodes.</returns> public IVisitableComponent Traverse(string sourceFile) { try { Validator val = new Validator(); using (FileStream fs = new FileStream(sourceFile, FileMode.Open)) _context = System.Xml.Schema.XmlSchema.Read(fs, new ValidationEventHandler(val.OnValidation)); if (val.HasErrors) { throw new ArgumentException("There were errors in the schema " + sourceFile + Environment.NewLine + val.Errors); } } catch (Exception ex) { throw new ArgumentException("The schema couldn't be loaded: " + sourceFile + Environment.NewLine + ex.Message, ex); } // Retrieve the current configuration from the Host. IConfigurationRetriever retriever = (IConfigurationRetriever)Host.GetService(typeof(IConfigurationRetriever)); if (retriever == null) { GeneratorHost.ThrowInvalidHostResponse(typeof(IConfigurationRetriever)); } _config = retriever.GetConfig("generator") as GeneratorSection; if (_config == null) { GeneratorHost.ThrowInvalidHostResponse("No <generator> section retrieved."); } if (!_context.IsCompiled) { _context.Compile(null); } XmlDocument doc = new XmlDocument(); doc.Load(sourceFile); VisitableSchemaRoot result = new VisitableSchemaRoot(_context, doc, _context.Id); foreach (XmlSchemaObject item in _context.Items) { if (item is XmlSchemaElement) { result.Add(Build(item as XmlSchemaElement, result)); } } return(result); }
/// <summary> /// Starts processing the file passed in the constructor. /// </summary> public void Start() { CodeCompileUnit unit; #region Initialize GeneratorHost variables GeneratorHost.Instance.Retriever = new ConfigurationRetriever( _config, ConfigurationSettings.AppPathConfig); ExtenderSection extender = (ExtenderSection) GeneratorHost.Instance.Retriever.GetConfig("extender"); if (extender == null) { GeneratorHost.ThrowInvalidHostResponse("Invalid <extender> section."); } GeneratorHost.Instance.Extender = extender; GeneratorSection options = (GeneratorSection) GeneratorHost.Instance.Retriever.GetConfig("generator"); if (options == null) { GeneratorHost.ThrowInvalidHostResponse("Invalid <generator> section."); } GeneratorHost.Instance.GeneratorOptions = options; // If the configured traverser implements INotifier, append the progress event handler. if (GeneratorHost.Instance.Extender.Traverser is INotifier) { ((INotifier)GeneratorHost.Instance.Extender.Traverser).Progress += new ProgressEventHandler(OnBubbleProgress); } GeneratorHost.Instance.InitSchema(); #endregion RunnerSection section = (RunnerSection) GeneratorHost.Instance.Retriever.GetConfig("runner"); if (section == null) { OnProgress("No <runner> section found to process!"); return; } // Load the ordered list of files and run the process. foreach (DictionaryEntry sch in section.Sources) { //TODO: move TargetAssembly setting to each source to run. //TODO: maybe check to include all sources in a single assembly? RunnerSource source = sch.Value as RunnerSource; // Reinitialize the current unit. #region Order the customizations in a sorted list. SortedList indexes = new SortedList(); ArrayList files = new ArrayList(); foreach (DictionaryEntry entry in section.Customizations) { RunnerCustomization cust = entry.Value as RunnerCustomization; files = indexes[cust.RunOrder] as ArrayList; if (files == null) { files = new ArrayList(); indexes.Add(cust.RunOrder, files); } files.Add(cust.File); } foreach (DictionaryEntry entry in source.Customizations) { RunnerCustomization cust = entry.Value as RunnerCustomization; files = indexes[cust.RunOrder] as ArrayList; if (files == null) { files = new ArrayList(); indexes.Add(cust.RunOrder, files); } files.Add(cust.File); } #endregion #region Execute the ordered entries foreach (DictionaryEntry entry in indexes) { unit = new CodeCompileUnit(); GeneratorHost.Instance.Manager.Initialize(entry.Value as ArrayList); GeneratorHost.Instance.State.Init(source, unit); RunSource(source); GenerateOutput(unit, source); } #endregion // If no customizations are defined, run the process anyway (empty structure). if (indexes.Count == 0) { unit = new CodeCompileUnit(); GeneratorHost.Instance.Manager.Initialize(new ArrayList()); GeneratorHost.Instance.State.Init(source, unit); RunSource(source); GenerateOutput(unit, source); } OnProgress("Finished!"); } }