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);
            }
        }
Exemple #2
0
        protected virtual void addRegisteredSchema(object sender, EventArgs args)
        {
            string fileName = XmlEditorService.BrowseForSchemaFile();

            // We need to present the window so that the keyboard focus returns to the correct parent window
            ((Gtk.Window)Toplevel).Present();

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

            string shortName = System.IO.Path.GetFileName(fileName);

            //load the schema
            XmlSchemaCompletionData schema = null;

            try {
                schema = new XmlSchemaCompletionData(fileName);
            } catch (Exception ex) {
                string msg = GettextCatalog.GetString("Schema '{0}' could not be loaded.", shortName);
                MessageService.ShowError(msg, ex);
                return;
            }

            // Make sure the schema has a target namespace.
            if (schema.NamespaceUri == null)
            {
                MessageService.ShowError(
                    GettextCatalog.GetString("Schema '{0}' has no target namespace.", shortName));
                return;
            }

            //if namaspace conflict, ask user whether they want to replace existing schema
            XmlSchemaCompletionData oldSchema = GetRegisteredSchema(schema.NamespaceUri);

            if (oldSchema != null)
            {
                bool replace = MessageService.Confirm(
                    GettextCatalog.GetString(
                        "A schema is already registered with the namespace '{0}'. Would you like to replace it?",
                        schema.NamespaceUri),
                    new AlertButton(GettextCatalog.GetString("Replace"))
                    );
                if (!replace)
                {
                    return;
                }

                //remove the old schema
                RemoveRegisteredSchema(oldSchema);
            }

            // Store the schema so we can add it for real later, if the "ok" button's clicked
            TreeIter newIter = AddRegisteredSchema(schema);

            registeredSchemasView.Selection.SelectIter(newIter);
            ScrollToSelection(registeredSchemasView);
        }
        public void AssignStylesheetCommand()
        {
            string fileName = XmlEditorService.BrowseForStylesheetFile();

            if (!string.IsNullOrEmpty(fileName))
            {
                stylesheetFileName = fileName;
            }
        }
 public static string CreateSchema(Document doc, string xml)
 {
     using (System.Data.DataSet dataSet = new System.Data.DataSet()) {
         dataSet.ReadXml(new StringReader(xml), System.Data.XmlReadMode.InferSchema);
         using (EncodedStringWriter writer = new EncodedStringWriter(Encoding.UTF8)) {
             using (XmlTextWriter xmlWriter = XmlEditorService.CreateXmlTextWriter(doc, writer)) {
                 dataSet.WriteXmlSchema(xmlWriter);
                 return(writer.ToString());
             }
         }
     }
 }
        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();
                }
            }
        }
 public void ValidateCommand()
 {
     TaskService.Errors.Clear();
     using (ProgressMonitor monitor = XmlEditorService.GetMonitor()) {
         if (IsSchema)
         {
             XmlEditorService.ValidateSchema(monitor, Editor.Text, FileName);
         }
         else
         {
             XmlEditorService.ValidateXml(monitor, Editor.Text, FileName);
         }
     }
 }
        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();
                }
            }
        }
        public async void ValidateCommand()
        {
            TaskService.Errors.Clear();
            using (var monitor = XmlEditorService.GetMonitor()) {
                monitor.BeginTask(GettextCatalog.GetString("Validating {0}...", FileName.FileName), 0);

                var errors = await XmlEditorService.Validate(Editor.Text, FileName, monitor.CancellationToken);

                if (errors.Count > 0)
                {
                    foreach (var err in errors)
                    {
                        monitor.Log.WriteLine(err);
                    }
                    monitor.ReportError("Validation failed");
                }
                else
                {
                    monitor.ReportSuccess("Validation succeeded");
                }

                UpdateErrors(errors);
            }
        }