home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / bin / xmlproc_val.python-xml < prev    next >
Encoding:
Text File  |  2007-03-31  |  4.1 KB  |  149 lines

  1. #! /usr/bin/python
  2.  
  3. """
  4. A command-line interface to the validating xmlproc parser. Prints error
  5. messages and can output the parsed data in various formats.
  6. """
  7.  
  8. usage=\
  9. """
  10. Usage:
  11.  
  12.   xvcmd.py [options] [urlstodocs]
  13.  
  14.   ---Options:  
  15.   -c catalog:   path to catalog file to use to resolve public identifiers
  16.   -l language:  ISO 3166 language code for language to use in error messages
  17.   -o format:    Format to output parsed XML. 'e': ESIS, 'x': canonical XML
  18.                 and 'n': normalized XML. No data will be output if this
  19.                 option is not specified.
  20.   urlstodocs:   URLs to the documents to parse. (You can use plain file names
  21.                 as well.) Can be omitted if a catalog is specified and contains
  22.                 a DOCUMENT entry.
  23.   -n:           Report qualified names as 'URI name'. (Namespace processing.)
  24.   --nowarn:     Suppress warnings.
  25.   --entstck:    Show entity stack on errors.
  26.   --rawxml:     Show raw XML string where error occurred.
  27.             
  28.   Catalog files with URLs that end in '.xml' are assumed to be XCatalogs,
  29.   all others are assumed to be SGML Open Catalogs.
  30.  
  31.   If the -c option is not specified the environment variables XMLXCATALOG
  32.   and XMLSOCATALOG will be used (in that order).
  33. """
  34.  
  35. from xml.parsers.xmlproc import xmlval,catalog,xcatalog,xmlproc,_outputters
  36. import sys, getopt, os, string
  37.  
  38. # --- Utilities
  39.  
  40. def print_usage(message):
  41.     print message
  42.     print usage
  43.     sys.exit(1)
  44.  
  45. # --- Initialization
  46.  
  47. print "xmlproc version %s" % xmlval.version
  48.  
  49. p=xmlval.XMLValidator()
  50.  
  51. # --- Interpreting options
  52.  
  53. try:
  54.     (options,sysids)=getopt.getopt(sys.argv[1:],"c:l:o:n",
  55.                                    ["nowarn","entstck","rawxml"])
  56. except getopt.error,e:
  57.     print_usage("Usage error: "+e.msg)
  58.     
  59. warnings=1
  60. entstack=0
  61. rawxml=0
  62. cat=None
  63. pf=None
  64. namespaces=0
  65. app=xmlproc.Application()
  66. err_lang=None
  67.  
  68. for option in options:
  69.     if option[0]=="-c":
  70.         cat=option[1]
  71.     elif option[0]=="-l":
  72.         try:
  73.             p.set_error_language(option[1])
  74.             err_lang=option[1]
  75.         except KeyError:
  76.             print "Error: Language '%s' not available" % option[1]
  77.     elif option[0]=="-o":
  78.         if string.lower(option[1]) == "e":
  79.             app = _outputters.ESISDocHandler()            
  80.         elif string.lower(option[1]) == "x":
  81.             app = _outputters.Canonizer()
  82.         elif string.lower(option[1]) == "n":
  83.             app = _outputters.DocGenerator()
  84.         else:
  85.             print_usage("Error: Unknown output format " + option[1])
  86.             
  87.     elif option[0]=="-n":
  88.         namespaces=1
  89.     elif option[0]=="--nowarn":
  90.         warnings=0
  91.     elif option[0]=="--entstck":
  92.         entstack=1
  93.     elif option[0]=="--rawxml":
  94.         rawxml=1
  95.  
  96. # Acting on option settings
  97.  
  98. err = _outputters.MyErrorHandler(p, p.parser, warnings, entstack, rawxml)
  99. p.set_error_handler(err)
  100.  
  101. if namespaces:
  102.     from xml.parsers.xmlproc import namespace
  103.  
  104.     nsf=namespace.NamespaceFilter(p)
  105.     nsf.set_application(app)
  106.     p.set_application(nsf)
  107. else:
  108.     p.set_application(app)
  109.  
  110. if cat!=None:
  111.     pf=xcatalog.FancyParserFactory(err_lang)
  112. elif cat==None and os.environ.has_key("XMLXCATALOG"):
  113.     cat=os.environ["XMLXCATALOG"]
  114.     pf=xcatalog.XCatParserFactory(err_lang)
  115. elif cat==None and os.environ.has_key("XMLSOCATALOG"):
  116.     cat=os.environ["XMLSOCATALOG"]
  117.     pf=catalog.CatParserFactory(err_lang)
  118.  
  119. if cat!=None:
  120.     print "Parsing catalog file '%s'" % cat
  121.     cat=catalog.xmlproc_catalog(cat,pf,err)
  122.     p.set_pubid_resolver(cat)
  123.  
  124. if len(sysids)==0:
  125.     if cat==None:
  126.         print_usage("You must specify a system identifier if no catalog is "
  127.                     "used")
  128.     elif cat.get_document_sysid()==None:
  129.         print_usage("You must specify a system identifier if the catalog has "
  130.                     "no DOCUMENT entry")
  131.  
  132.     sysids=[cat.get_document_sysid()]
  133.     print "Parsing DOCUMENT '%s' from catalog" % sysids[0]
  134.  
  135. # --- Parsing
  136.  
  137. for sysid in sysids:
  138.     print
  139.     print "Parsing '%s'" % sysid
  140.     p.parse_resource(sysid)
  141.     print
  142.     print "Parse complete, %d error(s)" % err.errors,
  143.     if warnings:
  144.         print "and %d warning(s)" % err.warnings
  145.     else:
  146.         print
  147.     err.reset()
  148.     p.reset()
  149.