From: Alexis Laferrière Date: Tue, 21 Jul 2015 15:36:45 +0000 (-0400) Subject: lib/dom: intro a checker/example program X-Git-Tag: v0.7.7~4^2~7 X-Git-Url: http://nitlanguage.org lib/dom: intro a checker/example program Signed-off-by: Alexis Laferrière --- diff --git a/lib/dom/examples/checker.nit b/lib/dom/examples/checker.nit new file mode 100644 index 0000000..cc3b341 --- /dev/null +++ b/lib/dom/examples/checker.nit @@ -0,0 +1,43 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# This file is free software, which comes along with NIT. This software is +# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. You can modify it is you want, provided this header +# is kept unaltered, and a notification of the changes is added. +# You are allowed to redistribute it and sell it, alone or is a part of +# another product. + +# Simple XML validity checker using the `dom` module +module checker + +import dom + +# Check arguments +if args.length != 1 then + print_error "Usage: checker xml_file" + exit 2 +end + +var path = args.first +if not path.file_exists then + print_error "Path '{path}' does not exist" + exit 3 +end + +# Read file +var content = path.to_path.read_all + +# Parse XML +var xml = content.to_xml + +# Check for errors +if xml isa XMLError then + print_error "XML file at '{path}' is invalid:" + print_error xml.message + var loc = xml.location + if loc != null then print_error loc + exit 1 +else + print "XML file at '{path}' is valid" +end diff --git a/tests/sav/checker.res b/tests/sav/checker.res new file mode 100644 index 0000000..e6fde82 --- /dev/null +++ b/tests/sav/checker.res @@ -0,0 +1 @@ +Usage: checker xml_file