public FormatConfigurationEntry(ExtendedTypeDefinition typeDefinition) : base("*")
 {
     if (typeDefinition == null)
     {
         throw PSTraceSource.NewArgumentNullException("typeDefinition");
     }
     this._typeDefinition = typeDefinition;
 }
 internal bool LoadFormattingData(ExtendedTypeDefinition typeDefinition, TypeInfoDataBase db, MshExpressionFactory expressionFactory)
 {
     if (typeDefinition == null)
     {
         throw PSTraceSource.NewArgumentNullException("typeDefinition");
     }
     if (typeDefinition.TypeName == null)
     {
         throw PSTraceSource.NewArgumentNullException("typeDefinition.TypeName");
     }
     if (db == null)
     {
         throw PSTraceSource.NewArgumentNullException("db");
     }
     if (expressionFactory == null)
     {
         throw PSTraceSource.NewArgumentNullException("expressionFactory");
     }
     base.expressionFactory = expressionFactory;
     base.ReportTrace("loading ExtendedTypeDefinition started");
     try
     {
         this.LoadData(typeDefinition, db);
     }
     catch (TooManyErrorsException)
     {
         return false;
     }
     catch (Exception exception)
     {
         base.ReportErrorForLoadingFromObjectModel(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFormattingData, typeDefinition.TypeName, exception.Message), typeDefinition.TypeName);
         throw;
     }
     if (base.HasErrors)
     {
         return false;
     }
     base.ReportTrace("ExtendedTypeDefinition loaded with no errors");
     return true;
 }
 private void LoadData(ExtendedTypeDefinition typeDefinition, TypeInfoDataBase db)
 {
     if (typeDefinition == null)
     {
         throw PSTraceSource.NewArgumentNullException("viewDefinition");
     }
     if (db == null)
     {
         throw PSTraceSource.NewArgumentNullException("db");
     }
     int num = 0;
     foreach (FormatViewDefinition definition in typeDefinition.FormatViewDefinition)
     {
         ViewDefinition item = this.LoadViewFromObjectModle(typeDefinition.TypeName, definition, num++);
         if (item != null)
         {
             base.ReportTrace(string.Format(CultureInfo.InvariantCulture, "{0} view {1} is loaded from the 'FormatViewDefinition' at index {2} in 'ExtendedTypeDefinition' with type name {3}", new object[] { ControlBase.GetControlShapeName(item.mainControl), item.name, num - 1, typeDefinition.TypeName }));
             db.viewDefinitionsSection.viewDefinitionList.Add(item);
         }
     }
 }
        /// <summary>
        /// load the content of the ExtendedTypeDefinition instance into the db.
        /// Only support following view controls:
        ///     TableControl
        ///     ListControl
        ///     WideControl
        ///     CustomControl
        /// </summary>
        /// <param name="typeDefinition">ExtendedTypeDefinition instances to load from, cannot be null</param>
        /// <param name="db">instance of the database to load into</param>
        /// <param name="isForHelpOutput">true if the formatter is used for formatting help objects</param>
        private void LoadData(ExtendedTypeDefinition typeDefinition, TypeInfoDataBase db, bool isForHelpOutput)
        {
            if (typeDefinition == null)
                throw PSTraceSource.NewArgumentNullException("viewDefinition");

            if (db == null)
                throw PSTraceSource.NewArgumentNullException("db");

            int viewIndex = 0;
            foreach (FormatViewDefinition formatView in typeDefinition.FormatViewDefinition)
            {
                ViewDefinition view = LoadViewFromObjectModel(typeDefinition.TypeNames, formatView, viewIndex++);
                if (view != null)
                {
                    ReportTrace(string.Format(CultureInfo.InvariantCulture,
                        "{0} view {1} is loaded from the 'FormatViewDefinition' at index {2} in 'ExtendedTypeDefinition' with type name {3}",
                        ControlBase.GetControlShapeName(view.mainControl),
                        view.name, viewIndex - 1, typeDefinition.TypeName));

                    // we are fine, add the view to the list
                    db.viewDefinitionsSection.viewDefinitionList.Add(view);

                    view.loadingInfo = this.LoadingInfo;
                    view.isHelpFormatter = isForHelpOutput;
                }
            }
        }
        /// <summary>
        /// entry point for the loader algorithm to load formatting data from ExtendedTypeDefinition
        /// </summary>
        /// <param name="typeDefinition">the ExtendedTypeDefinition instance to load formatting data from</param>
        /// <param name="db">database instance to load the formatting data into</param>
        /// <param name="expressionFactory">expression factory to validate the script block</param>
        /// <param name="isBuiltInFormatData">do we implicitly trust the script blocks (so they should run in full langauge mode)?</param>
        /// <param name="isForHelp">true when the view is for help output</param>
        /// <returns></returns>
        internal bool LoadFormattingData(
            ExtendedTypeDefinition typeDefinition,
            TypeInfoDataBase db,
            MshExpressionFactory expressionFactory,
            bool isBuiltInFormatData,
            bool isForHelp)
        {
            if (typeDefinition == null)
                throw PSTraceSource.NewArgumentNullException("typeDefinition");
            if (typeDefinition.TypeName == null)
                throw PSTraceSource.NewArgumentNullException("typeDefinition.TypeName");
            if (db == null)
                throw PSTraceSource.NewArgumentNullException("db");
            if (expressionFactory == null)
                throw PSTraceSource.NewArgumentNullException("expressionFactory");

            this.expressionFactory = expressionFactory;
            this.ReportTrace("loading ExtendedTypeDefinition started");

            try
            {
                this.SetLoadingInfoIsFullyTrusted(isBuiltInFormatData);
                this.SetLoadingInfoIsProductCode(isBuiltInFormatData);
                this.LoadData(typeDefinition, db, isForHelp);
            }
            catch (TooManyErrorsException)
            {
                // already logged an error before throwing
                return false;
            }
            catch (Exception e) // will rethrow
            {
                //Error in formatting data "{0}": {1}
                this.ReportErrorForLoadingFromObjectModel(
                    StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFormattingData, typeDefinition.TypeName, e.Message), typeDefinition.TypeName);
                throw;
            }
            if (this.HasErrors)
            {
                return false;
            }

            this.ReportTrace("ExtendedTypeDefinition loaded with no errors");
            return true;
        }
        /// <summary>
        /// Takes out the content from the database and writes them
        /// out
        /// </summary>
        protected override void ProcessRecord()
        {
            bool writeOldWay = PowerShellVersion == null ||
                               PowerShellVersion.Major < 5 ||
                               PowerShellVersion.Build < 11086;

            TypeInfoDataBase db = this.Context.FormatDBManager.Database;

            List<ViewDefinition> viewdefinitions = db.viewDefinitionsSection.viewDefinitionList;
            Dictionary<string, List<string>> typeGroupMap = GetTypeGroupMap(db.typeGroupSection.typeGroupDefinitionList);

            var typedefs = new Dictionary<ConsolidatedString, List<FormatViewDefinition>>(ConsolidatedString.EqualityComparer);

            foreach (ViewDefinition definition in viewdefinitions)
            {
                if (definition.isHelpFormatter)
                    continue;

                var consolidatedTypeName = CreateConsolidatedTypeName(definition, typeGroupMap);

                if (!ShouldGenerateView(consolidatedTypeName))
                    continue;

                PSControl control;

                var tableControlBody = definition.mainControl as TableControlBody;
                if (tableControlBody != null)
                {
                    control = new TableControl(tableControlBody, definition);
                }
                else
                {
                    var listControlBody = definition.mainControl as ListControlBody;
                    if (listControlBody != null)
                    {
                        control = new ListControl(listControlBody, definition);
                    }
                    else
                    {
                        var wideControlBody = definition.mainControl as WideControlBody;
                        if (wideControlBody != null)
                        {
                            control = new WideControl(wideControlBody, definition);
                            if (writeOldWay)
                            {
                                // Alignment was added to WideControl in V2, but wasn't
                                // used.  It was removed in V5, but old PowerShell clients
                                // expect this property or fail to rehydrate the remote object.
                                var psobj = new PSObject(control);
                                psobj.Properties.Add(new PSNoteProperty("Alignment", 0));
                            }
                        }
                        else
                        {
                            var complexControlBody = (ComplexControlBody)definition.mainControl;
                            control = new CustomControl(complexControlBody, definition);
                        }
                    }
                }

                // Older version of PowerShell do not know about something in the control, so
                // don't return it.
                if (writeOldWay && !control.CompatibleWithOldPowerShell())
                    continue;

                var formatdef = new FormatViewDefinition(definition.name, control, definition.InstanceId);

                List<FormatViewDefinition> viewList;
                if (!typedefs.TryGetValue(consolidatedTypeName, out viewList))
                {
                    viewList = new List<FormatViewDefinition>();
                    typedefs.Add(consolidatedTypeName, viewList);
                }
                viewList.Add(formatdef);
            }// foreach(ViewDefinition...

            // write out all the available type definitions
            foreach (var pair in typedefs)
            {
                var typeNames = pair.Key;

                if (writeOldWay)
                {
                    foreach (var typeName in typeNames)
                    {
                        var etd = new ExtendedTypeDefinition(typeName, pair.Value);
                        WriteObject(etd);
                    }
                }
                else
                {
                    var etd = new ExtendedTypeDefinition(typeNames[0], pair.Value);
                    for (int i = 1; i < typeNames.Count; i++)
                    {
                        etd.TypeNames.Add(typeNames[i]);
                    }
                    WriteObject(etd);
                }
            }
        }
 internal PSSnapInTypeAndFormatErrors(string psSnapinName, ExtendedTypeDefinition typeDefinition)
 {
     this.psSnapinName = psSnapinName;
     this.typeDefinition = typeDefinition;
     this.errors = new Collection<string>();
 }
Exemple #8
0
        private static void LoadFormatDataHelper(
            ExtendedTypeDefinition formatData,
            MshExpressionFactory expressionFactory, List<XmlLoaderLoggerEntry> logEntries, ref bool success,
            PSSnapInTypeAndFormatErrors file, TypeInfoDataBase db,
            bool isBuiltInFormatData,
            bool isForHelp)
        {
            using (TypeInfoDataBaseLoader loader = new TypeInfoDataBaseLoader())
            {
                if (!loader.LoadFormattingData(formatData, db, expressionFactory, isBuiltInFormatData, isForHelp))
                    success = false;

                foreach (XmlLoaderLoggerEntry entry in loader.LogEntries)
                {
                    // filter in only errors from the current file...
                    if (entry.entryType == XmlLoaderLoggerEntry.EntryType.Error)
                    {
                        string mshsnapinMessage = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError,
                            file.PSSnapinName, entry.message);
                        file.Errors.Add(mshsnapinMessage);
                    }
                }
                // now aggregate the entries...
                logEntries.AddRange(loader.LogEntries);
            }
        }