-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnxmlplus-validate
executable file
·47 lines (39 loc) · 1.65 KB
/
cnxmlplus-validate
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
#!/usr/bin/env python
from lxml import etree
import sys, os
import argparse
import inspect
from XmlValidator import XmlValidator
SPEC_PATH = os.path.join(os.path.dirname(inspect.getfile(XmlValidator)))
MY_PATH = os.path.realpath(os.path.dirname(__file__))
# Parse command line arguments
argumentParser = argparse.ArgumentParser(description='Check the validity of a CNXML+ document.')
argumentParser.add_argument(
'--spec', dest='specFilename',
default='spec.xml',
help='Filename of the XML specification document.')
argumentParser.add_argument(
'--clean-up', dest='produceCleanedXML', action='store_true',
help='Write to standard output a cleaned-up version of the input XML. Will delete all offending elements.')
argumentParser.add_argument(
'-o', dest='outputFilename',
help='Write output to given filename rather than stdout.')
argumentParser.add_argument(
'filename', nargs='+',
help='One or more filenames to process.')
commandlineArguments = argumentParser.parse_args()
isSingleFile = (len(commandlineArguments.filename) == 1)
if commandlineArguments.outputFilename is None:
outputFile = sys.stdout
else:
outputFile = open(commandlineArguments.outputFilename, 'wt')
xmlValidator = XmlValidator(open(os.path.join(SPEC_PATH, commandlineArguments.specFilename),'rt').read())
cleanUp = commandlineArguments.produceCleanedXML
for filename in commandlineArguments.filename:
if not isSingleFile:
sys.stderr.write(filename + '\n')
xmlValidator.validate(
open(filename,'rt').read(),
iCleanUp=cleanUp)
if cleanUp:
print etree.tostring(xmlValidator.dom, xml_declaration=True, encoding='utf-8')