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