Merge pull request #4224 from akoeplinger/fix-46602
[mono.git] / mcs / tools / mono-xmltool / xmltool.cs
index 27cc1fad61792c25057f82515fabb4aa0bbb24fd..174176f678888392853dc3fdfc604e0a58c662c0 100644 (file)
@@ -5,7 +5,7 @@ using System.Xml;
 using System.Xml.Schema;
 using System.Xml.Xsl;
 using System.Xml.XPath;
-#if !TARGET_JVM && !MSNET
+#if !MSNET
 using Commons.Xml.Nvdl;
 using Commons.Xml.Relaxng;
 using Commons.Xml.Relaxng.Rnc;
@@ -20,7 +20,6 @@ namespace Commons.Xml.Relaxng
                {
                        try {
                                Run (args);
-                               Console.Error.WriteLine ("done.");
                        } catch (Exception ex) {
                                if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
                                        Console.Error.WriteLine (ex);
@@ -41,6 +40,7 @@ options:
        --validate-rnc relax-ng-compact-grammar-file [instances]
        --validate-nvdl nvdl-script-xml [instances]
        --validate-xsd xml-schema [instances]
+       --validate-xsd2 xml-schema [instances] (in .NET 2.0 validator)
        --validate-dtd instances
        --transform stylesheet instance-xml [output-xml]
        --prettyprint [source] [result]
@@ -63,7 +63,7 @@ environment variable that affects behavior:
                        case "--help":
                                Usage ();
                                return;
-#if !TARGET_JVM && !MSNET
+#if !MSNET
                        case "--validate":
                                ValidateAuto (args);
                                return;
@@ -77,6 +77,9 @@ environment variable that affects behavior:
                                ValidateNvdl (args);
                                return;
 #endif
+                       case "--validate-xsd2":
+                               ValidateXsd2 (args);
+                               return;
                        case "--validate-xsd":
                                ValidateXsd (args);
                                return;
@@ -92,7 +95,7 @@ environment variable that affects behavior:
                        }
                }
 
-#if !TARGET_JVM && !MSNET
+#if !MSNET
                static void ValidateAuto (string [] args)
                {
                        if (args.Length < 1) {
@@ -121,13 +124,15 @@ environment variable that affects behavior:
                static void ValidateRelaxngCompact (string [] args)
                {
                        StreamReader sr = new StreamReader (args [1]);
-                       RelaxngPattern p = RncParser.ParseRnc (sr);
+                       RelaxngPattern p = RncParser.ParseRnc (sr, null, Path.GetFullPath (args [1]));
                        sr.Close ();
                        ValidateRelaxng (p, args);
                }
 
                static void ValidateRelaxng (RelaxngPattern p, string [] args)
                {
+                       p.Compile ();
+
                        if (args.Length < 2)
                                return;
 
@@ -137,6 +142,16 @@ environment variable that affects behavior:
                                        new RelaxngValidatingReader (xtr, p);
                                if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
                                        vr.ReportDetails = true;
+                               else
+                                       vr.InvalidNodeFound += delegate (XmlReader source, string message) {
+                                               IXmlLineInfo li = source as IXmlLineInfo;
+                                               Console.WriteLine ("ERROR: {0} (at {1} line {2} column {3})",
+                                                       message,
+                                                       source.BaseURI,
+                                                       li != null && li.HasLineInfo () ? li.LineNumber : 0,
+                                                       li != null && li.HasLineInfo () ? li.LinePosition : 0);
+                                               return true;
+                                       };
 
                                while (!vr.EOF)
                                        vr.Read ();
@@ -163,6 +178,7 @@ environment variable that affects behavior:
                        XmlTextReader schemaxml = new XmlTextReader (args [1]);
                        XSchema xsd = XSchema.Read (schemaxml, null);
                        schemaxml.Close ();
+                       xsd.Compile (null);
                        for (int i = 2; i < args.Length; i++) {
                                XmlTextReader xtr = new XmlTextReader (args [i]);
                                XmlValidatingReader xvr = new XmlValidatingReader (xtr);
@@ -173,6 +189,19 @@ environment variable that affects behavior:
                        }
                }
 
+               static void ValidateXsd2 (string [] args)
+               {
+                       XmlReaderSettings s = new XmlReaderSettings ();
+                       s.ValidationType = ValidationType.Schema;
+                       s.Schemas.Add (null, args [1]);
+                       for (int i = 2; i < args.Length; i++) {
+                               XmlReader xr = XmlReader.Create (args [i], s);
+                               while (!xr.EOF)
+                                       xr.Read ();
+                               xr.Close ();
+                       }
+               }
+
                static void ValidateDtd (string [] args)
                {
                        for (int i = 1; i < args.Length; i++) {
@@ -206,7 +235,7 @@ environment variable that affects behavior:
                        r.WhitespaceHandling = WhitespaceHandling.Significant;
                        XmlTextWriter w = null;
                        if (args.Length > 2)
-                               w = new XmlTextWriter (args [1], Encoding.UTF8);
+                               w = new XmlTextWriter (args [2], Encoding.UTF8);
                        else
                                w = new XmlTextWriter (Console.Out);
                        w.Formatting = Formatting.Indented;