Ejemplo n.º 1
0
        /// <summary>
        /// Exports the xml schema into a file.
        /// </summary>
        /// <param name="diagram">The exported diagram.</param>
        /// <param name="path">The file path.</param>
        private static void ExportXsd(Diagram diagram, string path)
        {
            Console.Write("Exporting diagram {0} to {1}.... ", diagram.Caption, path);
            if (translator == null)
            {
                // show start dialog of translation
                StartTranslation st = new StartTranslation();
                DialogResult     dr = st.ShowDialog();
                if (dr != DialogResult.OK)
                {
                    return;
                }

                Configuration config = new Configuration();
                if (!st.isDefConfigChecked())
                {
                    config.Load(st.getConfigFileName());
                }

                if (config == null)
                {
                    return;
                }

                translator = new XmlSchemaTranslator(config, "projectA"); // todo: ...
            }
            string xsdString = translator.Translate((PSMDiagram)diagram);

            xsdString = xsdString.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>",
                                          "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            File.WriteAllText(path, xsdString, Encoding.UTF8);
            Console.WriteLine("OK");
        }
Ejemplo n.º 2
0
        public override void Execute(object parameter)
        {
            // todo: uncomment the next statement to run a test
            //Test(); return;

            PSMDiagram diagram = (PSMDiagram)ActiveDiagramView.Diagram;

            if (diagram.Roots.Count < 1)
            {
                MessageBox.Show("PSM diagram is empty. Nothing to translate.", "XCase Warning");
                return;
            }

            // show dialog starting translation
            StartTranslation st = new StartTranslation();
            DialogResult     dr = st.ShowDialog();

            if (dr != DialogResult.OK)
            {
                return;
            }

            Configuration config = new Configuration();

            if (!st.isDefConfigChecked())
            {
                config.Load(st.getConfigFileName());
            }

            if (config == null)
            {
                return;
            }

            // get short name of current project
            string projectName = getProjectName();

            // call XML Schema translation
            XmlSchemaTranslator translator = new XmlSchemaTranslator(config, projectName);
            string resultMessage           = translator.Translate(diagram);

            if (resultMessage.Equals("ok"))
            {
                // get results and display them in nice window
                Dictionary <string, string> schemas = translator.getResults();
                XMLSchemaWindow.Show(MainWindow.dockManager, (PSMDiagram)ActiveDiagramView.Diagram, schemas, translator.Log);
            }
        }
Ejemplo n.º 3
0
        private void bValidate_Click(object sender, RoutedEventArgs e)
        {
            // show start dialog of translation
            StartTranslation st = new StartTranslation();

            System.Windows.Forms.DialogResult dr = st.ShowDialog();
            if (dr != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            Configuration config = new Configuration();

            if (!st.isDefConfigChecked())
            {
                config.Load(st.getConfigFileName());
            }

            if (config == null)
            {
                return;
            }

            XmlSchemaTranslator t = new XmlSchemaTranslator(config, "projectN");  //todo:...

            string       xmlSchemaText = t.Translate(Diagram);
            XmlReader    xmlfile       = null;
            XmlReader    schemaReader  = null;
            MemoryStream _msSchemaText = null;

            isValid = true;
            abort   = false;
            try
            {
                byte[] b = Encoding.Unicode.GetBytes(xmlSchemaText);
                _msSchemaText = new MemoryStream(b);
                schemaReader  = new XmlTextReader(_msSchemaText);
                XmlSchema schema = XmlSchema.Read(schemaReader, schemaSettings_ValidationEventHandler);
                schema.TargetNamespace = Diagram.Project.Schema.XMLNamespaceOrDefaultNamespace;

                XmlReaderSettings schemaSettings = new XmlReaderSettings();
                schemaSettings.Schemas.Add(schema);
                schemaSettings.ValidationType          = ValidationType.Schema;
                schemaSettings.ValidationEventHandler += schemaSettings_ValidationEventHandler;

                try
                {
                    xmlfile = XmlReader.Create(new StringReader(tbDocument.Text), schemaSettings);
                }
                catch (XmlSchemaValidationException ex)
                {
                    isValid = false;
                    MessageBox.Show(string.Format("Validation can not continue - schema is invalid. \r\n\r\n{0}", ex.Message), "Invalid schema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                if (isValid)
                {
                    while (xmlfile.Read() && !abort)
                    {
                    }
                }
            }
            catch (XmlSchemaValidationException ex)
            {
                isValid = false;
                MessageBox.Show(string.Format("{0} \r\n\r\nValidation can not continue.", ex.Message), "Invalid document", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            catch (Exception ex)
            {
                isValid = false;
                MessageBox.Show(string.Format("{0} \r\n\r\nValidation can not continue.", ex.Message), "Invalid document", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            finally
            {
                if (xmlfile != null)
                {
                    xmlfile.Close();
                }
                if (schemaReader != null)
                {
                    schemaReader.Close();
                }
                if (_msSchemaText != null)
                {
                    _msSchemaText.Dispose();
                }
            }

            if (isValid)
            {
                MessageBox.Show("Document is valid", "Document valid", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }