예제 #1
0
        public void ReadInvalidXml()
        {
            string testXml = @"
<Person>
  <FirstName>z</FirstName>
  <Address>
    <City>NY</City>
    <Unknown>1</Unknown>
  </Address>
</Person>";

            IObjectSchema     personSchema     = new PersonSchema().GetObjectSchema();
            var               container        = XDocument.Parse(testXml).ParseXmlToContainer(personSchema);
            IXmlParserContext xmlParserContext = container.GetMetadata <IXmlParserContext>();

            var validationRules = personSchema.Properties.GetValidationRules().ToArray();
            var messages        = container.Validate(validationRules).ToArray();

            messages.Should().HaveCount(2);
            messages[0].FormattedMessage.Should().Be("FirstName length should be greater then 1 but was 1");
            messages[1].FormattedMessage.Should().Be("LastName is marked as required but is not exists.");

            IObjectSchema addressSchemaStatic = personSchema.GetProperty("Address") !.GetSchema() !.ToObjectSchema();

            addressSchemaStatic.Properties.Should().HaveCount(2);

            IObjectSchema addressSchemaReal = xmlParserContext.GetSchema(personSchema.GetProperty("Address")).ToObjectSchema();

            addressSchemaReal.Properties.Should().HaveCount(3);

            IProperty[] notFromSchema = addressSchemaReal.GetPropertiesNotFromSchema().ToArray();
            notFromSchema.Should().HaveCount(1);
            notFromSchema[0].Name.Should().Be("Unknown");
            notFromSchema[0].Type.Should().Be(typeof(string));
        }
예제 #2
0
 private void AddTopSchemaTupleItem(IObjectSchema schema, SchemaInfoTuple schemaInfo)
 {
     if (!this.TopObjectSchemaInfoTupleTable.ContainsKey(schema))
     {
         this.TopObjectSchemaInfoTupleTable.Add(schema, schemaInfo);
     }
 }
예제 #3
0
 private void WriteCatalogRow(ISheet sheet, IObjectSchema schema, Int32 num, Int32 row, Int32 startCol)
 {
     sheet.SetCellValue(row, startCol, num);
     startCol += ExcelTemplateFormat.CatalogNumColLength;
     sheet.SetCellValue(row, startCol, schema.Name);
     startCol += ExcelTemplateFormat.CatalogTableNameColLength;
     sheet.SetCellValue(row, startCol, schema.Explain);
 }
예제 #4
0
        private TreeNode CreateTreeNode(IObjectSchema objectSchema)
        {
            TreeNode node = new TreeNode(objectSchema.Name);

            node.ToolTipText = objectSchema.Explain;
            node.Tag         = objectSchema;
            return(node);
        }
예제 #5
0
        private void ShowSourceTopSchema(IObjectSchema schemaData)
        {
            if (schemaData != null)
            {
                var schemaInfo = this.TopObjectSchemaInfoTupleTable[schemaData];

                switch (schemaInfo.SchemaType)
                {
                case SchemaType.Table:
                {
                    this._tabSchemaShow.SelectedTab       = this._pageColumnSchema;
                    this._pnlSchemaDefine.Visible         = false;
                    this._pnlColumnSchema.Visible         = true;
                    this._dgvColumnSchema.DataSource      = schemaInfo.ColumnSchemas;
                    this._dgvIndexColumnSchema.DataSource = schemaInfo.IndexColumnSchemas;
                }
                break;

                case SchemaType.View:
                {
                    this._pnlColumnSchema.Visible         = true;
                    this._pnlSchemaDefine.Visible         = false;
                    this._tabSchemaShow.SelectedTab       = this._pageColumnSchema;
                    this._dgvColumnSchema.DataSource      = schemaInfo.ColumnSchemas;
                    this._dgvIndexColumnSchema.DataSource = null;
                }
                break;

                case SchemaType.Procedure:
                {
                    this._pnlColumnSchema.Visible   = false;
                    this._pnlSchemaDefine.Visible   = true;
                    this._tabSchemaShow.SelectedTab = this._pageDefine;
                    this._richTbSchemaDefine.Text   = ((ProcedureSchema)schemaData).Definition;
                }
                break;

                case SchemaType.Function:
                {
                    this._pnlColumnSchema.Visible   = false;
                    this._pnlSchemaDefine.Visible   = true;
                    this._tabSchemaShow.SelectedTab = this._pageDefine;
                    this._richTbSchemaDefine.Text   = ((FunctionSchema)schemaData).Definition;
                }
                break;
                }
            }
            else
            {
                this.VisibleSchemaShowInfo();
            }
        }
예제 #6
0
 public Boolean Equal(IObjectSchema source, IObjectSchema target)
 {
     return(_internalSchemaEqualizer.Invoke(source, target));
 }
예제 #7
0
        public void Export(IList <IExportTargeter> exportTargeters, IList <SchemaInfoTuple> schemas, ExportConfig config)
        {
            List <SchemaInfoTuple> schemaInfos = new List <SchemaInfoTuple>(schemas);

            Dictionary <String, SchemaInfoTuple> schemaTable = new Dictionary <string, SchemaInfoTuple>();

            schemaInfos.ForEach(t =>
            {
                schemaTable.Add(t.ObjectSchema.Name, t);
            });
            Int32             total        = schemaTable.Count;
            Int32             current      = 0;
            ExcelExportConfig exportConfig = config as ExcelExportConfig;

            //若有分组,首先写入分组中的结构信息
            //在目标表合并一行用于写入分组名称
            //后续写入属于此组的 TableSchema 信息
            //若需要建立合并分组信息,则以组名创建 Sheet ,属于此组的所有 Schem 信息,写入到此 Sheet 中
            //完成一个组的写入,并移除已写入的 Schema 信息

            //完成所有分组的写入后,将剩下零散的 Schema 信息单独创建 Sheet 写入信息

            var templatePath = Path.Combine(this.ExcelExportTemplateDirectory, exportConfig.ExcelTemplatePath);
            //Start
            var workbook = OfficeAssistor.OpenExcel(templatePath);
            //新建目录 Sheet

            EPoint catalogLocation = new EPoint(2, 2);

            var catalogTemplateSheet = workbook.GetSheet(ExcelTemplateFormat.CatalogSheetTemplateName);
            var tableTemplateSheet   = workbook.GetSheet(ExcelTemplateFormat.TableSheetTemplateName);
            var catalogSheet         = workbook.CreateSheet(ExcelTemplateFormat.CatalogSheetName);



            foreach (var pair in exportConfig.GroupInfos)
            {
                var    schemaNames   = pair.Value;
                var    groupName     = pair.Key;
                ISheet schemaSheet   = null;
                EPoint sheetLocation = new EPoint(2, 2);
                if (exportConfig.IsMergeGroupToSheet)
                {
                    schemaSheet = workbook.CreateSheet(groupName);
                }



                //复制目录头
                catalogTemplateSheet.CopyRow(ExcelTemplateFormat.CSTHeadRowNum, catalogSheet, catalogLocation.X, catalogLocation.Y);
                //向目录表中写入 组名
                catalogSheet.SetCellValue(catalogLocation.X, 0, groupName);
                catalogLocation.X += ExcelTemplateFormat.RowSpan;

                //复制目录列
                catalogTemplateSheet.CopyRow(ExcelTemplateFormat.CSTColumnHeaderRowNum, catalogSheet, catalogLocation.X, catalogLocation.Y);
                catalogLocation.X += ExcelTemplateFormat.RowSpan;



                //写入目录行
                Int32 internalnum = 1;

                foreach (String schemaName in schemaNames)
                {
                    IObjectSchema objectSchema = schemaTable[schemaName].ObjectSchema;
                    catalogTemplateSheet.CopyRow(ExcelTemplateFormat.CSTRowTemlateNum, catalogSheet, catalogLocation.X, catalogLocation.Y);

                    var schemaInfo = schemaTable[schemaName];
                    this.RaiseExportProgressChanged(total, ++current, schemaInfo);
                    this.WriteCatalogRow(catalogSheet, objectSchema, internalnum++, catalogLocation.X, 0);
                    //写入对应表的列信息

                    if (!exportConfig.IsMergeGroupToSheet)
                    {
                        schemaSheet = workbook.CreateSheet(schemaInfo.ObjectSchema.Name);
                    }
                    this.WriteSchemaInfo(tableTemplateSheet, schemaSheet, sheetLocation, schemaInfo);
                    schemaTable.Remove(schemaName);
                    if (exportConfig.IsMergeGroupToSheet)
                    {
                        sheetLocation.X += ExcelTemplateFormat.RowSpan;
                    }

                    //目录表获取表名单元格,并添加链接
                    ICell cell = catalogSheet.GetCell(catalogLocation.X, 0 + ExcelTemplateFormat.CatalogNumColLength);

                    //TODO: 连接R1C1样式单元格 ,暂时只连接到 Sheet
                    IHyperlink link = OfficeAssistor.CreateHyperlink(schemaSheet /*, sheetLocation.X, sheetLocation.Y*/);

                    cell.Hyperlink = link;

                    catalogLocation.X += ExcelTemplateFormat.RowSpan;
                    Thread.Sleep(10);
                }
                catalogLocation.X += ExcelTemplateFormat.RowSpan;
            }

            if (schemaTable.Count > 0)
            {
                if (!exportConfig.EnableExclude)
                {
                    catalogTemplateSheet.CopyRow(ExcelTemplateFormat.CSTHeadRowNum, catalogSheet, catalogLocation.X, catalogLocation.Y);

                    catalogSheet.SetCellValue(catalogLocation.X, 0, ExcelTemplateFormat.CatalogSheetName);
                    catalogLocation.X += ExcelTemplateFormat.RowSpan;

                    //复制目录列
                    catalogTemplateSheet.CopyRow(ExcelTemplateFormat.CSTColumnHeaderRowNum, catalogSheet, catalogLocation.X, catalogLocation.Y);
                    catalogLocation.X += ExcelTemplateFormat.RowSpan;

                    Int32 num = 1;
                    foreach (var pair in schemaTable)
                    {
                        String schemaName    = pair.Key;
                        var    schemaInfo    = schemaTable[schemaName];
                        ISheet schemaSheet   = null;
                        EPoint sheetLocation = new EPoint(2, 2);
                        schemaSheet = workbook.CreateSheet(schemaName);

                        IObjectSchema objectSchema = schemaTable[schemaName].ObjectSchema;
                        catalogTemplateSheet.CopyRow(ExcelTemplateFormat.CSTRowTemlateNum, catalogSheet, catalogLocation.X, catalogLocation.Y);
                        this.RaiseExportProgressChanged(total, ++current, schemaInfo);
                        this.WriteCatalogRow(catalogSheet, objectSchema, num++, catalogLocation.X, 0);

                        this.WriteSchemaInfo(tableTemplateSheet, schemaSheet, sheetLocation, schemaInfo);

                        //目录表获取表名单元格,并添加链接
                        ICell cell = catalogSheet.GetCell(catalogLocation.X, 0 + ExcelTemplateFormat.CatalogNumColLength);

                        //TODO: 连接R1C1样式单元格 ,暂时只连接到 Sheet
                        IHyperlink link = OfficeAssistor.CreateHyperlink(schemaSheet);

                        cell.Hyperlink = link;

                        catalogLocation.X += ExcelTemplateFormat.RowSpan;
                        Thread.Sleep(10);
                    }
                }
                else
                {
                    this.RaiseExportProgressChanged(total - schemaTable.Count, current, null);
                }
            }

            //删除模板 Sheet
            workbook.RemoveSheetByName(ExcelTemplateFormat.CatalogSheetTemplateName);
            workbook.RemoveSheetByName(ExcelTemplateFormat.TableSheetTemplateName);
            this.SaveExcel(workbook, exportTargeters);
        }
        private static IPropertyContainer?ParseXmlElement(
            XElement objectElement,
            IObjectSchema objectSchema,
            IXmlParserSettings settings,
            IXmlParserContext context,
            IMutablePropertyContainer?container = null)
        {
            if (objectElement.HasElements)
            {
                container ??= new MutablePropertyContainer();

                foreach (XElement propertyElement in objectElement.Elements())
                {
                    string    elementName  = settings.GetElementName(propertyElement);
                    string    propertyName = settings.StringProvider.GetString(elementName);
                    IProperty?property     = objectSchema.GetProperty(propertyName);

                    if (propertyElement.HasElements)
                    {
                        IObjectSchema propertyInternalSchema = context.GetOrCreateNewSchemaCached(property).ToObjectSchema();

                        IPropertyContainer?internalObject = ParseXmlElement(propertyElement, propertyInternalSchema, settings, context);
                        if (internalObject != null && internalObject.Count > 0)
                        {
                            if (settings.SetSchemaForObjects)
                            {
                                internalObject.SetSchema(propertyInternalSchema);
                            }

                            if (property == null)
                            {
                                property = new Property <IPropertyContainer>(propertyName)
                                           .SetIsNotFromSchema()
                                           .SetSchema(propertyInternalSchema);

                                if (objectSchema is IMutableObjectSchema mutableObjectSchema)
                                {
                                    property = mutableObjectSchema.AddProperty(property);
                                }
                            }

                            IPropertyValue propertyValue = settings.PropertyValueFactory.CreateUntyped(property, internalObject);
                            container.Add(propertyValue);

                            // Validate property.
                            if (settings.ValidateOnParse)
                            {
                                ValidateProperty(context, container, property, propertyElement);
                            }
                        }
                    }
                    else
                    {
                        if (property != null && property.Type == typeof(IPropertyContainer))
                        {
                            // Composite object, no value.
                            bool isNullAllowed = property.GetOrEvaluateNullability().IsNullAllowed;
                            if (!isNullAllowed)
                            {
                                context.Messages.AddError(
                                    $"Property '{property.Name}' can not be null but xml element has no value.{GetXmlLineInfo(propertyElement)}");
                            }

                            continue;
                        }

                        if (property == null)
                        {
                            property = new Property <string>(propertyName)
                                       .SetIsNotFromSchema();

                            if (objectSchema is IMutableObjectSchema mutableObjectSchema)
                            {
                                property = mutableObjectSchema.AddProperty(property);
                            }
                        }

                        IValueParser valueParser = context.GetParserCached(property);
                        if (valueParser != EmptyParser.Instance)
                        {
                            string elementValue = propertyElement.Value;

                            // Parse value.
                            IParseResult parseResult = valueParser.ParseUntyped(elementValue);

                            if (parseResult.IsSuccess)
                            {
                                // Add property to container.
                                object?        parsedValue   = parseResult.ValueUntyped;
                                IPropertyValue propertyValue = settings.PropertyValueFactory.CreateUntyped(property, parsedValue);
                                container.Add(propertyValue);

                                // Validate property.
                                if (settings.ValidateOnParse)
                                {
                                    ValidateProperty(context, container, property, propertyElement);
                                }
                            }
                            else
                            {
                                string?parseResultErrorMessage = parseResult.Error?.FormattedMessage;
                                string parseResultError        = parseResultErrorMessage != null ? $" Error: '{parseResultErrorMessage}'." : string.Empty;
                                string errorMessage            = $"Property '{property.Name}' failed to parse from string '{elementValue}'.{parseResultError}{GetXmlLineInfo(propertyElement)}";
                                context.Messages.AddError(errorMessage);
                            }
                        }
                        else
                        {
                            string errorMessage = $"Property '{property.Name}' can not be parsed because no parser found for type {property.Type}.{GetXmlLineInfo(propertyElement)}";
                            context.Messages.AddError(errorMessage);
                        }
                    }
                }

                return(container);
            }

            return(null);
        }
예제 #9
0
        private void ShowTargetTopSchema(IObjectSchema schemaData)
        {
            if (schemaData != null)
            {
                var schemaInfo = this.TopObjectSchemaInfoTupleTable[schemaData];

                switch (schemaInfo.SchemaType)
                {
                case SchemaType.Table:
                {
                    this._tabTargetSchemaShow.SelectedTab       = this._pageTargetColumnSchema;
                    this._pnlTargetSchemaDefine.Visible         = false;
                    this._pnlTargetColumnSchema.Visible         = true;
                    this._dgvTargetColumnSchema.DataSource      = schemaInfo.ColumnSchemas;
                    this._dgvTargetIndexColumnSchema.DataSource = schemaInfo.IndexColumnSchemas;
                }
                break;

                case SchemaType.View:
                {
                    this._pnlTargetColumnSchema.Visible         = true;
                    this._pnlTargetSchemaDefine.Visible         = false;
                    this._tabTargetSchemaShow.SelectedTab       = this._pageTargetColumnSchema;
                    this._dgvTargetColumnSchema.DataSource      = schemaInfo.ColumnSchemas;
                    this._dgvTargetIndexColumnSchema.DataSource = null;
                }
                break;

                case SchemaType.Procedure:
                {
                    this._pnlTargetColumnSchema.Visible   = false;
                    this._pnlTargetSchemaDefine.Visible   = true;
                    this._tabTargetSchemaShow.SelectedTab = this._pageTargetDefine;
                    this._richTbTargetSchemaDefine.Text   = ((ProcedureSchema)schemaData).Definition;
                    var   differenceType      = this.GetSchemaDifferenceType(schemaData);
                    Int32 differenceTypeIndex = (Int32)differenceType;
                    Color foreColor           = this.DifferenceTypeColors[differenceTypeIndex];
                    this._richTbTargetSchemaDefine.ForeColor = foreColor;
                    if (differenceType == SchemaDifferenceType.Delete)
                    {
                        this._richTbTargetSchemaDefine.Font = new Font(
                            this._richTbTargetSchemaDefine.Font.FontFamily,
                            this._richTbTargetSchemaDefine.Font.Size,
                            FontStyle.Strikeout
                            );
                    }
                }
                break;

                case SchemaType.Function:
                {
                    this._pnlTargetColumnSchema.Visible   = false;
                    this._pnlTargetSchemaDefine.Visible   = true;
                    this._tabTargetSchemaShow.SelectedTab = this._pageTargetDefine;
                    this._richTbTargetSchemaDefine.Text   = ((FunctionSchema)schemaData).Definition;

                    var   differenceType      = this.GetSchemaDifferenceType(schemaData);
                    Int32 differenceTypeIndex = (Int32)differenceType;
                    Color foreColor           = this.DifferenceTypeColors[differenceTypeIndex];
                    this._richTbTargetSchemaDefine.ForeColor = foreColor;
                    if (differenceType == SchemaDifferenceType.Delete)
                    {
                        this._richTbTargetSchemaDefine.Font = new Font(
                            this._richTbTargetSchemaDefine.Font.FontFamily,
                            this._richTbTargetSchemaDefine.Font.Size,
                            FontStyle.Strikeout
                            );
                    }
                }
                break;
                }
            }
            else
            {
                this.VisibleSchemaShowInfo(true);
            }
        }
예제 #10
0
 private SchemaDifferenceType GetSchemaDifferenceType(IObjectSchema schemaObj)
 {
     return(this.ObjectSchemaDifferenceTable[schemaObj]);
 }