2010-02-08 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / tools / mono-xmltool / xmltool.cs
index 479a90651643a576caaea21fcedc49f61bef351b..918e4a2d48d3cf4a113bfa63f40b3ee904f0dad7 100644 (file)
@@ -5,7 +5,7 @@ using System.Xml;
 using System.Xml.Schema;
 using System.Xml.Xsl;
 using System.Xml.XPath;
-#if !TARGET_JVM
+#if !TARGET_JVM && !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,10 +40,12 @@ options:
        --validate-rnc relax-ng-compact-grammar-file [instances]
        --validate-nvdl nvdl-script-xml [instances]
        --validate-xsd xml-schema [instances]
-       --transform stylesheet instance-xml
+       --validate-xsd2 xml-schema [instances] (in .NET 2.0 validator)
+       --validate-dtd instances
+       --transform stylesheet instance-xml [output-xml]
        --prettyprint [source] [result]
 
-environment variable that affects on the behavior:
+environment variable that affects behavior:
 
        MONO_XMLTOOL_ERROR_DETAILS = yes : to get exception details.
 ");
@@ -62,7 +63,7 @@ environment variable that affects on the behavior:
                        case "--help":
                                Usage ();
                                return;
-#if !TARGET_JVM
+#if !TARGET_JVM && !MSNET
                        case "--validate":
                                ValidateAuto (args);
                                return;
@@ -76,9 +77,15 @@ environment variable that affects on the behavior:
                                ValidateNvdl (args);
                                return;
 #endif
+                       case "--validate-xsd2":
+                               ValidateXsd2 (args);
+                               return;
                        case "--validate-xsd":
                                ValidateXsd (args);
                                return;
+                       case "--validate-dtd":
+                               ValidateDtd (args);
+                               return;
                        case "--transform":
                                Transform (args);
                                return;
@@ -88,7 +95,7 @@ environment variable that affects on the behavior:
                        }
                }
 
-#if !TARGET_JVM
+#if !TARGET_JVM && !MSNET
                static void ValidateAuto (string [] args)
                {
                        if (args.Length < 1) {
@@ -117,13 +124,15 @@ environment variable that affects on the 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;
 
@@ -133,6 +142,16 @@ environment variable that affects on the 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 ();
@@ -159,6 +178,7 @@ environment variable that affects on the 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);
@@ -169,25 +189,53 @@ environment variable that affects on the 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++) {
+                               XmlValidatingReader xvr = new XmlValidatingReader (
+                                       new XmlTextReader (args [i]));
+                               xvr.ValidationType = ValidationType.DTD;
+                               xvr.EntityHandling = EntityHandling.ExpandEntities;
+                               while (!xvr.EOF)
+                                       xvr.Read ();
+                               xvr.Close ();
+                       }
+               }
+
                static void Transform (string [] args)
                {
                        XslTransform t = new XslTransform ();
                        t.Load (args [1]);
-                       XmlTextWriter xw = new XmlTextWriter (Console.Out);
-                       t.Transform (new XPathDocument (args [2], XmlSpace.Preserve), null, xw, null);
+                       TextWriter output = args.Length > 3 ?
+                               File.CreateText (args [3]) : Console.Out;
+                       t.Transform (new XPathDocument (args [2], XmlSpace.Preserve), null, output, null);
+                       output.Close ();
                }
 
                static void PrettyPrint (string [] args)
                {
                        XmlTextReader r = null;
-                       if (args.Length > 0)
+                       if (args.Length > 1)
                                r = new XmlTextReader (args [1]);
                        else
                                r = new XmlTextReader (Console.In);
                        r.WhitespaceHandling = WhitespaceHandling.Significant;
                        XmlTextWriter w = null;
-                       if (args.Length > 1)
-                               w = new XmlTextWriter (args [2], Encoding.UTF8);
+                       if (args.Length > 2)
+                               w = new XmlTextWriter (args [1], Encoding.UTF8);
                        else
                                w = new XmlTextWriter (Console.Out);
                        w.Formatting = Formatting.Indented;