cc3b3414f1e1d3f211cf59d2099d8930e170238d
[nit.git] / lib / dom / examples / checker.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Simple XML validity checker using the `dom` module
12 module checker
13
14 import dom
15
16 # Check arguments
17 if args.length != 1 then
18 print_error "Usage: checker xml_file"
19 exit 2
20 end
21
22 var path = args.first
23 if not path.file_exists then
24 print_error "Path '{path}' does not exist"
25 exit 3
26 end
27
28 # Read file
29 var content = path.to_path.read_all
30
31 # Parse XML
32 var xml = content.to_xml
33
34 # Check for errors
35 if xml isa XMLError then
36 print_error "XML file at '{path}' is invalid:"
37 print_error xml.message
38 var loc = xml.location
39 if loc != null then print_error loc
40 exit 1
41 else
42 print "XML file at '{path}' is valid"
43 end