merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / tools / mono-xmltool / xmltool.cs
1 using System;
2 using System.IO;
3 using System.Text;
4 using System.Xml;
5 using System.Xml.Schema;
6 using System.Xml.Xsl;
7 using System.Xml.XPath;
8 #if !TARGET_JVM && !MSNET
9 using Commons.Xml.Nvdl;
10 using Commons.Xml.Relaxng;
11 using Commons.Xml.Relaxng.Rnc;
12 #endif
13 using XSchema = System.Xml.Schema.XmlSchema;
14
15 namespace Commons.Xml.Relaxng
16 {
17         public class Driver
18         {
19                 public static void Main (string [] args)
20                 {
21                         try {
22                                 Run (args);
23                                 Console.Error.WriteLine ("done.");
24                         } catch (Exception ex) {
25                                 if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
26                                         Console.Error.WriteLine (ex);
27                                 else
28                                         Console.Error.WriteLine (ex.Message);
29                         }
30                 }
31
32                 static void Usage ()
33                 {
34                         Console.Error.WriteLine (@"
35 Usage: mono-xmltool [options]
36
37 options:
38
39         --validate [*.rng | *.rnc | *.nvdl | *.xsd] [instances]
40         --validate-rng relax-ng-grammar-xml [instances]
41         --validate-rnc relax-ng-compact-grammar-file [instances]
42         --validate-nvdl nvdl-script-xml [instances]
43         --validate-xsd xml-schema [instances]
44         --validate-dtd instances
45         --transform stylesheet instance-xml [output-xml]
46         --prettyprint [source] [result]
47
48 environment variable that affects behavior:
49
50         MONO_XMLTOOL_ERROR_DETAILS = yes : to get exception details.
51 ");
52                 }
53
54                 static void Run (string [] args)
55                 {
56                         if (args.Length == 0) {
57                                 Usage ();
58                                 return;
59                         }
60
61                         switch (args [0]) {
62                         default:
63                         case "--help":
64                                 Usage ();
65                                 return;
66 #if !TARGET_JVM && !MSNET
67                         case "--validate":
68                                 ValidateAuto (args);
69                                 return;
70                         case "--validate-rnc":
71                                 ValidateRelaxngCompact (args);
72                                 return;
73                         case "--validate-rng":
74                                 ValidateRelaxngXml (args);
75                                 return;
76                         case "--validate-nvdl":
77                                 ValidateNvdl (args);
78                                 return;
79 #endif
80                         case "--validate-xsd":
81                                 ValidateXsd (args);
82                                 return;
83                         case "--validate-dtd":
84                                 ValidateDtd (args);
85                                 return;
86                         case "--transform":
87                                 Transform (args);
88                                 return;
89                         case "--prettyprint":
90                                 PrettyPrint (args);
91                                 return;
92                         }
93                 }
94
95 #if !TARGET_JVM && !MSNET
96                 static void ValidateAuto (string [] args)
97                 {
98                         if (args.Length < 1) {
99                                 Usage ();
100                                 return;
101                         }
102
103                         if (args [1].EndsWith ("rng"))
104                                 ValidateRelaxngXml (args);
105                         else if (args [1].EndsWith ("rnc"))
106                                 ValidateRelaxngCompact (args);
107                         else if (args [1].EndsWith ("nvdl"))
108                                 ValidateNvdl (args);
109                         else if (args [1].EndsWith ("xsd"))
110                                 ValidateXsd (args);
111                 }
112
113                 static void ValidateRelaxngXml (string [] args)
114                 {
115                         XmlReader xr = new XmlTextReader (args [1]);
116                         RelaxngPattern p = RelaxngPattern.Read (xr);
117                         xr.Close ();
118                         ValidateRelaxng (p, args);
119                 }
120
121                 static void ValidateRelaxngCompact (string [] args)
122                 {
123                         StreamReader sr = new StreamReader (args [1]);
124                         RelaxngPattern p = RncParser.ParseRnc (sr);
125                         sr.Close ();
126                         ValidateRelaxng (p, args);
127                 }
128
129                 static void ValidateRelaxng (RelaxngPattern p, string [] args)
130                 {
131                         if (args.Length < 2)
132                                 return;
133
134                         for (int i = 2; i < args.Length; i++) {
135                                 XmlTextReader xtr = new XmlTextReader (args [i]);
136                                 RelaxngValidatingReader vr = 
137                                         new RelaxngValidatingReader (xtr, p);
138                                 if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
139                                         vr.ReportDetails = true;
140
141                                 while (!vr.EOF)
142                                         vr.Read ();
143                         }
144                 }
145
146                 static void ValidateNvdl (string [] args)
147                 {
148                         XmlTextReader nvdlxtr = new XmlTextReader (args [1]);
149                         NvdlRules nvdl = NvdlReader.Read (nvdlxtr);
150                         nvdlxtr.Close ();
151                         for (int i = 2; i < args.Length; i++) {
152                                 XmlTextReader xtr = new XmlTextReader (args [i]);
153                                 NvdlValidatingReader nvr = new NvdlValidatingReader (xtr, nvdl);
154                                 while (!nvr.EOF)
155                                         nvr.Read ();
156                                 xtr.Close ();
157                         }
158                 }
159 #endif
160
161                 static void ValidateXsd (string [] args)
162                 {
163                         XmlTextReader schemaxml = new XmlTextReader (args [1]);
164                         XSchema xsd = XSchema.Read (schemaxml, null);
165                         schemaxml.Close ();
166                         xsd.Compile (null);
167                         for (int i = 2; i < args.Length; i++) {
168                                 XmlTextReader xtr = new XmlTextReader (args [i]);
169                                 XmlValidatingReader xvr = new XmlValidatingReader (xtr);
170                                 xvr.Schemas.Add (xsd);
171                                 while (!xvr.EOF)
172                                         xvr.Read ();
173                                 xvr.Close ();
174                         }
175                 }
176
177                 static void ValidateDtd (string [] args)
178                 {
179                         for (int i = 1; i < args.Length; i++) {
180                                 XmlValidatingReader xvr = new XmlValidatingReader (
181                                         new XmlTextReader (args [i]));
182                                 xvr.ValidationType = ValidationType.DTD;
183                                 xvr.EntityHandling = EntityHandling.ExpandEntities;
184                                 while (!xvr.EOF)
185                                         xvr.Read ();
186                                 xvr.Close ();
187                         }
188                 }
189
190                 static void Transform (string [] args)
191                 {
192                         XslTransform t = new XslTransform ();
193                         t.Load (args [1]);
194                         TextWriter output = args.Length > 3 ?
195                                 File.CreateText (args [3]) : Console.Out;
196                         t.Transform (new XPathDocument (args [2], XmlSpace.Preserve), null, output, null);
197                         output.Close ();
198                 }
199
200                 static void PrettyPrint (string [] args)
201                 {
202                         XmlTextReader r = null;
203                         if (args.Length > 1)
204                                 r = new XmlTextReader (args [1]);
205                         else
206                                 r = new XmlTextReader (Console.In);
207                         r.WhitespaceHandling = WhitespaceHandling.Significant;
208                         XmlTextWriter w = null;
209                         if (args.Length > 2)
210                                 w = new XmlTextWriter (args [1], Encoding.UTF8);
211                         else
212                                 w = new XmlTextWriter (Console.Out);
213                         w.Formatting = Formatting.Indented;
214
215                         r.Read ();
216                         while (!r.EOF)
217                                 w.WriteNode (r, false);
218                         r.Close ();
219                         w.Close ();
220                 }
221         }
222 }
223