public void CreateSchemaCommand()
        {
            try {
                TaskService.Errors.Clear();

                string xml = Editor.Text;
                using (ProgressMonitor monitor = XmlEditorService.GetMonitor()) {
                    XmlDocument doc = XmlEditorService.ValidateWellFormedness(monitor, xml, FileName);
                    if (doc == null)
                    {
                        return;
                    }
                    monitor.BeginTask(GettextCatalog.GetString("Creating schema..."), 0);
                    try {
                        string schema = XmlEditorService.CreateSchema(Editor, xml);

                        string fileName = XmlEditorService.GenerateFileName(FileName, "{0}.xsd");
                        IdeApp.Workbench.NewDocument(fileName, "application/xml", schema);
                        monitor.ReportSuccess(GettextCatalog.GetString("Schema created."));
                    } catch (Exception ex) {
                        string msg = GettextCatalog.GetString("Error creating XML schema.");
                        LoggingService.LogError(msg, ex);
                        monitor.ReportError(msg, ex);
                    }
                }
            } catch (Exception ex) {
                MessageService.ShowError(ex.Message);
            }
        }
        public void RunXslTransformCommand()
        {
            if (string.IsNullOrEmpty(stylesheetFileName))
            {
                stylesheetFileName = XmlEditorService.BrowseForStylesheetFile();

                if (string.IsNullOrEmpty(stylesheetFileName))
                {
                    return;
                }
            }

            using (ProgressMonitor monitor = XmlEditorService.GetMonitor()) {
                try {
                    string xsltContent;
                    try {
                        xsltContent = GetFileContent(stylesheetFileName);
                    } catch (System.IO.IOException) {
                        monitor.ReportError(
                            GettextCatalog.GetString("Error reading file '{0}'.", stylesheetFileName), null);
                        return;
                    }
                    System.Xml.Xsl.XslCompiledTransform xslt =
                        XmlEditorService.ValidateStylesheet(monitor, xsltContent, stylesheetFileName);
                    if (xslt == null)
                    {
                        return;
                    }

                    XmlDocument doc = XmlEditorService.ValidateXml(monitor, Editor.Text, FileName);
                    if (doc == null)
                    {
                        return;
                    }

                    string newFileName = XmlEditorService.GenerateFileName(FileName, "-transformed{0}.xml");

                    monitor.BeginTask(GettextCatalog.GetString("Executing transform..."), 1);
                    using (XmlTextWriter output = XmlEditorService.CreateXmlTextWriter(Editor)) {
                        xslt.Transform(doc, null, output);
                        IdeApp.Workbench.NewDocument(
                            newFileName, "application/xml", output.ToString());
                    }
                    monitor.ReportSuccess(GettextCatalog.GetString("Transform completed."));
                    monitor.EndTask();
                } catch (Exception ex) {
                    string msg = GettextCatalog.GetString("Could not run transform.");
                    monitor.ReportError(msg, ex);
                    monitor.EndTask();
                }
            }
        }
Ejemplo n.º 3
0
        public void RunXslTransformCommand()
        {
            if (string.IsNullOrEmpty(stylesheetFileName))
            {
                stylesheetFileName = XmlEditorService.BrowseForStylesheetFile();

                if (string.IsNullOrEmpty(stylesheetFileName))
                {
                    return;
                }
            }

            using (ProgressMonitor monitor = XmlEditorService.GetMonitor()) {
                try {
                    string xsltContent;
                    try {
                        xsltContent = GetFileContent(stylesheetFileName);
                    } catch (IOException) {
                        monitor.ReportError(
                            GettextCatalog.GetString("Error reading file '{0}'.", stylesheetFileName), null);
                        return;
                    }
                    (var xslt, var errors) = XmlEditorService.CompileStylesheet(xsltContent, stylesheetFileName);
                    if (xslt == null)
                    {
                        monitor.ReportError(GettextCatalog.GetString("Failed to compile stylesheet"));
                        return;
                    }

                    string newFileName = XmlEditorService.GenerateFileName(FileName, "-transformed{0}.xml");

                    monitor.BeginTask(GettextCatalog.GetString("Executing transform..."), 1);

                    var output = new EncodedStringWriter(Encoding.UTF8);
                    using (XmlReader input = XmlReader.Create(new StringReader(Editor.Text), null, FileName)) {
                        using (XmlTextWriter writer = XmlEditorService.CreateXmlTextWriter(Editor, output)) {
                            xslt.Transform(input, writer);
                        }
                    }
                    IdeApp.Workbench.NewDocument(newFileName, "application/xml", output.ToString());

                    monitor.ReportSuccess(GettextCatalog.GetString("Transform completed."));
                    monitor.EndTask();
                } catch (Exception ex) {
                    string msg = GettextCatalog.GetString("Could not run transform.");
                    monitor.ReportError(msg, ex);
                    monitor.EndTask();
                }
            }
        }