lib/dom: intro a checker/example program
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 21 Jul 2015 15:36:45 +0000 (11:36 -0400)
committeralexis laf <xymus@tolkien.info.uqam.ca>
Wed, 29 Jul 2015 17:05:26 +0000 (13:05 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/dom/examples/checker.nit [new file with mode: 0644]
tests/sav/checker.res [new file with mode: 0644]

diff --git a/lib/dom/examples/checker.nit b/lib/dom/examples/checker.nit
new file mode 100644 (file)
index 0000000..cc3b341
--- /dev/null
@@ -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 (file)
index 0000000..e6fde82
--- /dev/null
@@ -0,0 +1 @@
+Usage: checker xml_file