Example #1
0
        private void bValidateAgainstPSMSchema_Click(object sender, RoutedEventArgs e)
        {
            XsdSchemaGenerator g = new XsdSchemaGenerator();

            g.Initialize(ValidationSchema);
            g.GenerateXSDStructure();
            XDocument xDocument = g.GetXsd();

            ValidateDocumentAgainstSchema(xDocument);
        }
Example #2
0
        public override void Execute(object parameter = null)
        {
            if (Current.ActiveDiagram != null && Current.ActiveDiagram is PSMDiagram)
            {
                XsdSchemaGenerator schemaGenerator = new XsdSchemaGenerator();
                schemaGenerator.Initialize((PSMSchema)Current.ActiveDiagram.Schema);
                schemaGenerator.GenerateXSDStructure();
                XDocument xmlSchemaDocument = schemaGenerator.GetXsd();

                if (Environment.MachineName.Contains("TRUPIK"))
                {
                    xmlSchemaDocument.Save(@"d:\Development\Exolutio\XSLTTest\LastSchema.xsd");
                }

                Current.MainWindow.FilePresenter.DisplayFile(xmlSchemaDocument, EDisplayedFileType.XSD, Current.ActiveDiagram.Caption + ".xsd", schemaGenerator.Log);
            }
        }
Example #3
0
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog
            {
                CheckFileExists = false,
                DefaultExt      = "xslt",
                Filter          = "XSLT Transformations|*.xslt"
            };

            string path = System.IO.Path.GetDirectoryName(schemaVersion2.Project.ProjectFile.FullName) +
                          //"\\" + System.IO.Path.GetFileNameWithoutExtension(schemaVersion2.Project.ProjectFile.FullName) + "-out" +
                          "\\SavedStylesheet.xslt";

            string xsdpath = System.IO.Path.GetDirectoryName(schemaVersion2.Project.ProjectFile.FullName) +
                             "\\LastSchema.xsd";

            XsdSchemaGenerator schemaGenerator = new XsdSchemaGenerator();

            schemaGenerator.Initialize(SchemaVersion1);
            schemaGenerator.GenerateXSDStructure();
            XDocument xmlSchemaDocument = schemaGenerator.GetXsd();

            if (path.Contains("Tests\\OCLAdaptation"))
            {
                if (!Directory.Exists(System.IO.Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
                }
                XmlDocumentHelper.SaveInUtf8(tbXslt.Text, path);
                xmlSchemaDocument.SaveInUtf8(xsdpath);
                ExolutioMessageBox.Show("Saved", "Saved", string.Empty);
            }
            else if (saveFileDialog.ShowDialog() == true)
            {
                XmlDocumentHelper.SaveInUtf8(tbXslt.Text, saveFileDialog.FileName);
                xmlSchemaDocument.SaveInUtf8(xsdpath);
                ExolutioMessageBox.Show("Saved", "Saved", string.Empty);
            }
        }
Example #4
0
        private static string TransformSAXON(string document, string xslt, string tmpDir, PSMSchema psmSchema = null, bool schemaAware = false)
        {
            Processor    processor    = new Processor();
            XsltCompiler xsltCompiler = processor.NewXsltCompiler();

            //xsltCompiler.SchemaAware = true;
            xsltCompiler.Processor.SetProperty(FeatureKeys.GENERATE_BYTE_CODE, "false");

            if (!Directory.Exists(tmpDir))
            {
                tmpDir = Path.GetTempPath();
            }

            using (StringReader sr = new StringReader(xslt))
            {
                XsltExecutable xsltExecutable = xsltCompiler.Compile(sr);

                string text;

                if (!schemaAware)
                {
                    int si = document.IndexOf("xmlns:xsi=\"");
                    int ei = document.IndexOf("\"", si + "xmlns:xsi=\"".Length) + 1;
                    text = si != -1 ? document.Remove(si, ei - si) : document;
                    si   = text.IndexOf("xmlns=\"");
                    ei   = text.IndexOf("\"", si + "xmlns=\"".Length) + 1;
                    string xmlns = si != -1 ? text.Substring(si, ei - si) : string.Empty;
                    text = si != -1 ? text.Remove(si, ei - si) : text;
                }
                else
                {
                    text = document;
                }

                string tmpDoc = tmpDir + "tmp.xml";

                if (schemaAware)
                {
                    XsdSchemaGenerator xsdGen = new XsdSchemaGenerator();
                    xsdGen.Initialize(psmSchema);
                    xsdGen.GenerateXSDStructure();
                    XDocument schema    = xsdGen.GetXsd();
                    string    schemaLoc = tmpDir + "LastSchema.xsd";
                    schema.Save(schemaLoc);
                }

                File.WriteAllText(tmpDoc, text.Replace("utf-16", "utf-8"), Encoding.UTF8);


                StringBuilder outputBuilder = new StringBuilder();
                //XmlWriterSettings outputWriterSettings = new XmlWriterSettings { Indent = true, CheckCharacters = false, NewLineOnAttributes = true };
                //XmlWriter outputWriter = XmlWriter.Create(outputBuilder, outputWriterSettings);
                //Debug.Assert(outputWriter != null);
                XsltArgumentList xsltArgumentList = new XsltArgumentList();
                FileStream       fs = null;
                try
                {
                    XsltTransformer xsltTransformer = xsltExecutable.Load();
                    if (schemaAware)
                    {
                        xsltTransformer.SchemaValidationMode = SchemaValidationMode.Strict;
                    }

                    fs = new FileStream(tmpDoc, FileMode.Open);
                    xsltTransformer.SetInputStream(fs, new Uri(@"file://" + tmpDoc));
                    XdmDestination destination = new XdmDestination();
                    xsltTransformer.Run(destination);

                    outputBuilder.Append(destination.XdmNode.OuterXml);
                    //outputWriter.Flush();
                    int pos1 = outputBuilder.ToString().IndexOf(">");
                    int pos2 = outputBuilder.ToString().IndexOf("/>");
                    int pos;
                    if (pos1 == -1)
                    {
                        pos = pos2;
                    }
                    else if (pos2 == -1)
                    {
                        pos = pos1;
                    }
                    else
                    {
                        pos = Math.Min(pos1, pos2);
                    }
                    //outputBuilder.Insert(pos,
                    //                     Environment.NewLine + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + xmlns);

                    StringReader outputReader = new StringReader(outputBuilder.ToString());
                    XDocument    d            = XDocument.Load(outputReader);
                    outputReader.Close();
                    return(d.PrettyPrintXML());
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
            }
        }
Example #5
0
        public void TestXSDTranslation(object directory)
        {
            DirectoryInfo testDir = new DirectoryInfo(directory.ToString());

            FileInfo projectFile = testDir.GetFiles("*.eXo").FirstOrDefault();

            if (projectFile == null)
            {
                Assert.Fail("No project file found for test {0}", testDir.Name);
            }

            FileInfo[] xsdFiles = testDir.GetFiles("*.xsd");

            Project project = serializationManager.LoadProject(projectFile);

            StringBuilder failMessage         = new StringBuilder();
            StringBuilder inconclusiveMessage = new StringBuilder();

            foreach (PSMSchema psmSchema in project.LatestVersion.PSMSchemas)
            {
                FileInfo referenceXSD = null;

                #region find referenc schema

                foreach (FileInfo xsdFile in xsdFiles)
                {
                    if (xsdFile.Name.Contains("DesiredSchema") || xsdFile.Name.Contains("LastSchema") ||
                        xsdFile.Name.Contains("generated"))
                    {
                        continue;
                    }

                    if (project.LatestVersion.PSMSchemas.Count == 1)
                    {
                        referenceXSD = xsdFile;
                    }
                    else
                    {
                        if (xsdFile.Name == psmSchema.Caption)
                        {
                            referenceXSD = xsdFile;
                        }
                    }
                }

                #endregion

                XsdSchemaGenerator generator = new XsdSchemaGenerator();
                generator.Initialize(psmSchema);
                generator.GenerateXSDStructure();

                XDocument testGeneratedXSD = generator.GetXsd();

                // remove comments
                testGeneratedXSD.RemoveComments();

                string testGeneratedXSDfile = testDir.FullName + "/" + testDir.Name + "-generated.xsd";
                using (XmlWriter w = XmlWriter.Create(testGeneratedXSDfile, new XmlWriterSettings()
                {
                    NewLineChars = "\n", Indent = true, IndentChars = "  "
                }))
                {
                    testGeneratedXSD.Save(w);
                }


                Console.WriteLine("XSD generated.");
                Console.WriteLine();

                string generatedXSDText = File.ReadAllText(testGeneratedXSDfile).Replace("utf-16", "utf-8");

                if (referenceXSD != null)
                {
                    XDocument refXSDDocument = XDocument.Load(referenceXSD.FullName);
                    refXSDDocument.RemoveComments();
                    StringBuilder refBuilder = new StringBuilder();
                    using (XmlWriter w = XmlWriter.Create(refBuilder, new XmlWriterSettings()
                    {
                        NewLineChars = "\n", Indent = true, IndentChars = "  "
                    }))
                    {
                        refXSDDocument.Save(w);
                    }

                    string referenceXSDText = refBuilder.ToString().Replace("utf-16", "utf-8");

                    if (generatedXSDText != referenceXSDText)
                    {
                        string message = string.Format("Generated xsd {0} differs from reference xsd {1}.", Path.GetFileName(testGeneratedXSDfile), referenceXSD.Name);
                        Console.WriteLine(message);
                        failMessage.AppendLine(message);
                    }
                    else
                    {
                        string message = string.Format("Generated xsd {0} is identical to reference xsd {1}.", Path.GetFileName(testGeneratedXSDfile), referenceXSD.Name);
                        Console.WriteLine(message);
                    }
                }
                else
                {
                    string message = string.Format("No reference xsd available. ");
                    inconclusiveMessage.AppendLine(message);
                    Console.WriteLine(message);
                }
                Console.WriteLine();
            }

            if (failMessage.Length > 0)
            {
                Assert.Fail(failMessage.ToString());
            }

            if (inconclusiveMessage.Length > 0)
            {
                Assert.Inconclusive(inconclusiveMessage.ToString());
            }

            Console.WriteLine("Test succeeded. ");
        }