Beispiel #1
0
        protected override void CommandAction(DTEHelper helper)
        {
            EnvDTE.Document document = helper.GetOpenedDocumentInCodeWindow(FileOperations.SupportsXmlType);

            if (document == null)
            {
                return;
            }

            var objTextDoc = document.Object(nameof(EnvDTE.TextDocument));

            if (objTextDoc != null && objTextDoc is EnvDTE.TextDocument textDocument)
            {
                string text = textDocument.StartPoint.CreateEditPoint().GetText(textDocument.EndPoint);

                if (!string.IsNullOrEmpty(text))
                {
                    if (ContentComparerHelper.TryParseXmlDocument(text, out var doc))
                    {
                        string docRootName = doc.Root.Name.ToString();

                        var schemasResources = AbstractDynamicCommandXsdSchemas.GetXsdSchemasByRootName(docRootName);

                        if (schemasResources != null)
                        {
                            ContentComparerHelper.ReplaceXsdSchemaInDocument(document, schemasResources);
                        }
                    }
                }
            }
        }
        private static bool ValidateXmlDocument(ConnectionData connectionData, IWriteToOutput iWriteToOutput, XDocument doc)
        {
            ContentComparerHelper.ClearRoot(doc);

            XmlSchemaSet schemas = new XmlSchemaSet();

            {
                var schemasResources = AbstractDynamicCommandXsdSchemas.GetXsdSchemas(AbstractDynamicCommandXsdSchemas.WebResourceDependencyXmlSchema);

                if (schemasResources != null)
                {
                    foreach (var fileName in schemasResources)
                    {
                        Uri uri = FileOperations.GetSchemaResourceUri(fileName);
                        StreamResourceInfo info = Application.GetResourceStream(uri);

                        using (StreamReader reader = new StreamReader(info.Stream))
                        {
                            schemas.Add(string.Empty, XmlReader.Create(reader));
                        }
                    }
                }
            }

            List <ValidationEventArgs> errors = new List <ValidationEventArgs>();

            doc.Validate(schemas, (o, e) =>
            {
                errors.Add(e);
            });

            if (errors.Count > 0)
            {
                iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.TextIsNotValidForFieldFormat1, AbstractDynamicCommandXsdSchemas.WebResourceDependencyXmlSchema);

                foreach (var item in errors)
                {
                    iWriteToOutput.WriteToOutput(connectionData, string.Empty);
                    iWriteToOutput.WriteToOutput(connectionData, string.Empty);
                    iWriteToOutput.WriteToOutput(connectionData, Properties.OutputStrings.XmlValidationMessageFormat2, item.Severity, item.Message);
                    iWriteToOutput.WriteErrorToOutput(connectionData, item.Exception);
                }

                iWriteToOutput.ActivateOutputWindow(connectionData);
            }

            return(errors.Count == 0);
        }
Beispiel #3
0
        protected override void CommandBeforeQueryStatus(EnvDTE80.DTE2 applicationObject, OleMenuCommand menuCommand)
        {
            CommonHandlers.ActionBeforeQueryStatusActiveDocumentIsXml(applicationObject, menuCommand, out var doc);

            if (doc == null)
            {
                menuCommand.Enabled = menuCommand.Visible = false;
                return;
            }

            string docRootName = doc.Name.ToString();

            var schemas = AbstractDynamicCommandXsdSchemas.GetSchemaByRootName(docRootName);

            if (!string.IsNullOrEmpty(schemas))
            {
                menuCommand.Text = string.Format(Properties.CommandNames.CodeXmlCommonXsdSchemaSetProperCommandFormat1, schemas);
            }
            else
            {
                menuCommand.Enabled = menuCommand.Visible = false;
            }
        }
Beispiel #4
0
        public static string FormatXmlByConfiguration(
            string xml
            , CommonConfiguration commonConfig
            , XmlOptionsControls xmlOptions
            , string schemaName        = null
            , string entityName        = null
            , string siteMapUniqueName = null
            , Guid?formId            = null
            , Guid?savedQueryId      = null
            , Guid?customControlId   = null
            , Guid?workflowId        = null
            , string webResourceName = null
            )
        {
            var result = xml;

            if ((xmlOptions & XmlOptionsControls.SetIntellisenseContext) != 0 &&
                commonConfig.SetIntellisenseContext
                )
            {
                if (entityName != null)
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextEntityName, replaceIntellisenseContextEntityNameFormat3, entityName);
                }

                if (siteMapUniqueName != null)
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextSiteMapNameUnique, replaceIntellisenseContextSiteMapNameUniqueFormat3, siteMapUniqueName);
                }

                if (savedQueryId.HasValue)
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextSavedQueryId, replaceIntellisenseContextSavedQueryIdFormat3, savedQueryId.ToString());
                }

                if (formId.HasValue)
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextFormId, replaceIntellisenseContextFormIdFormat3, formId.ToString());
                }

                if (customControlId.HasValue)
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextCustomControlId, replaceIntellisenseContextCustomControlIdFormat3, customControlId.ToString());
                }

                if (workflowId.HasValue)
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextWorkflowId, replaceIntellisenseContextWorkflowIdFormat3, workflowId.ToString());
                }

                if (!string.IsNullOrEmpty(webResourceName))
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContextWebResourceName, replaceIntellisenseContextWebResourceNameFormat3, webResourceName);
                }

                if (entityName != null ||
                    siteMapUniqueName != null ||
                    savedQueryId.HasValue ||
                    formId.HasValue ||
                    customControlId.HasValue ||
                    workflowId.HasValue ||
                    !string.IsNullOrEmpty(webResourceName)
                    )
                {
                    result = ReplaceOrInsertAttribute(result, patternIntellisenseContext, replaceIntellisenseContextNamespaceFormat3, Intellisense.Model.IntellisenseContext.IntellisenseContextNamespace.NamespaceName);
                }
            }

            if ((xmlOptions & XmlOptionsControls.SetXmlSchemas) != 0 &&
                commonConfig.SetXmlSchemasDuringExport
                )
            {
                var schemasResources = AbstractDynamicCommandXsdSchemas.GetXsdSchemas(schemaName);

                if (schemasResources != null)
                {
                    result = ContentComparerHelper.SetXsdSchema(result, schemasResources);
                }
            }

            if (TryParseXml(result, out XElement doc))
            {
                var sortRibbonCommandsAndRulesById = (xmlOptions & XmlOptionsControls.SortRibbonCommandsAndRulesById) != 0 && commonConfig.SortRibbonCommandsAndRulesById;
                var sortFormXmlElements            = (xmlOptions & XmlOptionsControls.SortFormXmlElements) != 0 && commonConfig.SortFormXmlElements;
                var sortXmlAttributes = (xmlOptions & XmlOptionsControls.SortXmlAttributes) != 0 && commonConfig.SortXmlAttributes;

                if (sortRibbonCommandsAndRulesById)
                {
                    SortRibbonCommandsAndRulesByIdInternal(doc);
                }

                if (sortFormXmlElements)
                {
                    SortFormXmlElementsInternal(doc);
                }

                if (sortXmlAttributes)
                {
                    SortXmlAttributesInternal(doc);
                }

                if ((xmlOptions & XmlOptionsControls.XmlAttributeOnNewLine) != 0 && commonConfig.ExportXmlAttributeOnNewLine)
                {
                    return(FormatXmlNewLineOnAttributes(doc));
                }
                else
                {
                    return(doc.ToString());
                }
            }
            else
            {
                return(result);
            }
        }