public void CreateSchemaCommand()
 {
     try {
         TaskService.Errors.Clear();
         string xml = Editor.Text;
         using (IProgressMonitor 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(Document, 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);
     }
 }
Ejemplo n.º 2
0
        public void RunXslTransformCommand()
        {
            if (string.IsNullOrEmpty(stylesheetFileName))
            {
                stylesheetFileName = XmlEditorService.BrowseForStylesheetFile();
                if (string.IsNullOrEmpty(stylesheetFileName))
                {
                    return;
                }
            }

            using (IProgressMonitor 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.XslTransform 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(Document))
                    {
                        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 FormatCommand()
        {
            TaskService.Errors.Clear();

            using (IProgressMonitor monitor = XmlEditorService.GetMonitor()) {
                bool        selection = (Editor.SelectionEndPosition - Editor.SelectionStartPosition) > 0;
                string      xml       = selection? Editor.SelectedText : Editor.Text;
                XmlDocument doc       = XmlEditorService.ValidateWellFormedness(monitor, xml, FileName);
                if (doc == null)
                {
                    return;
                }

                //if there's a line indent at the current location, prepend that to all new lines
                string extraIndent = null;
                if (selection)
                {
                    extraIndent = GetPositionIndent(Editor.SelectionStartPosition);
                }

                string formattedXml = XmlEditorService.IndentedFormat(xml);

                //convert newlines and prepend extra indents to each line if needed
                bool nonNativeNewline = (Editor.NewLine != Environment.NewLine);
                bool hasExtraIndent   = !string.IsNullOrEmpty(extraIndent);
                if (hasExtraIndent || nonNativeNewline)
                {
                    System.Text.StringBuilder builder = new System.Text.StringBuilder(formattedXml);

                    if (nonNativeNewline)
                    {
                        builder.Replace(Environment.NewLine, Editor.NewLine);
                    }

                    if (hasExtraIndent)
                    {
                        builder.Replace(Editor.NewLine, Editor.NewLine + extraIndent);
                        if (formattedXml.EndsWith(Environment.NewLine))
                        {
                            builder.Remove(builder.Length - 1 - extraIndent.Length, extraIndent.Length);
                        }
                    }
                    formattedXml = builder.ToString();
                }

                Editor.BeginAtomicUndo();
                if (selection)
                {
                    Editor.SelectedText = formattedXml;
                }
                else
                {
                    Editor.DeleteText(0, Editor.TextLength);
                    Editor.InsertText(0, formattedXml);
                }
                Editor.EndAtomicUndo();
            }
        }
        public void AssignStylesheetCommand()
        {
            // Prompt user for filename.
            string fileName = XmlEditorService.BrowseForStylesheetFile();

            if (!string.IsNullOrEmpty(stylesheetFileName))
            {
                stylesheetFileName = fileName;
            }
        }
        protected override void Run()
        {
            // Find active Xml View.
            XmlEditorViewContent view = XmlEditorService.GetActiveView();

            if (view != null)
            {
                view.XmlEditorView.RemoveXPathMarkers();
            }
        }
Ejemplo n.º 6
0
 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());
             }
         }
     }
 }
        protected override void Update(CommandInfo info)
        {
            XmlEditorViewContent view = XmlEditorService.GetActiveView();

            if (view != null && view.StylesheetFileName != null)
            {
                info.Enabled = true;
            }
            else
            {
                info.Enabled = false;
            }
        }
 public void ValidateCommand()
 {
     TaskService.Errors.Clear();
     using (IProgressMonitor monitor = XmlEditorService.GetMonitor()) {
         if (IsSchema)
         {
             XmlEditorService.ValidateSchema(monitor, Editor.Text, FileName);
         }
         else
         {
             XmlEditorService.ValidateXml(monitor, Editor.Text, FileName);
         }
     }
 }
Ejemplo n.º 9
0
        protected override void Run()
        {
            XmlEditorViewContent view = XmlEditorService.GetActiveView();

            if (view != null)
            {
                if (view.StylesheetFileName != null)
                {
                    try {
                        IdeApp.Workbench.OpenDocument(view.StylesheetFileName);
                    } catch (Exception ex) {
                        MonoDevelop.Core.LoggingService.LogError("Could not open document.", ex);
                        MonoDevelop.Ide.MessageService.ShowError("Could not open document.", ex.ToString());
                    }
                }
            }
        }