RtmDtdValidator.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 #
00004 #  @file RtmDtdValidator.py
00005 #  @brief XML validator based on specified DTD file
00006 #  @date $Date: 2005-05-12 09:06:19 $
00007 #  @author K.Kitagaki
00008 # 
00009 #  Copyright (C) 2004-2005
00010 #      Task-intelligence Research Group,
00011 #      Intelligent Systems Research Institute,
00012 #      National Institute of
00013 #          Advanced Industrial Science and Technology (AIST), Japan
00014 #      All rights reserved.
00015 # 
00016 #  $Id: RtmDtdValidator.py 775 2008-07-28 16:14:45Z n-ando $
00017 #
00018 
00019 # dtdValidator.py -- xml with DTD Validator
00020 # (C)K.Kitagaki 2004.8.26
00021 #
00022 # # usage
00023 # import dtdValidator
00024 # validator = dtdValidator.DtdValidator()
00025 # errors = validator.parse("filename.xml")
00026 #
00027 # for (filename, line, col, msg) in errors:
00028 #      print filename, line, col, msg
00029 #
00030 # -xml$B$r(BDTD$B$K4p$-8!>Z$9$k!#(B
00031 
00032 from _xmlplus.parsers.xmlproc import xmlval, xmlapp, errors
00033 
00034 # XML validator
00035 class DtdValidator:
00036     def __init__(self):
00037         self.parser = xmlval.XMLValidator()
00038         self.errors = ErrorRecorder(self.parser)
00039 
00040     def parse(self, fileName):
00041         sysid = fileName
00042 #        print sysid
00043         self.parser.reset()
00044         self.errors.reset()
00045         self.parser.set_error_handler(self.errors)
00046         self.parser.parse_resource(sysid)
00047 ##         for (sysid, line, col, msg) in self.errors.errors:
00048 ##             print sysid, line, col, msg
00049         return self.errors.errors
00050 
00051 # XML ErrorHandler
00052 class ErrorRecorder(xmlapp. ErrorHandler):
00053     def __init__(self, locator, warnings=1):
00054         xmlapp.ErrorHandler.__init__(self, locator)
00055         self.show_warnings=warnings
00056         self.reset()
00057 
00058     def warning(self,msg):
00059         if self.show_warnings:
00060             self.__add_error(msg)
00061 
00062     def error(self,msg):
00063         self.__add_error(msg)
00064 
00065     def fatal(self,msg):
00066         self.__add_error(msg)
00067 
00068     def reset(self):
00069         self.errors=[]
00070 
00071     def __add_error(self,msg):
00072         self.errors.append((self.locator.get_current_sysid(),
00073                             self.locator.get_line(),
00074                             self.locator.get_column(),
00075                             msg))


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:17