Welcome to mirror list, hosted at ThFree Co, Russian Federation.

xmltool.cs « mono-xmltool « tools « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bdbfdaf3f6b5c28a84ef2af6e4ff044142ff4d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Xsl;
using System.Xml.XPath;
#if !TARGET_JVM
using Commons.Xml.Nvdl;
using Commons.Xml.Relaxng;
using Commons.Xml.Relaxng.Rnc;
#endif
using XSchema = System.Xml.Schema.XmlSchema;

namespace Commons.Xml.Relaxng
{
	public class Driver
	{
		public static void Main (string [] args)
		{
			try {
				Run (args);
				Console.Error.WriteLine ("done.");
			} catch (Exception ex) {
				if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
					Console.Error.WriteLine (ex);
				else
					Console.Error.WriteLine (ex.Message);
			}
		}

		static void Usage ()
		{
			Console.Error.WriteLine (@"
Usage: mono-xmltool [options]

options:

	--validate [*.rng | *.rnc | *.nvdl | *.xsd] [instances]
	--validate-rng relax-ng-grammar-xml [instances]
	--validate-rnc relax-ng-compact-grammar-file [instances]
	--validate-nvdl nvdl-script-xml [instances]
	--validate-xsd xml-schema [instances]
	--transform stylesheet instance-xml [output-xml]
	--prettyprint [source] [result]

environment variable that affects on the behavior:

	MONO_XMLTOOL_ERROR_DETAILS = yes : to get exception details.
");
		}

		static void Run (string [] args)
		{
			if (args.Length == 0) {
				Usage ();
				return;
			}

			switch (args [0]) {
			default:
			case "--help":
				Usage ();
				return;
#if !TARGET_JVM
			case "--validate":
				ValidateAuto (args);
				return;
			case "--validate-rnc":
				ValidateRelaxngCompact (args);
				return;
			case "--validate-rng":
				ValidateRelaxngXml (args);
				return;
			case "--validate-nvdl":
				ValidateNvdl (args);
				return;
#endif
			case "--validate-xsd":
				ValidateXsd (args);
				return;
			case "--transform":
				Transform (args);
				return;
			case "--prettyprint":
				PrettyPrint (args);
				return;
			}
		}

#if !TARGET_JVM
		static void ValidateAuto (string [] args)
		{
			if (args.Length < 1) {
				Usage ();
				return;
			}

			if (args [1].EndsWith ("rng"))
				ValidateRelaxngXml (args);
			else if (args [1].EndsWith ("rnc"))
				ValidateRelaxngCompact (args);
			else if (args [1].EndsWith ("nvdl"))
				ValidateNvdl (args);
			else if (args [1].EndsWith ("xsd"))
				ValidateXsd (args);
		}

		static void ValidateRelaxngXml (string [] args)
		{
			XmlReader xr = new XmlTextReader (args [1]);
			RelaxngPattern p = RelaxngPattern.Read (xr);
			xr.Close ();
			ValidateRelaxng (p, args);
		}

		static void ValidateRelaxngCompact (string [] args)
		{
			StreamReader sr = new StreamReader (args [1]);
			RelaxngPattern p = RncParser.ParseRnc (sr);
			sr.Close ();
			ValidateRelaxng (p, args);
		}

		static void ValidateRelaxng (RelaxngPattern p, string [] args)
		{
			if (args.Length < 2)
				return;

			for (int i = 2; i < args.Length; i++) {
				XmlTextReader xtr = new XmlTextReader (args [i]);
				RelaxngValidatingReader vr = 
					new RelaxngValidatingReader (xtr, p);
				if (Environment.GetEnvironmentVariable ("MONO_XMLTOOL_ERROR_DETAILS") == "yes")
					vr.ReportDetails = true;

				while (!vr.EOF)
					vr.Read ();
			}
		}

		static void ValidateNvdl (string [] args)
		{
			XmlTextReader nvdlxtr = new XmlTextReader (args [1]);
			NvdlRules nvdl = NvdlReader.Read (nvdlxtr);
			nvdlxtr.Close ();
			for (int i = 2; i < args.Length; i++) {
				XmlTextReader xtr = new XmlTextReader (args [i]);
				NvdlValidatingReader nvr = new NvdlValidatingReader (xtr, nvdl);
				while (!nvr.EOF)
					nvr.Read ();
				xtr.Close ();
			}
		}
#endif

		static void ValidateXsd (string [] args)
		{
			XmlTextReader schemaxml = new XmlTextReader (args [1]);
			XSchema xsd = XSchema.Read (schemaxml, null);
			schemaxml.Close ();
			for (int i = 2; i < args.Length; i++) {
				XmlTextReader xtr = new XmlTextReader (args [i]);
				XmlValidatingReader xvr = new XmlValidatingReader (xtr);
				xvr.Schemas.Add (xsd);
				while (!xvr.EOF)
					xvr.Read ();
				xvr.Close ();
			}
		}

		static void Transform (string [] args)
		{
			XslTransform t = new XslTransform ();
			t.Load (args [1]);
			TextWriter output = args.Length > 3 ?
				File.CreateText (args [3]) : Console.Out;
			XmlTextWriter xw = new XmlTextWriter (output);
			t.Transform (new XPathDocument (args [2], XmlSpace.Preserve), null, xw, null);
			xw.Close ();
		}

		static void PrettyPrint (string [] args)
		{
			XmlTextReader r = null;
			if (args.Length > 0)
				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);
			else
				w = new XmlTextWriter (Console.Out);
			w.Formatting = Formatting.Indented;

			r.Read ();
			while (!r.EOF)
				w.WriteNode (r, false);
			r.Close ();
			w.Close ();
		}
	}
}